blob: c1e8f3ebe20d7399a5c412442e15f31c2996dd3a [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
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300566// ESSL 3.00.5 sections 3.8 and 3.9.
567// If it starts "gl_" or contains two consecutive underscores, it's reserved.
568// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300569bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530571 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300572 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530573 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300574 error(line, reservedErrMsg, "gl_");
575 return false;
576 }
577 if (sh::IsWebGLBasedSpec(mShaderSpec))
578 {
579 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530580 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300581 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300582 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000583 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300584 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530585 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300586 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300587 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000588 }
589 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300590 if (identifier.find("__") != TString::npos)
591 {
592 error(line,
593 "identifiers containing two consecutive underscores (__) are reserved as "
594 "possible future keywords",
595 identifier.c_str());
596 return false;
597 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300598 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000599}
600
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300601// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300602bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800603 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300604 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800606 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530607 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200608 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300609 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200611
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300612 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530613 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300614 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200615 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300616 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200617 {
jchen10cc2a10e2017-05-03 14:05:12 +0800618 std::string reason("cannot convert a variable with type ");
619 reason += getBasicString(argTyped->getBasicType());
620 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300621 return false;
622 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200623 if (argTyped->getBasicType() == EbtVoid)
624 {
625 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300626 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200627 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000628 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629
Olli Etuaho856c4972016-08-08 11:38:39 +0300630 if (type.isArray())
631 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300632 // The size of an unsized constructor should already have been determined.
633 ASSERT(!type.isUnsizedArray());
634 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
635 {
636 error(line, "array constructor needs one argument per array element", "constructor");
637 return false;
638 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300639 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
640 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800641 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300642 {
643 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500644 if (argType.isArray())
645 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300646 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500647 return false;
648 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300649 if (!argType.sameElementType(type))
650 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000651 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300652 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300653 }
654 }
655 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300656 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300657 {
658 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300659 if (fields.size() != arguments->size())
660 {
661 error(line,
662 "Number of constructor parameters does not match the number of structure fields",
663 "constructor");
664 return false;
665 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300666
667 for (size_t i = 0; i < fields.size(); i++)
668 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800669 if (i >= arguments->size() ||
670 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300671 {
672 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000673 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300674 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300675 }
676 }
677 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300678 else
679 {
680 // We're constructing a scalar, vector, or matrix.
681
682 // Note: It's okay to have too many components available, but not okay to have unused
683 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
684 // there is an extra argument, so 'overFull' will become true.
685
686 size_t size = 0;
687 bool full = false;
688 bool overFull = false;
689 bool matrixArg = false;
690 for (TIntermNode *arg : *arguments)
691 {
692 const TIntermTyped *argTyped = arg->getAsTyped();
693 ASSERT(argTyped != nullptr);
694
Olli Etuaho487b63a2017-05-23 15:55:09 +0300695 if (argTyped->getBasicType() == EbtStruct)
696 {
697 error(line, "a struct cannot be used as a constructor argument for this type",
698 "constructor");
699 return false;
700 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300701 if (argTyped->getType().isArray())
702 {
703 error(line, "constructing from a non-dereferenced array", "constructor");
704 return false;
705 }
706 if (argTyped->getType().isMatrix())
707 {
708 matrixArg = true;
709 }
710
711 size += argTyped->getType().getObjectSize();
712 if (full)
713 {
714 overFull = true;
715 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300716 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300717 {
718 full = true;
719 }
720 }
721
722 if (type.isMatrix() && matrixArg)
723 {
724 if (arguments->size() != 1)
725 {
726 error(line, "constructing matrix from matrix can only take one argument",
727 "constructor");
728 return false;
729 }
730 }
731 else
732 {
733 if (size != 1 && size < type.getObjectSize())
734 {
735 error(line, "not enough data provided for construction", "constructor");
736 return false;
737 }
738 if (overFull)
739 {
740 error(line, "too many arguments", "constructor");
741 return false;
742 }
743 }
744 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300745
Olli Etuaho8a176262016-08-16 14:23:01 +0300746 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000747}
748
Jamie Madillb98c3a82015-07-23 14:26:04 -0400749// This function checks to see if a void variable has been declared and raise an error message for
750// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000751//
752// returns true in case of an error
753//
Olli Etuaho856c4972016-08-08 11:38:39 +0300754bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400755 const TString &identifier,
756 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300758 if (type == EbtVoid)
759 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000760 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300761 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300762 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763
Olli Etuaho8a176262016-08-16 14:23:01 +0300764 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765}
766
Jamie Madillb98c3a82015-07-23 14:26:04 -0400767// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300768// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300769void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530771 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
772 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000773 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530774 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775}
776
Jamie Madillb98c3a82015-07-23 14:26:04 -0400777// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300778// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300779void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000780{
Martin Radev4a9cd802016-09-01 16:51:51 +0300781 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530782 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000783 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530784 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000785}
786
jchen10cc2a10e2017-05-03 14:05:12 +0800787bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
788 const TTypeSpecifierNonArray &pType,
789 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530791 if (pType.type == EbtStruct)
792 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300793 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530794 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000795 std::stringstream reasonStream;
796 reasonStream << reason << " (structure contains a sampler)";
797 std::string reasonStr = reasonStream.str();
798 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300799 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000800 }
jchen10cc2a10e2017-05-03 14:05:12 +0800801 // only samplers need to be checked from structs, since other opaque types can't be struct
802 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300803 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530804 }
jchen10cc2a10e2017-05-03 14:05:12 +0800805 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530806 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000807 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300808 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000809 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
Olli Etuaho8a176262016-08-16 14:23:01 +0300811 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
Olli Etuaho856c4972016-08-08 11:38:39 +0300814void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
815 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400816{
817 if (pType.layoutQualifier.location != -1)
818 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400819 error(line, "location must only be specified for a single input or output variable",
820 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400821 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400822}
823
Olli Etuaho856c4972016-08-08 11:38:39 +0300824void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
825 const TLayoutQualifier &layoutQualifier)
826{
827 if (layoutQualifier.location != -1)
828 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000829 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
830 if (mShaderVersion >= 310)
831 {
832 errorMsg =
833 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
834 }
835 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300836 }
837}
838
Martin Radev2cc85b32016-08-05 16:22:53 +0300839void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
840 TQualifier qualifier,
841 const TType &type)
842{
Martin Radev2cc85b32016-08-05 16:22:53 +0300843 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800844 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530845 {
jchen10cc2a10e2017-05-03 14:05:12 +0800846 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000847 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848}
849
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300851unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530853 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000854
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200855 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
856 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
857 // fold as array size.
858 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000859 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000860 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300861 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863
Olli Etuaho856c4972016-08-08 11:38:39 +0300864 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400865
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000866 if (constant->getBasicType() == EbtUInt)
867 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300868 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000869 }
870 else
871 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300872 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000873
Olli Etuaho856c4972016-08-08 11:38:39 +0300874 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000875 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400876 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300877 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000878 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400879
Olli Etuaho856c4972016-08-08 11:38:39 +0300880 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400881 }
882
Olli Etuaho856c4972016-08-08 11:38:39 +0300883 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400884 {
885 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300886 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400887 }
888
889 // The size of arrays is restricted here to prevent issues further down the
890 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
891 // 4096 registers so this should be reasonable even for aggressively optimizable code.
892 const unsigned int sizeLimit = 65536;
893
Olli Etuaho856c4972016-08-08 11:38:39 +0300894 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400895 {
896 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300897 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000898 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300899
900 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901}
902
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300904bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
905 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906{
Olli Etuaho8a176262016-08-16 14:23:01 +0300907 if ((elementQualifier.qualifier == EvqAttribute) ||
908 (elementQualifier.qualifier == EvqVertexIn) ||
909 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300910 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400911 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300912 TType(elementQualifier).getQualifierString());
913 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000914 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
Olli Etuaho8a176262016-08-16 14:23:01 +0300916 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917}
918
Olli Etuaho8a176262016-08-16 14:23:01 +0300919// See if this element type can be formed into an array.
920bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000922 //
923 // Can the type be an array?
924 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300925 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400926 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300927 error(line, "cannot declare arrays of arrays",
928 TType(elementType).getCompleteString().c_str());
929 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000930 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300931 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
932 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
933 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300934 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300935 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300936 {
937 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300938 TType(elementType).getCompleteString().c_str());
939 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300940 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941
Olli Etuaho8a176262016-08-16 14:23:01 +0300942 return true;
943}
944
945// Check if this qualified element type can be formed into an array.
946bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
947 const TPublicType &elementType)
948{
949 if (checkIsValidTypeForArray(indexLocation, elementType))
950 {
951 return checkIsValidQualifierForArray(indexLocation, elementType);
952 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000953 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954}
955
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300957void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
958 const TString &identifier,
959 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960{
Olli Etuaho3739d232015-04-08 12:23:44 +0300961 ASSERT(type != nullptr);
962 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000963 {
964 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300965 type->qualifier = EvqTemporary;
966
967 // Generate informative error messages for ESSL1.
968 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400969 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000970 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530971 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400972 "structures containing arrays may not be declared constant since they cannot be "
973 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530974 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000975 }
976 else
977 {
978 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
979 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300980 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000981 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300982 if (type->isUnsizedArray())
983 {
984 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300985 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986}
987
Olli Etuaho2935c582015-04-08 14:32:06 +0300988// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989// and update the symbol table.
990//
Olli Etuaho2935c582015-04-08 14:32:06 +0300991// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400993bool TParseContext::declareVariable(const TSourceLoc &line,
994 const TString &identifier,
995 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300996 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997{
Olli Etuaho2935c582015-04-08 14:32:06 +0300998 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999
Olli Etuaho43364892017-02-13 16:00:12 +00001000 checkBindingIsValid(line, type);
1001
Olli Etuaho856c4972016-08-08 11:38:39 +03001002 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003
Olli Etuaho2935c582015-04-08 14:32:06 +03001004 // gl_LastFragData may be redeclared with a new precision qualifier
1005 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1006 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001007 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1008 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001009 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001010 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001011 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001012 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001013 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001014 }
1015 }
1016 else
1017 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001018 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1019 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001020 return false;
1021 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001022 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
Olli Etuaho8a176262016-08-16 14:23:01 +03001024 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001025 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026
Olli Etuaho2935c582015-04-08 14:32:06 +03001027 (*variable) = new TVariable(&identifier, type);
1028 if (!symbolTable.declare(*variable))
1029 {
1030 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001031 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001032 return false;
1033 }
1034
Olli Etuaho8a176262016-08-16 14:23:01 +03001035 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001036 return false;
1037
1038 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039}
1040
Martin Radev70866b82016-07-22 15:27:42 +03001041void TParseContext::checkIsParameterQualifierValid(
1042 const TSourceLoc &line,
1043 const TTypeQualifierBuilder &typeQualifierBuilder,
1044 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301045{
Olli Etuahocce89652017-06-19 16:04:09 +03001046 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001047 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001048
1049 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301050 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001051 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1052 }
1053
1054 if (!IsImage(type->getBasicType()))
1055 {
Olli Etuaho43364892017-02-13 16:00:12 +00001056 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001057 }
1058 else
1059 {
1060 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001061 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062
Martin Radev70866b82016-07-22 15:27:42 +03001063 type->setQualifier(typeQualifier.qualifier);
1064
1065 if (typeQualifier.precision != EbpUndefined)
1066 {
1067 type->setPrecision(typeQualifier.precision);
1068 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069}
1070
Olli Etuaho856c4972016-08-08 11:38:39 +03001071bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001072{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001073 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001074 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301075 if (iter == extBehavior.end())
1076 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001077 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001078 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001079 }
zmo@google.comf5450912011-09-09 01:37:19 +00001080 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301081 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1082 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001083 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1084 // associated with more than one extension.
1085 if (extension == "GL_OVR_multiview")
1086 {
1087 return checkCanUseExtension(line, "GL_OVR_multiview2");
1088 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001089 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001090 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001091 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301092 if (iter->second == EBhWarn)
1093 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001094 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001095 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001096 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097
Olli Etuaho8a176262016-08-16 14:23:01 +03001098 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001101// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1102// compile-time or link-time errors are the same whether or not the declaration is empty".
1103// This function implements all the checks that are done on qualifiers regardless of if the
1104// declaration is empty.
1105void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1106 const sh::TLayoutQualifier &layoutQualifier,
1107 const TSourceLoc &location)
1108{
1109 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1110 {
1111 error(location, "Shared memory declarations cannot have layout specified", "layout");
1112 }
1113
1114 if (layoutQualifier.matrixPacking != EmpUnspecified)
1115 {
1116 error(location, "layout qualifier only valid for interface blocks",
1117 getMatrixPackingString(layoutQualifier.matrixPacking));
1118 return;
1119 }
1120
1121 if (layoutQualifier.blockStorage != EbsUnspecified)
1122 {
1123 error(location, "layout qualifier only valid for interface blocks",
1124 getBlockStorageString(layoutQualifier.blockStorage));
1125 return;
1126 }
1127
1128 if (qualifier == EvqFragmentOut)
1129 {
1130 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1131 {
1132 error(location, "invalid layout qualifier combination", "yuv");
1133 return;
1134 }
1135 }
1136 else
1137 {
1138 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1139 }
1140
Olli Etuaho95468d12017-05-04 11:14:34 +03001141 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1142 // parsing steps. So it needs to be checked here.
1143 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1144 {
1145 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1146 }
1147
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001148 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1149 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1150 {
1151 canHaveLocation = true;
1152 // We're not checking whether the uniform location is in range here since that depends on
1153 // the type of the variable.
1154 // The type can only be fully determined for non-empty declarations.
1155 }
1156 if (!canHaveLocation)
1157 {
1158 checkLocationIsNotSpecified(location, layoutQualifier);
1159 }
1160}
1161
jchen104cdac9e2017-05-08 11:01:20 +08001162void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1163 const TSourceLoc &location)
1164{
1165 if (publicType.precision != EbpHigh)
1166 {
1167 error(location, "Can only be highp", "atomic counter");
1168 }
1169 // dEQP enforces compile error if location is specified. See uniform_location.test.
1170 if (publicType.layoutQualifier.location != -1)
1171 {
1172 error(location, "location must not be set for atomic_uint", "layout");
1173 }
1174 if (publicType.layoutQualifier.binding == -1)
1175 {
1176 error(location, "no binding specified", "atomic counter");
1177 }
1178}
1179
Martin Radevb8b01222016-11-20 23:25:53 +02001180void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1181 const TSourceLoc &location)
1182{
1183 if (publicType.isUnsizedArray())
1184 {
1185 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1186 // error. It is assumed that this applies to empty declarations as well.
1187 error(location, "empty array declaration needs to specify a size", "");
1188 }
Martin Radevb8b01222016-11-20 23:25:53 +02001189}
1190
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001191// These checks are done for all declarations that are non-empty. They're done for non-empty
1192// declarations starting a declarator list, and declarators that follow an empty declaration.
1193void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1194 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001195{
Olli Etuahofa33d582015-04-09 14:33:12 +03001196 switch (publicType.qualifier)
1197 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001198 case EvqVaryingIn:
1199 case EvqVaryingOut:
1200 case EvqAttribute:
1201 case EvqVertexIn:
1202 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001203 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001204 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001205 {
1206 error(identifierLocation, "cannot be used with a structure",
1207 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001208 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001209 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001210
Jamie Madillb98c3a82015-07-23 14:26:04 -04001211 default:
1212 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001213 }
jchen10cc2a10e2017-05-03 14:05:12 +08001214 std::string reason(getBasicString(publicType.getBasicType()));
1215 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001216 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001217 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001218 {
1219 return;
1220 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001221
Andrei Volykhina5527072017-03-22 16:46:30 +03001222 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1223 publicType.qualifier != EvqConst) &&
1224 publicType.getBasicType() == EbtYuvCscStandardEXT)
1225 {
1226 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1227 getQualifierString(publicType.qualifier));
1228 return;
1229 }
1230
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001231 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1232 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001233 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1234 // But invalid shaders may still reach here with an unsized array declaration.
1235 if (!publicType.isUnsizedArray())
1236 {
1237 TType type(publicType);
1238 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1239 publicType.layoutQualifier);
1240 }
1241 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001242
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001243 // check for layout qualifier issues
1244 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001245
Martin Radev2cc85b32016-08-05 16:22:53 +03001246 if (IsImage(publicType.getBasicType()))
1247 {
1248
1249 switch (layoutQualifier.imageInternalFormat)
1250 {
1251 case EiifRGBA32F:
1252 case EiifRGBA16F:
1253 case EiifR32F:
1254 case EiifRGBA8:
1255 case EiifRGBA8_SNORM:
1256 if (!IsFloatImage(publicType.getBasicType()))
1257 {
1258 error(identifierLocation,
1259 "internal image format requires a floating image type",
1260 getBasicString(publicType.getBasicType()));
1261 return;
1262 }
1263 break;
1264 case EiifRGBA32I:
1265 case EiifRGBA16I:
1266 case EiifRGBA8I:
1267 case EiifR32I:
1268 if (!IsIntegerImage(publicType.getBasicType()))
1269 {
1270 error(identifierLocation,
1271 "internal image format requires an integer image type",
1272 getBasicString(publicType.getBasicType()));
1273 return;
1274 }
1275 break;
1276 case EiifRGBA32UI:
1277 case EiifRGBA16UI:
1278 case EiifRGBA8UI:
1279 case EiifR32UI:
1280 if (!IsUnsignedImage(publicType.getBasicType()))
1281 {
1282 error(identifierLocation,
1283 "internal image format requires an unsigned image type",
1284 getBasicString(publicType.getBasicType()));
1285 return;
1286 }
1287 break;
1288 case EiifUnspecified:
1289 error(identifierLocation, "layout qualifier", "No image internal format specified");
1290 return;
1291 default:
1292 error(identifierLocation, "layout qualifier", "unrecognized token");
1293 return;
1294 }
1295
1296 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1297 switch (layoutQualifier.imageInternalFormat)
1298 {
1299 case EiifR32F:
1300 case EiifR32I:
1301 case EiifR32UI:
1302 break;
1303 default:
1304 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1305 {
1306 error(identifierLocation, "layout qualifier",
1307 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1308 "image variables must be qualified readonly and/or writeonly");
1309 return;
1310 }
1311 break;
1312 }
1313 }
1314 else
1315 {
Olli Etuaho43364892017-02-13 16:00:12 +00001316 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001317 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1318 }
jchen104cdac9e2017-05-08 11:01:20 +08001319
1320 if (IsAtomicCounter(publicType.getBasicType()))
1321 {
1322 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1323 }
1324 else
1325 {
1326 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1327 }
Olli Etuaho43364892017-02-13 16:00:12 +00001328}
Martin Radev2cc85b32016-08-05 16:22:53 +03001329
Olli Etuaho43364892017-02-13 16:00:12 +00001330void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1331{
1332 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1333 int arraySize = type.isArray() ? type.getArraySize() : 1;
1334 if (IsImage(type.getBasicType()))
1335 {
1336 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1337 }
1338 else if (IsSampler(type.getBasicType()))
1339 {
1340 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1341 }
jchen104cdac9e2017-05-08 11:01:20 +08001342 else if (IsAtomicCounter(type.getBasicType()))
1343 {
1344 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1345 }
Olli Etuaho43364892017-02-13 16:00:12 +00001346 else
1347 {
1348 ASSERT(!IsOpaqueType(type.getBasicType()));
1349 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001350 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001351}
1352
Olli Etuaho856c4972016-08-08 11:38:39 +03001353void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1354 const TString &layoutQualifierName,
1355 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001356{
1357
1358 if (mShaderVersion < versionRequired)
1359 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001360 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001361 }
1362}
1363
Olli Etuaho856c4972016-08-08 11:38:39 +03001364bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1365 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001366{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001367 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001368 for (size_t i = 0u; i < localSize.size(); ++i)
1369 {
1370 if (localSize[i] != -1)
1371 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001372 error(location,
1373 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1374 "global layout declaration",
1375 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001376 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001377 }
1378 }
1379
Olli Etuaho8a176262016-08-16 14:23:01 +03001380 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001381}
1382
Olli Etuaho43364892017-02-13 16:00:12 +00001383void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001384 TLayoutImageInternalFormat internalFormat)
1385{
1386 if (internalFormat != EiifUnspecified)
1387 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001388 error(location, "invalid layout qualifier: only valid when used with images",
1389 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001390 }
Olli Etuaho43364892017-02-13 16:00:12 +00001391}
1392
1393void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1394{
1395 if (binding != -1)
1396 {
1397 error(location,
1398 "invalid layout qualifier: only valid when used with opaque types or blocks",
1399 "binding");
1400 }
1401}
1402
jchen104cdac9e2017-05-08 11:01:20 +08001403void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1404{
1405 if (offset != -1)
1406 {
1407 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1408 "offset");
1409 }
1410}
1411
Olli Etuaho43364892017-02-13 16:00:12 +00001412void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1413{
1414 // Expects arraySize to be 1 when setting binding for only a single variable.
1415 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1416 {
1417 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1418 }
1419}
1420
1421void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1422 int binding,
1423 int arraySize)
1424{
1425 // Expects arraySize to be 1 when setting binding for only a single variable.
1426 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1427 {
1428 error(location, "sampler binding greater than maximum texture units", "binding");
1429 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001430}
1431
jchen10af713a22017-04-19 09:10:56 +08001432void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1433{
1434 int size = (arraySize == 0 ? 1 : arraySize);
1435 if (binding + size > mMaxUniformBufferBindings)
1436 {
1437 error(location, "interface block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1438 "binding");
1439 }
1440}
jchen104cdac9e2017-05-08 11:01:20 +08001441void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1442{
1443 if (binding >= mMaxAtomicCounterBindings)
1444 {
1445 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1446 "binding");
1447 }
1448}
jchen10af713a22017-04-19 09:10:56 +08001449
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001450void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1451 int objectLocationCount,
1452 const TLayoutQualifier &layoutQualifier)
1453{
1454 int loc = layoutQualifier.location;
1455 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1456 {
1457 error(location, "Uniform location out of range", "location");
1458 }
1459}
1460
Andrei Volykhina5527072017-03-22 16:46:30 +03001461void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1462{
1463 if (yuv != false)
1464 {
1465 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1466 }
1467}
1468
Olli Etuaho383b7912016-08-05 11:22:59 +03001469void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001470 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001471{
1472 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1473 {
1474 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1475 if (qual == EvqOut || qual == EvqInOut)
1476 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001477 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001478 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001479 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001480 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001481 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001482 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001483 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001484 }
1485 }
1486 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001487}
1488
Martin Radev70866b82016-07-22 15:27:42 +03001489void TParseContext::checkInvariantVariableQualifier(bool invariant,
1490 const TQualifier qualifier,
1491 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001492{
Martin Radev70866b82016-07-22 15:27:42 +03001493 if (!invariant)
1494 return;
1495
1496 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001497 {
Martin Radev70866b82016-07-22 15:27:42 +03001498 // input variables in the fragment shader can be also qualified as invariant
1499 if (!sh::CanBeInvariantESSL1(qualifier))
1500 {
1501 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1502 }
1503 }
1504 else
1505 {
1506 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1507 {
1508 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1509 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001510 }
1511}
1512
Arun Patole7e7e68d2015-05-22 12:02:25 +05301513bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001514{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001515 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001516 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1517 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001518}
1519
Arun Patole7e7e68d2015-05-22 12:02:25 +05301520bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001521{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001522 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001523}
1524
Jamie Madillb98c3a82015-07-23 14:26:04 -04001525void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1526 const char *extName,
1527 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001528{
1529 pp::SourceLocation srcLoc;
1530 srcLoc.file = loc.first_file;
1531 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001532 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001533}
1534
Jamie Madillb98c3a82015-07-23 14:26:04 -04001535void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1536 const char *name,
1537 const char *value,
1538 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001539{
1540 pp::SourceLocation srcLoc;
1541 srcLoc.file = loc.first_file;
1542 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001543 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001544}
1545
Martin Radev4c4c8e72016-08-04 12:25:34 +03001546sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001547{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001548 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001549 for (size_t i = 0u; i < result.size(); ++i)
1550 {
1551 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1552 {
1553 result[i] = 1;
1554 }
1555 else
1556 {
1557 result[i] = mComputeShaderLocalSize[i];
1558 }
1559 }
1560 return result;
1561}
1562
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001563/////////////////////////////////////////////////////////////////////////////////
1564//
1565// Non-Errors.
1566//
1567/////////////////////////////////////////////////////////////////////////////////
1568
Jamie Madill5c097022014-08-20 16:38:32 -04001569const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1570 const TString *name,
1571 const TSymbol *symbol)
1572{
Yunchao Hed7297bf2017-04-19 15:27:10 +08001573 const TVariable *variable = nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001574
1575 if (!symbol)
1576 {
1577 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001578 }
1579 else if (!symbol->isVariable())
1580 {
1581 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001582 }
1583 else
1584 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001585 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001586
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001587 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001588 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001589 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001590 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001591 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001592
1593 // Reject shaders using both gl_FragData and gl_FragColor
1594 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001595 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001596 {
1597 mUsesFragData = true;
1598 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001599 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001600 {
1601 mUsesFragColor = true;
1602 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001603 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1604 {
1605 mUsesSecondaryOutputs = true;
1606 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001607
1608 // This validation is not quite correct - it's only an error to write to
1609 // both FragData and FragColor. For simplicity, and because users shouldn't
1610 // be rewarded for reading from undefined varaibles, return an error
1611 // if they are both referenced, rather than assigned.
1612 if (mUsesFragData && mUsesFragColor)
1613 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001614 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1615 if (mUsesSecondaryOutputs)
1616 {
1617 errorMessage =
1618 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1619 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1620 }
1621 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001622 }
Martin Radevb0883602016-08-04 17:48:58 +03001623
1624 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1625 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1626 qualifier == EvqWorkGroupSize)
1627 {
1628 error(location,
1629 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1630 "gl_WorkGroupSize");
1631 }
Jamie Madill5c097022014-08-20 16:38:32 -04001632 }
1633
1634 if (!variable)
1635 {
1636 TType type(EbtFloat, EbpUndefined);
1637 TVariable *fakeVariable = new TVariable(name, type);
1638 symbolTable.declare(fakeVariable);
1639 variable = fakeVariable;
1640 }
1641
1642 return variable;
1643}
1644
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001645TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1646 const TString *name,
1647 const TSymbol *symbol)
1648{
1649 const TVariable *variable = getNamedVariable(location, name, symbol);
1650
Olli Etuaho09b04a22016-12-15 13:30:26 +00001651 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1652 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1653 {
1654 // WEBGL_multiview spec
1655 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1656 "gl_ViewID_OVR");
1657 }
1658
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001659 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001660 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001661 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001662 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001663 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001664 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1665 mComputeShaderLocalSizeDeclared)
1666 {
1667 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1668 // needs to be added to the AST as a constant and not as a symbol.
1669 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1670 TConstantUnion *constArray = new TConstantUnion[3];
1671 for (size_t i = 0; i < 3; ++i)
1672 {
1673 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1674 }
1675
1676 ASSERT(variable->getType().getBasicType() == EbtUInt);
1677 ASSERT(variable->getType().getObjectSize() == 3);
1678
1679 TType type(variable->getType());
1680 type.setQualifier(EvqConst);
1681 return intermediate.addConstantUnion(constArray, type, location);
1682 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001683 else
1684 {
1685 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1686 variable->getType(), location);
1687 }
1688}
1689
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690// Initializers show up in several places in the grammar. Have one set of
1691// code to handle them here.
1692//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001693// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001694bool TParseContext::executeInitializer(const TSourceLoc &line,
1695 const TString &identifier,
1696 const TPublicType &pType,
1697 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001698 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001699{
Olli Etuaho13389b62016-10-16 11:48:18 +01001700 ASSERT(initNode != nullptr);
1701 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001702 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001703
Olli Etuaho2935c582015-04-08 14:32:06 +03001704 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001705 if (type.isUnsizedArray())
1706 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001707 // We have not checked yet whether the initializer actually is an array or not.
1708 if (initializer->isArray())
1709 {
1710 type.setArraySize(initializer->getArraySize());
1711 }
1712 else
1713 {
1714 // Having a non-array initializer for an unsized array will result in an error later,
1715 // so we don't generate an error message here.
1716 type.setArraySize(1u);
1717 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001718 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001719 if (!declareVariable(line, identifier, type, &variable))
1720 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001721 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001722 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723
Olli Etuahob0c645e2015-05-12 14:25:36 +03001724 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001725 if (symbolTable.atGlobalLevel() &&
1726 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001727 {
1728 // Error message does not completely match behavior with ESSL 1.00, but
1729 // we want to steer developers towards only using constant expressions.
1730 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001731 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001732 }
1733 if (globalInitWarning)
1734 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001735 warning(
1736 line,
1737 "global variable initializers should be constant expressions "
1738 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1739 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001740 }
1741
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001742 //
1743 // identifier must be of type constant, a global, or a temporary
1744 //
1745 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301746 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1747 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001748 error(line, " cannot initialize this type of qualifier ",
1749 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001750 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001751 }
1752 //
1753 // test for and propagate constant
1754 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755
Arun Patole7e7e68d2015-05-22 12:02:25 +05301756 if (qualifier == EvqConst)
1757 {
1758 if (qualifier != initializer->getType().getQualifier())
1759 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001760 std::stringstream reasonStream;
1761 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1762 << "'";
1763 std::string reason = reasonStream.str();
1764 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001765 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001766 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001767 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301768 if (type != initializer->getType())
1769 {
1770 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001771 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001772 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001773 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001774 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001775
1776 // Save the constant folded value to the variable if possible. For example array
1777 // initializers are not folded, since that way copying the array literal to multiple places
1778 // in the shader is avoided.
1779 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1780 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301781 if (initializer->getAsConstantUnion())
1782 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001783 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001784 ASSERT(*initNode == nullptr);
1785 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301786 }
1787 else if (initializer->getAsSymbolNode())
1788 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001789 const TSymbol *symbol =
1790 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1791 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001792
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001793 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001794 if (constArray)
1795 {
1796 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001797 ASSERT(*initNode == nullptr);
1798 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001799 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001800 }
1801 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001802
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001803 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1804 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001805 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1806 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001807 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001808 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001809 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001810 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811
Olli Etuaho914b79a2017-06-19 16:03:19 +03001812 return true;
1813}
1814
1815TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1816 const TString &identifier,
1817 TIntermTyped *initializer,
1818 const TSourceLoc &loc)
1819{
1820 checkIsScalarBool(loc, pType);
1821 TIntermBinary *initNode = nullptr;
1822 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1823 {
1824 // The initializer is valid. The init condition needs to have a node - either the
1825 // initializer node, or a constant node in case the initialized variable is const and won't
1826 // be recorded in the AST.
1827 if (initNode == nullptr)
1828 {
1829 return initializer;
1830 }
1831 else
1832 {
1833 TIntermDeclaration *declaration = new TIntermDeclaration();
1834 declaration->appendDeclarator(initNode);
1835 return declaration;
1836 }
1837 }
1838 return nullptr;
1839}
1840
1841TIntermNode *TParseContext::addLoop(TLoopType type,
1842 TIntermNode *init,
1843 TIntermNode *cond,
1844 TIntermTyped *expr,
1845 TIntermNode *body,
1846 const TSourceLoc &line)
1847{
1848 TIntermNode *node = nullptr;
1849 TIntermTyped *typedCond = nullptr;
1850 if (cond)
1851 {
1852 typedCond = cond->getAsTyped();
1853 }
1854 if (cond == nullptr || typedCond)
1855 {
Olli Etuahocce89652017-06-19 16:04:09 +03001856 if (type == ELoopDoWhile)
1857 {
1858 checkIsScalarBool(line, typedCond);
1859 }
1860 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1861 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1862 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1863 !typedCond->isVector()));
1864
Olli Etuaho914b79a2017-06-19 16:03:19 +03001865 node = new TIntermLoop(type, init, typedCond, expr, TIntermediate::EnsureBlock(body));
1866 node->setLine(line);
1867 return node;
1868 }
1869
Olli Etuahocce89652017-06-19 16:04:09 +03001870 ASSERT(type != ELoopDoWhile);
1871
Olli Etuaho914b79a2017-06-19 16:03:19 +03001872 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1873 ASSERT(declaration);
1874 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1875 ASSERT(declarator->getLeft()->getAsSymbolNode());
1876
1877 // The condition is a declaration. In the AST representation we don't support declarations as
1878 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1879 // the loop.
1880 TIntermBlock *block = new TIntermBlock();
1881
1882 TIntermDeclaration *declareCondition = new TIntermDeclaration();
1883 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
1884 block->appendStatement(declareCondition);
1885
1886 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
1887 declarator->getRight()->deepCopy());
1888 TIntermLoop *loop =
1889 new TIntermLoop(type, init, conditionInit, expr, TIntermediate::EnsureBlock(body));
1890 block->appendStatement(loop);
1891 loop->setLine(line);
1892 block->setLine(line);
1893 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894}
1895
Olli Etuahocce89652017-06-19 16:04:09 +03001896TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
1897 TIntermNodePair code,
1898 const TSourceLoc &loc)
1899{
1900 checkIsScalarBool(loc, cond);
1901
1902 // For compile time constant conditions, prune the code now.
1903 if (cond->getAsConstantUnion())
1904 {
1905 if (cond->getAsConstantUnion()->getBConst(0) == true)
1906 {
1907 return TIntermediate::EnsureBlock(code.node1);
1908 }
1909 else
1910 {
1911 return TIntermediate::EnsureBlock(code.node2);
1912 }
1913 }
1914
1915 TIntermIfElse *node = new TIntermIfElse(cond, TIntermediate::EnsureBlock(code.node1),
1916 TIntermediate::EnsureBlock(code.node2));
1917 node->setLine(loc);
1918
1919 return node;
1920}
1921
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001922void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1923{
1924 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1925 typeSpecifier->getBasicType());
1926
1927 if (mShaderVersion < 300 && typeSpecifier->array)
1928 {
1929 error(typeSpecifier->getLine(), "not supported", "first-class array");
1930 typeSpecifier->clearArrayness();
1931 }
1932}
1933
Martin Radev70866b82016-07-22 15:27:42 +03001934TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301935 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001936{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001937 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001938
Martin Radev70866b82016-07-22 15:27:42 +03001939 TPublicType returnType = typeSpecifier;
1940 returnType.qualifier = typeQualifier.qualifier;
1941 returnType.invariant = typeQualifier.invariant;
1942 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001943 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001944 returnType.precision = typeSpecifier.precision;
1945
1946 if (typeQualifier.precision != EbpUndefined)
1947 {
1948 returnType.precision = typeQualifier.precision;
1949 }
1950
Martin Radev4a9cd802016-09-01 16:51:51 +03001951 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1952 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001953
Martin Radev4a9cd802016-09-01 16:51:51 +03001954 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1955 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001956
Martin Radev4a9cd802016-09-01 16:51:51 +03001957 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001958
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001959 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001960 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001961 if (typeSpecifier.array)
1962 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001963 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001964 returnType.clearArrayness();
1965 }
1966
Martin Radev70866b82016-07-22 15:27:42 +03001967 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001968 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001969 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001970 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001971 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001972 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001973
Martin Radev70866b82016-07-22 15:27:42 +03001974 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001975 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001976 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001977 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001978 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001979 }
1980 }
1981 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001982 {
Martin Radev70866b82016-07-22 15:27:42 +03001983 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001984 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001985 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001986 }
Martin Radev70866b82016-07-22 15:27:42 +03001987 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1988 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001989 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001990 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1991 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001992 }
Martin Radev70866b82016-07-22 15:27:42 +03001993 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001994 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001995 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001996 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001997 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001998 }
1999
2000 return returnType;
2001}
2002
Olli Etuaho856c4972016-08-08 11:38:39 +03002003void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2004 const TPublicType &type,
2005 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002006{
2007 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002008 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002009 {
2010 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002011 }
2012
2013 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2014 switch (qualifier)
2015 {
2016 case EvqVertexIn:
2017 // ESSL 3.00 section 4.3.4
2018 if (type.array)
2019 {
2020 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002021 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002022 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002023 return;
2024 case EvqFragmentOut:
2025 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002026 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002027 {
2028 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002029 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002030 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002031 return;
2032 default:
2033 break;
2034 }
2035
2036 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2037 // restrictions.
2038 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002039 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2040 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002041 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2042 {
2043 error(qualifierLocation, "must use 'flat' interpolation here",
2044 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002045 }
2046
Martin Radev4a9cd802016-09-01 16:51:51 +03002047 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002048 {
2049 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2050 // These restrictions are only implied by the ESSL 3.00 spec, but
2051 // the ESSL 3.10 spec lists these restrictions explicitly.
2052 if (type.array)
2053 {
2054 error(qualifierLocation, "cannot be an array of structures",
2055 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002056 }
2057 if (type.isStructureContainingArrays())
2058 {
2059 error(qualifierLocation, "cannot be a structure containing an array",
2060 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002061 }
2062 if (type.isStructureContainingType(EbtStruct))
2063 {
2064 error(qualifierLocation, "cannot be a structure containing a structure",
2065 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002066 }
2067 if (type.isStructureContainingType(EbtBool))
2068 {
2069 error(qualifierLocation, "cannot be a structure containing a bool",
2070 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002071 }
2072 }
2073}
2074
Martin Radev2cc85b32016-08-05 16:22:53 +03002075void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2076{
2077 if (qualifier.getType() == QtStorage)
2078 {
2079 const TStorageQualifierWrapper &storageQualifier =
2080 static_cast<const TStorageQualifierWrapper &>(qualifier);
2081 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2082 !symbolTable.atGlobalLevel())
2083 {
2084 error(storageQualifier.getLine(),
2085 "Local variables can only use the const storage qualifier.",
2086 storageQualifier.getQualifierString().c_str());
2087 }
2088 }
2089}
2090
Olli Etuaho43364892017-02-13 16:00:12 +00002091void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002092 const TSourceLoc &location)
2093{
2094 if (memoryQualifier.readonly)
2095 {
2096 error(location, "Only allowed with images.", "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002097 }
2098 if (memoryQualifier.writeonly)
2099 {
2100 error(location, "Only allowed with images.", "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002101 }
Martin Radev049edfa2016-11-11 14:35:37 +02002102 if (memoryQualifier.coherent)
2103 {
2104 error(location, "Only allowed with images.", "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002105 }
2106 if (memoryQualifier.restrictQualifier)
2107 {
2108 error(location, "Only allowed with images.", "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002109 }
2110 if (memoryQualifier.volatileQualifier)
2111 {
2112 error(location, "Only allowed with images.", "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002113 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002114}
2115
jchen104cdac9e2017-05-08 11:01:20 +08002116// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2117// intermediate tree.
2118void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2119 size_t size,
2120 bool forceAppend,
2121 const TSourceLoc &loc,
2122 TType &type)
2123{
2124 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2125 int offset;
2126 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2127 {
2128 offset = bindingState.appendSpan(size);
2129 }
2130 else
2131 {
2132 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2133 }
2134 if (offset == -1)
2135 {
2136 error(loc, "Offset overlapping", "atomic counter");
2137 return;
2138 }
2139 TLayoutQualifier qualifier = type.getLayoutQualifier();
2140 qualifier.offset = offset;
2141 type.setLayoutQualifier(qualifier);
2142}
2143
Olli Etuaho13389b62016-10-16 11:48:18 +01002144TIntermDeclaration *TParseContext::parseSingleDeclaration(
2145 TPublicType &publicType,
2146 const TSourceLoc &identifierOrTypeLocation,
2147 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002148{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002149 TType type(publicType);
2150 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2151 mDirectiveHandler.pragma().stdgl.invariantAll)
2152 {
2153 TQualifier qualifier = type.getQualifier();
2154
2155 // The directive handler has already taken care of rejecting invalid uses of this pragma
2156 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2157 // affected variable declarations:
2158 //
2159 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2160 // elsewhere, in TranslatorGLSL.)
2161 //
2162 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2163 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2164 // the way this is currently implemented we have to enable this compiler option before
2165 // parsing the shader and determining the shading language version it uses. If this were
2166 // implemented as a post-pass, the workaround could be more targeted.
2167 //
2168 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2169 // the specification, but there are desktop OpenGL drivers that expect that this is the
2170 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2171 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2172 {
2173 type.setInvariant(true);
2174 }
2175 }
2176
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002177 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2178 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002179
Olli Etuahobab4c082015-04-24 16:38:49 +03002180 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002181 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002182
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002183 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002184 if (emptyDeclaration)
2185 {
Martin Radevb8b01222016-11-20 23:25:53 +02002186 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002187 // In most cases we don't need to create a symbol node for an empty declaration.
2188 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2189 if (type.getBasicType() == EbtStruct)
2190 {
2191 symbol = intermediate.addSymbol(0, "", type, identifierOrTypeLocation);
2192 }
jchen104cdac9e2017-05-08 11:01:20 +08002193 else if (IsAtomicCounter(publicType.getBasicType()))
2194 {
2195 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2196 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002197 }
2198 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002199 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002200 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002201
Olli Etuaho856c4972016-08-08 11:38:39 +03002202 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002203
jchen104cdac9e2017-05-08 11:01:20 +08002204 if (IsAtomicCounter(publicType.getBasicType()))
2205 {
2206
2207 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2208 identifierOrTypeLocation, type);
2209 }
2210
Olli Etuaho2935c582015-04-08 14:32:06 +03002211 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002212 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002213
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002214 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002215 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002216 symbol = intermediate.addSymbol(variable->getUniqueId(), identifier, type,
2217 identifierOrTypeLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002218 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002219 }
2220
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002221 TIntermDeclaration *declaration = new TIntermDeclaration();
2222 declaration->setLine(identifierOrTypeLocation);
2223 if (symbol)
2224 {
2225 declaration->appendDeclarator(symbol);
2226 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002227 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002228}
2229
Olli Etuaho13389b62016-10-16 11:48:18 +01002230TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2231 const TSourceLoc &identifierLocation,
2232 const TString &identifier,
2233 const TSourceLoc &indexLocation,
2234 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002235{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002236 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002237
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002238 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2239 identifierLocation);
2240
2241 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002242
Olli Etuaho856c4972016-08-08 11:38:39 +03002243 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002244
Olli Etuaho8a176262016-08-16 14:23:01 +03002245 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002246
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002247 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002248
Olli Etuaho856c4972016-08-08 11:38:39 +03002249 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002250 // Make the type an array even if size check failed.
2251 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2252 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002253
jchen104cdac9e2017-05-08 11:01:20 +08002254 if (IsAtomicCounter(publicType.getBasicType()))
2255 {
2256 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2257 identifierLocation, arrayType);
2258 }
2259
Olli Etuaho2935c582015-04-08 14:32:06 +03002260 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002261 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002262
Olli Etuaho13389b62016-10-16 11:48:18 +01002263 TIntermDeclaration *declaration = new TIntermDeclaration();
2264 declaration->setLine(identifierLocation);
2265
Olli Etuahoe7847b02015-03-16 11:56:12 +02002266 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002267 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002268 {
Jamie Madill60ed9812013-06-06 11:56:46 -04002269 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002270 declaration->appendDeclarator(symbol);
2271 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002272
Olli Etuaho13389b62016-10-16 11:48:18 +01002273 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002274}
2275
Olli Etuaho13389b62016-10-16 11:48:18 +01002276TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2277 const TSourceLoc &identifierLocation,
2278 const TString &identifier,
2279 const TSourceLoc &initLocation,
2280 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002281{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002282 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002283
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002284 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2285 identifierLocation);
2286
2287 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002288
Olli Etuaho13389b62016-10-16 11:48:18 +01002289 TIntermDeclaration *declaration = new TIntermDeclaration();
2290 declaration->setLine(identifierLocation);
2291
2292 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002293 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002294 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002295 if (initNode)
2296 {
2297 declaration->appendDeclarator(initNode);
2298 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002299 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002300 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002301}
2302
Olli Etuaho13389b62016-10-16 11:48:18 +01002303TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002304 TPublicType &publicType,
2305 const TSourceLoc &identifierLocation,
2306 const TString &identifier,
2307 const TSourceLoc &indexLocation,
2308 TIntermTyped *indexExpression,
2309 const TSourceLoc &initLocation,
2310 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002311{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002312 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002313
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002314 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2315 identifierLocation);
2316
2317 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002318
Olli Etuaho8a176262016-08-16 14:23:01 +03002319 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002320
2321 TPublicType arrayType(publicType);
2322
Olli Etuaho856c4972016-08-08 11:38:39 +03002323 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002324 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2325 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002326 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002327 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002328 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002329 }
2330 // Make the type an array even if size check failed.
2331 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2332 arrayType.setArraySize(size);
2333
Olli Etuaho13389b62016-10-16 11:48:18 +01002334 TIntermDeclaration *declaration = new TIntermDeclaration();
2335 declaration->setLine(identifierLocation);
2336
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002337 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002338 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002339 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002340 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002341 if (initNode)
2342 {
2343 declaration->appendDeclarator(initNode);
2344 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002345 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002346
2347 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002348}
2349
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002350TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002351 const TTypeQualifierBuilder &typeQualifierBuilder,
2352 const TSourceLoc &identifierLoc,
2353 const TString *identifier,
2354 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002355{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002356 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002357
Martin Radev70866b82016-07-22 15:27:42 +03002358 if (!typeQualifier.invariant)
2359 {
2360 error(identifierLoc, "Expected invariant", identifier->c_str());
2361 return nullptr;
2362 }
2363 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2364 {
2365 return nullptr;
2366 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002367 if (!symbol)
2368 {
2369 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002370 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002371 }
Martin Radev70866b82016-07-22 15:27:42 +03002372 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002373 {
Martin Radev70866b82016-07-22 15:27:42 +03002374 error(identifierLoc, "invariant declaration specifies qualifier",
2375 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002376 }
Martin Radev70866b82016-07-22 15:27:42 +03002377 if (typeQualifier.precision != EbpUndefined)
2378 {
2379 error(identifierLoc, "invariant declaration specifies precision",
2380 getPrecisionString(typeQualifier.precision));
2381 }
2382 if (!typeQualifier.layoutQualifier.isEmpty())
2383 {
2384 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2385 }
2386
2387 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2388 ASSERT(variable);
2389 const TType &type = variable->getType();
2390
2391 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2392 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002393 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002394
2395 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2396
2397 TIntermSymbol *intermSymbol =
2398 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2399
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002400 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002401}
2402
Olli Etuaho13389b62016-10-16 11:48:18 +01002403void TParseContext::parseDeclarator(TPublicType &publicType,
2404 const TSourceLoc &identifierLocation,
2405 const TString &identifier,
2406 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002407{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002408 // If the declaration starting this declarator list was empty (example: int,), some checks were
2409 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002410 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002411 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002412 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2413 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002414 }
2415
Olli Etuaho856c4972016-08-08 11:38:39 +03002416 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002417
Olli Etuaho856c4972016-08-08 11:38:39 +03002418 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002419
Olli Etuaho2935c582015-04-08 14:32:06 +03002420 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002421 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002422 if (IsAtomicCounter(publicType.getBasicType()))
2423 {
2424 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2425 identifierLocation, type);
2426 }
Olli Etuaho43364892017-02-13 16:00:12 +00002427 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002428
Olli Etuaho43364892017-02-13 16:00:12 +00002429 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002430 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002431 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002432 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002433 declarationOut->appendDeclarator(symbol);
2434 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002435}
2436
Olli Etuaho13389b62016-10-16 11:48:18 +01002437void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2438 const TSourceLoc &identifierLocation,
2439 const TString &identifier,
2440 const TSourceLoc &arrayLocation,
2441 TIntermTyped *indexExpression,
2442 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002443{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002444 // If the declaration starting this declarator list was empty (example: int,), some checks were
2445 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002446 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002447 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002448 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2449 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002450 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002451
Olli Etuaho856c4972016-08-08 11:38:39 +03002452 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002453
Olli Etuaho856c4972016-08-08 11:38:39 +03002454 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002455
Olli Etuaho8a176262016-08-16 14:23:01 +03002456 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002457 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002458 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002459 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002460 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002461
jchen104cdac9e2017-05-08 11:01:20 +08002462 if (IsAtomicCounter(publicType.getBasicType()))
2463 {
2464 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2465 true, identifierLocation, arrayType);
2466 }
2467
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002468 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002469 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002470
Jamie Madillb98c3a82015-07-23 14:26:04 -04002471 TIntermSymbol *symbol =
2472 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002473 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002474 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002475
Olli Etuaho13389b62016-10-16 11:48:18 +01002476 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002477 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002478}
2479
Olli Etuaho13389b62016-10-16 11:48:18 +01002480void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2481 const TSourceLoc &identifierLocation,
2482 const TString &identifier,
2483 const TSourceLoc &initLocation,
2484 TIntermTyped *initializer,
2485 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002486{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002487 // If the declaration starting this declarator list was empty (example: int,), some checks were
2488 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002489 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002490 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002491 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2492 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002493 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002494
Olli Etuaho856c4972016-08-08 11:38:39 +03002495 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002496
Olli Etuaho13389b62016-10-16 11:48:18 +01002497 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002498 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002499 {
2500 //
2501 // build the intermediate representation
2502 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002503 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002504 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002505 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002506 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002507 }
2508}
2509
Olli Etuaho13389b62016-10-16 11:48:18 +01002510void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2511 const TSourceLoc &identifierLocation,
2512 const TString &identifier,
2513 const TSourceLoc &indexLocation,
2514 TIntermTyped *indexExpression,
2515 const TSourceLoc &initLocation,
2516 TIntermTyped *initializer,
2517 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002518{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002519 // If the declaration starting this declarator list was empty (example: int,), some checks were
2520 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002521 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002522 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002523 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2524 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002525 }
2526
Olli Etuaho856c4972016-08-08 11:38:39 +03002527 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002528
Olli Etuaho8a176262016-08-16 14:23:01 +03002529 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002530
2531 TPublicType arrayType(publicType);
2532
Olli Etuaho856c4972016-08-08 11:38:39 +03002533 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002534 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2535 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002536 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002537 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002538 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002539 }
2540 // Make the type an array even if size check failed.
2541 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2542 arrayType.setArraySize(size);
2543
2544 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002545 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002546 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002547 {
2548 if (initNode)
2549 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002550 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002551 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002552 }
2553}
2554
jchen104cdac9e2017-05-08 11:01:20 +08002555void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2556 const TSourceLoc &location)
2557{
2558 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2559 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2560 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2561 {
2562 error(location, "Requires both binding and offset", "layout");
2563 return;
2564 }
2565 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2566}
2567
Olli Etuahocce89652017-06-19 16:04:09 +03002568void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2569 const TPublicType &type,
2570 const TSourceLoc &loc)
2571{
2572 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2573 !getFragmentPrecisionHigh())
2574 {
2575 error(loc, "precision is not supported in fragment shader", "highp");
2576 }
2577
2578 if (!CanSetDefaultPrecisionOnType(type))
2579 {
2580 error(loc, "illegal type argument for default precision qualifier",
2581 getBasicString(type.getBasicType()));
2582 return;
2583 }
2584 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2585}
2586
Martin Radev70866b82016-07-22 15:27:42 +03002587void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002588{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002589 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002590 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002591
Martin Radev70866b82016-07-22 15:27:42 +03002592 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2593 typeQualifier.line);
2594
Jamie Madillc2128ff2016-07-04 10:26:17 -04002595 // It should never be the case, but some strange parser errors can send us here.
2596 if (layoutQualifier.isEmpty())
2597 {
2598 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002599 return;
2600 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002601
Martin Radev802abe02016-08-04 17:48:32 +03002602 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002603 {
Olli Etuaho43364892017-02-13 16:00:12 +00002604 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002605 return;
2606 }
2607
Olli Etuaho43364892017-02-13 16:00:12 +00002608 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2609
2610 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002611
2612 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2613
Andrei Volykhina5527072017-03-22 16:46:30 +03002614 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2615
jchen104cdac9e2017-05-08 11:01:20 +08002616 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2617
Martin Radev802abe02016-08-04 17:48:32 +03002618 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002619 {
Martin Radev802abe02016-08-04 17:48:32 +03002620 if (mComputeShaderLocalSizeDeclared &&
2621 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2622 {
2623 error(typeQualifier.line, "Work group size does not match the previous declaration",
2624 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002625 return;
2626 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002627
Martin Radev802abe02016-08-04 17:48:32 +03002628 if (mShaderVersion < 310)
2629 {
2630 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002631 return;
2632 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002633
Martin Radev4c4c8e72016-08-04 12:25:34 +03002634 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002635 {
2636 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002637 return;
2638 }
2639
2640 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2641 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2642
2643 const TConstantUnion *maxComputeWorkGroupSizeData =
2644 maxComputeWorkGroupSize->getConstPointer();
2645
2646 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2647 {
2648 if (layoutQualifier.localSize[i] != -1)
2649 {
2650 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2651 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2652 if (mComputeShaderLocalSize[i] < 1 ||
2653 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2654 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002655 std::stringstream reasonStream;
2656 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2657 << maxComputeWorkGroupSizeValue;
2658 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002659
Olli Etuaho4de340a2016-12-16 09:32:03 +00002660 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002661 return;
2662 }
2663 }
2664 }
2665
2666 mComputeShaderLocalSizeDeclared = true;
2667 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002668 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002669 {
2670 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2671 // specification.
2672 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2673 {
2674 error(typeQualifier.line, "Number of views does not match the previous declaration",
2675 "layout");
2676 return;
2677 }
2678
2679 if (layoutQualifier.numViews == -1)
2680 {
2681 error(typeQualifier.line, "No num_views specified", "layout");
2682 return;
2683 }
2684
2685 if (layoutQualifier.numViews > mMaxNumViews)
2686 {
2687 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2688 "layout");
2689 return;
2690 }
2691
2692 mNumViews = layoutQualifier.numViews;
2693 }
Martin Radev802abe02016-08-04 17:48:32 +03002694 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002695 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002696 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002697 {
Martin Radev802abe02016-08-04 17:48:32 +03002698 return;
2699 }
2700
2701 if (typeQualifier.qualifier != EvqUniform)
2702 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002703 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2704 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002705 return;
2706 }
2707
2708 if (mShaderVersion < 300)
2709 {
2710 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2711 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002712 return;
2713 }
2714
Olli Etuaho09b04a22016-12-15 13:30:26 +00002715 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002716
2717 if (layoutQualifier.matrixPacking != EmpUnspecified)
2718 {
2719 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2720 }
2721
2722 if (layoutQualifier.blockStorage != EbsUnspecified)
2723 {
2724 mDefaultBlockStorage = layoutQualifier.blockStorage;
2725 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002726 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002727}
2728
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002729TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2730 const TFunction &function,
2731 const TSourceLoc &location,
2732 bool insertParametersToSymbolTable)
2733{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03002734 checkIsNotReserved(location, function.getName());
2735
Olli Etuahofe486322017-03-21 09:30:54 +00002736 TIntermFunctionPrototype *prototype =
2737 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002738 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2739 // point to the data that already exists in the symbol table.
2740 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2741 prototype->setLine(location);
2742
2743 for (size_t i = 0; i < function.getParamCount(); i++)
2744 {
2745 const TConstParameter &param = function.getParam(i);
2746
2747 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2748 // be used for unused args).
2749 if (param.name != nullptr)
2750 {
2751 TVariable *variable = new TVariable(param.name, *param.type);
2752
2753 // Insert the parameter in the symbol table.
2754 if (insertParametersToSymbolTable && !symbolTable.declare(variable))
2755 {
2756 error(location, "redefinition", variable->getName().c_str());
2757 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2758 continue;
2759 }
2760 TIntermSymbol *symbol = intermediate.addSymbol(
2761 variable->getUniqueId(), variable->getName(), variable->getType(), location);
2762 prototype->appendParameter(symbol);
2763 }
2764 else
2765 {
2766 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2767 }
2768 }
2769 return prototype;
2770}
2771
Olli Etuaho16c745a2017-01-16 17:02:27 +00002772TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2773 const TFunction &parsedFunction,
2774 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002775{
Olli Etuaho476197f2016-10-11 13:59:08 +01002776 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2777 // first declaration. Either way the instance in the symbol table is used to track whether the
2778 // function is declared multiple times.
2779 TFunction *function = static_cast<TFunction *>(
2780 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2781 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002782 {
2783 // ESSL 1.00.17 section 4.2.7.
2784 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2785 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002786 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002787 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002788
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002789 TIntermFunctionPrototype *prototype =
2790 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002791
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002792 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002793
2794 if (!symbolTable.atGlobalLevel())
2795 {
2796 // ESSL 3.00.4 section 4.2.4.
2797 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002798 }
2799
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002800 return prototype;
2801}
2802
Olli Etuaho336b1472016-10-05 16:37:55 +01002803TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002804 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002805 TIntermBlock *functionBody,
2806 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002807{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002808 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002809 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2810 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002811 error(location, "function does not return a value:",
2812 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002813 }
2814
Olli Etuahof51fdd22016-10-03 10:03:40 +01002815 if (functionBody == nullptr)
2816 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002817 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002818 functionBody->setLine(location);
2819 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002820 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002821 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002822 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002823
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002824 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002825 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002826}
2827
Olli Etuaho476197f2016-10-11 13:59:08 +01002828void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2829 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002830 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002831{
Olli Etuaho476197f2016-10-11 13:59:08 +01002832 ASSERT(function);
2833 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002834 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002835 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002836
2837 if (builtIn)
2838 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002839 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002840 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002841 else
Jamie Madill185fb402015-06-12 15:48:48 -04002842 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002843 TFunction *prevDec = static_cast<TFunction *>(
2844 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2845
2846 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2847 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2848 // occurance.
2849 if (*function != prevDec)
2850 {
2851 // Swap the parameters of the previous declaration to the parameters of the function
2852 // definition (parameter names may differ).
2853 prevDec->swapParameters(**function);
2854
2855 // The function definition will share the same symbol as any previous declaration.
2856 *function = prevDec;
2857 }
2858
2859 if ((*function)->isDefined())
2860 {
2861 error(location, "function already has a body", (*function)->getName().c_str());
2862 }
2863
2864 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002865 }
Jamie Madill185fb402015-06-12 15:48:48 -04002866
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002867 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002868 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002869 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002870
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002871 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002872 setLoopNestingLevel(0);
2873}
2874
Jamie Madillb98c3a82015-07-23 14:26:04 -04002875TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002876{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002877 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002878 // We don't know at this point whether this is a function definition or a prototype.
2879 // The definition production code will check for redefinitions.
2880 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002881 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002882 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2883 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002884 //
2885 TFunction *prevDec =
2886 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302887
Martin Radevda6254b2016-12-14 17:00:36 +02002888 if (getShaderVersion() >= 300 &&
2889 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2890 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302891 {
Martin Radevda6254b2016-12-14 17:00:36 +02002892 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302893 // Therefore overloading or redefining builtin functions is an error.
2894 error(location, "Name of a built-in function cannot be redeclared as function",
2895 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302896 }
2897 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002898 {
2899 if (prevDec->getReturnType() != function->getReturnType())
2900 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002901 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002902 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002903 }
2904 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2905 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002906 if (prevDec->getParam(i).type->getQualifier() !=
2907 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002908 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002909 error(location,
2910 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002911 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002912 }
2913 }
2914 }
2915
2916 //
2917 // Check for previously declared variables using the same name.
2918 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002919 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002920 if (prevSym)
2921 {
2922 if (!prevSym->isFunction())
2923 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002924 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002925 }
2926 }
2927 else
2928 {
2929 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002930 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002931 }
2932
2933 // We're at the inner scope level of the function's arguments and body statement.
2934 // Add the function prototype to the surrounding scope instead.
2935 symbolTable.getOuterLevel()->insert(function);
2936
Olli Etuaho78d13742017-01-18 13:06:10 +00002937 // Raise error message if main function takes any parameters or return anything other than void
2938 if (function->getName() == "main")
2939 {
2940 if (function->getParamCount() > 0)
2941 {
2942 error(location, "function cannot take any parameter(s)", "main");
2943 }
2944 if (function->getReturnType().getBasicType() != EbtVoid)
2945 {
2946 error(location, "main function cannot return a value",
2947 function->getReturnType().getBasicString());
2948 }
2949 }
2950
Jamie Madill185fb402015-06-12 15:48:48 -04002951 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002952 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2953 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002954 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2955 //
2956 return function;
2957}
2958
Olli Etuaho9de84a52016-06-14 17:36:01 +03002959TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2960 const TString *name,
2961 const TSourceLoc &location)
2962{
2963 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2964 {
2965 error(location, "no qualifiers allowed for function return",
2966 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002967 }
2968 if (!type.layoutQualifier.isEmpty())
2969 {
2970 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002971 }
jchen10cc2a10e2017-05-03 14:05:12 +08002972 // make sure an opaque type is not involved as well...
2973 std::string reason(getBasicString(type.getBasicType()));
2974 reason += "s can't be function return values";
2975 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002976 if (mShaderVersion < 300)
2977 {
2978 // Array return values are forbidden, but there's also no valid syntax for declaring array
2979 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002980 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002981
2982 if (type.isStructureContainingArrays())
2983 {
2984 // ESSL 1.00.17 section 6.1 Function Definitions
2985 error(location, "structures containing arrays can't be function return values",
2986 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002987 }
2988 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002989
2990 // Add the function as a prototype after parsing it (we do not support recursion)
2991 return new TFunction(name, new TType(type));
2992}
2993
Olli Etuahocce89652017-06-19 16:04:09 +03002994TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
2995{
Olli Etuahocce89652017-06-19 16:04:09 +03002996 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
2997 return new TFunction(name, returnType);
2998}
2999
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003000TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003001{
Olli Etuahocce89652017-06-19 16:04:09 +03003002 if (mShaderVersion < 300 && publicType.array)
3003 {
3004 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3005 "[]");
3006 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003007 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003008 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003009 error(publicType.getLine(), "constructor can't be a structure definition",
3010 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003011 }
3012
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003013 TType *type = new TType(publicType);
3014 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003015 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003016 error(publicType.getLine(), "cannot construct this type",
3017 getBasicString(publicType.getBasicType()));
3018 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003019 }
3020
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003021 return new TFunction(nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003022}
3023
Olli Etuahocce89652017-06-19 16:04:09 +03003024TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3025 const TString *name,
3026 const TSourceLoc &nameLoc)
3027{
3028 if (publicType.getBasicType() == EbtVoid)
3029 {
3030 error(nameLoc, "illegal use of type 'void'", name->c_str());
3031 }
3032 checkIsNotReserved(nameLoc, *name);
3033 TType *type = new TType(publicType);
3034 TParameter param = {name, type};
3035 return param;
3036}
3037
3038TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3039 const TSourceLoc &identifierLoc,
3040 TIntermTyped *arraySize,
3041 const TSourceLoc &arrayLoc,
3042 TPublicType *type)
3043{
3044 checkIsValidTypeForArray(arrayLoc, *type);
3045 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3046 type->setArraySize(size);
3047 return parseParameterDeclarator(*type, identifier, identifierLoc);
3048}
3049
Jamie Madillb98c3a82015-07-23 14:26:04 -04003050// This function is used to test for the correctness of the parameters passed to various constructor
3051// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003052//
Olli Etuaho856c4972016-08-08 11:38:39 +03003053// 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 +00003054//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003055TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003056 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303057 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003058{
Olli Etuaho856c4972016-08-08 11:38:39 +03003059 if (type.isUnsizedArray())
3060 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003061 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003062 {
3063 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3064 type.setArraySize(1u);
3065 return TIntermTyped::CreateZero(type);
3066 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003067 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03003068 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003069
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003070 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003071 {
Olli Etuaho72d10202017-01-19 15:58:30 +00003072 return TIntermTyped::CreateZero(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003073 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003074
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003075 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003076 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003077
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003078 TIntermTyped *constConstructor =
3079 intermediate.foldAggregateBuiltIn(constructorNode, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003080 if (constConstructor)
3081 {
3082 return constConstructor;
3083 }
3084
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003085 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003086}
3087
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003088//
3089// Interface/uniform blocks
3090//
Olli Etuaho13389b62016-10-16 11:48:18 +01003091TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003092 const TTypeQualifierBuilder &typeQualifierBuilder,
3093 const TSourceLoc &nameLine,
3094 const TString &blockName,
3095 TFieldList *fieldList,
3096 const TString *instanceName,
3097 const TSourceLoc &instanceLine,
3098 TIntermTyped *arrayIndex,
3099 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003100{
Olli Etuaho856c4972016-08-08 11:38:39 +03003101 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003102
Olli Etuaho77ba4082016-12-16 12:01:18 +00003103 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003104
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003105 if (typeQualifier.qualifier != EvqUniform)
3106 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003107 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
3108 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003109 }
3110
Martin Radev70866b82016-07-22 15:27:42 +03003111 if (typeQualifier.invariant)
3112 {
3113 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3114 }
3115
Olli Etuaho43364892017-02-13 16:00:12 +00003116 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3117
jchen10af713a22017-04-19 09:10:56 +08003118 // add array index
3119 unsigned int arraySize = 0;
3120 if (arrayIndex != nullptr)
3121 {
3122 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3123 }
3124
3125 if (mShaderVersion < 310)
3126 {
3127 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3128 }
3129 else
3130 {
3131 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.layoutQualifier.binding,
3132 arraySize);
3133 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003134
Andrei Volykhina5527072017-03-22 16:46:30 +03003135 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3136
Jamie Madill099c0f32013-06-20 11:55:52 -04003137 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003138 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003139
Jamie Madill099c0f32013-06-20 11:55:52 -04003140 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3141 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003142 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003143 }
3144
Jamie Madill1566ef72013-06-20 11:55:54 -04003145 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3146 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003147 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04003148 }
3149
Olli Etuaho856c4972016-08-08 11:38:39 +03003150 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003151
Martin Radev2cc85b32016-08-05 16:22:53 +03003152 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3153
Arun Patole7e7e68d2015-05-22 12:02:25 +05303154 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
3155 if (!symbolTable.declare(blockNameSymbol))
3156 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003157 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003158 }
3159
Jamie Madill98493dd2013-07-08 14:39:03 -04003160 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303161 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3162 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003163 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303164 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003165 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303166 {
jchen10cc2a10e2017-05-03 14:05:12 +08003167 std::string reason("unsupported type - ");
3168 reason += fieldType->getBasicString();
3169 reason += " types are not allowed in interface blocks";
3170 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003171 }
3172
Jamie Madill98493dd2013-07-08 14:39:03 -04003173 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003174 switch (qualifier)
3175 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003176 case EvqGlobal:
3177 case EvqUniform:
3178 break;
3179 default:
3180 error(field->line(), "invalid qualifier on interface block member",
3181 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003182 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003183 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003184
Martin Radev70866b82016-07-22 15:27:42 +03003185 if (fieldType->isInvariant())
3186 {
3187 error(field->line(), "invalid qualifier on interface block member", "invariant");
3188 }
3189
Jamie Madilla5efff92013-06-06 11:56:47 -04003190 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003191 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003192 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003193 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003194
Jamie Madill98493dd2013-07-08 14:39:03 -04003195 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003196 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003197 error(field->line(), "invalid layout qualifier: cannot be used here",
3198 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003199 }
3200
Jamie Madill98493dd2013-07-08 14:39:03 -04003201 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003202 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003203 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003204 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003205 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003206 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003207 warning(field->line(),
3208 "extraneous layout qualifier: only has an effect on matrix types",
3209 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003210 }
3211
Jamie Madill98493dd2013-07-08 14:39:03 -04003212 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003213 }
3214
Jamie Madillb98c3a82015-07-23 14:26:04 -04003215 TInterfaceBlock *interfaceBlock =
3216 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
3217 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
3218 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003219
3220 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003221 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003222
Jamie Madill98493dd2013-07-08 14:39:03 -04003223 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003224 {
3225 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003226 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3227 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003228 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303229 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003230
3231 // set parent pointer of the field variable
3232 fieldType->setInterfaceBlock(interfaceBlock);
3233
Arun Patole7e7e68d2015-05-22 12:02:25 +05303234 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003235 fieldVariable->setQualifier(typeQualifier.qualifier);
3236
Arun Patole7e7e68d2015-05-22 12:02:25 +05303237 if (!symbolTable.declare(fieldVariable))
3238 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003239 error(field->line(), "redefinition of an interface block member name",
3240 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003241 }
3242 }
3243 }
3244 else
3245 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003246 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003247
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003248 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05303249 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003250 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04003251
Arun Patole7e7e68d2015-05-22 12:02:25 +05303252 if (!symbolTable.declare(instanceTypeDef))
3253 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003254 error(instanceLine, "redefinition of an interface block instance name",
3255 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003256 }
3257
Jamie Madillb98c3a82015-07-23 14:26:04 -04003258 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003259 symbolName = instanceTypeDef->getName();
3260 }
3261
Olli Etuaho13389b62016-10-16 11:48:18 +01003262 TIntermSymbol *blockSymbol =
3263 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
3264 TIntermDeclaration *declaration = new TIntermDeclaration();
3265 declaration->appendDeclarator(blockSymbol);
3266 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003267
3268 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003269 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003270}
3271
Olli Etuaho383b7912016-08-05 11:22:59 +03003272void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003273{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003274 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003275
3276 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003277 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303278 if (mStructNestingLevel > 1)
3279 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003280 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003281 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003282}
3283
3284void TParseContext::exitStructDeclaration()
3285{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003286 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003287}
3288
Olli Etuaho8a176262016-08-16 14:23:01 +03003289void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003290{
Jamie Madillacb4b812016-11-07 13:50:29 -05003291 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303292 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003293 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003294 }
3295
Arun Patole7e7e68d2015-05-22 12:02:25 +05303296 if (field.type()->getBasicType() != EbtStruct)
3297 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003298 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003299 }
3300
3301 // We're already inside a structure definition at this point, so add
3302 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303303 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3304 {
Jamie Madill41a49272014-03-18 16:10:13 -04003305 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003306 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3307 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003308 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003309 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003310 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003311 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003312}
3313
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003314//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003315// Parse an array index expression
3316//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003317TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3318 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303319 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003320{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003321 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3322 {
3323 if (baseExpression->getAsSymbolNode())
3324 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303325 error(location, " left of '[' is not of type array, matrix, or vector ",
3326 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003327 }
3328 else
3329 {
3330 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3331 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003332
3333 TConstantUnion *unionArray = new TConstantUnion[1];
3334 unionArray->setFConst(0.0f);
3335 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
3336 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003337 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003338
Jamie Madill21c1e452014-12-29 11:33:41 -05003339 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3340
Olli Etuaho36b05142015-11-12 13:10:42 +02003341 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3342 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3343 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3344 // index is a constant expression.
3345 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3346 {
3347 if (baseExpression->isInterfaceBlock())
3348 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003349 error(location,
3350 "array indexes for interface blocks arrays must be constant integral expressions",
3351 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003352 }
3353 else if (baseExpression->getQualifier() == EvqFragmentOut)
3354 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003355 error(location,
3356 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003357 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003358 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3359 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003360 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003361 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003362 }
3363
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003364 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003365 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003366 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3367 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3368 // constant fold expressions that are not constant expressions). The most compatible way to
3369 // handle this case is to report a warning instead of an error and force the index to be in
3370 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003371 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003372 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003373
3374 int safeIndex = -1;
3375
3376 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003377 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003378 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003379 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003380 if (mShaderSpec == SH_WEBGL2_SPEC)
3381 {
3382 // Error has been already generated if index is not const.
3383 if (indexExpression->getQualifier() == EvqConst)
3384 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003385 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003386 }
3387 safeIndex = 0;
3388 }
3389 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3390 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003391 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003392 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003393 "GL_EXT_draw_buffers is disabled",
3394 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003395 safeIndex = 0;
3396 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003397 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003398 // Only do generic out-of-range check if similar error hasn't already been reported.
3399 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003400 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003401 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3402 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003403 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003404 }
3405 }
3406 else if (baseExpression->isMatrix())
3407 {
3408 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003409 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003410 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003411 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003412 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003413 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003414 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3415 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003416 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003417 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003418
3419 ASSERT(safeIndex >= 0);
3420 // Data of constant unions can't be changed, because it may be shared with other
3421 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3422 // sanitized object.
3423 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003424 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003425 TConstantUnion *safeConstantUnion = new TConstantUnion();
3426 safeConstantUnion->setIConst(safeIndex);
3427 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003428 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003429
3430 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003431 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003432 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003433 else
3434 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003435 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003436 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003437 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003438}
3439
Olli Etuaho90892fb2016-07-14 14:44:51 +03003440int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3441 const TSourceLoc &location,
3442 int index,
3443 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003444 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003445{
3446 if (index >= arraySize || index < 0)
3447 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003448 std::stringstream reasonStream;
3449 reasonStream << reason << " '" << index << "'";
3450 std::string token = reasonStream.str();
3451 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003452 if (index < 0)
3453 {
3454 return 0;
3455 }
3456 else
3457 {
3458 return arraySize - 1;
3459 }
3460 }
3461 return index;
3462}
3463
Jamie Madillb98c3a82015-07-23 14:26:04 -04003464TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3465 const TSourceLoc &dotLocation,
3466 const TString &fieldString,
3467 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003468{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003469 if (baseExpression->isArray())
3470 {
3471 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003472 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003473 }
3474
3475 if (baseExpression->isVector())
3476 {
3477 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003478 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3479 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003480 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003481 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003482 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003483 }
3484
Olli Etuahob6fa0432016-09-28 16:28:05 +01003485 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003486 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003487 else if (baseExpression->getBasicType() == EbtStruct)
3488 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303489 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003490 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003491 {
3492 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003493 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003494 }
3495 else
3496 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003497 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003498 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003499 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003500 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003501 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003502 {
3503 fieldFound = true;
3504 break;
3505 }
3506 }
3507 if (fieldFound)
3508 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003509 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3510 index->setLine(fieldLocation);
3511 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003512 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003513 }
3514 else
3515 {
3516 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003517 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003518 }
3519 }
3520 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003521 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003522 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303523 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003524 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003525 {
3526 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003527 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003528 }
3529 else
3530 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003531 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003532 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003533 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003534 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003535 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003536 {
3537 fieldFound = true;
3538 break;
3539 }
3540 }
3541 if (fieldFound)
3542 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003543 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3544 index->setLine(fieldLocation);
3545 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003546 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003547 }
3548 else
3549 {
3550 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003551 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003552 }
3553 }
3554 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003555 else
3556 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003557 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003558 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003559 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303560 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003561 }
3562 else
3563 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303564 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003565 " field selection requires structure, vector, or interface block on left hand "
3566 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303567 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003568 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003569 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003570 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003571}
3572
Jamie Madillb98c3a82015-07-23 14:26:04 -04003573TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3574 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003575{
Martin Radev802abe02016-08-04 17:48:32 +03003576 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003577
3578 if (qualifierType == "shared")
3579 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003580 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003581 {
3582 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3583 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003584 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003585 }
3586 else if (qualifierType == "packed")
3587 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003588 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003589 {
3590 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3591 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003592 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003593 }
3594 else if (qualifierType == "std140")
3595 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003596 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003597 }
3598 else if (qualifierType == "row_major")
3599 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003600 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003601 }
3602 else if (qualifierType == "column_major")
3603 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003604 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003605 }
3606 else if (qualifierType == "location")
3607 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003608 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3609 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003610 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003611 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3612 mShaderType == GL_FRAGMENT_SHADER)
3613 {
3614 qualifier.yuv = true;
3615 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003616 else if (qualifierType == "rgba32f")
3617 {
3618 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3619 qualifier.imageInternalFormat = EiifRGBA32F;
3620 }
3621 else if (qualifierType == "rgba16f")
3622 {
3623 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3624 qualifier.imageInternalFormat = EiifRGBA16F;
3625 }
3626 else if (qualifierType == "r32f")
3627 {
3628 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3629 qualifier.imageInternalFormat = EiifR32F;
3630 }
3631 else if (qualifierType == "rgba8")
3632 {
3633 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3634 qualifier.imageInternalFormat = EiifRGBA8;
3635 }
3636 else if (qualifierType == "rgba8_snorm")
3637 {
3638 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3639 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3640 }
3641 else if (qualifierType == "rgba32i")
3642 {
3643 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3644 qualifier.imageInternalFormat = EiifRGBA32I;
3645 }
3646 else if (qualifierType == "rgba16i")
3647 {
3648 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3649 qualifier.imageInternalFormat = EiifRGBA16I;
3650 }
3651 else if (qualifierType == "rgba8i")
3652 {
3653 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3654 qualifier.imageInternalFormat = EiifRGBA8I;
3655 }
3656 else if (qualifierType == "r32i")
3657 {
3658 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3659 qualifier.imageInternalFormat = EiifR32I;
3660 }
3661 else if (qualifierType == "rgba32ui")
3662 {
3663 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3664 qualifier.imageInternalFormat = EiifRGBA32UI;
3665 }
3666 else if (qualifierType == "rgba16ui")
3667 {
3668 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3669 qualifier.imageInternalFormat = EiifRGBA16UI;
3670 }
3671 else if (qualifierType == "rgba8ui")
3672 {
3673 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3674 qualifier.imageInternalFormat = EiifRGBA8UI;
3675 }
3676 else if (qualifierType == "r32ui")
3677 {
3678 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3679 qualifier.imageInternalFormat = EiifR32UI;
3680 }
3681
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003682 else
3683 {
3684 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003685 }
3686
Jamie Madilla5efff92013-06-06 11:56:47 -04003687 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003688}
3689
Martin Radev802abe02016-08-04 17:48:32 +03003690void TParseContext::parseLocalSize(const TString &qualifierType,
3691 const TSourceLoc &qualifierTypeLine,
3692 int intValue,
3693 const TSourceLoc &intValueLine,
3694 const std::string &intValueString,
3695 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003696 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003697{
Olli Etuaho856c4972016-08-08 11:38:39 +03003698 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003699 if (intValue < 1)
3700 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003701 std::stringstream reasonStream;
3702 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3703 std::string reason = reasonStream.str();
3704 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003705 }
3706 (*localSize)[index] = intValue;
3707}
3708
Olli Etuaho09b04a22016-12-15 13:30:26 +00003709void TParseContext::parseNumViews(int intValue,
3710 const TSourceLoc &intValueLine,
3711 const std::string &intValueString,
3712 int *numViews)
3713{
3714 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3715 // specification.
3716 if (intValue < 1)
3717 {
3718 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3719 }
3720 *numViews = intValue;
3721}
3722
Jamie Madillb98c3a82015-07-23 14:26:04 -04003723TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3724 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003725 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303726 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003727{
Martin Radev802abe02016-08-04 17:48:32 +03003728 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003729
Martin Radev802abe02016-08-04 17:48:32 +03003730 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003731
Martin Radev802abe02016-08-04 17:48:32 +03003732 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003733 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003734 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003735 if (intValue < 0)
3736 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003737 error(intValueLine, "out of range: location must be non-negative",
3738 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003739 }
3740 else
3741 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003742 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003743 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003744 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003745 }
Olli Etuaho43364892017-02-13 16:00:12 +00003746 else if (qualifierType == "binding")
3747 {
3748 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3749 if (intValue < 0)
3750 {
3751 error(intValueLine, "out of range: binding must be non-negative",
3752 intValueString.c_str());
3753 }
3754 else
3755 {
3756 qualifier.binding = intValue;
3757 }
3758 }
jchen104cdac9e2017-05-08 11:01:20 +08003759 else if (qualifierType == "offset")
3760 {
3761 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3762 if (intValue < 0)
3763 {
3764 error(intValueLine, "out of range: offset must be non-negative",
3765 intValueString.c_str());
3766 }
3767 else
3768 {
3769 qualifier.offset = intValue;
3770 }
3771 }
Martin Radev802abe02016-08-04 17:48:32 +03003772 else if (qualifierType == "local_size_x")
3773 {
3774 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3775 &qualifier.localSize);
3776 }
3777 else if (qualifierType == "local_size_y")
3778 {
3779 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3780 &qualifier.localSize);
3781 }
3782 else if (qualifierType == "local_size_z")
3783 {
3784 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3785 &qualifier.localSize);
3786 }
Olli Etuaho95468d12017-05-04 11:14:34 +03003787 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00003788 mShaderType == GL_VERTEX_SHADER)
3789 {
3790 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3791 }
Martin Radev802abe02016-08-04 17:48:32 +03003792 else
3793 {
3794 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003795 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003796
Jamie Madilla5efff92013-06-06 11:56:47 -04003797 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003798}
3799
Olli Etuaho613b9592016-09-05 12:05:53 +03003800TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3801{
3802 return new TTypeQualifierBuilder(
3803 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3804 mShaderVersion);
3805}
3806
Olli Etuahocce89652017-06-19 16:04:09 +03003807TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
3808 const TSourceLoc &loc)
3809{
3810 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
3811 return new TStorageQualifierWrapper(qualifier, loc);
3812}
3813
3814TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
3815{
3816 if (getShaderType() == GL_VERTEX_SHADER)
3817 {
3818 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
3819 }
3820 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
3821}
3822
3823TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
3824{
3825 if (declaringFunction())
3826 {
3827 return new TStorageQualifierWrapper(EvqIn, loc);
3828 }
3829 if (getShaderType() == GL_FRAGMENT_SHADER)
3830 {
3831 if (mShaderVersion < 300)
3832 {
3833 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3834 }
3835 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
3836 }
3837 if (getShaderType() == GL_VERTEX_SHADER)
3838 {
3839 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
3840 {
3841 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3842 }
3843 return new TStorageQualifierWrapper(EvqVertexIn, loc);
3844 }
3845 return new TStorageQualifierWrapper(EvqComputeIn, loc);
3846}
3847
3848TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
3849{
3850 if (declaringFunction())
3851 {
3852 return new TStorageQualifierWrapper(EvqOut, loc);
3853 }
3854 if (mShaderVersion < 300)
3855 {
3856 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
3857 }
3858 if (getShaderType() != GL_VERTEX_SHADER && getShaderType() != GL_FRAGMENT_SHADER)
3859 {
3860 error(loc, "storage qualifier supported in vertex and fragment shaders only", "out");
3861 }
3862 if (getShaderType() == GL_VERTEX_SHADER)
3863 {
3864 return new TStorageQualifierWrapper(EvqVertexOut, loc);
3865 }
3866 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
3867}
3868
3869TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
3870{
3871 if (!declaringFunction())
3872 {
3873 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
3874 }
3875 return new TStorageQualifierWrapper(EvqInOut, loc);
3876}
3877
Jamie Madillb98c3a82015-07-23 14:26:04 -04003878TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003879 TLayoutQualifier rightQualifier,
3880 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003881{
Martin Radevc28888b2016-07-22 15:27:42 +03003882 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003883 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003884}
3885
Olli Etuahocce89652017-06-19 16:04:09 +03003886TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
3887{
3888 checkIsNotReserved(loc, *identifier);
3889 TType *type = new TType(EbtVoid, EbpUndefined);
3890 return new TField(type, identifier, loc);
3891}
3892
3893TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
3894 const TSourceLoc &loc,
3895 TIntermTyped *arraySize,
3896 const TSourceLoc &arraySizeLoc)
3897{
3898 checkIsNotReserved(loc, *identifier);
3899
3900 TType *type = new TType(EbtVoid, EbpUndefined);
3901 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
3902 type->setArraySize(size);
3903
3904 return new TField(type, identifier, loc);
3905}
3906
Olli Etuaho4de340a2016-12-16 09:32:03 +00003907TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3908 const TFieldList *newlyAddedFields,
3909 const TSourceLoc &location)
3910{
3911 for (TField *field : *newlyAddedFields)
3912 {
3913 for (TField *oldField : *processedFields)
3914 {
3915 if (oldField->name() == field->name())
3916 {
3917 error(location, "duplicate field name in structure", field->name().c_str());
3918 }
3919 }
3920 processedFields->push_back(field);
3921 }
3922 return processedFields;
3923}
3924
Martin Radev70866b82016-07-22 15:27:42 +03003925TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3926 const TTypeQualifierBuilder &typeQualifierBuilder,
3927 TPublicType *typeSpecifier,
3928 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003929{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003930 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003931
Martin Radev70866b82016-07-22 15:27:42 +03003932 typeSpecifier->qualifier = typeQualifier.qualifier;
3933 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003934 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003935 typeSpecifier->invariant = typeQualifier.invariant;
3936 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303937 {
Martin Radev70866b82016-07-22 15:27:42 +03003938 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003939 }
Martin Radev70866b82016-07-22 15:27:42 +03003940 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003941}
3942
Jamie Madillb98c3a82015-07-23 14:26:04 -04003943TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3944 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003945{
Martin Radev4a9cd802016-09-01 16:51:51 +03003946 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3947 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003948
Martin Radev4a9cd802016-09-01 16:51:51 +03003949 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003950
Martin Radev4a9cd802016-09-01 16:51:51 +03003951 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003952
Arun Patole7e7e68d2015-05-22 12:02:25 +05303953 for (unsigned int i = 0; i < fieldList->size(); ++i)
3954 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003955 //
3956 // Careful not to replace already known aspects of type, like array-ness
3957 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303958 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003959 type->setBasicType(typeSpecifier.getBasicType());
3960 type->setPrimarySize(typeSpecifier.getPrimarySize());
3961 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003962 type->setPrecision(typeSpecifier.precision);
3963 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003964 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003965 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003966 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003967
3968 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303969 if (type->isArray())
3970 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003971 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003972 }
3973 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003974 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003975 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303976 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003977 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003978 }
3979
Martin Radev4a9cd802016-09-01 16:51:51 +03003980 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003981 }
3982
Jamie Madill98493dd2013-07-08 14:39:03 -04003983 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003984}
3985
Martin Radev4a9cd802016-09-01 16:51:51 +03003986TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3987 const TSourceLoc &nameLine,
3988 const TString *structName,
3989 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003990{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303991 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003992 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003993
Jamie Madill9b820842015-02-12 10:40:10 -05003994 // Store a bool in the struct if we're at global scope, to allow us to
3995 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003996 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003997
Jamie Madill98493dd2013-07-08 14:39:03 -04003998 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003999 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004000 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05304001 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
4002 if (!symbolTable.declare(userTypeDef))
4003 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004004 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004005 }
4006 }
4007
4008 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004009 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004010 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004011 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004012 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004013 switch (qualifier)
4014 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004015 case EvqGlobal:
4016 case EvqTemporary:
4017 break;
4018 default:
4019 error(field.line(), "invalid qualifier on struct member",
4020 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004021 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004022 }
Martin Radev70866b82016-07-22 15:27:42 +03004023 if (field.type()->isInvariant())
4024 {
4025 error(field.line(), "invalid qualifier on struct member", "invariant");
4026 }
jchen104cdac9e2017-05-08 11:01:20 +08004027 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4028 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004029 {
4030 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4031 }
4032
Olli Etuaho43364892017-02-13 16:00:12 +00004033 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4034
4035 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004036
4037 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004038 }
4039
Martin Radev4a9cd802016-09-01 16:51:51 +03004040 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuahocce89652017-06-19 16:04:09 +03004041 typeSpecifierNonArray.initializeStruct(structureType, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004042 exitStructDeclaration();
4043
Martin Radev4a9cd802016-09-01 16:51:51 +03004044 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004045}
4046
Jamie Madillb98c3a82015-07-23 14:26:04 -04004047TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004048 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004049 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004050{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004051 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004052 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004053 init->isVector())
4054 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004055 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4056 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004057 return nullptr;
4058 }
4059
Olli Etuahoac5274d2015-02-20 10:19:08 +02004060 if (statementList)
4061 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004062 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004063 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004064 return nullptr;
4065 }
4066 }
4067
Olli Etuahoa3a36662015-02-17 13:46:51 +02004068 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
4069 if (node == nullptr)
4070 {
4071 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02004072 return nullptr;
4073 }
4074 return node;
4075}
4076
4077TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4078{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004079 if (mSwitchNestingLevel == 0)
4080 {
4081 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004082 return nullptr;
4083 }
4084 if (condition == nullptr)
4085 {
4086 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004087 return nullptr;
4088 }
4089 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004090 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004091 {
4092 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004093 }
4094 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004095 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4096 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4097 // fold in case labels.
4098 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004099 {
4100 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004101 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02004102 TIntermCase *node = intermediate.addCase(condition, loc);
4103 if (node == nullptr)
4104 {
4105 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02004106 return nullptr;
4107 }
4108 return node;
4109}
4110
4111TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4112{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004113 if (mSwitchNestingLevel == 0)
4114 {
4115 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004116 return nullptr;
4117 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02004118 TIntermCase *node = intermediate.addCase(nullptr, loc);
4119 if (node == nullptr)
4120 {
4121 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02004122 return nullptr;
4123 }
4124 return node;
4125}
4126
Jamie Madillb98c3a82015-07-23 14:26:04 -04004127TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4128 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004129 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004130{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004131 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004132
4133 switch (op)
4134 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004135 case EOpLogicalNot:
4136 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4137 child->isVector())
4138 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004139 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004140 return nullptr;
4141 }
4142 break;
4143 case EOpBitwiseNot:
4144 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4145 child->isMatrix() || child->isArray())
4146 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004147 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004148 return nullptr;
4149 }
4150 break;
4151 case EOpPostIncrement:
4152 case EOpPreIncrement:
4153 case EOpPostDecrement:
4154 case EOpPreDecrement:
4155 case EOpNegative:
4156 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004157 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4158 child->getBasicType() == EbtBool || child->isArray() ||
4159 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004160 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004161 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004162 return nullptr;
4163 }
4164 // Operators for built-ins are already type checked against their prototype.
4165 default:
4166 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004167 }
4168
Olli Etuahof119a262016-08-19 15:54:22 +03004169 TIntermUnary *node = new TIntermUnary(op, child);
4170 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004171
Olli Etuaho77ba4082016-12-16 12:01:18 +00004172 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03004173 if (foldedNode)
4174 return foldedNode;
4175
4176 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004177}
4178
Olli Etuaho09b22472015-02-11 11:47:26 +02004179TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4180{
Olli Etuahocce89652017-06-19 16:04:09 +03004181 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004182 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004183 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004184 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004185 return child;
4186 }
4187 return node;
4188}
4189
Jamie Madillb98c3a82015-07-23 14:26:04 -04004190TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4191 TIntermTyped *child,
4192 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004193{
Olli Etuaho856c4972016-08-08 11:38:39 +03004194 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004195 return addUnaryMath(op, child, loc);
4196}
4197
Jamie Madillb98c3a82015-07-23 14:26:04 -04004198bool TParseContext::binaryOpCommonCheck(TOperator op,
4199 TIntermTyped *left,
4200 TIntermTyped *right,
4201 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004202{
jchen10b4cf5652017-05-05 18:51:17 +08004203 // Check opaque types are not allowed to be operands in expressions other than array indexing
4204 // and structure member selection.
4205 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4206 {
4207 switch (op)
4208 {
4209 case EOpIndexDirect:
4210 case EOpIndexIndirect:
4211 break;
4212 case EOpIndexDirectStruct:
4213 UNREACHABLE();
4214
4215 default:
4216 error(loc, "Invalid operation for variables with an opaque type",
4217 GetOperatorString(op));
4218 return false;
4219 }
4220 }
jchen10cc2a10e2017-05-03 14:05:12 +08004221
Olli Etuaho244be012016-08-18 15:26:02 +03004222 if (left->getType().getStruct() || right->getType().getStruct())
4223 {
4224 switch (op)
4225 {
4226 case EOpIndexDirectStruct:
4227 ASSERT(left->getType().getStruct());
4228 break;
4229 case EOpEqual:
4230 case EOpNotEqual:
4231 case EOpAssign:
4232 case EOpInitialize:
4233 if (left->getType() != right->getType())
4234 {
4235 return false;
4236 }
4237 break;
4238 default:
4239 error(loc, "Invalid operation for structs", GetOperatorString(op));
4240 return false;
4241 }
4242 }
4243
Olli Etuaho94050052017-05-08 14:17:44 +03004244 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4245 {
4246 switch (op)
4247 {
4248 case EOpIndexDirectInterfaceBlock:
4249 ASSERT(left->getType().getInterfaceBlock());
4250 break;
4251 default:
4252 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4253 return false;
4254 }
4255 }
4256
Olli Etuahod6b14282015-03-17 14:31:35 +02004257 if (left->isArray() || right->isArray())
4258 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004259 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004260 {
4261 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4262 return false;
4263 }
4264
4265 if (left->isArray() != right->isArray())
4266 {
4267 error(loc, "array / non-array mismatch", GetOperatorString(op));
4268 return false;
4269 }
4270
4271 switch (op)
4272 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004273 case EOpEqual:
4274 case EOpNotEqual:
4275 case EOpAssign:
4276 case EOpInitialize:
4277 break;
4278 default:
4279 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4280 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004281 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004282 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02004283 if (left->getArraySize() != right->getArraySize())
4284 {
4285 error(loc, "array size mismatch", GetOperatorString(op));
4286 return false;
4287 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004288 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004289
4290 // Check ops which require integer / ivec parameters
4291 bool isBitShift = false;
4292 switch (op)
4293 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004294 case EOpBitShiftLeft:
4295 case EOpBitShiftRight:
4296 case EOpBitShiftLeftAssign:
4297 case EOpBitShiftRightAssign:
4298 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4299 // check that the basic type is an integer type.
4300 isBitShift = true;
4301 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4302 {
4303 return false;
4304 }
4305 break;
4306 case EOpBitwiseAnd:
4307 case EOpBitwiseXor:
4308 case EOpBitwiseOr:
4309 case EOpBitwiseAndAssign:
4310 case EOpBitwiseXorAssign:
4311 case EOpBitwiseOrAssign:
4312 // It is enough to check the type of only one operand, since later it
4313 // is checked that the operand types match.
4314 if (!IsInteger(left->getBasicType()))
4315 {
4316 return false;
4317 }
4318 break;
4319 default:
4320 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004321 }
4322
4323 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4324 // So the basic type should usually match.
4325 if (!isBitShift && left->getBasicType() != right->getBasicType())
4326 {
4327 return false;
4328 }
4329
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004330 // Check that:
4331 // 1. Type sizes match exactly on ops that require that.
4332 // 2. Restrictions for structs that contain arrays or samplers are respected.
4333 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004334 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004335 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004336 case EOpAssign:
4337 case EOpInitialize:
4338 case EOpEqual:
4339 case EOpNotEqual:
4340 // ESSL 1.00 sections 5.7, 5.8, 5.9
4341 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4342 {
4343 error(loc, "undefined operation for structs containing arrays",
4344 GetOperatorString(op));
4345 return false;
4346 }
4347 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4348 // we interpret the spec so that this extends to structs containing samplers,
4349 // similarly to ESSL 1.00 spec.
4350 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4351 left->getType().isStructureContainingSamplers())
4352 {
4353 error(loc, "undefined operation for structs containing samplers",
4354 GetOperatorString(op));
4355 return false;
4356 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004357
Olli Etuahoe1805592017-01-02 16:41:20 +00004358 if ((left->getNominalSize() != right->getNominalSize()) ||
4359 (left->getSecondarySize() != right->getSecondarySize()))
4360 {
4361 error(loc, "dimension mismatch", GetOperatorString(op));
4362 return false;
4363 }
4364 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004365 case EOpLessThan:
4366 case EOpGreaterThan:
4367 case EOpLessThanEqual:
4368 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004369 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004370 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004371 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004372 return false;
4373 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004374 break;
4375 case EOpAdd:
4376 case EOpSub:
4377 case EOpDiv:
4378 case EOpIMod:
4379 case EOpBitShiftLeft:
4380 case EOpBitShiftRight:
4381 case EOpBitwiseAnd:
4382 case EOpBitwiseXor:
4383 case EOpBitwiseOr:
4384 case EOpAddAssign:
4385 case EOpSubAssign:
4386 case EOpDivAssign:
4387 case EOpIModAssign:
4388 case EOpBitShiftLeftAssign:
4389 case EOpBitShiftRightAssign:
4390 case EOpBitwiseAndAssign:
4391 case EOpBitwiseXorAssign:
4392 case EOpBitwiseOrAssign:
4393 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4394 {
4395 return false;
4396 }
4397
4398 // Are the sizes compatible?
4399 if (left->getNominalSize() != right->getNominalSize() ||
4400 left->getSecondarySize() != right->getSecondarySize())
4401 {
4402 // If the nominal sizes of operands do not match:
4403 // One of them must be a scalar.
4404 if (!left->isScalar() && !right->isScalar())
4405 return false;
4406
4407 // In the case of compound assignment other than multiply-assign,
4408 // the right side needs to be a scalar. Otherwise a vector/matrix
4409 // would be assigned to a scalar. A scalar can't be shifted by a
4410 // vector either.
4411 if (!right->isScalar() &&
4412 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4413 return false;
4414 }
4415 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004416 default:
4417 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004418 }
4419
Olli Etuahod6b14282015-03-17 14:31:35 +02004420 return true;
4421}
4422
Olli Etuaho1dded802016-08-18 18:13:13 +03004423bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4424 const TType &left,
4425 const TType &right)
4426{
4427 switch (op)
4428 {
4429 case EOpMul:
4430 case EOpMulAssign:
4431 return left.getNominalSize() == right.getNominalSize() &&
4432 left.getSecondarySize() == right.getSecondarySize();
4433 case EOpVectorTimesScalar:
4434 return true;
4435 case EOpVectorTimesScalarAssign:
4436 ASSERT(!left.isMatrix() && !right.isMatrix());
4437 return left.isVector() && !right.isVector();
4438 case EOpVectorTimesMatrix:
4439 return left.getNominalSize() == right.getRows();
4440 case EOpVectorTimesMatrixAssign:
4441 ASSERT(!left.isMatrix() && right.isMatrix());
4442 return left.isVector() && left.getNominalSize() == right.getRows() &&
4443 left.getNominalSize() == right.getCols();
4444 case EOpMatrixTimesVector:
4445 return left.getCols() == right.getNominalSize();
4446 case EOpMatrixTimesScalar:
4447 return true;
4448 case EOpMatrixTimesScalarAssign:
4449 ASSERT(left.isMatrix() && !right.isMatrix());
4450 return !right.isVector();
4451 case EOpMatrixTimesMatrix:
4452 return left.getCols() == right.getRows();
4453 case EOpMatrixTimesMatrixAssign:
4454 ASSERT(left.isMatrix() && right.isMatrix());
4455 // We need to check two things:
4456 // 1. The matrix multiplication step is valid.
4457 // 2. The result will have the same number of columns as the lvalue.
4458 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4459
4460 default:
4461 UNREACHABLE();
4462 return false;
4463 }
4464}
4465
Jamie Madillb98c3a82015-07-23 14:26:04 -04004466TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4467 TIntermTyped *left,
4468 TIntermTyped *right,
4469 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004470{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004471 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004472 return nullptr;
4473
Olli Etuahofc1806e2015-03-17 13:03:11 +02004474 switch (op)
4475 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004476 case EOpEqual:
4477 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004478 case EOpLessThan:
4479 case EOpGreaterThan:
4480 case EOpLessThanEqual:
4481 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004482 break;
4483 case EOpLogicalOr:
4484 case EOpLogicalXor:
4485 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004486 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4487 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004488 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004489 {
4490 return nullptr;
4491 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004492 // Basic types matching should have been already checked.
4493 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004494 break;
4495 case EOpAdd:
4496 case EOpSub:
4497 case EOpDiv:
4498 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004499 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4500 !right->getType().getStruct());
4501 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004502 {
4503 return nullptr;
4504 }
4505 break;
4506 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004507 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4508 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004509 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004510 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004511 {
4512 return nullptr;
4513 }
4514 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004515 default:
4516 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004517 }
4518
Olli Etuaho1dded802016-08-18 18:13:13 +03004519 if (op == EOpMul)
4520 {
4521 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4522 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4523 {
4524 return nullptr;
4525 }
4526 }
4527
Olli Etuaho3fdec912016-08-18 15:08:06 +03004528 TIntermBinary *node = new TIntermBinary(op, left, right);
4529 node->setLine(loc);
4530
Olli Etuaho3fdec912016-08-18 15:08:06 +03004531 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004532 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004533 if (foldedNode)
4534 return foldedNode;
4535
4536 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004537}
4538
Jamie Madillb98c3a82015-07-23 14:26:04 -04004539TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4540 TIntermTyped *left,
4541 TIntermTyped *right,
4542 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004543{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004544 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004545 if (node == 0)
4546 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004547 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4548 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004549 return left;
4550 }
4551 return node;
4552}
4553
Jamie Madillb98c3a82015-07-23 14:26:04 -04004554TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4555 TIntermTyped *left,
4556 TIntermTyped *right,
4557 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004558{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004559 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004560 if (node == 0)
4561 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004562 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4563 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004564 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004565 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004566 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4567 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004568 }
4569 return node;
4570}
4571
Olli Etuaho13389b62016-10-16 11:48:18 +01004572TIntermBinary *TParseContext::createAssign(TOperator op,
4573 TIntermTyped *left,
4574 TIntermTyped *right,
4575 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004576{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004577 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004578 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004579 if (op == EOpMulAssign)
4580 {
4581 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4582 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4583 {
4584 return nullptr;
4585 }
4586 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004587 TIntermBinary *node = new TIntermBinary(op, left, right);
4588 node->setLine(loc);
4589
Olli Etuaho3fdec912016-08-18 15:08:06 +03004590 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004591 }
4592 return nullptr;
4593}
4594
Jamie Madillb98c3a82015-07-23 14:26:04 -04004595TIntermTyped *TParseContext::addAssign(TOperator op,
4596 TIntermTyped *left,
4597 TIntermTyped *right,
4598 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004599{
Olli Etuahocce89652017-06-19 16:04:09 +03004600 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02004601 TIntermTyped *node = createAssign(op, left, right, loc);
4602 if (node == nullptr)
4603 {
4604 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004605 return left;
4606 }
4607 return node;
4608}
4609
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004610TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4611 TIntermTyped *right,
4612 const TSourceLoc &loc)
4613{
Corentin Wallez0d959252016-07-12 17:26:32 -04004614 // WebGL2 section 5.26, the following results in an error:
4615 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004616 if (mShaderSpec == SH_WEBGL2_SPEC &&
4617 (left->isArray() || left->getBasicType() == EbtVoid ||
4618 left->getType().isStructureContainingArrays() || right->isArray() ||
4619 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004620 {
4621 error(loc,
4622 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4623 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004624 }
4625
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004626 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004627}
4628
Olli Etuaho49300862015-02-20 14:54:49 +02004629TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4630{
4631 switch (op)
4632 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004633 case EOpContinue:
4634 if (mLoopNestingLevel <= 0)
4635 {
4636 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004637 }
4638 break;
4639 case EOpBreak:
4640 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4641 {
4642 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004643 }
4644 break;
4645 case EOpReturn:
4646 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4647 {
4648 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004649 }
4650 break;
Olli Etuahocce89652017-06-19 16:04:09 +03004651 case EOpKill:
4652 if (mShaderType != GL_FRAGMENT_SHADER)
4653 {
4654 error(loc, "discard supported in fragment shaders only", "discard");
4655 }
4656 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004657 default:
Olli Etuahocce89652017-06-19 16:04:09 +03004658 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004659 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004660 }
Olli Etuahocce89652017-06-19 16:04:09 +03004661 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02004662}
4663
Jamie Madillb98c3a82015-07-23 14:26:04 -04004664TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03004665 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004666 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004667{
Olli Etuahocce89652017-06-19 16:04:09 +03004668 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02004669 {
Olli Etuahocce89652017-06-19 16:04:09 +03004670 ASSERT(op == EOpReturn);
4671 mFunctionReturnsValue = true;
4672 if (mCurrentFunctionType->getBasicType() == EbtVoid)
4673 {
4674 error(loc, "void function cannot return a value", "return");
4675 }
4676 else if (*mCurrentFunctionType != expression->getType())
4677 {
4678 error(loc, "function return is not matching type:", "return");
4679 }
Olli Etuaho49300862015-02-20 14:54:49 +02004680 }
Olli Etuahocce89652017-06-19 16:04:09 +03004681 TIntermBranch *node = new TIntermBranch(op, expression);
4682 node->setLine(loc);
4683 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02004684}
4685
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004686void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4687{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004688 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004689 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004690 TIntermNode *offset = nullptr;
4691 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03004692 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
4693 name == "textureProjLodOffset" || name == "textureGradOffset" ||
4694 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004695 {
4696 offset = arguments->back();
4697 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03004698 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004699 {
4700 // A bias parameter might follow the offset parameter.
4701 ASSERT(arguments->size() >= 3);
4702 offset = (*arguments)[2];
4703 }
4704 if (offset != nullptr)
4705 {
4706 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4707 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4708 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004709 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03004710 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004711 }
4712 else
4713 {
4714 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4715 size_t size = offsetConstantUnion->getType().getObjectSize();
4716 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4717 for (size_t i = 0u; i < size; ++i)
4718 {
4719 int offsetValue = values[i].getIConst();
4720 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4721 {
4722 std::stringstream tokenStream;
4723 tokenStream << offsetValue;
4724 std::string token = tokenStream.str();
4725 error(offset->getLine(), "Texture offset value out of valid range",
4726 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004727 }
4728 }
4729 }
4730 }
4731}
4732
Martin Radev2cc85b32016-08-05 16:22:53 +03004733// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4734void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4735{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004736 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004737 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4738
4739 if (name.compare(0, 5, "image") == 0)
4740 {
4741 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004742 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004743
Olli Etuaho485eefd2017-02-14 17:40:06 +00004744 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004745
4746 if (name.compare(5, 5, "Store") == 0)
4747 {
4748 if (memoryQualifier.readonly)
4749 {
4750 error(imageNode->getLine(),
4751 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004752 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004753 }
4754 }
4755 else if (name.compare(5, 4, "Load") == 0)
4756 {
4757 if (memoryQualifier.writeonly)
4758 {
4759 error(imageNode->getLine(),
4760 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004761 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004762 }
4763 }
4764 }
4765}
4766
4767// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4768void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4769 const TFunction *functionDefinition,
4770 const TIntermAggregate *functionCall)
4771{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004772 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004773
4774 const TIntermSequence &arguments = *functionCall->getSequence();
4775
4776 ASSERT(functionDefinition->getParamCount() == arguments.size());
4777
4778 for (size_t i = 0; i < arguments.size(); ++i)
4779 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004780 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4781 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004782 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4783 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4784
4785 if (IsImage(functionArgumentType.getBasicType()))
4786 {
4787 const TMemoryQualifier &functionArgumentMemoryQualifier =
4788 functionArgumentType.getMemoryQualifier();
4789 const TMemoryQualifier &functionParameterMemoryQualifier =
4790 functionParameterType.getMemoryQualifier();
4791 if (functionArgumentMemoryQualifier.readonly &&
4792 !functionParameterMemoryQualifier.readonly)
4793 {
4794 error(functionCall->getLine(),
4795 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004796 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004797 }
4798
4799 if (functionArgumentMemoryQualifier.writeonly &&
4800 !functionParameterMemoryQualifier.writeonly)
4801 {
4802 error(functionCall->getLine(),
4803 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004804 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004805 }
Martin Radev049edfa2016-11-11 14:35:37 +02004806
4807 if (functionArgumentMemoryQualifier.coherent &&
4808 !functionParameterMemoryQualifier.coherent)
4809 {
4810 error(functionCall->getLine(),
4811 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004812 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004813 }
4814
4815 if (functionArgumentMemoryQualifier.volatileQualifier &&
4816 !functionParameterMemoryQualifier.volatileQualifier)
4817 {
4818 error(functionCall->getLine(),
4819 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004820 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004821 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004822 }
4823 }
4824}
4825
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004826TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004827{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004828 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004829}
4830
4831TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004832 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004833 TIntermNode *thisNode,
4834 const TSourceLoc &loc)
4835{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004836 if (thisNode != nullptr)
4837 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004838 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004839 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004840
4841 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004842 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004843 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004844 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004845 }
4846 else
4847 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004848 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004849 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4850 }
4851}
4852
4853TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4854 TIntermSequence *arguments,
4855 TIntermNode *thisNode,
4856 const TSourceLoc &loc)
4857{
4858 TConstantUnion *unionArray = new TConstantUnion[1];
4859 int arraySize = 0;
4860 TIntermTyped *typedThis = thisNode->getAsTyped();
4861 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4862 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4863 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4864 // So accessing fnCall->getName() below is safe.
4865 if (fnCall->getName() != "length")
4866 {
4867 error(loc, "invalid method", fnCall->getName().c_str());
4868 }
4869 else if (!arguments->empty())
4870 {
4871 error(loc, "method takes no parameters", "length");
4872 }
4873 else if (typedThis == nullptr || !typedThis->isArray())
4874 {
4875 error(loc, "length can only be called on arrays", "length");
4876 }
4877 else
4878 {
4879 arraySize = typedThis->getArraySize();
4880 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004881 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004882 // This code path can be hit with expressions like these:
4883 // (a = b).length()
4884 // (func()).length()
4885 // (int[3](0, 1, 2)).length()
4886 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4887 // expression.
4888 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4889 // spec section 5.9 which allows "An array, vector or matrix expression with the
4890 // length method applied".
4891 error(loc, "length can only be called on array names, not on array expressions",
4892 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004893 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004894 }
4895 unionArray->setIConst(arraySize);
4896 return intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
4897}
4898
4899TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4900 TIntermSequence *arguments,
4901 const TSourceLoc &loc)
4902{
4903 // First find by unmangled name to check whether the function name has been
4904 // hidden by a variable name or struct typename.
4905 // If a function is found, check for one with a matching argument list.
4906 bool builtIn;
4907 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4908 if (symbol != nullptr && !symbol->isFunction())
4909 {
4910 error(loc, "function name expected", fnCall->getName().c_str());
4911 }
4912 else
4913 {
4914 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4915 mShaderVersion, &builtIn);
4916 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004917 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004918 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4919 }
4920 else
4921 {
4922 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004923 //
4924 // A declared function.
4925 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004926 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004927 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004928 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004929 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004930 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004931 if (builtIn && op != EOpNull)
4932 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004933 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004934 if (fnCandidate->getParamCount() == 1)
4935 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004936 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004937 TIntermNode *unaryParamNode = arguments->front();
4938 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004939 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004940 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004941 }
4942 else
4943 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004944 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00004945 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004946 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004947
4948 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004949 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304950
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004951 // See if we can constant fold a built-in. Note that this may be possible even
4952 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004953 TIntermTyped *foldedNode =
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004954 intermediate.foldAggregateBuiltIn(callNode, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304955 if (foldedNode)
4956 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004957 return foldedNode;
Arun Patole274f0702015-05-05 13:33:30 +05304958 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004959 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004960 }
4961 }
4962 else
4963 {
4964 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004965 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004966
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004967 // If builtIn == false, the function is user defined - could be an overloaded
4968 // built-in as well.
4969 // if builtIn == true, it's a builtIn function with no op associated with it.
4970 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004971 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004972 {
Olli Etuahofe486322017-03-21 09:30:54 +00004973 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004974 checkTextureOffsetConst(callNode);
4975 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004976 }
4977 else
4978 {
Olli Etuahofe486322017-03-21 09:30:54 +00004979 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004980 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004981 }
4982
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004983 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004984
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004985 callNode->setLine(loc);
4986
4987 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004988 }
4989 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004990 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004991
4992 // Error message was already written. Put on a dummy node for error recovery.
4993 return TIntermTyped::CreateZero(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004994}
4995
Jamie Madillb98c3a82015-07-23 14:26:04 -04004996TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004997 TIntermTyped *trueExpression,
4998 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004999 const TSourceLoc &loc)
5000{
Olli Etuaho856c4972016-08-08 11:38:39 +03005001 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03005002
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005003 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005004 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005005 binaryOpError(loc, "?:", trueExpression->getCompleteString(),
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005006 falseExpression->getCompleteString());
5007 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005008 }
Olli Etuahode318b22016-10-25 16:18:25 +01005009 if (IsOpaqueType(trueExpression->getBasicType()))
5010 {
5011 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005012 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005013 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5014 // Note that structs containing opaque types don't need to be checked as structs are
5015 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005016 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005017 return falseExpression;
5018 }
5019
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005020 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005021 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005022 // ESSL 3.00.6 section 5.7:
5023 // Ternary operator support is optional for arrays. No certainty that it works across all
5024 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5025 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005026 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005027 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005028 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005029 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005030 }
Olli Etuaho94050052017-05-08 14:17:44 +03005031 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5032 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005033 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005034 return falseExpression;
5035 }
5036
Corentin Wallez0d959252016-07-12 17:26:32 -04005037 // WebGL2 section 5.26, the following results in an error:
5038 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005039 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005040 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005041 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005042 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005043 }
5044
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005045 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03005046}
Olli Etuaho49300862015-02-20 14:54:49 +02005047
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005048//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005049// Parse an array of strings using yyparse.
5050//
5051// Returns 0 for success.
5052//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005053int PaParseStrings(size_t count,
5054 const char *const string[],
5055 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305056 TParseContext *context)
5057{
Yunchao He4f285442017-04-21 12:15:49 +08005058 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005059 return 1;
5060
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005061 if (glslang_initialize(context))
5062 return 1;
5063
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005064 int error = glslang_scan(count, string, length, context);
5065 if (!error)
5066 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005067
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005068 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005069
alokp@chromium.org6b495712012-06-29 00:06:58 +00005070 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005071}
Jamie Madill45bcc782016-11-07 13:58:48 -05005072
5073} // namespace sh