blob: e4aa5a1b3f9508e9b0b5cd6252b68bc2ff574a48 [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
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040013#include "compiler/translator/Cache.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020014#include "compiler/translator/glslang.h"
15#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030017#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
Jamie Madill45bcc782016-11-07 13:58:48 -050019namespace sh
20{
21
alokp@chromium.org8b851c62012-06-15 16:25:11 +000022///////////////////////////////////////////////////////////////////////
23//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024// Sub- vector and matrix fields
25//
26////////////////////////////////////////////////////////////////////////
27
Martin Radev2cc85b32016-08-05 16:22:53 +030028namespace
29{
30
31const int kWebGLMaxStructNesting = 4;
32
33bool ContainsSampler(const TType &type)
34{
35 if (IsSampler(type.getBasicType()))
36 return true;
37
38 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
39 {
40 const TFieldList &fields = type.getStruct()->fields();
41 for (unsigned int i = 0; i < fields.size(); ++i)
42 {
43 if (ContainsSampler(*fields[i]->type()))
44 return true;
45 }
46 }
47
48 return false;
49}
50
51bool ContainsImage(const TType &type)
52{
53 if (IsImage(type.getBasicType()))
54 return true;
55
56 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
57 {
58 const TFieldList &fields = type.getStruct()->fields();
59 for (unsigned int i = 0; i < fields.size(); ++i)
60 {
61 if (ContainsImage(*fields[i]->type()))
62 return true;
63 }
64 }
65
66 return false;
67}
68
Olli Etuaho485eefd2017-02-14 17:40:06 +000069// Get a token from an image argument to use as an error message token.
70const char *GetImageArgumentToken(TIntermTyped *imageNode)
71{
72 ASSERT(IsImage(imageNode->getBasicType()));
73 while (imageNode->getAsBinaryNode() &&
74 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
75 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
76 {
77 imageNode = imageNode->getAsBinaryNode()->getLeft();
78 }
79 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
80 if (imageSymbol)
81 {
82 return imageSymbol->getSymbol().c_str();
83 }
84 return "image";
85}
86
Martin Radev2cc85b32016-08-05 16:22:53 +030087} // namespace
88
Jamie Madillacb4b812016-11-07 13:50:29 -050089TParseContext::TParseContext(TSymbolTable &symt,
90 TExtensionBehavior &ext,
91 sh::GLenum type,
92 ShShaderSpec spec,
93 ShCompileOptions options,
94 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +000095 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -050096 const ShBuiltInResources &resources)
97 : intermediate(),
98 symbolTable(symt),
99 mDeferredSingleDeclarationErrorCheck(false),
100 mShaderType(type),
101 mShaderSpec(spec),
102 mCompileOptions(options),
103 mShaderVersion(100),
104 mTreeRoot(nullptr),
105 mLoopNestingLevel(0),
106 mStructNestingLevel(0),
107 mSwitchNestingLevel(0),
108 mCurrentFunctionType(nullptr),
109 mFunctionReturnsValue(false),
110 mChecksPrecisionErrors(checksPrecErrors),
111 mFragmentPrecisionHighOnESSL1(false),
112 mDefaultMatrixPacking(EmpColumnMajor),
113 mDefaultBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000114 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500115 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000116 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500117 mShaderVersion,
118 mShaderType,
119 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000120 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500121 mScanner(nullptr),
122 mUsesFragData(false),
123 mUsesFragColor(false),
124 mUsesSecondaryOutputs(false),
125 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
126 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000127 mMultiviewAvailable(resources.OVR_multiview == 1),
Jamie Madillacb4b812016-11-07 13:50:29 -0500128 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000129 mNumViews(-1),
130 mMaxNumViews(resources.MaxViewsOVR),
Jamie Madillacb4b812016-11-07 13:50:29 -0500131 mDeclaringFunction(false)
132{
133 mComputeShaderLocalSize.fill(-1);
134}
135
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136//
137// Look at a '.' field selector string and change it into offsets
138// for a vector.
139//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400140bool TParseContext::parseVectorFields(const TString &compString,
141 int vecSize,
142 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530143 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400145 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530146 if (fields.num > 4)
147 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000148 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000149 return false;
150 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151
Jamie Madillb98c3a82015-07-23 14:26:04 -0400152 enum
153 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000154 exyzw,
155 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000156 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000157 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158
Arun Patole7e7e68d2015-05-22 12:02:25 +0530159 for (int i = 0; i < fields.num; ++i)
160 {
161 switch (compString[i])
162 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400163 case 'x':
164 fields.offsets[i] = 0;
165 fieldSet[i] = exyzw;
166 break;
167 case 'r':
168 fields.offsets[i] = 0;
169 fieldSet[i] = ergba;
170 break;
171 case 's':
172 fields.offsets[i] = 0;
173 fieldSet[i] = estpq;
174 break;
175 case 'y':
176 fields.offsets[i] = 1;
177 fieldSet[i] = exyzw;
178 break;
179 case 'g':
180 fields.offsets[i] = 1;
181 fieldSet[i] = ergba;
182 break;
183 case 't':
184 fields.offsets[i] = 1;
185 fieldSet[i] = estpq;
186 break;
187 case 'z':
188 fields.offsets[i] = 2;
189 fieldSet[i] = exyzw;
190 break;
191 case 'b':
192 fields.offsets[i] = 2;
193 fieldSet[i] = ergba;
194 break;
195 case 'p':
196 fields.offsets[i] = 2;
197 fieldSet[i] = estpq;
198 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530199
Jamie Madillb98c3a82015-07-23 14:26:04 -0400200 case 'w':
201 fields.offsets[i] = 3;
202 fieldSet[i] = exyzw;
203 break;
204 case 'a':
205 fields.offsets[i] = 3;
206 fieldSet[i] = ergba;
207 break;
208 case 'q':
209 fields.offsets[i] = 3;
210 fieldSet[i] = estpq;
211 break;
212 default:
213 error(line, "illegal vector field selection", compString.c_str());
214 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000215 }
216 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217
Arun Patole7e7e68d2015-05-22 12:02:25 +0530218 for (int i = 0; i < fields.num; ++i)
219 {
220 if (fields.offsets[i] >= vecSize)
221 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400222 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000223 return false;
224 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225
Arun Patole7e7e68d2015-05-22 12:02:25 +0530226 if (i > 0)
227 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400228 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530229 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400230 error(line, "illegal - vector component fields not from the same set",
231 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000232 return false;
233 }
234 }
235 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000237 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238}
239
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000240///////////////////////////////////////////////////////////////////////
241//
242// Errors
243//
244////////////////////////////////////////////////////////////////////////
245
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000246//
247// Used by flex/bison to output all syntax and parsing errors.
248//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000249void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000251 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252}
253
Olli Etuaho4de340a2016-12-16 09:32:03 +0000254void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530255{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000256 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000257}
258
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200259void TParseContext::outOfRangeError(bool isError,
260 const TSourceLoc &loc,
261 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000262 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200263{
264 if (isError)
265 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000266 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200267 }
268 else
269 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000270 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200271 }
272}
273
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000274//
275// Same error message for all places assignments don't work.
276//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530277void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000279 std::stringstream reasonStream;
280 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
281 std::string reason = reasonStream.str();
282 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283}
284
285//
286// Same error message for all places unary operations don't work.
287//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530288void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000290 std::stringstream reasonStream;
291 reasonStream << "wrong operand type - no operation '" << op
292 << "' exists that takes an operand of type " << operand
293 << " (or there is no acceptable conversion)";
294 std::string reason = reasonStream.str();
295 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296}
297
298//
299// Same error message for all binary operations don't work.
300//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400301void TParseContext::binaryOpError(const TSourceLoc &line,
302 const char *op,
303 TString left,
304 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000306 std::stringstream reasonStream;
307 reasonStream << "wrong operand types - no operation '" << op
308 << "' exists that takes a left-hand operand of type '" << left
309 << "' and a right operand of type '" << right
310 << "' (or there is no acceptable conversion)";
311 std::string reason = reasonStream.str();
312 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313}
314
Olli Etuaho856c4972016-08-08 11:38:39 +0300315void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
316 TPrecision precision,
317 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530318{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400319 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300320 return;
Martin Radev70866b82016-07-22 15:27:42 +0300321
322 if (precision != EbpUndefined && !SupportsPrecision(type))
323 {
324 error(line, "illegal type for precision qualifier", getBasicString(type));
325 }
326
Olli Etuaho183d7e22015-11-20 15:59:09 +0200327 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530328 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200329 switch (type)
330 {
331 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400332 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300333 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200334 case EbtInt:
335 case EbtUInt:
336 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400337 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300338 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200339 default:
340 if (IsSampler(type))
341 {
342 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300343 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200344 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300345 if (IsImage(type))
346 {
347 error(line, "No precision specified (image)", "");
348 return;
349 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200350 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000351 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000352}
353
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354// Both test and if necessary, spit out an error, to see if the node is really
355// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300356bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500358 TIntermSymbol *symNode = node->getAsSymbolNode();
359 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100360 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
361
362 if (swizzleNode)
363 {
364 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
365 if (ok && swizzleNode->hasDuplicateOffsets())
366 {
367 error(line, " l-value of swizzle cannot have duplicate components", op);
368 return false;
369 }
370 return ok;
371 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372
Arun Patole7e7e68d2015-05-22 12:02:25 +0530373 if (binaryNode)
374 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400375 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530376 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400377 case EOpIndexDirect:
378 case EOpIndexIndirect:
379 case EOpIndexDirectStruct:
380 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300381 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400382 default:
383 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000384 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000385 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300386 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388
Arun Patole7e7e68d2015-05-22 12:02:25 +0530389 const char *message = 0;
390 switch (node->getQualifier())
391 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400392 case EvqConst:
393 message = "can't modify a const";
394 break;
395 case EvqConstReadOnly:
396 message = "can't modify a const";
397 break;
398 case EvqAttribute:
399 message = "can't modify an attribute";
400 break;
401 case EvqFragmentIn:
402 message = "can't modify an input";
403 break;
404 case EvqVertexIn:
405 message = "can't modify an input";
406 break;
407 case EvqUniform:
408 message = "can't modify a uniform";
409 break;
410 case EvqVaryingIn:
411 message = "can't modify a varying";
412 break;
413 case EvqFragCoord:
414 message = "can't modify gl_FragCoord";
415 break;
416 case EvqFrontFacing:
417 message = "can't modify gl_FrontFacing";
418 break;
419 case EvqPointCoord:
420 message = "can't modify gl_PointCoord";
421 break;
Martin Radevb0883602016-08-04 17:48:58 +0300422 case EvqNumWorkGroups:
423 message = "can't modify gl_NumWorkGroups";
424 break;
425 case EvqWorkGroupSize:
426 message = "can't modify gl_WorkGroupSize";
427 break;
428 case EvqWorkGroupID:
429 message = "can't modify gl_WorkGroupID";
430 break;
431 case EvqLocalInvocationID:
432 message = "can't modify gl_LocalInvocationID";
433 break;
434 case EvqGlobalInvocationID:
435 message = "can't modify gl_GlobalInvocationID";
436 break;
437 case EvqLocalInvocationIndex:
438 message = "can't modify gl_LocalInvocationIndex";
439 break;
Martin Radev802abe02016-08-04 17:48:32 +0300440 case EvqComputeIn:
441 message = "can't modify work group size variable";
442 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400443 default:
444 //
445 // Type that can't be written to?
446 //
447 if (node->getBasicType() == EbtVoid)
448 {
449 message = "can't modify void";
450 }
451 if (IsSampler(node->getBasicType()))
452 {
453 message = "can't modify a sampler";
454 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300455 if (IsImage(node->getBasicType()))
456 {
457 message = "can't modify an image";
458 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000459 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460
Arun Patole7e7e68d2015-05-22 12:02:25 +0530461 if (message == 0 && binaryNode == 0 && symNode == 0)
462 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000463 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000464
Olli Etuaho8a176262016-08-16 14:23:01 +0300465 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000466 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000467
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000468 //
469 // Everything else is okay, no error.
470 //
471 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300472 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000474 //
475 // If we get here, we have an error and a message.
476 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530477 if (symNode)
478 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000479 const char *symbol = symNode->getSymbol().c_str();
480 std::stringstream reasonStream;
481 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
482 std::string reason = reasonStream.str();
483 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000484 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530485 else
486 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000487 std::stringstream reasonStream;
488 reasonStream << "l-value required (" << message << ")";
489 std::string reason = reasonStream.str();
490 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000491 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000492
Olli Etuaho8a176262016-08-16 14:23:01 +0300493 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494}
495
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496// Both test, and if necessary spit out an error, to see if the node is really
497// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300498void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000499{
Olli Etuaho383b7912016-08-05 11:22:59 +0300500 if (node->getQualifier() != EvqConst)
501 {
502 error(node->getLine(), "constant expression required", "");
503 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000504}
505
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506// Both test, and if necessary spit out an error, to see if the node is really
507// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300508void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000509{
Olli Etuaho383b7912016-08-05 11:22:59 +0300510 if (!node->isScalarInt())
511 {
512 error(node->getLine(), "integer expression required", token);
513 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514}
515
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516// Both test, and if necessary spit out an error, to see if we are currently
517// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800518bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519{
Olli Etuaho856c4972016-08-08 11:38:39 +0300520 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300521 {
522 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800523 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300524 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800525 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526}
527
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000528// For now, keep it simple: if it starts "gl_", it's reserved, independent
529// of scope. Except, if the symbol table is at the built-in push-level,
530// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000531// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
532// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300533bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530535 static const char *reservedErrMsg = "reserved built-in name";
536 if (!symbolTable.atBuiltInLevel())
537 {
538 if (identifier.compare(0, 3, "gl_") == 0)
539 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000540 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300541 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000542 }
Jamie Madillacb4b812016-11-07 13:50:29 -0500543 if (sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530544 {
545 if (identifier.compare(0, 6, "webgl_") == 0)
546 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000547 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300548 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000549 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530550 if (identifier.compare(0, 7, "_webgl_") == 0)
551 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000552 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300553 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000554 }
555 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530556 if (identifier.find("__") != TString::npos)
557 {
558 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400559 "identifiers containing two consecutive underscores (__) are reserved as "
560 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530561 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300562 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000563 }
564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
Olli Etuaho8a176262016-08-16 14:23:01 +0300566 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567}
568
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000569// Make sure there is enough data provided to the constructor to build
570// something of the type of the constructor. Also returns the type of
571// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300572bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800573 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300574 TOperator op,
575 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400578 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530579 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400580 case EOpConstructMat2:
581 case EOpConstructMat2x3:
582 case EOpConstructMat2x4:
583 case EOpConstructMat3x2:
584 case EOpConstructMat3:
585 case EOpConstructMat3x4:
586 case EOpConstructMat4x2:
587 case EOpConstructMat4x3:
588 case EOpConstructMat4:
589 constructingMatrix = true;
590 break;
591 default:
592 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000595 //
596 // Note: It's okay to have too many components available, but not okay to have unused
597 // arguments. 'full' will go to true when enough args have been seen. If we loop
598 // again, there is an extra argument, so 'overfull' will become true.
599 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000600
Jamie Madillb98c3a82015-07-23 14:26:04 -0400601 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400602 bool full = false;
603 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 bool matrixInMatrix = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500605 bool arrayArg = false;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800606 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530607 {
Olli Etuaho72d10202017-01-19 15:58:30 +0000608 const TIntermTyped *argTyped = arg->getAsTyped();
609 size += argTyped->getType().getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530610
Olli Etuaho72d10202017-01-19 15:58:30 +0000611 if (constructingMatrix && argTyped->getType().isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000612 matrixInMatrix = true;
613 if (full)
614 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300615 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616 full = true;
Olli Etuaho72d10202017-01-19 15:58:30 +0000617 if (argTyped->getType().isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000618 arrayArg = true;
619 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530620
Olli Etuaho856c4972016-08-08 11:38:39 +0300621 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300622 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300623 // The size of an unsized constructor should already have been determined.
624 ASSERT(!type.isUnsizedArray());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800625 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300626 {
627 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300628 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300629 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000630 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631
Arun Patole7e7e68d2015-05-22 12:02:25 +0530632 if (arrayArg && op != EOpConstructStruct)
633 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000634 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300635 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000636 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637
Olli Etuaho856c4972016-08-08 11:38:39 +0300638 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530639 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800640 if (arguments->size() != 1)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530641 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400642 error(line, "constructing matrix from matrix can only take one argument",
643 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300644 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000645 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
Arun Patole7e7e68d2015-05-22 12:02:25 +0530648 if (overFull)
649 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000650 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300651 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000652 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530653
Olli Etuaho856c4972016-08-08 11:38:39 +0300654 if (op == EOpConstructStruct && !type.isArray() &&
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800655 type.getStruct()->fields().size() != arguments->size())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400657 error(line,
658 "Number of constructor parameters does not match the number of structure fields",
659 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300660 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662
Olli Etuaho856c4972016-08-08 11:38:39 +0300663 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530664 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300665 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
666 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530667 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000668 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300669 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000670 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000671 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000672
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800673 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530674 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200675 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300676 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200678
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800679 for (TIntermNode *const &argNode : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530680 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200681 TIntermTyped *argTyped = argNode->getAsTyped();
682 ASSERT(argTyped != nullptr);
683 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
684 {
685 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300686 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200687 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300688 if (op != EOpConstructStruct && IsImage(argTyped->getBasicType()))
689 {
690 error(line, "cannot convert an image", "constructor");
691 return false;
692 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200693 if (argTyped->getBasicType() == EbtVoid)
694 {
695 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300696 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200697 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000698 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000699
Olli Etuaho856c4972016-08-08 11:38:39 +0300700 if (type.isArray())
701 {
702 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
703 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800704 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300705 {
706 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500707 // It has already been checked that the argument is not an array, but we can arrive
708 // here due to prior error conditions.
709 if (argType.isArray())
710 {
711 return false;
712 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300713 if (!argType.sameElementType(type))
714 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000715 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300716 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300717 }
718 }
719 }
720 else if (op == EOpConstructStruct)
721 {
722 const TFieldList &fields = type.getStruct()->fields();
Olli Etuaho856c4972016-08-08 11:38:39 +0300723
724 for (size_t i = 0; i < fields.size(); i++)
725 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800726 if (i >= arguments->size() ||
727 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300728 {
729 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000730 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300731 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300732 }
733 }
734 }
735
Olli Etuaho8a176262016-08-16 14:23:01 +0300736 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000737}
738
Jamie Madillb98c3a82015-07-23 14:26:04 -0400739// This function checks to see if a void variable has been declared and raise an error message for
740// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741//
742// returns true in case of an error
743//
Olli Etuaho856c4972016-08-08 11:38:39 +0300744bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400745 const TString &identifier,
746 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000747{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300748 if (type == EbtVoid)
749 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000750 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300751 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300752 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753
Olli Etuaho8a176262016-08-16 14:23:01 +0300754 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000755}
756
Jamie Madillb98c3a82015-07-23 14:26:04 -0400757// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300758// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300759void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530761 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
762 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000763 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530764 }
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 TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770{
Martin Radev4a9cd802016-09-01 16:51:51 +0300771 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530772 {
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
Olli Etuaho856c4972016-08-08 11:38:39 +0300777bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300778 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400779 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000780{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530781 if (pType.type == EbtStruct)
782 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300783 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530784 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000785 std::stringstream reasonStream;
786 reasonStream << reason << " (structure contains a sampler)";
787 std::string reasonStr = reasonStream.str();
788 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300789 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000790 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530791
Olli Etuaho8a176262016-08-16 14:23:01 +0300792 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530793 }
794 else if (IsSampler(pType.type))
795 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000796 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300797 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000798 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799
Olli Etuaho8a176262016-08-16 14:23:01 +0300800 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801}
802
Martin Radev2cc85b32016-08-05 16:22:53 +0300803bool TParseContext::checkIsNotImage(const TSourceLoc &line,
804 const TTypeSpecifierNonArray &pType,
805 const char *reason)
806{
807 if (pType.type == EbtStruct)
808 {
809 if (ContainsImage(*pType.userDef))
810 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000811 std::stringstream reasonStream;
812 reasonStream << reason << " (structure contains an image)";
813 std::string reasonStr = reasonStream.str();
814 error(line, reasonStr.c_str(), getBasicString(pType.type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300815
816 return false;
817 }
818
819 return true;
820 }
821 else if (IsImage(pType.type))
822 {
823 error(line, reason, getBasicString(pType.type));
824
825 return false;
826 }
827
828 return true;
829}
830
Olli Etuaho856c4972016-08-08 11:38:39 +0300831void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
832 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400833{
834 if (pType.layoutQualifier.location != -1)
835 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400836 error(line, "location must only be specified for a single input or output variable",
837 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400838 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400839}
840
Olli Etuaho856c4972016-08-08 11:38:39 +0300841void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
842 const TLayoutQualifier &layoutQualifier)
843{
844 if (layoutQualifier.location != -1)
845 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000846 error(location, "invalid layout qualifier: only valid on program inputs and outputs",
847 "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300848 }
849}
850
Martin Radev2cc85b32016-08-05 16:22:53 +0300851void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
852 TQualifier qualifier,
853 const TType &type)
854{
855 checkOutParameterIsNotSampler(line, qualifier, type);
856 checkOutParameterIsNotImage(line, qualifier, type);
857}
858
Olli Etuaho856c4972016-08-08 11:38:39 +0300859void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
860 TQualifier qualifier,
861 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862{
Martin Radev2cc85b32016-08-05 16:22:53 +0300863 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
864 if (IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530865 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000866 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000867 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868}
869
Martin Radev2cc85b32016-08-05 16:22:53 +0300870void TParseContext::checkOutParameterIsNotImage(const TSourceLoc &line,
871 TQualifier qualifier,
872 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873{
Martin Radev2cc85b32016-08-05 16:22:53 +0300874 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
875 if (IsImage(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530876 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300877 error(line, "images cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000878 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879}
880
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300882unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530884 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000885
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200886 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
887 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
888 // fold as array size.
889 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000890 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000891 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300892 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000893 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894
Olli Etuaho856c4972016-08-08 11:38:39 +0300895 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400896
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000897 if (constant->getBasicType() == EbtUInt)
898 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300899 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000900 }
901 else
902 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300903 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000904
Olli Etuaho856c4972016-08-08 11:38:39 +0300905 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000906 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400907 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300908 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000909 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400910
Olli Etuaho856c4972016-08-08 11:38:39 +0300911 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400912 }
913
Olli Etuaho856c4972016-08-08 11:38:39 +0300914 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400915 {
916 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300917 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400918 }
919
920 // The size of arrays is restricted here to prevent issues further down the
921 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
922 // 4096 registers so this should be reasonable even for aggressively optimizable code.
923 const unsigned int sizeLimit = 65536;
924
Olli Etuaho856c4972016-08-08 11:38:39 +0300925 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400926 {
927 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300928 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000929 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300930
931 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932}
933
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300935bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
936 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937{
Olli Etuaho8a176262016-08-16 14:23:01 +0300938 if ((elementQualifier.qualifier == EvqAttribute) ||
939 (elementQualifier.qualifier == EvqVertexIn) ||
940 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300941 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400942 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300943 TType(elementQualifier).getQualifierString());
944 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000945 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946
Olli Etuaho8a176262016-08-16 14:23:01 +0300947 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
949
Olli Etuaho8a176262016-08-16 14:23:01 +0300950// See if this element type can be formed into an array.
951bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000953 //
954 // Can the type be an array?
955 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300956 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400957 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300958 error(line, "cannot declare arrays of arrays",
959 TType(elementType).getCompleteString().c_str());
960 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000961 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300962 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
963 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
964 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300965 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300966 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300967 {
968 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300969 TType(elementType).getCompleteString().c_str());
970 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300971 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
Olli Etuaho8a176262016-08-16 14:23:01 +0300973 return true;
974}
975
976// Check if this qualified element type can be formed into an array.
977bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
978 const TPublicType &elementType)
979{
980 if (checkIsValidTypeForArray(indexLocation, elementType))
981 {
982 return checkIsValidQualifierForArray(indexLocation, elementType);
983 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000984 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985}
986
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300988void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
989 const TString &identifier,
990 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991{
Olli Etuaho3739d232015-04-08 12:23:44 +0300992 ASSERT(type != nullptr);
993 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000994 {
995 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300996 type->qualifier = EvqTemporary;
997
998 // Generate informative error messages for ESSL1.
999 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001000 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001001 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301002 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001003 "structures containing arrays may not be declared constant since they cannot be "
1004 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301005 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001006 }
1007 else
1008 {
1009 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1010 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001011 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001012 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001013 if (type->isUnsizedArray())
1014 {
1015 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001016 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017}
1018
Olli Etuaho2935c582015-04-08 14:32:06 +03001019// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001020// and update the symbol table.
1021//
Olli Etuaho2935c582015-04-08 14:32:06 +03001022// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001024bool TParseContext::declareVariable(const TSourceLoc &line,
1025 const TString &identifier,
1026 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001027 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028{
Olli Etuaho2935c582015-04-08 14:32:06 +03001029 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030
Olli Etuaho856c4972016-08-08 11:38:39 +03001031 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032
Olli Etuaho2935c582015-04-08 14:32:06 +03001033 // gl_LastFragData may be redeclared with a new precision qualifier
1034 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1035 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001036 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1037 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001038 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001039 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001040 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001041 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001042 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001043 }
1044 }
1045 else
1046 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001047 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1048 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001049 return false;
1050 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001051 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001052
Olli Etuaho8a176262016-08-16 14:23:01 +03001053 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001054 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001055
Olli Etuaho2935c582015-04-08 14:32:06 +03001056 (*variable) = new TVariable(&identifier, type);
1057 if (!symbolTable.declare(*variable))
1058 {
1059 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001060 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001061 return false;
1062 }
1063
Olli Etuaho8a176262016-08-16 14:23:01 +03001064 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001065 return false;
1066
1067 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068}
1069
Martin Radev70866b82016-07-22 15:27:42 +03001070void TParseContext::checkIsParameterQualifierValid(
1071 const TSourceLoc &line,
1072 const TTypeQualifierBuilder &typeQualifierBuilder,
1073 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301074{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001075 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001076
1077 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301078 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001079 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1080 }
1081
1082 if (!IsImage(type->getBasicType()))
1083 {
1084 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, line);
1085 }
1086 else
1087 {
1088 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001089 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090
Martin Radev70866b82016-07-22 15:27:42 +03001091 type->setQualifier(typeQualifier.qualifier);
1092
1093 if (typeQualifier.precision != EbpUndefined)
1094 {
1095 type->setPrecision(typeQualifier.precision);
1096 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097}
1098
Olli Etuaho856c4972016-08-08 11:38:39 +03001099bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001100{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001101 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001102 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301103 if (iter == extBehavior.end())
1104 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001105 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001106 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001107 }
zmo@google.comf5450912011-09-09 01:37:19 +00001108 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301109 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1110 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001111 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1112 // associated with more than one extension.
1113 if (extension == "GL_OVR_multiview")
1114 {
1115 return checkCanUseExtension(line, "GL_OVR_multiview2");
1116 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001117 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001118 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001119 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301120 if (iter->second == EBhWarn)
1121 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001122 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001123 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001124 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001125
Olli Etuaho8a176262016-08-16 14:23:01 +03001126 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001127}
1128
Martin Radevb8b01222016-11-20 23:25:53 +02001129void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1130 const TSourceLoc &location)
1131{
1132 if (publicType.isUnsizedArray())
1133 {
1134 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1135 // error. It is assumed that this applies to empty declarations as well.
1136 error(location, "empty array declaration needs to specify a size", "");
1137 }
1138 if (publicType.qualifier == EvqShared && !publicType.layoutQualifier.isEmpty())
1139 {
1140 error(location, "Shared memory declarations cannot have layout specified", "layout");
1141 }
1142}
1143
Jamie Madillb98c3a82015-07-23 14:26:04 -04001144// These checks are common for all declarations starting a declarator list, and declarators that
1145// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +03001146void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001147 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001148{
Olli Etuahofa33d582015-04-09 14:33:12 +03001149 switch (publicType.qualifier)
1150 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001151 case EvqVaryingIn:
1152 case EvqVaryingOut:
1153 case EvqAttribute:
1154 case EvqVertexIn:
1155 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001156 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001157 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001158 {
1159 error(identifierLocation, "cannot be used with a structure",
1160 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001161 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001162 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001163
Jamie Madillb98c3a82015-07-23 14:26:04 -04001164 default:
1165 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001166 }
1167
Jamie Madillb98c3a82015-07-23 14:26:04 -04001168 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001169 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1170 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001171 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001172 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001173 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001174 if (publicType.qualifier != EvqUniform &&
1175 !checkIsNotImage(identifierLocation, publicType.typeSpecifierNonArray,
1176 "images must be uniform"))
1177 {
1178 return;
1179 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001180
1181 // check for layout qualifier issues
1182 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1183
1184 if (layoutQualifier.matrixPacking != EmpUnspecified)
1185 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001186 error(identifierLocation, "layout qualifier only valid for interface blocks",
1187 getMatrixPackingString(layoutQualifier.matrixPacking));
Olli Etuaho383b7912016-08-05 11:22:59 +03001188 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001189 }
1190
1191 if (layoutQualifier.blockStorage != EbsUnspecified)
1192 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001193 error(identifierLocation, "layout qualifier only valid for interface blocks",
1194 getBlockStorageString(layoutQualifier.blockStorage));
Olli Etuaho383b7912016-08-05 11:22:59 +03001195 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001196 }
1197
Olli Etuaho383b7912016-08-05 11:22:59 +03001198 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001199 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001200 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001201 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001202
1203 if (IsImage(publicType.getBasicType()))
1204 {
1205
1206 switch (layoutQualifier.imageInternalFormat)
1207 {
1208 case EiifRGBA32F:
1209 case EiifRGBA16F:
1210 case EiifR32F:
1211 case EiifRGBA8:
1212 case EiifRGBA8_SNORM:
1213 if (!IsFloatImage(publicType.getBasicType()))
1214 {
1215 error(identifierLocation,
1216 "internal image format requires a floating image type",
1217 getBasicString(publicType.getBasicType()));
1218 return;
1219 }
1220 break;
1221 case EiifRGBA32I:
1222 case EiifRGBA16I:
1223 case EiifRGBA8I:
1224 case EiifR32I:
1225 if (!IsIntegerImage(publicType.getBasicType()))
1226 {
1227 error(identifierLocation,
1228 "internal image format requires an integer image type",
1229 getBasicString(publicType.getBasicType()));
1230 return;
1231 }
1232 break;
1233 case EiifRGBA32UI:
1234 case EiifRGBA16UI:
1235 case EiifRGBA8UI:
1236 case EiifR32UI:
1237 if (!IsUnsignedImage(publicType.getBasicType()))
1238 {
1239 error(identifierLocation,
1240 "internal image format requires an unsigned image type",
1241 getBasicString(publicType.getBasicType()));
1242 return;
1243 }
1244 break;
1245 case EiifUnspecified:
1246 error(identifierLocation, "layout qualifier", "No image internal format specified");
1247 return;
1248 default:
1249 error(identifierLocation, "layout qualifier", "unrecognized token");
1250 return;
1251 }
1252
1253 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1254 switch (layoutQualifier.imageInternalFormat)
1255 {
1256 case EiifR32F:
1257 case EiifR32I:
1258 case EiifR32UI:
1259 break;
1260 default:
1261 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1262 {
1263 error(identifierLocation, "layout qualifier",
1264 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1265 "image variables must be qualified readonly and/or writeonly");
1266 return;
1267 }
1268 break;
1269 }
1270 }
1271 else
1272 {
1273
1274 if (!checkInternalFormatIsNotSpecified(identifierLocation,
1275 layoutQualifier.imageInternalFormat))
1276 {
1277 return;
1278 }
1279
1280 if (!checkIsMemoryQualifierNotSpecified(publicType.memoryQualifier, identifierLocation))
1281 {
1282 return;
1283 }
1284 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001285}
1286
Olli Etuaho856c4972016-08-08 11:38:39 +03001287void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1288 const TString &layoutQualifierName,
1289 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001290{
1291
1292 if (mShaderVersion < versionRequired)
1293 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001294 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001295 }
1296}
1297
Olli Etuaho856c4972016-08-08 11:38:39 +03001298bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1299 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001300{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001301 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001302 for (size_t i = 0u; i < localSize.size(); ++i)
1303 {
1304 if (localSize[i] != -1)
1305 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001306 error(location,
1307 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1308 "global layout declaration",
1309 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001310 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001311 }
1312 }
1313
Olli Etuaho8a176262016-08-16 14:23:01 +03001314 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001315}
1316
Martin Radev2cc85b32016-08-05 16:22:53 +03001317bool TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
1318 TLayoutImageInternalFormat internalFormat)
1319{
1320 if (internalFormat != EiifUnspecified)
1321 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001322 error(location, "invalid layout qualifier: only valid when used with images",
1323 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001324 return false;
1325 }
1326 return true;
1327}
1328
Olli Etuaho383b7912016-08-05 11:22:59 +03001329void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001330 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001331{
1332 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1333 {
1334 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1335 if (qual == EvqOut || qual == EvqInOut)
1336 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001337 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001338 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001339 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001340 TString unmangledName =
1341 TFunction::unmangleName(fnCall->getFunctionSymbolInfo()->getName());
Olli Etuaho856c4972016-08-08 11:38:39 +03001342 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001343 "Constant value cannot be passed for 'out' or 'inout' parameters.",
1344 unmangledName.c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001345 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001346 }
1347 }
1348 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001349}
1350
Martin Radev70866b82016-07-22 15:27:42 +03001351void TParseContext::checkInvariantVariableQualifier(bool invariant,
1352 const TQualifier qualifier,
1353 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001354{
Martin Radev70866b82016-07-22 15:27:42 +03001355 if (!invariant)
1356 return;
1357
1358 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001359 {
Martin Radev70866b82016-07-22 15:27:42 +03001360 // input variables in the fragment shader can be also qualified as invariant
1361 if (!sh::CanBeInvariantESSL1(qualifier))
1362 {
1363 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1364 }
1365 }
1366 else
1367 {
1368 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1369 {
1370 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1371 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001372 }
1373}
1374
Arun Patole7e7e68d2015-05-22 12:02:25 +05301375bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001376{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001377 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001378 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1379 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001380}
1381
Arun Patole7e7e68d2015-05-22 12:02:25 +05301382bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001383{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001384 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001385}
1386
Jamie Madillb98c3a82015-07-23 14:26:04 -04001387void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1388 const char *extName,
1389 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001390{
1391 pp::SourceLocation srcLoc;
1392 srcLoc.file = loc.first_file;
1393 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001394 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001395}
1396
Jamie Madillb98c3a82015-07-23 14:26:04 -04001397void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1398 const char *name,
1399 const char *value,
1400 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001401{
1402 pp::SourceLocation srcLoc;
1403 srcLoc.file = loc.first_file;
1404 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001405 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001406}
1407
Martin Radev4c4c8e72016-08-04 12:25:34 +03001408sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001409{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001410 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001411 for (size_t i = 0u; i < result.size(); ++i)
1412 {
1413 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1414 {
1415 result[i] = 1;
1416 }
1417 else
1418 {
1419 result[i] = mComputeShaderLocalSize[i];
1420 }
1421 }
1422 return result;
1423}
1424
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001425/////////////////////////////////////////////////////////////////////////////////
1426//
1427// Non-Errors.
1428//
1429/////////////////////////////////////////////////////////////////////////////////
1430
Jamie Madill5c097022014-08-20 16:38:32 -04001431const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1432 const TString *name,
1433 const TSymbol *symbol)
1434{
1435 const TVariable *variable = NULL;
1436
1437 if (!symbol)
1438 {
1439 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001440 }
1441 else if (!symbol->isVariable())
1442 {
1443 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001444 }
1445 else
1446 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001447 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001448
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001449 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001450 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001451 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001452 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001453 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001454
1455 // Reject shaders using both gl_FragData and gl_FragColor
1456 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001457 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001458 {
1459 mUsesFragData = true;
1460 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001461 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001462 {
1463 mUsesFragColor = true;
1464 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001465 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1466 {
1467 mUsesSecondaryOutputs = true;
1468 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001469
1470 // This validation is not quite correct - it's only an error to write to
1471 // both FragData and FragColor. For simplicity, and because users shouldn't
1472 // be rewarded for reading from undefined varaibles, return an error
1473 // if they are both referenced, rather than assigned.
1474 if (mUsesFragData && mUsesFragColor)
1475 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001476 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1477 if (mUsesSecondaryOutputs)
1478 {
1479 errorMessage =
1480 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1481 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1482 }
1483 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001484 }
Martin Radevb0883602016-08-04 17:48:58 +03001485
1486 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1487 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1488 qualifier == EvqWorkGroupSize)
1489 {
1490 error(location,
1491 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1492 "gl_WorkGroupSize");
1493 }
Jamie Madill5c097022014-08-20 16:38:32 -04001494 }
1495
1496 if (!variable)
1497 {
1498 TType type(EbtFloat, EbpUndefined);
1499 TVariable *fakeVariable = new TVariable(name, type);
1500 symbolTable.declare(fakeVariable);
1501 variable = fakeVariable;
1502 }
1503
1504 return variable;
1505}
1506
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001507TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1508 const TString *name,
1509 const TSymbol *symbol)
1510{
1511 const TVariable *variable = getNamedVariable(location, name, symbol);
1512
Olli Etuaho09b04a22016-12-15 13:30:26 +00001513 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1514 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1515 {
1516 // WEBGL_multiview spec
1517 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1518 "gl_ViewID_OVR");
1519 }
1520
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001521 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001522 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001523 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001524 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001525 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001526 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1527 mComputeShaderLocalSizeDeclared)
1528 {
1529 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1530 // needs to be added to the AST as a constant and not as a symbol.
1531 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1532 TConstantUnion *constArray = new TConstantUnion[3];
1533 for (size_t i = 0; i < 3; ++i)
1534 {
1535 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1536 }
1537
1538 ASSERT(variable->getType().getBasicType() == EbtUInt);
1539 ASSERT(variable->getType().getObjectSize() == 3);
1540
1541 TType type(variable->getType());
1542 type.setQualifier(EvqConst);
1543 return intermediate.addConstantUnion(constArray, type, location);
1544 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001545 else
1546 {
1547 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1548 variable->getType(), location);
1549 }
1550}
1551
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001552//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001553// Initializers show up in several places in the grammar. Have one set of
1554// code to handle them here.
1555//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001556// Returns true on error, false if no error
1557//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001558bool TParseContext::executeInitializer(const TSourceLoc &line,
1559 const TString &identifier,
1560 const TPublicType &pType,
1561 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001562 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001563{
Olli Etuaho13389b62016-10-16 11:48:18 +01001564 ASSERT(initNode != nullptr);
1565 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001566 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001567
Olli Etuaho2935c582015-04-08 14:32:06 +03001568 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001569 if (type.isUnsizedArray())
1570 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001571 // We have not checked yet whether the initializer actually is an array or not.
1572 if (initializer->isArray())
1573 {
1574 type.setArraySize(initializer->getArraySize());
1575 }
1576 else
1577 {
1578 // Having a non-array initializer for an unsized array will result in an error later,
1579 // so we don't generate an error message here.
1580 type.setArraySize(1u);
1581 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001582 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001583 if (!declareVariable(line, identifier, type, &variable))
1584 {
1585 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001586 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587
Olli Etuahob0c645e2015-05-12 14:25:36 +03001588 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001589 if (symbolTable.atGlobalLevel() &&
1590 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001591 {
1592 // Error message does not completely match behavior with ESSL 1.00, but
1593 // we want to steer developers towards only using constant expressions.
1594 error(line, "global variable initializers must be constant expressions", "=");
1595 return true;
1596 }
1597 if (globalInitWarning)
1598 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001599 warning(
1600 line,
1601 "global variable initializers should be constant expressions "
1602 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1603 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001604 }
1605
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001606 //
1607 // identifier must be of type constant, a global, or a temporary
1608 //
1609 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301610 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1611 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001612 error(line, " cannot initialize this type of qualifier ",
1613 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001614 return true;
1615 }
1616 //
1617 // test for and propagate constant
1618 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619
Arun Patole7e7e68d2015-05-22 12:02:25 +05301620 if (qualifier == EvqConst)
1621 {
1622 if (qualifier != initializer->getType().getQualifier())
1623 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001624 std::stringstream reasonStream;
1625 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1626 << "'";
1627 std::string reason = reasonStream.str();
1628 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001629 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001630 return true;
1631 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301632 if (type != initializer->getType())
1633 {
1634 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001635 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001636 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001637 return true;
1638 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001639
1640 // Save the constant folded value to the variable if possible. For example array
1641 // initializers are not folded, since that way copying the array literal to multiple places
1642 // in the shader is avoided.
1643 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1644 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301645 if (initializer->getAsConstantUnion())
1646 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001647 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001648 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001649 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301650 }
1651 else if (initializer->getAsSymbolNode())
1652 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001653 const TSymbol *symbol =
1654 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1655 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001657 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001658 if (constArray)
1659 {
1660 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001661 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001662 return false;
1663 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001664 }
1665 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001666
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001667 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1668 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001669 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1670 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001671 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001672 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1673 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001674 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001676 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677}
1678
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001679void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1680{
1681 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1682 typeSpecifier->getBasicType());
1683
1684 if (mShaderVersion < 300 && typeSpecifier->array)
1685 {
1686 error(typeSpecifier->getLine(), "not supported", "first-class array");
1687 typeSpecifier->clearArrayness();
1688 }
1689}
1690
Martin Radev70866b82016-07-22 15:27:42 +03001691TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301692 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001693{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001694 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001695
Martin Radev70866b82016-07-22 15:27:42 +03001696 TPublicType returnType = typeSpecifier;
1697 returnType.qualifier = typeQualifier.qualifier;
1698 returnType.invariant = typeQualifier.invariant;
1699 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001700 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001701 returnType.precision = typeSpecifier.precision;
1702
1703 if (typeQualifier.precision != EbpUndefined)
1704 {
1705 returnType.precision = typeQualifier.precision;
1706 }
1707
Martin Radev4a9cd802016-09-01 16:51:51 +03001708 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1709 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001710
Martin Radev4a9cd802016-09-01 16:51:51 +03001711 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1712 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001713
Martin Radev4a9cd802016-09-01 16:51:51 +03001714 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001715
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001716 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001717 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001718 if (typeSpecifier.array)
1719 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001720 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001721 returnType.clearArrayness();
1722 }
1723
Martin Radev70866b82016-07-22 15:27:42 +03001724 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001725 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001726 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001727 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001728 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001729 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001730
Martin Radev70866b82016-07-22 15:27:42 +03001731 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001732 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001733 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001734 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001735 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001736 }
1737 }
1738 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001739 {
Martin Radev70866b82016-07-22 15:27:42 +03001740 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001741 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001742 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001743 }
Martin Radev70866b82016-07-22 15:27:42 +03001744 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1745 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001746 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001747 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1748 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001749 }
Martin Radev70866b82016-07-22 15:27:42 +03001750 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001751 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001752 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001753 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001754 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001755 }
1756
1757 return returnType;
1758}
1759
Olli Etuaho856c4972016-08-08 11:38:39 +03001760void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1761 const TPublicType &type,
1762 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001763{
1764 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001765 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001766 {
1767 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001768 }
1769
1770 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1771 switch (qualifier)
1772 {
1773 case EvqVertexIn:
1774 // ESSL 3.00 section 4.3.4
1775 if (type.array)
1776 {
1777 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001778 }
1779 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1780 return;
1781 case EvqFragmentOut:
1782 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001783 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001784 {
1785 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001786 }
1787 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1788 return;
1789 default:
1790 break;
1791 }
1792
1793 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1794 // restrictions.
1795 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001796 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1797 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001798 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1799 {
1800 error(qualifierLocation, "must use 'flat' interpolation here",
1801 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001802 }
1803
Martin Radev4a9cd802016-09-01 16:51:51 +03001804 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001805 {
1806 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1807 // These restrictions are only implied by the ESSL 3.00 spec, but
1808 // the ESSL 3.10 spec lists these restrictions explicitly.
1809 if (type.array)
1810 {
1811 error(qualifierLocation, "cannot be an array of structures",
1812 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001813 }
1814 if (type.isStructureContainingArrays())
1815 {
1816 error(qualifierLocation, "cannot be a structure containing an array",
1817 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001818 }
1819 if (type.isStructureContainingType(EbtStruct))
1820 {
1821 error(qualifierLocation, "cannot be a structure containing a structure",
1822 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001823 }
1824 if (type.isStructureContainingType(EbtBool))
1825 {
1826 error(qualifierLocation, "cannot be a structure containing a bool",
1827 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001828 }
1829 }
1830}
1831
Martin Radev2cc85b32016-08-05 16:22:53 +03001832void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1833{
1834 if (qualifier.getType() == QtStorage)
1835 {
1836 const TStorageQualifierWrapper &storageQualifier =
1837 static_cast<const TStorageQualifierWrapper &>(qualifier);
1838 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1839 !symbolTable.atGlobalLevel())
1840 {
1841 error(storageQualifier.getLine(),
1842 "Local variables can only use the const storage qualifier.",
1843 storageQualifier.getQualifierString().c_str());
1844 }
1845 }
1846}
1847
1848bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1849 const TSourceLoc &location)
1850{
1851 if (memoryQualifier.readonly)
1852 {
1853 error(location, "Only allowed with images.", "readonly");
1854 return false;
1855 }
1856 if (memoryQualifier.writeonly)
1857 {
1858 error(location, "Only allowed with images.", "writeonly");
1859 return false;
1860 }
Martin Radev049edfa2016-11-11 14:35:37 +02001861 if (memoryQualifier.coherent)
1862 {
1863 error(location, "Only allowed with images.", "coherent");
1864 return false;
1865 }
1866 if (memoryQualifier.restrictQualifier)
1867 {
1868 error(location, "Only allowed with images.", "restrict");
1869 return false;
1870 }
1871 if (memoryQualifier.volatileQualifier)
1872 {
1873 error(location, "Only allowed with images.", "volatile");
1874 return false;
1875 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001876 return true;
1877}
1878
Olli Etuaho13389b62016-10-16 11:48:18 +01001879TIntermDeclaration *TParseContext::parseSingleDeclaration(
1880 TPublicType &publicType,
1881 const TSourceLoc &identifierOrTypeLocation,
1882 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001883{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001884 TType type(publicType);
1885 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1886 mDirectiveHandler.pragma().stdgl.invariantAll)
1887 {
1888 TQualifier qualifier = type.getQualifier();
1889
1890 // The directive handler has already taken care of rejecting invalid uses of this pragma
1891 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1892 // affected variable declarations:
1893 //
1894 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1895 // elsewhere, in TranslatorGLSL.)
1896 //
1897 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1898 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1899 // the way this is currently implemented we have to enable this compiler option before
1900 // parsing the shader and determining the shading language version it uses. If this were
1901 // implemented as a post-pass, the workaround could be more targeted.
1902 //
1903 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1904 // the specification, but there are desktop OpenGL drivers that expect that this is the
1905 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1906 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1907 {
1908 type.setInvariant(true);
1909 }
1910 }
1911
1912 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001913
Olli Etuahobab4c082015-04-24 16:38:49 +03001914 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001915
Olli Etuahobab4c082015-04-24 16:38:49 +03001916 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1917
Olli Etuaho13389b62016-10-16 11:48:18 +01001918 TIntermDeclaration *declaration = new TIntermDeclaration();
1919 declaration->setLine(identifierOrTypeLocation);
1920
Olli Etuahobab4c082015-04-24 16:38:49 +03001921 if (emptyDeclaration)
1922 {
Martin Radevb8b01222016-11-20 23:25:53 +02001923 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobab4c082015-04-24 16:38:49 +03001924 }
1925 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001926 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001927 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001928
Olli Etuaho856c4972016-08-08 11:38:39 +03001929 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001930
Olli Etuaho2935c582015-04-08 14:32:06 +03001931 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001932 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001933
1934 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001935 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001936 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001937 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001938 }
1939
Olli Etuaho13389b62016-10-16 11:48:18 +01001940 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1941 // that may just declare a type.
1942 declaration->appendDeclarator(symbol);
1943
1944 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001945}
1946
Olli Etuaho13389b62016-10-16 11:48:18 +01001947TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1948 const TSourceLoc &identifierLocation,
1949 const TString &identifier,
1950 const TSourceLoc &indexLocation,
1951 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001952{
Olli Etuahofa33d582015-04-09 14:33:12 +03001953 mDeferredSingleDeclarationErrorCheck = false;
1954
Olli Etuaho383b7912016-08-05 11:22:59 +03001955 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001956
Olli Etuaho856c4972016-08-08 11:38:39 +03001957 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001958
Olli Etuaho8a176262016-08-16 14:23:01 +03001959 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001960
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001961 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001962
Olli Etuaho856c4972016-08-08 11:38:39 +03001963 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001964 // Make the type an array even if size check failed.
1965 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1966 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001967
Olli Etuaho2935c582015-04-08 14:32:06 +03001968 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001969 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001970
Olli Etuaho13389b62016-10-16 11:48:18 +01001971 TIntermDeclaration *declaration = new TIntermDeclaration();
1972 declaration->setLine(identifierLocation);
1973
Olli Etuahoe7847b02015-03-16 11:56:12 +02001974 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001975 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001976 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001977 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001978 declaration->appendDeclarator(symbol);
1979 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001980
Olli Etuaho13389b62016-10-16 11:48:18 +01001981 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001982}
1983
Olli Etuaho13389b62016-10-16 11:48:18 +01001984TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1985 const TSourceLoc &identifierLocation,
1986 const TString &identifier,
1987 const TSourceLoc &initLocation,
1988 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001989{
Olli Etuahofa33d582015-04-09 14:33:12 +03001990 mDeferredSingleDeclarationErrorCheck = false;
1991
Olli Etuaho383b7912016-08-05 11:22:59 +03001992 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001993
Olli Etuaho13389b62016-10-16 11:48:18 +01001994 TIntermDeclaration *declaration = new TIntermDeclaration();
1995 declaration->setLine(identifierLocation);
1996
1997 TIntermBinary *initNode = nullptr;
1998 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001999 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002000 if (initNode)
2001 {
2002 declaration->appendDeclarator(initNode);
2003 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002004 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002005 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002006}
2007
Olli Etuaho13389b62016-10-16 11:48:18 +01002008TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002009 TPublicType &publicType,
2010 const TSourceLoc &identifierLocation,
2011 const TString &identifier,
2012 const TSourceLoc &indexLocation,
2013 TIntermTyped *indexExpression,
2014 const TSourceLoc &initLocation,
2015 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002016{
2017 mDeferredSingleDeclarationErrorCheck = false;
2018
Olli Etuaho383b7912016-08-05 11:22:59 +03002019 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002020
Olli Etuaho8a176262016-08-16 14:23:01 +03002021 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002022
2023 TPublicType arrayType(publicType);
2024
Olli Etuaho856c4972016-08-08 11:38:39 +03002025 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002026 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2027 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002028 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002029 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002030 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002031 }
2032 // Make the type an array even if size check failed.
2033 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2034 arrayType.setArraySize(size);
2035
Olli Etuaho13389b62016-10-16 11:48:18 +01002036 TIntermDeclaration *declaration = new TIntermDeclaration();
2037 declaration->setLine(identifierLocation);
2038
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002039 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002040 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002041 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2042 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002043 if (initNode)
2044 {
2045 declaration->appendDeclarator(initNode);
2046 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002047 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002048
2049 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002050}
2051
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002052TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002053 const TTypeQualifierBuilder &typeQualifierBuilder,
2054 const TSourceLoc &identifierLoc,
2055 const TString *identifier,
2056 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002057{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002058 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002059
Martin Radev70866b82016-07-22 15:27:42 +03002060 if (!typeQualifier.invariant)
2061 {
2062 error(identifierLoc, "Expected invariant", identifier->c_str());
2063 return nullptr;
2064 }
2065 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2066 {
2067 return nullptr;
2068 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002069 if (!symbol)
2070 {
2071 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002072 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002073 }
Martin Radev70866b82016-07-22 15:27:42 +03002074 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002075 {
Martin Radev70866b82016-07-22 15:27:42 +03002076 error(identifierLoc, "invariant declaration specifies qualifier",
2077 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002078 }
Martin Radev70866b82016-07-22 15:27:42 +03002079 if (typeQualifier.precision != EbpUndefined)
2080 {
2081 error(identifierLoc, "invariant declaration specifies precision",
2082 getPrecisionString(typeQualifier.precision));
2083 }
2084 if (!typeQualifier.layoutQualifier.isEmpty())
2085 {
2086 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2087 }
2088
2089 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2090 ASSERT(variable);
2091 const TType &type = variable->getType();
2092
2093 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2094 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002095 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002096
2097 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2098
2099 TIntermSymbol *intermSymbol =
2100 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2101
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002102 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002103}
2104
Olli Etuaho13389b62016-10-16 11:48:18 +01002105void TParseContext::parseDeclarator(TPublicType &publicType,
2106 const TSourceLoc &identifierLocation,
2107 const TString &identifier,
2108 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002109{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002110 // If the declaration starting this declarator list was empty (example: int,), some checks were
2111 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002112 if (mDeferredSingleDeclarationErrorCheck)
2113 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002114 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002115 mDeferredSingleDeclarationErrorCheck = false;
2116 }
2117
Olli Etuaho856c4972016-08-08 11:38:39 +03002118 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002119
Olli Etuaho856c4972016-08-08 11:38:39 +03002120 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002121
Olli Etuaho2935c582015-04-08 14:32:06 +03002122 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002123 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002124
Jamie Madillb98c3a82015-07-23 14:26:04 -04002125 TIntermSymbol *symbol =
2126 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002127 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002128 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002129 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002130 declarationOut->appendDeclarator(symbol);
2131 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002132}
2133
Olli Etuaho13389b62016-10-16 11:48:18 +01002134void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2135 const TSourceLoc &identifierLocation,
2136 const TString &identifier,
2137 const TSourceLoc &arrayLocation,
2138 TIntermTyped *indexExpression,
2139 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002140{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002141 // If the declaration starting this declarator list was empty (example: int,), some checks were
2142 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002143 if (mDeferredSingleDeclarationErrorCheck)
2144 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002145 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002146 mDeferredSingleDeclarationErrorCheck = false;
2147 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002148
Olli Etuaho856c4972016-08-08 11:38:39 +03002149 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002150
Olli Etuaho856c4972016-08-08 11:38:39 +03002151 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002152
Olli Etuaho8a176262016-08-16 14:23:01 +03002153 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002154 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002155 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002156 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002157 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002158
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002159 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002160 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002161
Jamie Madillb98c3a82015-07-23 14:26:04 -04002162 TIntermSymbol *symbol =
2163 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002164 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002165 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002166
Olli Etuaho13389b62016-10-16 11:48:18 +01002167 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002168 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002169}
2170
Olli Etuaho13389b62016-10-16 11:48:18 +01002171void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2172 const TSourceLoc &identifierLocation,
2173 const TString &identifier,
2174 const TSourceLoc &initLocation,
2175 TIntermTyped *initializer,
2176 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002177{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002178 // If the declaration starting this declarator list was empty (example: int,), some checks were
2179 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002180 if (mDeferredSingleDeclarationErrorCheck)
2181 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002182 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002183 mDeferredSingleDeclarationErrorCheck = false;
2184 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002185
Olli Etuaho856c4972016-08-08 11:38:39 +03002186 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002187
Olli Etuaho13389b62016-10-16 11:48:18 +01002188 TIntermBinary *initNode = nullptr;
2189 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002190 {
2191 //
2192 // build the intermediate representation
2193 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002194 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002195 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002196 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002197 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002198 }
2199}
2200
Olli Etuaho13389b62016-10-16 11:48:18 +01002201void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2202 const TSourceLoc &identifierLocation,
2203 const TString &identifier,
2204 const TSourceLoc &indexLocation,
2205 TIntermTyped *indexExpression,
2206 const TSourceLoc &initLocation,
2207 TIntermTyped *initializer,
2208 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002209{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002210 // If the declaration starting this declarator list was empty (example: int,), some checks were
2211 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002212 if (mDeferredSingleDeclarationErrorCheck)
2213 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002214 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002215 mDeferredSingleDeclarationErrorCheck = false;
2216 }
2217
Olli Etuaho856c4972016-08-08 11:38:39 +03002218 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002219
Olli Etuaho8a176262016-08-16 14:23:01 +03002220 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002221
2222 TPublicType arrayType(publicType);
2223
Olli Etuaho856c4972016-08-08 11:38:39 +03002224 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002225 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2226 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002227 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002228 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002229 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002230 }
2231 // Make the type an array even if size check failed.
2232 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2233 arrayType.setArraySize(size);
2234
2235 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002236 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002237 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2238 {
2239 if (initNode)
2240 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002241 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002242 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002243 }
2244}
2245
Martin Radev70866b82016-07-22 15:27:42 +03002246void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002247{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002248 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002249 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002250
Martin Radev70866b82016-07-22 15:27:42 +03002251 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2252 typeQualifier.line);
2253
Jamie Madillc2128ff2016-07-04 10:26:17 -04002254 // It should never be the case, but some strange parser errors can send us here.
2255 if (layoutQualifier.isEmpty())
2256 {
2257 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002258 return;
2259 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002260
Martin Radev802abe02016-08-04 17:48:32 +03002261 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002262 {
Martin Radev802abe02016-08-04 17:48:32 +03002263 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002264 return;
2265 }
2266
Martin Radev2cc85b32016-08-05 16:22:53 +03002267 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2268
2269 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2270
Martin Radev802abe02016-08-04 17:48:32 +03002271 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002272 {
Martin Radev802abe02016-08-04 17:48:32 +03002273 if (mComputeShaderLocalSizeDeclared &&
2274 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2275 {
2276 error(typeQualifier.line, "Work group size does not match the previous declaration",
2277 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002278 return;
2279 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002280
Martin Radev802abe02016-08-04 17:48:32 +03002281 if (mShaderVersion < 310)
2282 {
2283 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002284 return;
2285 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002286
Martin Radev4c4c8e72016-08-04 12:25:34 +03002287 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002288 {
2289 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002290 return;
2291 }
2292
2293 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2294 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2295
2296 const TConstantUnion *maxComputeWorkGroupSizeData =
2297 maxComputeWorkGroupSize->getConstPointer();
2298
2299 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2300 {
2301 if (layoutQualifier.localSize[i] != -1)
2302 {
2303 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2304 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2305 if (mComputeShaderLocalSize[i] < 1 ||
2306 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2307 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002308 std::stringstream reasonStream;
2309 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2310 << maxComputeWorkGroupSizeValue;
2311 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002312
Olli Etuaho4de340a2016-12-16 09:32:03 +00002313 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002314 return;
2315 }
2316 }
2317 }
2318
2319 mComputeShaderLocalSizeDeclared = true;
2320 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00002321 else if (mMultiviewAvailable &&
2322 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
2323 typeQualifier.qualifier == EvqVertexIn)
2324 {
2325 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2326 // specification.
2327 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2328 {
2329 error(typeQualifier.line, "Number of views does not match the previous declaration",
2330 "layout");
2331 return;
2332 }
2333
2334 if (layoutQualifier.numViews == -1)
2335 {
2336 error(typeQualifier.line, "No num_views specified", "layout");
2337 return;
2338 }
2339
2340 if (layoutQualifier.numViews > mMaxNumViews)
2341 {
2342 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2343 "layout");
2344 return;
2345 }
2346
2347 mNumViews = layoutQualifier.numViews;
2348 }
Martin Radev802abe02016-08-04 17:48:32 +03002349 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002350 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002351 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002352 {
Martin Radev802abe02016-08-04 17:48:32 +03002353 return;
2354 }
2355
2356 if (typeQualifier.qualifier != EvqUniform)
2357 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002358 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2359 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002360 return;
2361 }
2362
2363 if (mShaderVersion < 300)
2364 {
2365 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2366 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002367 return;
2368 }
2369
Olli Etuaho09b04a22016-12-15 13:30:26 +00002370 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002371
2372 if (layoutQualifier.matrixPacking != EmpUnspecified)
2373 {
2374 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2375 }
2376
2377 if (layoutQualifier.blockStorage != EbsUnspecified)
2378 {
2379 mDefaultBlockStorage = layoutQualifier.blockStorage;
2380 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002381 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002382}
2383
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002384TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2385 const TFunction &function,
2386 const TSourceLoc &location,
2387 bool insertParametersToSymbolTable)
2388{
2389 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(function.getReturnType());
2390 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2391 // point to the data that already exists in the symbol table.
2392 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2393 prototype->setLine(location);
2394
2395 for (size_t i = 0; i < function.getParamCount(); i++)
2396 {
2397 const TConstParameter &param = function.getParam(i);
2398
2399 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2400 // be used for unused args).
2401 if (param.name != nullptr)
2402 {
2403 TVariable *variable = new TVariable(param.name, *param.type);
2404
2405 // Insert the parameter in the symbol table.
2406 if (insertParametersToSymbolTable && !symbolTable.declare(variable))
2407 {
2408 error(location, "redefinition", variable->getName().c_str());
2409 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2410 continue;
2411 }
2412 TIntermSymbol *symbol = intermediate.addSymbol(
2413 variable->getUniqueId(), variable->getName(), variable->getType(), location);
2414 prototype->appendParameter(symbol);
2415 }
2416 else
2417 {
2418 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2419 }
2420 }
2421 return prototype;
2422}
2423
Olli Etuaho16c745a2017-01-16 17:02:27 +00002424TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2425 const TFunction &parsedFunction,
2426 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002427{
Olli Etuaho476197f2016-10-11 13:59:08 +01002428 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2429 // first declaration. Either way the instance in the symbol table is used to track whether the
2430 // function is declared multiple times.
2431 TFunction *function = static_cast<TFunction *>(
2432 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2433 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002434 {
2435 // ESSL 1.00.17 section 4.2.7.
2436 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2437 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002438 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002439 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002440
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002441 TIntermFunctionPrototype *prototype =
2442 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002443
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002444 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002445
2446 if (!symbolTable.atGlobalLevel())
2447 {
2448 // ESSL 3.00.4 section 4.2.4.
2449 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002450 }
2451
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002452 return prototype;
2453}
2454
Olli Etuaho336b1472016-10-05 16:37:55 +01002455TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002456 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002457 TIntermBlock *functionBody,
2458 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002459{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002460 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002461 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2462 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002463 error(location, "function does not return a value:",
2464 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002465 }
2466
Olli Etuahof51fdd22016-10-03 10:03:40 +01002467 if (functionBody == nullptr)
2468 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002469 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002470 functionBody->setLine(location);
2471 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002472 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002473 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002474 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002475
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002476 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002477 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002478}
2479
Olli Etuaho476197f2016-10-11 13:59:08 +01002480void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2481 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002482 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002483{
Olli Etuaho476197f2016-10-11 13:59:08 +01002484 ASSERT(function);
2485 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002486 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002487 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002488
2489 if (builtIn)
2490 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002491 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002492 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002493 else
Jamie Madill185fb402015-06-12 15:48:48 -04002494 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002495 TFunction *prevDec = static_cast<TFunction *>(
2496 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2497
2498 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2499 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2500 // occurance.
2501 if (*function != prevDec)
2502 {
2503 // Swap the parameters of the previous declaration to the parameters of the function
2504 // definition (parameter names may differ).
2505 prevDec->swapParameters(**function);
2506
2507 // The function definition will share the same symbol as any previous declaration.
2508 *function = prevDec;
2509 }
2510
2511 if ((*function)->isDefined())
2512 {
2513 error(location, "function already has a body", (*function)->getName().c_str());
2514 }
2515
2516 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002517 }
Jamie Madill185fb402015-06-12 15:48:48 -04002518
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002519 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002520 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002521 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002522
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002523 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002524 setLoopNestingLevel(0);
2525}
2526
Jamie Madillb98c3a82015-07-23 14:26:04 -04002527TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002528{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002529 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002530 // We don't know at this point whether this is a function definition or a prototype.
2531 // The definition production code will check for redefinitions.
2532 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002533 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002534 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2535 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002536 //
2537 TFunction *prevDec =
2538 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302539
Martin Radevda6254b2016-12-14 17:00:36 +02002540 if (getShaderVersion() >= 300 &&
2541 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2542 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302543 {
Martin Radevda6254b2016-12-14 17:00:36 +02002544 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302545 // Therefore overloading or redefining builtin functions is an error.
2546 error(location, "Name of a built-in function cannot be redeclared as function",
2547 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302548 }
2549 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002550 {
2551 if (prevDec->getReturnType() != function->getReturnType())
2552 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002553 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002554 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002555 }
2556 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2557 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002558 if (prevDec->getParam(i).type->getQualifier() !=
2559 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002560 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002561 error(location,
2562 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002563 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002564 }
2565 }
2566 }
2567
2568 //
2569 // Check for previously declared variables using the same name.
2570 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002571 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002572 if (prevSym)
2573 {
2574 if (!prevSym->isFunction())
2575 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002576 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002577 }
2578 }
2579 else
2580 {
2581 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002582 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002583 }
2584
2585 // We're at the inner scope level of the function's arguments and body statement.
2586 // Add the function prototype to the surrounding scope instead.
2587 symbolTable.getOuterLevel()->insert(function);
2588
Olli Etuaho78d13742017-01-18 13:06:10 +00002589 // Raise error message if main function takes any parameters or return anything other than void
2590 if (function->getName() == "main")
2591 {
2592 if (function->getParamCount() > 0)
2593 {
2594 error(location, "function cannot take any parameter(s)", "main");
2595 }
2596 if (function->getReturnType().getBasicType() != EbtVoid)
2597 {
2598 error(location, "main function cannot return a value",
2599 function->getReturnType().getBasicString());
2600 }
2601 }
2602
Jamie Madill185fb402015-06-12 15:48:48 -04002603 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002604 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2605 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002606 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2607 //
2608 return function;
2609}
2610
Olli Etuaho9de84a52016-06-14 17:36:01 +03002611TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2612 const TString *name,
2613 const TSourceLoc &location)
2614{
2615 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2616 {
2617 error(location, "no qualifiers allowed for function return",
2618 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002619 }
2620 if (!type.layoutQualifier.isEmpty())
2621 {
2622 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002623 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002624 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002625 checkIsNotSampler(location, type.typeSpecifierNonArray,
2626 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002627 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002628 if (mShaderVersion < 300)
2629 {
2630 // Array return values are forbidden, but there's also no valid syntax for declaring array
2631 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002632 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002633
2634 if (type.isStructureContainingArrays())
2635 {
2636 // ESSL 1.00.17 section 6.1 Function Definitions
2637 error(location, "structures containing arrays can't be function return values",
2638 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002639 }
2640 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002641
2642 // Add the function as a prototype after parsing it (we do not support recursion)
2643 return new TFunction(name, new TType(type));
2644}
2645
Jamie Madill06145232015-05-13 13:10:01 -04002646TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002647{
Jamie Madill06145232015-05-13 13:10:01 -04002648 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002649 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002650 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002651 error(publicType.getLine(), "constructor can't be a structure definition",
2652 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002653 }
2654
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002655 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002656 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002657 {
2658 op = EOpConstructStruct;
2659 }
2660 else
2661 {
Geoff Lang156d7192016-07-21 16:11:00 -04002662 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002663 if (op == EOpNull)
2664 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002665 error(publicType.getLine(), "cannot construct this type",
2666 getBasicString(publicType.getBasicType()));
2667 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002668 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002669 }
2670 }
2671
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002672 const TType *type = new TType(publicType);
Olli Etuaho72d10202017-01-19 15:58:30 +00002673 return new TFunction(nullptr, type, op);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002674}
2675
Jamie Madillb98c3a82015-07-23 14:26:04 -04002676// This function is used to test for the correctness of the parameters passed to various constructor
2677// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002678//
Olli Etuaho856c4972016-08-08 11:38:39 +03002679// 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 +00002680//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002681TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002682 TOperator op,
Olli Etuaho72d10202017-01-19 15:58:30 +00002683 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302684 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002685{
Olli Etuaho856c4972016-08-08 11:38:39 +03002686 if (type.isUnsizedArray())
2687 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002688 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002689 {
2690 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2691 type.setArraySize(1u);
2692 return TIntermTyped::CreateZero(type);
2693 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002694 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03002695 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002696
Olli Etuaho72d10202017-01-19 15:58:30 +00002697 if (!checkConstructorArguments(line, arguments, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002698 {
Olli Etuaho72d10202017-01-19 15:58:30 +00002699 return TIntermTyped::CreateZero(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03002700 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002701
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002702 TIntermAggregate *constructorNode = new TIntermAggregate(type, op, arguments);
2703 constructorNode->setLine(line);
2704 ASSERT(constructorNode->isConstructor());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002706 TIntermTyped *constConstructor =
2707 intermediate.foldAggregateBuiltIn(constructorNode, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002708 if (constConstructor)
2709 {
2710 return constConstructor;
2711 }
2712
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002713 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714}
2715
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002716//
2717// Interface/uniform blocks
2718//
Olli Etuaho13389b62016-10-16 11:48:18 +01002719TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002720 const TTypeQualifierBuilder &typeQualifierBuilder,
2721 const TSourceLoc &nameLine,
2722 const TString &blockName,
2723 TFieldList *fieldList,
2724 const TString *instanceName,
2725 const TSourceLoc &instanceLine,
2726 TIntermTyped *arrayIndex,
2727 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002728{
Olli Etuaho856c4972016-08-08 11:38:39 +03002729 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002730
Olli Etuaho77ba4082016-12-16 12:01:18 +00002731 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002732
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002733 if (typeQualifier.qualifier != EvqUniform)
2734 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002735 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
2736 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002737 }
2738
Martin Radev70866b82016-07-22 15:27:42 +03002739 if (typeQualifier.invariant)
2740 {
2741 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2742 }
2743
Martin Radev2cc85b32016-08-05 16:22:53 +03002744 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2745
Jamie Madill099c0f32013-06-20 11:55:52 -04002746 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002747 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002748
Jamie Madill099c0f32013-06-20 11:55:52 -04002749 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2750 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002751 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002752 }
2753
Jamie Madill1566ef72013-06-20 11:55:54 -04002754 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2755 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002756 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002757 }
2758
Olli Etuaho856c4972016-08-08 11:38:39 +03002759 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002760
Martin Radev2cc85b32016-08-05 16:22:53 +03002761 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2762
Arun Patole7e7e68d2015-05-22 12:02:25 +05302763 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2764 if (!symbolTable.declare(blockNameSymbol))
2765 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002766 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002767 }
2768
Jamie Madill98493dd2013-07-08 14:39:03 -04002769 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302770 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2771 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002772 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302773 TType *fieldType = field->type();
2774 if (IsSampler(fieldType->getBasicType()))
2775 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002776 error(field->line(),
2777 "unsupported type - sampler types are not allowed in interface blocks",
2778 fieldType->getBasicString());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002779 }
2780
Martin Radev2cc85b32016-08-05 16:22:53 +03002781 if (IsImage(fieldType->getBasicType()))
2782 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002783 error(field->line(),
2784 "unsupported type - image types are not allowed in interface blocks",
2785 fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002786 }
2787
Jamie Madill98493dd2013-07-08 14:39:03 -04002788 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002789 switch (qualifier)
2790 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002791 case EvqGlobal:
2792 case EvqUniform:
2793 break;
2794 default:
2795 error(field->line(), "invalid qualifier on interface block member",
2796 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002797 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002798 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002799
Martin Radev70866b82016-07-22 15:27:42 +03002800 if (fieldType->isInvariant())
2801 {
2802 error(field->line(), "invalid qualifier on interface block member", "invariant");
2803 }
2804
Jamie Madilla5efff92013-06-06 11:56:47 -04002805 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002806 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002807 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002808
Jamie Madill98493dd2013-07-08 14:39:03 -04002809 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002810 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002811 error(field->line(), "invalid layout qualifier: cannot be used here",
2812 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04002813 }
2814
Jamie Madill98493dd2013-07-08 14:39:03 -04002815 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002816 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002817 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002818 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002819 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002820 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002821 warning(field->line(),
2822 "extraneous layout qualifier: only has an effect on matrix types",
2823 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04002824 }
2825
Jamie Madill98493dd2013-07-08 14:39:03 -04002826 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002827 }
2828
Jamie Madill98493dd2013-07-08 14:39:03 -04002829 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002830 unsigned int arraySize = 0;
2831 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002832 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002833 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002834 }
2835
Jamie Madillb98c3a82015-07-23 14:26:04 -04002836 TInterfaceBlock *interfaceBlock =
2837 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2838 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2839 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002840
2841 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002842 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002843
Jamie Madill98493dd2013-07-08 14:39:03 -04002844 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002845 {
2846 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002847 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2848 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002849 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302850 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002851
2852 // set parent pointer of the field variable
2853 fieldType->setInterfaceBlock(interfaceBlock);
2854
Arun Patole7e7e68d2015-05-22 12:02:25 +05302855 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002856 fieldVariable->setQualifier(typeQualifier.qualifier);
2857
Arun Patole7e7e68d2015-05-22 12:02:25 +05302858 if (!symbolTable.declare(fieldVariable))
2859 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002860 error(field->line(), "redefinition of an interface block member name",
2861 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002862 }
2863 }
2864 }
2865 else
2866 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002867 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002868
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002869 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302870 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002871 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002872
Arun Patole7e7e68d2015-05-22 12:02:25 +05302873 if (!symbolTable.declare(instanceTypeDef))
2874 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002875 error(instanceLine, "redefinition of an interface block instance name",
2876 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002877 }
2878
Jamie Madillb98c3a82015-07-23 14:26:04 -04002879 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002880 symbolName = instanceTypeDef->getName();
2881 }
2882
Olli Etuaho13389b62016-10-16 11:48:18 +01002883 TIntermSymbol *blockSymbol =
2884 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2885 TIntermDeclaration *declaration = new TIntermDeclaration();
2886 declaration->appendDeclarator(blockSymbol);
2887 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002888
2889 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002890 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002891}
2892
Olli Etuaho383b7912016-08-05 11:22:59 +03002893void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002894{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002895 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002896
2897 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00002898 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302899 if (mStructNestingLevel > 1)
2900 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002901 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002902 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002903}
2904
2905void TParseContext::exitStructDeclaration()
2906{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002907 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002908}
2909
Olli Etuaho8a176262016-08-16 14:23:01 +03002910void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002911{
Jamie Madillacb4b812016-11-07 13:50:29 -05002912 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302913 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002914 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002915 }
2916
Arun Patole7e7e68d2015-05-22 12:02:25 +05302917 if (field.type()->getBasicType() != EbtStruct)
2918 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002919 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002920 }
2921
2922 // We're already inside a structure definition at this point, so add
2923 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302924 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2925 {
Jamie Madill41a49272014-03-18 16:10:13 -04002926 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002927 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2928 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002929 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00002930 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03002931 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002932 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002933}
2934
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002935//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002936// Parse an array index expression
2937//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002938TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2939 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302940 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002941{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002942 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2943 {
2944 if (baseExpression->getAsSymbolNode())
2945 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302946 error(location, " left of '[' is not of type array, matrix, or vector ",
2947 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002948 }
2949 else
2950 {
2951 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2952 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002953
2954 TConstantUnion *unionArray = new TConstantUnion[1];
2955 unionArray->setFConst(0.0f);
2956 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2957 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002958 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002959
Jamie Madill21c1e452014-12-29 11:33:41 -05002960 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2961
Olli Etuaho36b05142015-11-12 13:10:42 +02002962 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2963 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2964 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2965 // index is a constant expression.
2966 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2967 {
2968 if (baseExpression->isInterfaceBlock())
2969 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002970 error(location,
2971 "array indexes for interface blocks arrays must be constant integral expressions",
2972 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02002973 }
2974 else if (baseExpression->getQualifier() == EvqFragmentOut)
2975 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002976 error(location,
2977 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02002978 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002979 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2980 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002981 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02002982 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002983 }
2984
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002985 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002986 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002987 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2988 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2989 // constant fold expressions that are not constant expressions). The most compatible way to
2990 // handle this case is to report a warning instead of an error and force the index to be in
2991 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002992 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002993 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002994
2995 int safeIndex = -1;
2996
2997 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002998 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002999 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003000 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003001 if (mShaderSpec == SH_WEBGL2_SPEC)
3002 {
3003 // Error has been already generated if index is not const.
3004 if (indexExpression->getQualifier() == EvqConst)
3005 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003006 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003007 }
3008 safeIndex = 0;
3009 }
3010 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3011 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003012 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003013 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003014 "GL_EXT_draw_buffers is disabled",
3015 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003016 safeIndex = 0;
3017 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003018 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003019 // Only do generic out-of-range check if similar error hasn't already been reported.
3020 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003021 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003022 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3023 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003024 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003025 }
3026 }
3027 else if (baseExpression->isMatrix())
3028 {
3029 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003030 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003031 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003032 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003033 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003034 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003035 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3036 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003037 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003038 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003039
3040 ASSERT(safeIndex >= 0);
3041 // Data of constant unions can't be changed, because it may be shared with other
3042 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3043 // sanitized object.
3044 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003045 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003046 TConstantUnion *safeConstantUnion = new TConstantUnion();
3047 safeConstantUnion->setIConst(safeIndex);
3048 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003049 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003050
3051 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003052 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003053 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003054 else
3055 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003056 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003057 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003058 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003059}
3060
Olli Etuaho90892fb2016-07-14 14:44:51 +03003061int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3062 const TSourceLoc &location,
3063 int index,
3064 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003065 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003066{
3067 if (index >= arraySize || index < 0)
3068 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003069 std::stringstream reasonStream;
3070 reasonStream << reason << " '" << index << "'";
3071 std::string token = reasonStream.str();
3072 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003073 if (index < 0)
3074 {
3075 return 0;
3076 }
3077 else
3078 {
3079 return arraySize - 1;
3080 }
3081 }
3082 return index;
3083}
3084
Jamie Madillb98c3a82015-07-23 14:26:04 -04003085TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3086 const TSourceLoc &dotLocation,
3087 const TString &fieldString,
3088 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003089{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003090 if (baseExpression->isArray())
3091 {
3092 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003093 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003094 }
3095
3096 if (baseExpression->isVector())
3097 {
3098 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003099 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3100 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003101 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003102 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003103 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003104 }
3105
Olli Etuahob6fa0432016-09-28 16:28:05 +01003106 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003107 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003108 else if (baseExpression->getBasicType() == EbtStruct)
3109 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303110 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003111 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003112 {
3113 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003114 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003115 }
3116 else
3117 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003118 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003119 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003120 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003121 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003122 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003123 {
3124 fieldFound = true;
3125 break;
3126 }
3127 }
3128 if (fieldFound)
3129 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003130 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3131 index->setLine(fieldLocation);
3132 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003133 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003134 }
3135 else
3136 {
3137 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003138 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003139 }
3140 }
3141 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003142 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003143 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303144 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003145 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003146 {
3147 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003148 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003149 }
3150 else
3151 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003152 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003153 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003154 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003155 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003156 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003157 {
3158 fieldFound = true;
3159 break;
3160 }
3161 }
3162 if (fieldFound)
3163 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003164 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3165 index->setLine(fieldLocation);
3166 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003167 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003168 }
3169 else
3170 {
3171 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003172 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003173 }
3174 }
3175 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003176 else
3177 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003178 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003179 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003180 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303181 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003182 }
3183 else
3184 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303185 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003186 " field selection requires structure, vector, or interface block on left hand "
3187 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303188 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003189 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003190 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003191 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003192}
3193
Jamie Madillb98c3a82015-07-23 14:26:04 -04003194TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3195 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003196{
Martin Radev802abe02016-08-04 17:48:32 +03003197 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003198
3199 if (qualifierType == "shared")
3200 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003201 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003202 {
3203 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3204 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003205 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003206 }
3207 else if (qualifierType == "packed")
3208 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003209 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003210 {
3211 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3212 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003213 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003214 }
3215 else if (qualifierType == "std140")
3216 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003217 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003218 }
3219 else if (qualifierType == "row_major")
3220 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003221 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003222 }
3223 else if (qualifierType == "column_major")
3224 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003225 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003226 }
3227 else if (qualifierType == "location")
3228 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003229 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3230 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003231 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003232 else if (qualifierType == "rgba32f")
3233 {
3234 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3235 qualifier.imageInternalFormat = EiifRGBA32F;
3236 }
3237 else if (qualifierType == "rgba16f")
3238 {
3239 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3240 qualifier.imageInternalFormat = EiifRGBA16F;
3241 }
3242 else if (qualifierType == "r32f")
3243 {
3244 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3245 qualifier.imageInternalFormat = EiifR32F;
3246 }
3247 else if (qualifierType == "rgba8")
3248 {
3249 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3250 qualifier.imageInternalFormat = EiifRGBA8;
3251 }
3252 else if (qualifierType == "rgba8_snorm")
3253 {
3254 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3255 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3256 }
3257 else if (qualifierType == "rgba32i")
3258 {
3259 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3260 qualifier.imageInternalFormat = EiifRGBA32I;
3261 }
3262 else if (qualifierType == "rgba16i")
3263 {
3264 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3265 qualifier.imageInternalFormat = EiifRGBA16I;
3266 }
3267 else if (qualifierType == "rgba8i")
3268 {
3269 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3270 qualifier.imageInternalFormat = EiifRGBA8I;
3271 }
3272 else if (qualifierType == "r32i")
3273 {
3274 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3275 qualifier.imageInternalFormat = EiifR32I;
3276 }
3277 else if (qualifierType == "rgba32ui")
3278 {
3279 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3280 qualifier.imageInternalFormat = EiifRGBA32UI;
3281 }
3282 else if (qualifierType == "rgba16ui")
3283 {
3284 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3285 qualifier.imageInternalFormat = EiifRGBA16UI;
3286 }
3287 else if (qualifierType == "rgba8ui")
3288 {
3289 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3290 qualifier.imageInternalFormat = EiifRGBA8UI;
3291 }
3292 else if (qualifierType == "r32ui")
3293 {
3294 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3295 qualifier.imageInternalFormat = EiifR32UI;
3296 }
3297
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003298 else
3299 {
3300 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003301 }
3302
Jamie Madilla5efff92013-06-06 11:56:47 -04003303 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003304}
3305
Martin Radev802abe02016-08-04 17:48:32 +03003306void TParseContext::parseLocalSize(const TString &qualifierType,
3307 const TSourceLoc &qualifierTypeLine,
3308 int intValue,
3309 const TSourceLoc &intValueLine,
3310 const std::string &intValueString,
3311 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003312 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003313{
Olli Etuaho856c4972016-08-08 11:38:39 +03003314 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003315 if (intValue < 1)
3316 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003317 std::stringstream reasonStream;
3318 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3319 std::string reason = reasonStream.str();
3320 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003321 }
3322 (*localSize)[index] = intValue;
3323}
3324
Olli Etuaho09b04a22016-12-15 13:30:26 +00003325void TParseContext::parseNumViews(int intValue,
3326 const TSourceLoc &intValueLine,
3327 const std::string &intValueString,
3328 int *numViews)
3329{
3330 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3331 // specification.
3332 if (intValue < 1)
3333 {
3334 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3335 }
3336 *numViews = intValue;
3337}
3338
Jamie Madillb98c3a82015-07-23 14:26:04 -04003339TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3340 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003341 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303342 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003343{
Martin Radev802abe02016-08-04 17:48:32 +03003344 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003345
Martin Radev802abe02016-08-04 17:48:32 +03003346 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003347
Martin Radev802abe02016-08-04 17:48:32 +03003348 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003349 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003350 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003351 if (intValue < 0)
3352 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003353 error(intValueLine, "out of range: location must be non-negative",
3354 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003355 }
3356 else
3357 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003358 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003359 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003360 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003361 }
Martin Radev802abe02016-08-04 17:48:32 +03003362 else if (qualifierType == "local_size_x")
3363 {
3364 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3365 &qualifier.localSize);
3366 }
3367 else if (qualifierType == "local_size_y")
3368 {
3369 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3370 &qualifier.localSize);
3371 }
3372 else if (qualifierType == "local_size_z")
3373 {
3374 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3375 &qualifier.localSize);
3376 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00003377 else if (qualifierType == "num_views" && mMultiviewAvailable &&
3378 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
3379 mShaderType == GL_VERTEX_SHADER)
3380 {
3381 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3382 }
Martin Radev802abe02016-08-04 17:48:32 +03003383 else
3384 {
3385 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003386 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003387
Jamie Madilla5efff92013-06-06 11:56:47 -04003388 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003389}
3390
Olli Etuaho613b9592016-09-05 12:05:53 +03003391TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3392{
3393 return new TTypeQualifierBuilder(
3394 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3395 mShaderVersion);
3396}
3397
Jamie Madillb98c3a82015-07-23 14:26:04 -04003398TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003399 TLayoutQualifier rightQualifier,
3400 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003401{
Martin Radevc28888b2016-07-22 15:27:42 +03003402 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003403 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003404}
3405
Olli Etuaho4de340a2016-12-16 09:32:03 +00003406TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3407 const TFieldList *newlyAddedFields,
3408 const TSourceLoc &location)
3409{
3410 for (TField *field : *newlyAddedFields)
3411 {
3412 for (TField *oldField : *processedFields)
3413 {
3414 if (oldField->name() == field->name())
3415 {
3416 error(location, "duplicate field name in structure", field->name().c_str());
3417 }
3418 }
3419 processedFields->push_back(field);
3420 }
3421 return processedFields;
3422}
3423
Martin Radev70866b82016-07-22 15:27:42 +03003424TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3425 const TTypeQualifierBuilder &typeQualifierBuilder,
3426 TPublicType *typeSpecifier,
3427 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003428{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003429 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003430
Martin Radev70866b82016-07-22 15:27:42 +03003431 typeSpecifier->qualifier = typeQualifier.qualifier;
3432 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003433 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003434 typeSpecifier->invariant = typeQualifier.invariant;
3435 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303436 {
Martin Radev70866b82016-07-22 15:27:42 +03003437 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003438 }
Martin Radev70866b82016-07-22 15:27:42 +03003439 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003440}
3441
Jamie Madillb98c3a82015-07-23 14:26:04 -04003442TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3443 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003444{
Martin Radev4a9cd802016-09-01 16:51:51 +03003445 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3446 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003447
Martin Radev4a9cd802016-09-01 16:51:51 +03003448 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003449
Martin Radev4a9cd802016-09-01 16:51:51 +03003450 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003451
Arun Patole7e7e68d2015-05-22 12:02:25 +05303452 for (unsigned int i = 0; i < fieldList->size(); ++i)
3453 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003454 //
3455 // Careful not to replace already known aspects of type, like array-ness
3456 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303457 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003458 type->setBasicType(typeSpecifier.getBasicType());
3459 type->setPrimarySize(typeSpecifier.getPrimarySize());
3460 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003461 type->setPrecision(typeSpecifier.precision);
3462 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003463 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003464 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003465 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003466
3467 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303468 if (type->isArray())
3469 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003470 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003471 }
3472 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003473 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003474 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303475 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003476 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003477 }
3478
Martin Radev4a9cd802016-09-01 16:51:51 +03003479 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003480 }
3481
Jamie Madill98493dd2013-07-08 14:39:03 -04003482 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003483}
3484
Martin Radev4a9cd802016-09-01 16:51:51 +03003485TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3486 const TSourceLoc &nameLine,
3487 const TString *structName,
3488 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003489{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303490 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003491 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003492
Jamie Madill9b820842015-02-12 10:40:10 -05003493 // Store a bool in the struct if we're at global scope, to allow us to
3494 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003495 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003496
Jamie Madill98493dd2013-07-08 14:39:03 -04003497 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003498 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003499 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303500 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3501 if (!symbolTable.declare(userTypeDef))
3502 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003503 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003504 }
3505 }
3506
3507 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003508 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003509 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003510 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003511 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003512 switch (qualifier)
3513 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003514 case EvqGlobal:
3515 case EvqTemporary:
3516 break;
3517 default:
3518 error(field.line(), "invalid qualifier on struct member",
3519 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003520 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003521 }
Martin Radev70866b82016-07-22 15:27:42 +03003522 if (field.type()->isInvariant())
3523 {
3524 error(field.line(), "invalid qualifier on struct member", "invariant");
3525 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003526 if (IsImage(field.type()->getBasicType()))
3527 {
3528 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3529 }
3530
3531 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003532
3533 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003534 }
3535
Martin Radev4a9cd802016-09-01 16:51:51 +03003536 TTypeSpecifierNonArray typeSpecifierNonArray;
3537 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3538 typeSpecifierNonArray.userDef = structureType;
3539 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003540 exitStructDeclaration();
3541
Martin Radev4a9cd802016-09-01 16:51:51 +03003542 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003543}
3544
Jamie Madillb98c3a82015-07-23 14:26:04 -04003545TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003546 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003547 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003548{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003549 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003550 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003551 init->isVector())
3552 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003553 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3554 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003555 return nullptr;
3556 }
3557
Olli Etuahoac5274d2015-02-20 10:19:08 +02003558 if (statementList)
3559 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00003560 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02003561 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003562 return nullptr;
3563 }
3564 }
3565
Olli Etuahoa3a36662015-02-17 13:46:51 +02003566 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3567 if (node == nullptr)
3568 {
3569 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003570 return nullptr;
3571 }
3572 return node;
3573}
3574
3575TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3576{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003577 if (mSwitchNestingLevel == 0)
3578 {
3579 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003580 return nullptr;
3581 }
3582 if (condition == nullptr)
3583 {
3584 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003585 return nullptr;
3586 }
3587 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003588 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003589 {
3590 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003591 }
3592 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003593 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3594 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3595 // fold in case labels.
3596 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003597 {
3598 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003599 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003600 TIntermCase *node = intermediate.addCase(condition, loc);
3601 if (node == nullptr)
3602 {
3603 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003604 return nullptr;
3605 }
3606 return node;
3607}
3608
3609TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3610{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003611 if (mSwitchNestingLevel == 0)
3612 {
3613 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003614 return nullptr;
3615 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003616 TIntermCase *node = intermediate.addCase(nullptr, loc);
3617 if (node == nullptr)
3618 {
3619 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003620 return nullptr;
3621 }
3622 return node;
3623}
3624
Jamie Madillb98c3a82015-07-23 14:26:04 -04003625TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3626 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003627 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003628{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003629 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003630
3631 switch (op)
3632 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003633 case EOpLogicalNot:
3634 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3635 child->isVector())
3636 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003637 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003638 return nullptr;
3639 }
3640 break;
3641 case EOpBitwiseNot:
3642 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3643 child->isMatrix() || child->isArray())
3644 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003645 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003646 return nullptr;
3647 }
3648 break;
3649 case EOpPostIncrement:
3650 case EOpPreIncrement:
3651 case EOpPostDecrement:
3652 case EOpPreDecrement:
3653 case EOpNegative:
3654 case EOpPositive:
3655 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003656 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003657 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003658 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003659 return nullptr;
3660 }
3661 // Operators for built-ins are already type checked against their prototype.
3662 default:
3663 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003664 }
3665
Olli Etuahof119a262016-08-19 15:54:22 +03003666 TIntermUnary *node = new TIntermUnary(op, child);
3667 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003668
Olli Etuaho77ba4082016-12-16 12:01:18 +00003669 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03003670 if (foldedNode)
3671 return foldedNode;
3672
3673 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003674}
3675
Olli Etuaho09b22472015-02-11 11:47:26 +02003676TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3677{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003678 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003679 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003680 {
Olli Etuaho09b22472015-02-11 11:47:26 +02003681 return child;
3682 }
3683 return node;
3684}
3685
Jamie Madillb98c3a82015-07-23 14:26:04 -04003686TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3687 TIntermTyped *child,
3688 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003689{
Olli Etuaho856c4972016-08-08 11:38:39 +03003690 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003691 return addUnaryMath(op, child, loc);
3692}
3693
Jamie Madillb98c3a82015-07-23 14:26:04 -04003694bool TParseContext::binaryOpCommonCheck(TOperator op,
3695 TIntermTyped *left,
3696 TIntermTyped *right,
3697 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003698{
Olli Etuaho244be012016-08-18 15:26:02 +03003699 if (left->getType().getStruct() || right->getType().getStruct())
3700 {
3701 switch (op)
3702 {
3703 case EOpIndexDirectStruct:
3704 ASSERT(left->getType().getStruct());
3705 break;
3706 case EOpEqual:
3707 case EOpNotEqual:
3708 case EOpAssign:
3709 case EOpInitialize:
3710 if (left->getType() != right->getType())
3711 {
3712 return false;
3713 }
3714 break;
3715 default:
3716 error(loc, "Invalid operation for structs", GetOperatorString(op));
3717 return false;
3718 }
3719 }
3720
Olli Etuahod6b14282015-03-17 14:31:35 +02003721 if (left->isArray() || right->isArray())
3722 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003723 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003724 {
3725 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3726 return false;
3727 }
3728
3729 if (left->isArray() != right->isArray())
3730 {
3731 error(loc, "array / non-array mismatch", GetOperatorString(op));
3732 return false;
3733 }
3734
3735 switch (op)
3736 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003737 case EOpEqual:
3738 case EOpNotEqual:
3739 case EOpAssign:
3740 case EOpInitialize:
3741 break;
3742 default:
3743 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3744 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003745 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003746 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003747 if (left->getArraySize() != right->getArraySize())
3748 {
3749 error(loc, "array size mismatch", GetOperatorString(op));
3750 return false;
3751 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003752 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003753
3754 // Check ops which require integer / ivec parameters
3755 bool isBitShift = false;
3756 switch (op)
3757 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003758 case EOpBitShiftLeft:
3759 case EOpBitShiftRight:
3760 case EOpBitShiftLeftAssign:
3761 case EOpBitShiftRightAssign:
3762 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3763 // check that the basic type is an integer type.
3764 isBitShift = true;
3765 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3766 {
3767 return false;
3768 }
3769 break;
3770 case EOpBitwiseAnd:
3771 case EOpBitwiseXor:
3772 case EOpBitwiseOr:
3773 case EOpBitwiseAndAssign:
3774 case EOpBitwiseXorAssign:
3775 case EOpBitwiseOrAssign:
3776 // It is enough to check the type of only one operand, since later it
3777 // is checked that the operand types match.
3778 if (!IsInteger(left->getBasicType()))
3779 {
3780 return false;
3781 }
3782 break;
3783 default:
3784 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003785 }
3786
3787 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3788 // So the basic type should usually match.
3789 if (!isBitShift && left->getBasicType() != right->getBasicType())
3790 {
3791 return false;
3792 }
3793
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003794 // Check that:
3795 // 1. Type sizes match exactly on ops that require that.
3796 // 2. Restrictions for structs that contain arrays or samplers are respected.
3797 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003798 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003799 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003800 case EOpAssign:
3801 case EOpInitialize:
3802 case EOpEqual:
3803 case EOpNotEqual:
3804 // ESSL 1.00 sections 5.7, 5.8, 5.9
3805 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3806 {
3807 error(loc, "undefined operation for structs containing arrays",
3808 GetOperatorString(op));
3809 return false;
3810 }
3811 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3812 // we interpret the spec so that this extends to structs containing samplers,
3813 // similarly to ESSL 1.00 spec.
3814 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3815 left->getType().isStructureContainingSamplers())
3816 {
3817 error(loc, "undefined operation for structs containing samplers",
3818 GetOperatorString(op));
3819 return false;
3820 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003821
3822 if ((op == EOpAssign || op == EOpInitialize) &&
3823 left->getType().isStructureContainingImages())
3824 {
3825 error(loc, "undefined operation for structs containing images",
3826 GetOperatorString(op));
3827 return false;
3828 }
Olli Etuahoe1805592017-01-02 16:41:20 +00003829 if ((left->getNominalSize() != right->getNominalSize()) ||
3830 (left->getSecondarySize() != right->getSecondarySize()))
3831 {
3832 error(loc, "dimension mismatch", GetOperatorString(op));
3833 return false;
3834 }
3835 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003836 case EOpLessThan:
3837 case EOpGreaterThan:
3838 case EOpLessThanEqual:
3839 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00003840 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003841 {
Olli Etuahoe1805592017-01-02 16:41:20 +00003842 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003843 return false;
3844 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003845 break;
3846 case EOpAdd:
3847 case EOpSub:
3848 case EOpDiv:
3849 case EOpIMod:
3850 case EOpBitShiftLeft:
3851 case EOpBitShiftRight:
3852 case EOpBitwiseAnd:
3853 case EOpBitwiseXor:
3854 case EOpBitwiseOr:
3855 case EOpAddAssign:
3856 case EOpSubAssign:
3857 case EOpDivAssign:
3858 case EOpIModAssign:
3859 case EOpBitShiftLeftAssign:
3860 case EOpBitShiftRightAssign:
3861 case EOpBitwiseAndAssign:
3862 case EOpBitwiseXorAssign:
3863 case EOpBitwiseOrAssign:
3864 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3865 {
3866 return false;
3867 }
3868
3869 // Are the sizes compatible?
3870 if (left->getNominalSize() != right->getNominalSize() ||
3871 left->getSecondarySize() != right->getSecondarySize())
3872 {
3873 // If the nominal sizes of operands do not match:
3874 // One of them must be a scalar.
3875 if (!left->isScalar() && !right->isScalar())
3876 return false;
3877
3878 // In the case of compound assignment other than multiply-assign,
3879 // the right side needs to be a scalar. Otherwise a vector/matrix
3880 // would be assigned to a scalar. A scalar can't be shifted by a
3881 // vector either.
3882 if (!right->isScalar() &&
3883 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3884 return false;
3885 }
3886 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003887 default:
3888 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003889 }
3890
Olli Etuahod6b14282015-03-17 14:31:35 +02003891 return true;
3892}
3893
Olli Etuaho1dded802016-08-18 18:13:13 +03003894bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3895 const TType &left,
3896 const TType &right)
3897{
3898 switch (op)
3899 {
3900 case EOpMul:
3901 case EOpMulAssign:
3902 return left.getNominalSize() == right.getNominalSize() &&
3903 left.getSecondarySize() == right.getSecondarySize();
3904 case EOpVectorTimesScalar:
3905 return true;
3906 case EOpVectorTimesScalarAssign:
3907 ASSERT(!left.isMatrix() && !right.isMatrix());
3908 return left.isVector() && !right.isVector();
3909 case EOpVectorTimesMatrix:
3910 return left.getNominalSize() == right.getRows();
3911 case EOpVectorTimesMatrixAssign:
3912 ASSERT(!left.isMatrix() && right.isMatrix());
3913 return left.isVector() && left.getNominalSize() == right.getRows() &&
3914 left.getNominalSize() == right.getCols();
3915 case EOpMatrixTimesVector:
3916 return left.getCols() == right.getNominalSize();
3917 case EOpMatrixTimesScalar:
3918 return true;
3919 case EOpMatrixTimesScalarAssign:
3920 ASSERT(left.isMatrix() && !right.isMatrix());
3921 return !right.isVector();
3922 case EOpMatrixTimesMatrix:
3923 return left.getCols() == right.getRows();
3924 case EOpMatrixTimesMatrixAssign:
3925 ASSERT(left.isMatrix() && right.isMatrix());
3926 // We need to check two things:
3927 // 1. The matrix multiplication step is valid.
3928 // 2. The result will have the same number of columns as the lvalue.
3929 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3930
3931 default:
3932 UNREACHABLE();
3933 return false;
3934 }
3935}
3936
Jamie Madillb98c3a82015-07-23 14:26:04 -04003937TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3938 TIntermTyped *left,
3939 TIntermTyped *right,
3940 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003941{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003942 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003943 return nullptr;
3944
Olli Etuahofc1806e2015-03-17 13:03:11 +02003945 switch (op)
3946 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003947 case EOpEqual:
3948 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04003949 case EOpLessThan:
3950 case EOpGreaterThan:
3951 case EOpLessThanEqual:
3952 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04003953 break;
3954 case EOpLogicalOr:
3955 case EOpLogicalXor:
3956 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003957 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3958 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003959 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003960 {
3961 return nullptr;
3962 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003963 // Basic types matching should have been already checked.
3964 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003965 break;
3966 case EOpAdd:
3967 case EOpSub:
3968 case EOpDiv:
3969 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003970 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3971 !right->getType().getStruct());
3972 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003973 {
3974 return nullptr;
3975 }
3976 break;
3977 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003978 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3979 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003980 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003981 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003982 {
3983 return nullptr;
3984 }
3985 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003986 default:
3987 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003988 }
3989
Olli Etuaho1dded802016-08-18 18:13:13 +03003990 if (op == EOpMul)
3991 {
3992 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3993 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3994 {
3995 return nullptr;
3996 }
3997 }
3998
Olli Etuaho3fdec912016-08-18 15:08:06 +03003999 TIntermBinary *node = new TIntermBinary(op, left, right);
4000 node->setLine(loc);
4001
Olli Etuaho3fdec912016-08-18 15:08:06 +03004002 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004003 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004004 if (foldedNode)
4005 return foldedNode;
4006
4007 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004008}
4009
Jamie Madillb98c3a82015-07-23 14:26:04 -04004010TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4011 TIntermTyped *left,
4012 TIntermTyped *right,
4013 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004014{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004015 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004016 if (node == 0)
4017 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004018 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4019 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004020 return left;
4021 }
4022 return node;
4023}
4024
Jamie Madillb98c3a82015-07-23 14:26:04 -04004025TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4026 TIntermTyped *left,
4027 TIntermTyped *right,
4028 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004029{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004030 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004031 if (node == 0)
4032 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004033 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4034 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004035 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004036 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004037 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4038 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004039 }
4040 return node;
4041}
4042
Olli Etuaho13389b62016-10-16 11:48:18 +01004043TIntermBinary *TParseContext::createAssign(TOperator op,
4044 TIntermTyped *left,
4045 TIntermTyped *right,
4046 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004047{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004048 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004049 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004050 if (op == EOpMulAssign)
4051 {
4052 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4053 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4054 {
4055 return nullptr;
4056 }
4057 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004058 TIntermBinary *node = new TIntermBinary(op, left, right);
4059 node->setLine(loc);
4060
Olli Etuaho3fdec912016-08-18 15:08:06 +03004061 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004062 }
4063 return nullptr;
4064}
4065
Jamie Madillb98c3a82015-07-23 14:26:04 -04004066TIntermTyped *TParseContext::addAssign(TOperator op,
4067 TIntermTyped *left,
4068 TIntermTyped *right,
4069 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004070{
4071 TIntermTyped *node = createAssign(op, left, right, loc);
4072 if (node == nullptr)
4073 {
4074 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004075 return left;
4076 }
4077 return node;
4078}
4079
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004080TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4081 TIntermTyped *right,
4082 const TSourceLoc &loc)
4083{
Corentin Wallez0d959252016-07-12 17:26:32 -04004084 // WebGL2 section 5.26, the following results in an error:
4085 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004086 if (mShaderSpec == SH_WEBGL2_SPEC &&
4087 (left->isArray() || left->getBasicType() == EbtVoid ||
4088 left->getType().isStructureContainingArrays() || right->isArray() ||
4089 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004090 {
4091 error(loc,
4092 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4093 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004094 }
4095
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004096 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004097}
4098
Olli Etuaho49300862015-02-20 14:54:49 +02004099TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4100{
4101 switch (op)
4102 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004103 case EOpContinue:
4104 if (mLoopNestingLevel <= 0)
4105 {
4106 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004107 }
4108 break;
4109 case EOpBreak:
4110 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4111 {
4112 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004113 }
4114 break;
4115 case EOpReturn:
4116 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4117 {
4118 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004119 }
4120 break;
4121 default:
4122 // No checks for discard
4123 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004124 }
4125 return intermediate.addBranch(op, loc);
4126}
4127
Jamie Madillb98c3a82015-07-23 14:26:04 -04004128TIntermBranch *TParseContext::addBranch(TOperator op,
4129 TIntermTyped *returnValue,
4130 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004131{
4132 ASSERT(op == EOpReturn);
4133 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004134 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004135 {
4136 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004137 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004138 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004139 {
4140 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004141 }
4142 return intermediate.addBranch(op, returnValue, loc);
4143}
4144
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004145void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4146{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004147 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004148 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004149 TIntermNode *offset = nullptr;
4150 TIntermSequence *arguments = functionCall->getSequence();
4151 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4152 name.compare(0, 16, "textureLodOffset") == 0 ||
4153 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4154 name.compare(0, 17, "textureGradOffset") == 0 ||
4155 name.compare(0, 21, "textureProjGradOffset") == 0)
4156 {
4157 offset = arguments->back();
4158 }
4159 else if (name.compare(0, 13, "textureOffset") == 0 ||
4160 name.compare(0, 17, "textureProjOffset") == 0)
4161 {
4162 // A bias parameter might follow the offset parameter.
4163 ASSERT(arguments->size() >= 3);
4164 offset = (*arguments)[2];
4165 }
4166 if (offset != nullptr)
4167 {
4168 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4169 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4170 {
4171 TString unmangledName = TFunction::unmangleName(name);
4172 error(functionCall->getLine(), "Texture offset must be a constant expression",
4173 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004174 }
4175 else
4176 {
4177 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4178 size_t size = offsetConstantUnion->getType().getObjectSize();
4179 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4180 for (size_t i = 0u; i < size; ++i)
4181 {
4182 int offsetValue = values[i].getIConst();
4183 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4184 {
4185 std::stringstream tokenStream;
4186 tokenStream << offsetValue;
4187 std::string token = tokenStream.str();
4188 error(offset->getLine(), "Texture offset value out of valid range",
4189 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004190 }
4191 }
4192 }
4193 }
4194}
4195
Martin Radev2cc85b32016-08-05 16:22:53 +03004196// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4197void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4198{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004199 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004200 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4201
4202 if (name.compare(0, 5, "image") == 0)
4203 {
4204 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004205 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004206
Olli Etuaho485eefd2017-02-14 17:40:06 +00004207 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004208
4209 if (name.compare(5, 5, "Store") == 0)
4210 {
4211 if (memoryQualifier.readonly)
4212 {
4213 error(imageNode->getLine(),
4214 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004215 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004216 }
4217 }
4218 else if (name.compare(5, 4, "Load") == 0)
4219 {
4220 if (memoryQualifier.writeonly)
4221 {
4222 error(imageNode->getLine(),
4223 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004224 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004225 }
4226 }
4227 }
4228}
4229
4230// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4231void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4232 const TFunction *functionDefinition,
4233 const TIntermAggregate *functionCall)
4234{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004235 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004236
4237 const TIntermSequence &arguments = *functionCall->getSequence();
4238
4239 ASSERT(functionDefinition->getParamCount() == arguments.size());
4240
4241 for (size_t i = 0; i < arguments.size(); ++i)
4242 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004243 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4244 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004245 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4246 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4247
4248 if (IsImage(functionArgumentType.getBasicType()))
4249 {
4250 const TMemoryQualifier &functionArgumentMemoryQualifier =
4251 functionArgumentType.getMemoryQualifier();
4252 const TMemoryQualifier &functionParameterMemoryQualifier =
4253 functionParameterType.getMemoryQualifier();
4254 if (functionArgumentMemoryQualifier.readonly &&
4255 !functionParameterMemoryQualifier.readonly)
4256 {
4257 error(functionCall->getLine(),
4258 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004259 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004260 }
4261
4262 if (functionArgumentMemoryQualifier.writeonly &&
4263 !functionParameterMemoryQualifier.writeonly)
4264 {
4265 error(functionCall->getLine(),
4266 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004267 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004268 }
Martin Radev049edfa2016-11-11 14:35:37 +02004269
4270 if (functionArgumentMemoryQualifier.coherent &&
4271 !functionParameterMemoryQualifier.coherent)
4272 {
4273 error(functionCall->getLine(),
4274 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004275 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004276 }
4277
4278 if (functionArgumentMemoryQualifier.volatileQualifier &&
4279 !functionParameterMemoryQualifier.volatileQualifier)
4280 {
4281 error(functionCall->getLine(),
4282 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004283 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004284 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004285 }
4286 }
4287}
4288
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004289TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004290{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004291 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004292}
4293
4294TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004295 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004296 TIntermNode *thisNode,
4297 const TSourceLoc &loc)
4298{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004299 if (thisNode != nullptr)
4300 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004301 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004302 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004303
4304 TOperator op = fnCall->getBuiltInOp();
4305 if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004306 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004307 return addConstructor(arguments, op, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004308 }
4309 else
4310 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004311 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4312 }
4313}
4314
4315TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4316 TIntermSequence *arguments,
4317 TIntermNode *thisNode,
4318 const TSourceLoc &loc)
4319{
4320 TConstantUnion *unionArray = new TConstantUnion[1];
4321 int arraySize = 0;
4322 TIntermTyped *typedThis = thisNode->getAsTyped();
4323 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4324 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4325 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4326 // So accessing fnCall->getName() below is safe.
4327 if (fnCall->getName() != "length")
4328 {
4329 error(loc, "invalid method", fnCall->getName().c_str());
4330 }
4331 else if (!arguments->empty())
4332 {
4333 error(loc, "method takes no parameters", "length");
4334 }
4335 else if (typedThis == nullptr || !typedThis->isArray())
4336 {
4337 error(loc, "length can only be called on arrays", "length");
4338 }
4339 else
4340 {
4341 arraySize = typedThis->getArraySize();
4342 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004343 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004344 // This code path can be hit with expressions like these:
4345 // (a = b).length()
4346 // (func()).length()
4347 // (int[3](0, 1, 2)).length()
4348 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4349 // expression.
4350 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4351 // spec section 5.9 which allows "An array, vector or matrix expression with the
4352 // length method applied".
4353 error(loc, "length can only be called on array names, not on array expressions",
4354 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004355 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004356 }
4357 unionArray->setIConst(arraySize);
4358 return intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
4359}
4360
4361TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4362 TIntermSequence *arguments,
4363 const TSourceLoc &loc)
4364{
4365 // First find by unmangled name to check whether the function name has been
4366 // hidden by a variable name or struct typename.
4367 // If a function is found, check for one with a matching argument list.
4368 bool builtIn;
4369 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4370 if (symbol != nullptr && !symbol->isFunction())
4371 {
4372 error(loc, "function name expected", fnCall->getName().c_str());
4373 }
4374 else
4375 {
4376 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4377 mShaderVersion, &builtIn);
4378 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004379 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004380 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4381 }
4382 else
4383 {
4384 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004385 //
4386 // A declared function.
4387 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004388 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004389 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004390 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004391 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004392 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004393 if (builtIn && op != EOpNull)
4394 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004395 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004396 if (fnCandidate->getParamCount() == 1)
4397 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004398 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004399 TIntermNode *unaryParamNode = arguments->front();
4400 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004401 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004402 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004403 }
4404 else
4405 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004406 TIntermAggregate *callNode =
4407 new TIntermAggregate(fnCandidate->getReturnType(), op, arguments);
4408 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004409
4410 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004411 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304412
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004413 // See if we can constant fold a built-in. Note that this may be possible even
4414 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004415 TIntermTyped *foldedNode =
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004416 intermediate.foldAggregateBuiltIn(callNode, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304417 if (foldedNode)
4418 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004419 return foldedNode;
Arun Patole274f0702015-05-05 13:33:30 +05304420 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004421 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004422 }
4423 }
4424 else
4425 {
4426 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004427 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004428
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004429 // If builtIn == false, the function is user defined - could be an overloaded
4430 // built-in as well.
4431 // if builtIn == true, it's a builtIn function with no op associated with it.
4432 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004433 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004434 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004435 callNode = new TIntermAggregate(fnCandidate->getReturnType(),
4436 EOpCallBuiltInFunction, arguments);
4437 // Note that name needs to be set before texture function type is determined.
4438 callNode->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
4439 callNode->setBuiltInFunctionPrecision();
4440 checkTextureOffsetConst(callNode);
4441 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004442 }
4443 else
4444 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004445 callNode = new TIntermAggregate(fnCandidate->getReturnType(),
4446 EOpCallFunctionInAST, arguments);
4447 callNode->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
4448 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004449 }
4450
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004451 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004452
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004453 callNode->setLine(loc);
4454
4455 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004456 }
4457 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004458 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004459
4460 // Error message was already written. Put on a dummy node for error recovery.
4461 return TIntermTyped::CreateZero(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004462}
4463
Jamie Madillb98c3a82015-07-23 14:26:04 -04004464TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004465 TIntermTyped *trueExpression,
4466 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004467 const TSourceLoc &loc)
4468{
Olli Etuaho856c4972016-08-08 11:38:39 +03004469 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004470
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004471 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004472 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004473 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4474 falseExpression->getCompleteString());
4475 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004476 }
Olli Etuahode318b22016-10-25 16:18:25 +01004477 if (IsOpaqueType(trueExpression->getBasicType()))
4478 {
4479 // ESSL 1.00 section 4.1.7
4480 // ESSL 3.00 section 4.1.7
4481 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4482 // Note that structs containing opaque types don't need to be checked as structs are
4483 // forbidden below.
4484 error(loc, "ternary operator is not allowed for opaque types", ":");
4485 return falseExpression;
4486 }
4487
Olli Etuahoa2d53032015-04-15 14:14:44 +03004488 // ESSL1 sections 5.2 and 5.7:
4489 // ESSL3 section 5.7:
4490 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004491 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004492 {
4493 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004494 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004495 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004496 // WebGL2 section 5.26, the following results in an error:
4497 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004498 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004499 {
4500 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004501 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004502 }
4503
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004504 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004505}
Olli Etuaho49300862015-02-20 14:54:49 +02004506
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004507//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004508// Parse an array of strings using yyparse.
4509//
4510// Returns 0 for success.
4511//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004512int PaParseStrings(size_t count,
4513 const char *const string[],
4514 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304515 TParseContext *context)
4516{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004517 if ((count == 0) || (string == NULL))
4518 return 1;
4519
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004520 if (glslang_initialize(context))
4521 return 1;
4522
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004523 int error = glslang_scan(count, string, length, context);
4524 if (!error)
4525 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004526
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004527 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004528
alokp@chromium.org6b495712012-06-29 00:06:58 +00004529 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004530}
Jamie Madill45bcc782016-11-07 13:58:48 -05004531
4532} // namespace sh