blob: df8ce9fca673f1777c8649991323a1754d064f53 [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
69} // namespace
70
Jamie Madillacb4b812016-11-07 13:50:29 -050071TParseContext::TParseContext(TSymbolTable &symt,
72 TExtensionBehavior &ext,
73 sh::GLenum type,
74 ShShaderSpec spec,
75 ShCompileOptions options,
76 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +000077 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -050078 const ShBuiltInResources &resources)
79 : intermediate(),
80 symbolTable(symt),
81 mDeferredSingleDeclarationErrorCheck(false),
82 mShaderType(type),
83 mShaderSpec(spec),
84 mCompileOptions(options),
85 mShaderVersion(100),
86 mTreeRoot(nullptr),
87 mLoopNestingLevel(0),
88 mStructNestingLevel(0),
89 mSwitchNestingLevel(0),
90 mCurrentFunctionType(nullptr),
91 mFunctionReturnsValue(false),
92 mChecksPrecisionErrors(checksPrecErrors),
93 mFragmentPrecisionHighOnESSL1(false),
94 mDefaultMatrixPacking(EmpColumnMajor),
95 mDefaultBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +000096 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -050097 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +000098 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -050099 mShaderVersion,
100 mShaderType,
101 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000102 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500103 mScanner(nullptr),
104 mUsesFragData(false),
105 mUsesFragColor(false),
106 mUsesSecondaryOutputs(false),
107 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
108 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
109 mComputeShaderLocalSizeDeclared(false),
110 mDeclaringFunction(false)
111{
112 mComputeShaderLocalSize.fill(-1);
113}
114
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000115//
116// Look at a '.' field selector string and change it into offsets
117// for a vector.
118//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400119bool TParseContext::parseVectorFields(const TString &compString,
120 int vecSize,
121 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530122 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400124 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530125 if (fields.num > 4)
126 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000127 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000128 return false;
129 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130
Jamie Madillb98c3a82015-07-23 14:26:04 -0400131 enum
132 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000133 exyzw,
134 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000135 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000136 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137
Arun Patole7e7e68d2015-05-22 12:02:25 +0530138 for (int i = 0; i < fields.num; ++i)
139 {
140 switch (compString[i])
141 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400142 case 'x':
143 fields.offsets[i] = 0;
144 fieldSet[i] = exyzw;
145 break;
146 case 'r':
147 fields.offsets[i] = 0;
148 fieldSet[i] = ergba;
149 break;
150 case 's':
151 fields.offsets[i] = 0;
152 fieldSet[i] = estpq;
153 break;
154 case 'y':
155 fields.offsets[i] = 1;
156 fieldSet[i] = exyzw;
157 break;
158 case 'g':
159 fields.offsets[i] = 1;
160 fieldSet[i] = ergba;
161 break;
162 case 't':
163 fields.offsets[i] = 1;
164 fieldSet[i] = estpq;
165 break;
166 case 'z':
167 fields.offsets[i] = 2;
168 fieldSet[i] = exyzw;
169 break;
170 case 'b':
171 fields.offsets[i] = 2;
172 fieldSet[i] = ergba;
173 break;
174 case 'p':
175 fields.offsets[i] = 2;
176 fieldSet[i] = estpq;
177 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530178
Jamie Madillb98c3a82015-07-23 14:26:04 -0400179 case 'w':
180 fields.offsets[i] = 3;
181 fieldSet[i] = exyzw;
182 break;
183 case 'a':
184 fields.offsets[i] = 3;
185 fieldSet[i] = ergba;
186 break;
187 case 'q':
188 fields.offsets[i] = 3;
189 fieldSet[i] = estpq;
190 break;
191 default:
192 error(line, "illegal vector field selection", compString.c_str());
193 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000194 }
195 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000196
Arun Patole7e7e68d2015-05-22 12:02:25 +0530197 for (int i = 0; i < fields.num; ++i)
198 {
199 if (fields.offsets[i] >= vecSize)
200 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400201 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000202 return false;
203 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204
Arun Patole7e7e68d2015-05-22 12:02:25 +0530205 if (i > 0)
206 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400207 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530208 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400209 error(line, "illegal - vector component fields not from the same set",
210 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000211 return false;
212 }
213 }
214 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000215
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000216 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217}
218
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000219///////////////////////////////////////////////////////////////////////
220//
221// Errors
222//
223////////////////////////////////////////////////////////////////////////
224
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225//
226// Used by flex/bison to output all syntax and parsing errors.
227//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000228void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000229{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000230 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000231}
232
Olli Etuaho4de340a2016-12-16 09:32:03 +0000233void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530234{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000235 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000236}
237
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200238void TParseContext::outOfRangeError(bool isError,
239 const TSourceLoc &loc,
240 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000241 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200242{
243 if (isError)
244 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000245 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200246 }
247 else
248 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000249 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200250 }
251}
252
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253//
254// Same error message for all places assignments don't work.
255//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530256void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000258 std::stringstream reasonStream;
259 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
260 std::string reason = reasonStream.str();
261 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262}
263
264//
265// Same error message for all places unary operations don't work.
266//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530267void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000269 std::stringstream reasonStream;
270 reasonStream << "wrong operand type - no operation '" << op
271 << "' exists that takes an operand of type " << operand
272 << " (or there is no acceptable conversion)";
273 std::string reason = reasonStream.str();
274 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275}
276
277//
278// Same error message for all binary operations don't work.
279//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400280void TParseContext::binaryOpError(const TSourceLoc &line,
281 const char *op,
282 TString left,
283 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000285 std::stringstream reasonStream;
286 reasonStream << "wrong operand types - no operation '" << op
287 << "' exists that takes a left-hand operand of type '" << left
288 << "' and a right operand of type '" << right
289 << "' (or there is no acceptable conversion)";
290 std::string reason = reasonStream.str();
291 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292}
293
Olli Etuaho856c4972016-08-08 11:38:39 +0300294void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
295 TPrecision precision,
296 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530297{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400298 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300299 return;
Martin Radev70866b82016-07-22 15:27:42 +0300300
301 if (precision != EbpUndefined && !SupportsPrecision(type))
302 {
303 error(line, "illegal type for precision qualifier", getBasicString(type));
304 }
305
Olli Etuaho183d7e22015-11-20 15:59:09 +0200306 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200308 switch (type)
309 {
310 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400311 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300312 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200313 case EbtInt:
314 case EbtUInt:
315 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400316 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300317 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200318 default:
319 if (IsSampler(type))
320 {
321 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300322 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200323 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300324 if (IsImage(type))
325 {
326 error(line, "No precision specified (image)", "");
327 return;
328 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200329 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000330 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000331}
332
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333// Both test and if necessary, spit out an error, to see if the node is really
334// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300335bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500337 TIntermSymbol *symNode = node->getAsSymbolNode();
338 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100339 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
340
341 if (swizzleNode)
342 {
343 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
344 if (ok && swizzleNode->hasDuplicateOffsets())
345 {
346 error(line, " l-value of swizzle cannot have duplicate components", op);
347 return false;
348 }
349 return ok;
350 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351
Arun Patole7e7e68d2015-05-22 12:02:25 +0530352 if (binaryNode)
353 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400354 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530355 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400356 case EOpIndexDirect:
357 case EOpIndexIndirect:
358 case EOpIndexDirectStruct:
359 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300360 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400361 default:
362 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000363 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000364 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300365 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000366 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367
Arun Patole7e7e68d2015-05-22 12:02:25 +0530368 const char *message = 0;
369 switch (node->getQualifier())
370 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400371 case EvqConst:
372 message = "can't modify a const";
373 break;
374 case EvqConstReadOnly:
375 message = "can't modify a const";
376 break;
377 case EvqAttribute:
378 message = "can't modify an attribute";
379 break;
380 case EvqFragmentIn:
381 message = "can't modify an input";
382 break;
383 case EvqVertexIn:
384 message = "can't modify an input";
385 break;
386 case EvqUniform:
387 message = "can't modify a uniform";
388 break;
389 case EvqVaryingIn:
390 message = "can't modify a varying";
391 break;
392 case EvqFragCoord:
393 message = "can't modify gl_FragCoord";
394 break;
395 case EvqFrontFacing:
396 message = "can't modify gl_FrontFacing";
397 break;
398 case EvqPointCoord:
399 message = "can't modify gl_PointCoord";
400 break;
Martin Radevb0883602016-08-04 17:48:58 +0300401 case EvqNumWorkGroups:
402 message = "can't modify gl_NumWorkGroups";
403 break;
404 case EvqWorkGroupSize:
405 message = "can't modify gl_WorkGroupSize";
406 break;
407 case EvqWorkGroupID:
408 message = "can't modify gl_WorkGroupID";
409 break;
410 case EvqLocalInvocationID:
411 message = "can't modify gl_LocalInvocationID";
412 break;
413 case EvqGlobalInvocationID:
414 message = "can't modify gl_GlobalInvocationID";
415 break;
416 case EvqLocalInvocationIndex:
417 message = "can't modify gl_LocalInvocationIndex";
418 break;
Martin Radev802abe02016-08-04 17:48:32 +0300419 case EvqComputeIn:
420 message = "can't modify work group size variable";
421 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400422 default:
423 //
424 // Type that can't be written to?
425 //
426 if (node->getBasicType() == EbtVoid)
427 {
428 message = "can't modify void";
429 }
430 if (IsSampler(node->getBasicType()))
431 {
432 message = "can't modify a sampler";
433 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300434 if (IsImage(node->getBasicType()))
435 {
436 message = "can't modify an image";
437 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000438 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000439
Arun Patole7e7e68d2015-05-22 12:02:25 +0530440 if (message == 0 && binaryNode == 0 && symNode == 0)
441 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000442 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000443
Olli Etuaho8a176262016-08-16 14:23:01 +0300444 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000445 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000446
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000447 //
448 // Everything else is okay, no error.
449 //
450 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300451 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000453 //
454 // If we get here, we have an error and a message.
455 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530456 if (symNode)
457 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000458 const char *symbol = symNode->getSymbol().c_str();
459 std::stringstream reasonStream;
460 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
461 std::string reason = reasonStream.str();
462 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000463 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530464 else
465 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000466 std::stringstream reasonStream;
467 reasonStream << "l-value required (" << message << ")";
468 std::string reason = reasonStream.str();
469 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000470 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471
Olli Etuaho8a176262016-08-16 14:23:01 +0300472 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473}
474
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475// Both test, and if necessary spit out an error, to see if the node is really
476// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300477void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000478{
Olli Etuaho383b7912016-08-05 11:22:59 +0300479 if (node->getQualifier() != EvqConst)
480 {
481 error(node->getLine(), "constant expression required", "");
482 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483}
484
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485// Both test, and if necessary spit out an error, to see if the node is really
486// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300487void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488{
Olli Etuaho383b7912016-08-05 11:22:59 +0300489 if (!node->isScalarInt())
490 {
491 error(node->getLine(), "integer expression required", token);
492 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493}
494
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495// Both test, and if necessary spit out an error, to see if we are currently
496// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800497bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000498{
Olli Etuaho856c4972016-08-08 11:38:39 +0300499 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300500 {
501 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800502 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300503 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800504 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000505}
506
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507// For now, keep it simple: if it starts "gl_", it's reserved, independent
508// of scope. Except, if the symbol table is at the built-in push-level,
509// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000510// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
511// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300512bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000513{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530514 static const char *reservedErrMsg = "reserved built-in name";
515 if (!symbolTable.atBuiltInLevel())
516 {
517 if (identifier.compare(0, 3, "gl_") == 0)
518 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000519 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300520 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 }
Jamie Madillacb4b812016-11-07 13:50:29 -0500522 if (sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530523 {
524 if (identifier.compare(0, 6, "webgl_") == 0)
525 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000526 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300527 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000528 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530529 if (identifier.compare(0, 7, "_webgl_") == 0)
530 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000531 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300532 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000533 }
534 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530535 if (identifier.find("__") != TString::npos)
536 {
537 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400538 "identifiers containing two consecutive underscores (__) are reserved as "
539 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530540 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300541 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000542 }
543 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544
Olli Etuaho8a176262016-08-16 14:23:01 +0300545 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000546}
547
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000548// Make sure there is enough data provided to the constructor to build
549// something of the type of the constructor. Also returns the type of
550// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300551bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
552 TIntermNode *argumentsNode,
553 const TFunction &function,
554 TOperator op,
555 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000556{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000557 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400558 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530559 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400560 case EOpConstructMat2:
561 case EOpConstructMat2x3:
562 case EOpConstructMat2x4:
563 case EOpConstructMat3x2:
564 case EOpConstructMat3:
565 case EOpConstructMat3x4:
566 case EOpConstructMat4x2:
567 case EOpConstructMat4x3:
568 case EOpConstructMat4:
569 constructingMatrix = true;
570 break;
571 default:
572 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000573 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000574
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000575 //
576 // Note: It's okay to have too many components available, but not okay to have unused
577 // arguments. 'full' will go to true when enough args have been seen. If we loop
578 // again, there is an extra argument, so 'overfull' will become true.
579 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000580
Jamie Madillb98c3a82015-07-23 14:26:04 -0400581 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400582 bool full = false;
583 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 bool matrixInMatrix = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500585 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530586 for (size_t i = 0; i < function.getParamCount(); ++i)
587 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700588 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000589 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530590
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000591 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000592 matrixInMatrix = true;
593 if (full)
594 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300595 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000597 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 arrayArg = true;
599 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530600
Olli Etuaho856c4972016-08-08 11:38:39 +0300601 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300602 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300603 // The size of an unsized constructor should already have been determined.
604 ASSERT(!type.isUnsizedArray());
605 if (static_cast<size_t>(type.getArraySize()) != function.getParamCount())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300606 {
607 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300608 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300609 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000611
Arun Patole7e7e68d2015-05-22 12:02:25 +0530612 if (arrayArg && op != EOpConstructStruct)
613 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000614 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300615 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617
Olli Etuaho856c4972016-08-08 11:38:39 +0300618 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530619 {
620 if (function.getParamCount() != 1)
621 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400622 error(line, "constructing matrix from matrix can only take one argument",
623 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300624 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000625 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000626 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627
Arun Patole7e7e68d2015-05-22 12:02:25 +0530628 if (overFull)
629 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000630 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300631 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000632 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530633
Olli Etuaho856c4972016-08-08 11:38:39 +0300634 if (op == EOpConstructStruct && !type.isArray() &&
635 type.getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530636 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400637 error(line,
638 "Number of constructor parameters does not match the number of structure fields",
639 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300640 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000641 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642
Olli Etuaho856c4972016-08-08 11:38:39 +0300643 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530644 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300645 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
646 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530647 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000648 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300649 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000650 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000651 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200653 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530654 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200655 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300656 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000657 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200658
659 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
660 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530661 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200662 TIntermTyped *argTyped = argNode->getAsTyped();
663 ASSERT(argTyped != nullptr);
664 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
665 {
666 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300667 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200668 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300669 if (op != EOpConstructStruct && IsImage(argTyped->getBasicType()))
670 {
671 error(line, "cannot convert an image", "constructor");
672 return false;
673 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200674 if (argTyped->getBasicType() == EbtVoid)
675 {
676 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300677 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200678 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000679 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000680
Olli Etuaho856c4972016-08-08 11:38:39 +0300681 if (type.isArray())
682 {
683 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
684 // the array.
685 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
686 {
687 const TType &argType = argNode->getAsTyped()->getType();
688 // It has already been checked that the argument is not an array.
689 ASSERT(!argType.isArray());
690 if (!argType.sameElementType(type))
691 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000692 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300693 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300694 }
695 }
696 }
697 else if (op == EOpConstructStruct)
698 {
699 const TFieldList &fields = type.getStruct()->fields();
700 TIntermSequence *args = argumentsAgg->getSequence();
701
702 for (size_t i = 0; i < fields.size(); i++)
703 {
704 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
705 {
706 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000707 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300708 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300709 }
710 }
711 }
712
Olli Etuaho8a176262016-08-16 14:23:01 +0300713 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000714}
715
Jamie Madillb98c3a82015-07-23 14:26:04 -0400716// This function checks to see if a void variable has been declared and raise an error message for
717// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000718//
719// returns true in case of an error
720//
Olli Etuaho856c4972016-08-08 11:38:39 +0300721bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400722 const TString &identifier,
723 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300725 if (type == EbtVoid)
726 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000727 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300728 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300729 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000730
Olli Etuaho8a176262016-08-16 14:23:01 +0300731 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732}
733
Jamie Madillb98c3a82015-07-23 14:26:04 -0400734// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300735// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300736void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000737{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530738 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
739 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000740 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530741 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742}
743
Jamie Madillb98c3a82015-07-23 14:26:04 -0400744// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300745// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300746void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000747{
Martin Radev4a9cd802016-09-01 16:51:51 +0300748 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530749 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000750 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530751 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752}
753
Olli Etuaho856c4972016-08-08 11:38:39 +0300754bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300755 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400756 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530758 if (pType.type == EbtStruct)
759 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300760 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530761 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000762 std::stringstream reasonStream;
763 reasonStream << reason << " (structure contains a sampler)";
764 std::string reasonStr = reasonStream.str();
765 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300766 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000767 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530768
Olli Etuaho8a176262016-08-16 14:23:01 +0300769 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530770 }
771 else if (IsSampler(pType.type))
772 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000773 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300774 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000775 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776
Olli Etuaho8a176262016-08-16 14:23:01 +0300777 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778}
779
Martin Radev2cc85b32016-08-05 16:22:53 +0300780bool TParseContext::checkIsNotImage(const TSourceLoc &line,
781 const TTypeSpecifierNonArray &pType,
782 const char *reason)
783{
784 if (pType.type == EbtStruct)
785 {
786 if (ContainsImage(*pType.userDef))
787 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000788 std::stringstream reasonStream;
789 reasonStream << reason << " (structure contains an image)";
790 std::string reasonStr = reasonStream.str();
791 error(line, reasonStr.c_str(), getBasicString(pType.type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300792
793 return false;
794 }
795
796 return true;
797 }
798 else if (IsImage(pType.type))
799 {
800 error(line, reason, getBasicString(pType.type));
801
802 return false;
803 }
804
805 return true;
806}
807
Olli Etuaho856c4972016-08-08 11:38:39 +0300808void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
809 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400810{
811 if (pType.layoutQualifier.location != -1)
812 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400813 error(line, "location must only be specified for a single input or output variable",
814 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400815 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400816}
817
Olli Etuaho856c4972016-08-08 11:38:39 +0300818void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
819 const TLayoutQualifier &layoutQualifier)
820{
821 if (layoutQualifier.location != -1)
822 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000823 error(location, "invalid layout qualifier: only valid on program inputs and outputs",
824 "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300825 }
826}
827
Martin Radev2cc85b32016-08-05 16:22:53 +0300828void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
829 TQualifier qualifier,
830 const TType &type)
831{
832 checkOutParameterIsNotSampler(line, qualifier, type);
833 checkOutParameterIsNotImage(line, qualifier, type);
834}
835
Olli Etuaho856c4972016-08-08 11:38:39 +0300836void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
837 TQualifier qualifier,
838 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839{
Martin Radev2cc85b32016-08-05 16:22:53 +0300840 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
841 if (IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530842 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000843 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000844 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845}
846
Martin Radev2cc85b32016-08-05 16:22:53 +0300847void TParseContext::checkOutParameterIsNotImage(const TSourceLoc &line,
848 TQualifier qualifier,
849 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850{
Martin Radev2cc85b32016-08-05 16:22:53 +0300851 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
852 if (IsImage(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530853 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300854 error(line, "images cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000855 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300859unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530861 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000862
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200863 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
864 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
865 // fold as array size.
866 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000867 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000868 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300869 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000870 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871
Olli Etuaho856c4972016-08-08 11:38:39 +0300872 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400873
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000874 if (constant->getBasicType() == EbtUInt)
875 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300876 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000877 }
878 else
879 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300880 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000881
Olli Etuaho856c4972016-08-08 11:38:39 +0300882 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000883 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400884 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300885 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000886 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400887
Olli Etuaho856c4972016-08-08 11:38:39 +0300888 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400889 }
890
Olli Etuaho856c4972016-08-08 11:38:39 +0300891 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400892 {
893 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300894 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400895 }
896
897 // The size of arrays is restricted here to prevent issues further down the
898 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
899 // 4096 registers so this should be reasonable even for aggressively optimizable code.
900 const unsigned int sizeLimit = 65536;
901
Olli Etuaho856c4972016-08-08 11:38:39 +0300902 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400903 {
904 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300905 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300907
908 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909}
910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300912bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
913 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914{
Olli Etuaho8a176262016-08-16 14:23:01 +0300915 if ((elementQualifier.qualifier == EvqAttribute) ||
916 (elementQualifier.qualifier == EvqVertexIn) ||
917 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300918 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400919 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300920 TType(elementQualifier).getQualifierString());
921 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000922 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923
Olli Etuaho8a176262016-08-16 14:23:01 +0300924 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925}
926
Olli Etuaho8a176262016-08-16 14:23:01 +0300927// See if this element type can be formed into an array.
928bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000930 //
931 // Can the type be an array?
932 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300933 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400934 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300935 error(line, "cannot declare arrays of arrays",
936 TType(elementType).getCompleteString().c_str());
937 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000938 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300939 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
940 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
941 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300942 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300943 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300944 {
945 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300946 TType(elementType).getCompleteString().c_str());
947 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300948 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949
Olli Etuaho8a176262016-08-16 14:23:01 +0300950 return true;
951}
952
953// Check if this qualified element type can be formed into an array.
954bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
955 const TPublicType &elementType)
956{
957 if (checkIsValidTypeForArray(indexLocation, elementType))
958 {
959 return checkIsValidQualifierForArray(indexLocation, elementType);
960 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000961 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962}
963
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300965void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
966 const TString &identifier,
967 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968{
Olli Etuaho3739d232015-04-08 12:23:44 +0300969 ASSERT(type != nullptr);
970 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000971 {
972 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300973 type->qualifier = EvqTemporary;
974
975 // Generate informative error messages for ESSL1.
976 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400977 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000978 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530979 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400980 "structures containing arrays may not be declared constant since they cannot be "
981 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530982 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000983 }
984 else
985 {
986 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
987 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300988 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000989 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300990 if (type->isUnsizedArray())
991 {
992 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300993 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994}
995
Olli Etuaho2935c582015-04-08 14:32:06 +0300996// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997// and update the symbol table.
998//
Olli Etuaho2935c582015-04-08 14:32:06 +0300999// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001001bool TParseContext::declareVariable(const TSourceLoc &line,
1002 const TString &identifier,
1003 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001004 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005{
Olli Etuaho2935c582015-04-08 14:32:06 +03001006 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007
Olli Etuaho856c4972016-08-08 11:38:39 +03001008 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009
Olli Etuaho2935c582015-04-08 14:32:06 +03001010 // gl_LastFragData may be redeclared with a new precision qualifier
1011 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1012 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001013 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1014 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001015 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001016 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001017 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001018 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001019 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001020 }
1021 }
1022 else
1023 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001024 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1025 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001026 return false;
1027 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001028 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029
Olli Etuaho8a176262016-08-16 14:23:01 +03001030 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001031 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032
Olli Etuaho2935c582015-04-08 14:32:06 +03001033 (*variable) = new TVariable(&identifier, type);
1034 if (!symbolTable.declare(*variable))
1035 {
1036 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001037 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001038 return false;
1039 }
1040
Olli Etuaho8a176262016-08-16 14:23:01 +03001041 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001042 return false;
1043
1044 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045}
1046
Martin Radev70866b82016-07-22 15:27:42 +03001047void TParseContext::checkIsParameterQualifierValid(
1048 const TSourceLoc &line,
1049 const TTypeQualifierBuilder &typeQualifierBuilder,
1050 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301051{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001052 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001053
1054 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301055 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001056 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1057 }
1058
1059 if (!IsImage(type->getBasicType()))
1060 {
1061 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, line);
1062 }
1063 else
1064 {
1065 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001066 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067
Martin Radev70866b82016-07-22 15:27:42 +03001068 type->setQualifier(typeQualifier.qualifier);
1069
1070 if (typeQualifier.precision != EbpUndefined)
1071 {
1072 type->setPrecision(typeQualifier.precision);
1073 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074}
1075
Olli Etuaho856c4972016-08-08 11:38:39 +03001076bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001077{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001078 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001079 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301080 if (iter == extBehavior.end())
1081 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001082 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001083 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001084 }
zmo@google.comf5450912011-09-09 01:37:19 +00001085 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301086 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1087 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001088 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001089 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001090 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301091 if (iter->second == EBhWarn)
1092 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001093 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001094 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001095 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096
Olli Etuaho8a176262016-08-16 14:23:01 +03001097 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098}
1099
Martin Radevb8b01222016-11-20 23:25:53 +02001100void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1101 const TSourceLoc &location)
1102{
1103 if (publicType.isUnsizedArray())
1104 {
1105 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1106 // error. It is assumed that this applies to empty declarations as well.
1107 error(location, "empty array declaration needs to specify a size", "");
1108 }
1109 if (publicType.qualifier == EvqShared && !publicType.layoutQualifier.isEmpty())
1110 {
1111 error(location, "Shared memory declarations cannot have layout specified", "layout");
1112 }
1113}
1114
Jamie Madillb98c3a82015-07-23 14:26:04 -04001115// These checks are common for all declarations starting a declarator list, and declarators that
1116// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +03001117void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001118 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001119{
Olli Etuahofa33d582015-04-09 14:33:12 +03001120 switch (publicType.qualifier)
1121 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001122 case EvqVaryingIn:
1123 case EvqVaryingOut:
1124 case EvqAttribute:
1125 case EvqVertexIn:
1126 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001127 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001128 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001129 {
1130 error(identifierLocation, "cannot be used with a structure",
1131 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001132 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001133 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001134
Jamie Madillb98c3a82015-07-23 14:26:04 -04001135 default:
1136 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001137 }
1138
Jamie Madillb98c3a82015-07-23 14:26:04 -04001139 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001140 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1141 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001142 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001143 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001144 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001145 if (publicType.qualifier != EvqUniform &&
1146 !checkIsNotImage(identifierLocation, publicType.typeSpecifierNonArray,
1147 "images must be uniform"))
1148 {
1149 return;
1150 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001151
1152 // check for layout qualifier issues
1153 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1154
1155 if (layoutQualifier.matrixPacking != EmpUnspecified)
1156 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001157 error(identifierLocation, "layout qualifier only valid for interface blocks",
1158 getMatrixPackingString(layoutQualifier.matrixPacking));
Olli Etuaho383b7912016-08-05 11:22:59 +03001159 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001160 }
1161
1162 if (layoutQualifier.blockStorage != EbsUnspecified)
1163 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001164 error(identifierLocation, "layout qualifier only valid for interface blocks",
1165 getBlockStorageString(layoutQualifier.blockStorage));
Olli Etuaho383b7912016-08-05 11:22:59 +03001166 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001167 }
1168
Olli Etuaho383b7912016-08-05 11:22:59 +03001169 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001170 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001171 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001172 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001173
1174 if (IsImage(publicType.getBasicType()))
1175 {
1176
1177 switch (layoutQualifier.imageInternalFormat)
1178 {
1179 case EiifRGBA32F:
1180 case EiifRGBA16F:
1181 case EiifR32F:
1182 case EiifRGBA8:
1183 case EiifRGBA8_SNORM:
1184 if (!IsFloatImage(publicType.getBasicType()))
1185 {
1186 error(identifierLocation,
1187 "internal image format requires a floating image type",
1188 getBasicString(publicType.getBasicType()));
1189 return;
1190 }
1191 break;
1192 case EiifRGBA32I:
1193 case EiifRGBA16I:
1194 case EiifRGBA8I:
1195 case EiifR32I:
1196 if (!IsIntegerImage(publicType.getBasicType()))
1197 {
1198 error(identifierLocation,
1199 "internal image format requires an integer image type",
1200 getBasicString(publicType.getBasicType()));
1201 return;
1202 }
1203 break;
1204 case EiifRGBA32UI:
1205 case EiifRGBA16UI:
1206 case EiifRGBA8UI:
1207 case EiifR32UI:
1208 if (!IsUnsignedImage(publicType.getBasicType()))
1209 {
1210 error(identifierLocation,
1211 "internal image format requires an unsigned image type",
1212 getBasicString(publicType.getBasicType()));
1213 return;
1214 }
1215 break;
1216 case EiifUnspecified:
1217 error(identifierLocation, "layout qualifier", "No image internal format specified");
1218 return;
1219 default:
1220 error(identifierLocation, "layout qualifier", "unrecognized token");
1221 return;
1222 }
1223
1224 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1225 switch (layoutQualifier.imageInternalFormat)
1226 {
1227 case EiifR32F:
1228 case EiifR32I:
1229 case EiifR32UI:
1230 break;
1231 default:
1232 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1233 {
1234 error(identifierLocation, "layout qualifier",
1235 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1236 "image variables must be qualified readonly and/or writeonly");
1237 return;
1238 }
1239 break;
1240 }
1241 }
1242 else
1243 {
1244
1245 if (!checkInternalFormatIsNotSpecified(identifierLocation,
1246 layoutQualifier.imageInternalFormat))
1247 {
1248 return;
1249 }
1250
1251 if (!checkIsMemoryQualifierNotSpecified(publicType.memoryQualifier, identifierLocation))
1252 {
1253 return;
1254 }
1255 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001256}
1257
Olli Etuaho856c4972016-08-08 11:38:39 +03001258void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1259 const TString &layoutQualifierName,
1260 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001261{
1262
1263 if (mShaderVersion < versionRequired)
1264 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001265 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001266 }
1267}
1268
Olli Etuaho856c4972016-08-08 11:38:39 +03001269bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1270 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001271{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001272 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001273 for (size_t i = 0u; i < localSize.size(); ++i)
1274 {
1275 if (localSize[i] != -1)
1276 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001277 error(location,
1278 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1279 "global layout declaration",
1280 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001281 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001282 }
1283 }
1284
Olli Etuaho8a176262016-08-16 14:23:01 +03001285 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001286}
1287
Martin Radev2cc85b32016-08-05 16:22:53 +03001288bool TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
1289 TLayoutImageInternalFormat internalFormat)
1290{
1291 if (internalFormat != EiifUnspecified)
1292 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001293 error(location, "invalid layout qualifier: only valid when used with images",
1294 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001295 return false;
1296 }
1297 return true;
1298}
1299
Olli Etuaho383b7912016-08-05 11:22:59 +03001300void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001301 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001302{
1303 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1304 {
1305 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1306 if (qual == EvqOut || qual == EvqInOut)
1307 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001308 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001309 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001310 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001311 TString unmangledName =
1312 TFunction::unmangleName(fnCall->getFunctionSymbolInfo()->getName());
Olli Etuaho856c4972016-08-08 11:38:39 +03001313 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001314 "Constant value cannot be passed for 'out' or 'inout' parameters.",
1315 unmangledName.c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001316 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001317 }
1318 }
1319 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001320}
1321
Martin Radev70866b82016-07-22 15:27:42 +03001322void TParseContext::checkInvariantVariableQualifier(bool invariant,
1323 const TQualifier qualifier,
1324 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001325{
Martin Radev70866b82016-07-22 15:27:42 +03001326 if (!invariant)
1327 return;
1328
1329 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001330 {
Martin Radev70866b82016-07-22 15:27:42 +03001331 // input variables in the fragment shader can be also qualified as invariant
1332 if (!sh::CanBeInvariantESSL1(qualifier))
1333 {
1334 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1335 }
1336 }
1337 else
1338 {
1339 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1340 {
1341 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1342 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001343 }
1344}
1345
Arun Patole7e7e68d2015-05-22 12:02:25 +05301346bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001347{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001348 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001349 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1350 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001351}
1352
Arun Patole7e7e68d2015-05-22 12:02:25 +05301353bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001354{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001355 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001356}
1357
Jamie Madillb98c3a82015-07-23 14:26:04 -04001358void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1359 const char *extName,
1360 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001361{
1362 pp::SourceLocation srcLoc;
1363 srcLoc.file = loc.first_file;
1364 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001365 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001366}
1367
Jamie Madillb98c3a82015-07-23 14:26:04 -04001368void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1369 const char *name,
1370 const char *value,
1371 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001372{
1373 pp::SourceLocation srcLoc;
1374 srcLoc.file = loc.first_file;
1375 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001376 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001377}
1378
Martin Radev4c4c8e72016-08-04 12:25:34 +03001379sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001380{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001381 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001382 for (size_t i = 0u; i < result.size(); ++i)
1383 {
1384 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1385 {
1386 result[i] = 1;
1387 }
1388 else
1389 {
1390 result[i] = mComputeShaderLocalSize[i];
1391 }
1392 }
1393 return result;
1394}
1395
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001396/////////////////////////////////////////////////////////////////////////////////
1397//
1398// Non-Errors.
1399//
1400/////////////////////////////////////////////////////////////////////////////////
1401
Jamie Madill5c097022014-08-20 16:38:32 -04001402const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1403 const TString *name,
1404 const TSymbol *symbol)
1405{
1406 const TVariable *variable = NULL;
1407
1408 if (!symbol)
1409 {
1410 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001411 }
1412 else if (!symbol->isVariable())
1413 {
1414 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001415 }
1416 else
1417 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001418 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001419
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001420 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001421 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001422 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001423 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001424 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001425
1426 // Reject shaders using both gl_FragData and gl_FragColor
1427 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001428 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001429 {
1430 mUsesFragData = true;
1431 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001432 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001433 {
1434 mUsesFragColor = true;
1435 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001436 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1437 {
1438 mUsesSecondaryOutputs = true;
1439 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001440
1441 // This validation is not quite correct - it's only an error to write to
1442 // both FragData and FragColor. For simplicity, and because users shouldn't
1443 // be rewarded for reading from undefined varaibles, return an error
1444 // if they are both referenced, rather than assigned.
1445 if (mUsesFragData && mUsesFragColor)
1446 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001447 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1448 if (mUsesSecondaryOutputs)
1449 {
1450 errorMessage =
1451 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1452 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1453 }
1454 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001455 }
Martin Radevb0883602016-08-04 17:48:58 +03001456
1457 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1458 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1459 qualifier == EvqWorkGroupSize)
1460 {
1461 error(location,
1462 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1463 "gl_WorkGroupSize");
1464 }
Jamie Madill5c097022014-08-20 16:38:32 -04001465 }
1466
1467 if (!variable)
1468 {
1469 TType type(EbtFloat, EbpUndefined);
1470 TVariable *fakeVariable = new TVariable(name, type);
1471 symbolTable.declare(fakeVariable);
1472 variable = fakeVariable;
1473 }
1474
1475 return variable;
1476}
1477
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001478TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1479 const TString *name,
1480 const TSymbol *symbol)
1481{
1482 const TVariable *variable = getNamedVariable(location, name, symbol);
1483
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001484 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001485 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001486 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001487 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001488 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001489 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1490 mComputeShaderLocalSizeDeclared)
1491 {
1492 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1493 // needs to be added to the AST as a constant and not as a symbol.
1494 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1495 TConstantUnion *constArray = new TConstantUnion[3];
1496 for (size_t i = 0; i < 3; ++i)
1497 {
1498 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1499 }
1500
1501 ASSERT(variable->getType().getBasicType() == EbtUInt);
1502 ASSERT(variable->getType().getObjectSize() == 3);
1503
1504 TType type(variable->getType());
1505 type.setQualifier(EvqConst);
1506 return intermediate.addConstantUnion(constArray, type, location);
1507 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001508 else
1509 {
1510 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1511 variable->getType(), location);
1512 }
1513}
1514
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001515//
1516// Look up a function name in the symbol table, and make sure it is a function.
1517//
1518// Return the function symbol if found, otherwise 0.
1519//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001520const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1521 TFunction *call,
1522 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301523 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001524{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001525 // First find by unmangled name to check whether the function name has been
1526 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001527 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301528 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1529 if (symbol == 0 || symbol->isFunction())
1530 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001531 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001532 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001533
Arun Patole7e7e68d2015-05-22 12:02:25 +05301534 if (symbol == 0)
1535 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001536 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001537 return 0;
1538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001539
Arun Patole7e7e68d2015-05-22 12:02:25 +05301540 if (!symbol->isFunction())
1541 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001542 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001543 return 0;
1544 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001545
Jamie Madillb98c3a82015-07-23 14:26:04 -04001546 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001547}
1548
1549//
1550// Initializers show up in several places in the grammar. Have one set of
1551// code to handle them here.
1552//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001553// Returns true on error, false if no error
1554//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001555bool TParseContext::executeInitializer(const TSourceLoc &line,
1556 const TString &identifier,
1557 const TPublicType &pType,
1558 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001559 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001560{
Olli Etuaho13389b62016-10-16 11:48:18 +01001561 ASSERT(initNode != nullptr);
1562 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001563 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001564
Olli Etuaho2935c582015-04-08 14:32:06 +03001565 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001566 if (type.isUnsizedArray())
1567 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001568 // We have not checked yet whether the initializer actually is an array or not.
1569 if (initializer->isArray())
1570 {
1571 type.setArraySize(initializer->getArraySize());
1572 }
1573 else
1574 {
1575 // Having a non-array initializer for an unsized array will result in an error later,
1576 // so we don't generate an error message here.
1577 type.setArraySize(1u);
1578 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001579 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001580 if (!declareVariable(line, identifier, type, &variable))
1581 {
1582 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001583 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001584
Olli Etuahob0c645e2015-05-12 14:25:36 +03001585 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001586 if (symbolTable.atGlobalLevel() &&
1587 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001588 {
1589 // Error message does not completely match behavior with ESSL 1.00, but
1590 // we want to steer developers towards only using constant expressions.
1591 error(line, "global variable initializers must be constant expressions", "=");
1592 return true;
1593 }
1594 if (globalInitWarning)
1595 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001596 warning(
1597 line,
1598 "global variable initializers should be constant expressions "
1599 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1600 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001601 }
1602
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001603 //
1604 // identifier must be of type constant, a global, or a temporary
1605 //
1606 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301607 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1608 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001609 error(line, " cannot initialize this type of qualifier ",
1610 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001611 return true;
1612 }
1613 //
1614 // test for and propagate constant
1615 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001616
Arun Patole7e7e68d2015-05-22 12:02:25 +05301617 if (qualifier == EvqConst)
1618 {
1619 if (qualifier != initializer->getType().getQualifier())
1620 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001621 std::stringstream reasonStream;
1622 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1623 << "'";
1624 std::string reason = reasonStream.str();
1625 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001626 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001627 return true;
1628 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301629 if (type != initializer->getType())
1630 {
1631 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001632 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001633 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001634 return true;
1635 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001636
1637 // Save the constant folded value to the variable if possible. For example array
1638 // initializers are not folded, since that way copying the array literal to multiple places
1639 // in the shader is avoided.
1640 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1641 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301642 if (initializer->getAsConstantUnion())
1643 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001644 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001645 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001646 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301647 }
1648 else if (initializer->getAsSymbolNode())
1649 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001650 const TSymbol *symbol =
1651 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1652 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001653
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001654 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001655 if (constArray)
1656 {
1657 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001658 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001659 return false;
1660 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001661 }
1662 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001663
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001664 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1665 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001666 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1667 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001668 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001669 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1670 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001671 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001673 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001674}
1675
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001676void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1677{
1678 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1679 typeSpecifier->getBasicType());
1680
1681 if (mShaderVersion < 300 && typeSpecifier->array)
1682 {
1683 error(typeSpecifier->getLine(), "not supported", "first-class array");
1684 typeSpecifier->clearArrayness();
1685 }
1686}
1687
Martin Radev70866b82016-07-22 15:27:42 +03001688TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301689 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001690{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001691 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001692
Martin Radev70866b82016-07-22 15:27:42 +03001693 TPublicType returnType = typeSpecifier;
1694 returnType.qualifier = typeQualifier.qualifier;
1695 returnType.invariant = typeQualifier.invariant;
1696 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001697 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001698 returnType.precision = typeSpecifier.precision;
1699
1700 if (typeQualifier.precision != EbpUndefined)
1701 {
1702 returnType.precision = typeQualifier.precision;
1703 }
1704
Martin Radev4a9cd802016-09-01 16:51:51 +03001705 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1706 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001707
Martin Radev4a9cd802016-09-01 16:51:51 +03001708 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1709 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001710
Martin Radev4a9cd802016-09-01 16:51:51 +03001711 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001712
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001713 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001714 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001715 if (typeSpecifier.array)
1716 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001717 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001718 returnType.clearArrayness();
1719 }
1720
Martin Radev70866b82016-07-22 15:27:42 +03001721 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001722 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001723 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001724 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001725 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001726 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001727
Martin Radev70866b82016-07-22 15:27:42 +03001728 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001729 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001730 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001731 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001732 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001733 }
1734 }
1735 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001736 {
Martin Radev70866b82016-07-22 15:27:42 +03001737 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001738 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001739 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001740 }
Martin Radev70866b82016-07-22 15:27:42 +03001741 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1742 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001743 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001744 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1745 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001746 }
Martin Radev70866b82016-07-22 15:27:42 +03001747 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001748 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001749 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001750 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001751 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001752 }
1753
1754 return returnType;
1755}
1756
Olli Etuaho856c4972016-08-08 11:38:39 +03001757void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1758 const TPublicType &type,
1759 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001760{
1761 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001762 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001763 {
1764 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001765 }
1766
1767 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1768 switch (qualifier)
1769 {
1770 case EvqVertexIn:
1771 // ESSL 3.00 section 4.3.4
1772 if (type.array)
1773 {
1774 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001775 }
1776 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1777 return;
1778 case EvqFragmentOut:
1779 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001780 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001781 {
1782 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001783 }
1784 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1785 return;
1786 default:
1787 break;
1788 }
1789
1790 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1791 // restrictions.
1792 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001793 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1794 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001795 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1796 {
1797 error(qualifierLocation, "must use 'flat' interpolation here",
1798 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001799 }
1800
Martin Radev4a9cd802016-09-01 16:51:51 +03001801 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001802 {
1803 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1804 // These restrictions are only implied by the ESSL 3.00 spec, but
1805 // the ESSL 3.10 spec lists these restrictions explicitly.
1806 if (type.array)
1807 {
1808 error(qualifierLocation, "cannot be an array of structures",
1809 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001810 }
1811 if (type.isStructureContainingArrays())
1812 {
1813 error(qualifierLocation, "cannot be a structure containing an array",
1814 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001815 }
1816 if (type.isStructureContainingType(EbtStruct))
1817 {
1818 error(qualifierLocation, "cannot be a structure containing a structure",
1819 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001820 }
1821 if (type.isStructureContainingType(EbtBool))
1822 {
1823 error(qualifierLocation, "cannot be a structure containing a bool",
1824 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001825 }
1826 }
1827}
1828
Martin Radev2cc85b32016-08-05 16:22:53 +03001829void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1830{
1831 if (qualifier.getType() == QtStorage)
1832 {
1833 const TStorageQualifierWrapper &storageQualifier =
1834 static_cast<const TStorageQualifierWrapper &>(qualifier);
1835 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1836 !symbolTable.atGlobalLevel())
1837 {
1838 error(storageQualifier.getLine(),
1839 "Local variables can only use the const storage qualifier.",
1840 storageQualifier.getQualifierString().c_str());
1841 }
1842 }
1843}
1844
1845bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1846 const TSourceLoc &location)
1847{
1848 if (memoryQualifier.readonly)
1849 {
1850 error(location, "Only allowed with images.", "readonly");
1851 return false;
1852 }
1853 if (memoryQualifier.writeonly)
1854 {
1855 error(location, "Only allowed with images.", "writeonly");
1856 return false;
1857 }
Martin Radev049edfa2016-11-11 14:35:37 +02001858 if (memoryQualifier.coherent)
1859 {
1860 error(location, "Only allowed with images.", "coherent");
1861 return false;
1862 }
1863 if (memoryQualifier.restrictQualifier)
1864 {
1865 error(location, "Only allowed with images.", "restrict");
1866 return false;
1867 }
1868 if (memoryQualifier.volatileQualifier)
1869 {
1870 error(location, "Only allowed with images.", "volatile");
1871 return false;
1872 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001873 return true;
1874}
1875
Olli Etuaho13389b62016-10-16 11:48:18 +01001876TIntermDeclaration *TParseContext::parseSingleDeclaration(
1877 TPublicType &publicType,
1878 const TSourceLoc &identifierOrTypeLocation,
1879 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001880{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001881 TType type(publicType);
1882 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1883 mDirectiveHandler.pragma().stdgl.invariantAll)
1884 {
1885 TQualifier qualifier = type.getQualifier();
1886
1887 // The directive handler has already taken care of rejecting invalid uses of this pragma
1888 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1889 // affected variable declarations:
1890 //
1891 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1892 // elsewhere, in TranslatorGLSL.)
1893 //
1894 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1895 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1896 // the way this is currently implemented we have to enable this compiler option before
1897 // parsing the shader and determining the shading language version it uses. If this were
1898 // implemented as a post-pass, the workaround could be more targeted.
1899 //
1900 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1901 // the specification, but there are desktop OpenGL drivers that expect that this is the
1902 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1903 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1904 {
1905 type.setInvariant(true);
1906 }
1907 }
1908
1909 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001910
Olli Etuahobab4c082015-04-24 16:38:49 +03001911 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001912
Olli Etuahobab4c082015-04-24 16:38:49 +03001913 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1914
Olli Etuaho13389b62016-10-16 11:48:18 +01001915 TIntermDeclaration *declaration = new TIntermDeclaration();
1916 declaration->setLine(identifierOrTypeLocation);
1917
Olli Etuahobab4c082015-04-24 16:38:49 +03001918 if (emptyDeclaration)
1919 {
Martin Radevb8b01222016-11-20 23:25:53 +02001920 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobab4c082015-04-24 16:38:49 +03001921 }
1922 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001923 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001924 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001925
Olli Etuaho856c4972016-08-08 11:38:39 +03001926 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001927
Olli Etuaho2935c582015-04-08 14:32:06 +03001928 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001929 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001930
1931 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001932 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001933 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001934 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001935 }
1936
Olli Etuaho13389b62016-10-16 11:48:18 +01001937 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1938 // that may just declare a type.
1939 declaration->appendDeclarator(symbol);
1940
1941 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001942}
1943
Olli Etuaho13389b62016-10-16 11:48:18 +01001944TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1945 const TSourceLoc &identifierLocation,
1946 const TString &identifier,
1947 const TSourceLoc &indexLocation,
1948 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001949{
Olli Etuahofa33d582015-04-09 14:33:12 +03001950 mDeferredSingleDeclarationErrorCheck = false;
1951
Olli Etuaho383b7912016-08-05 11:22:59 +03001952 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001953
Olli Etuaho856c4972016-08-08 11:38:39 +03001954 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001955
Olli Etuaho8a176262016-08-16 14:23:01 +03001956 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001957
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001958 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001959
Olli Etuaho856c4972016-08-08 11:38:39 +03001960 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001961 // Make the type an array even if size check failed.
1962 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1963 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001964
Olli Etuaho2935c582015-04-08 14:32:06 +03001965 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001966 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001967
Olli Etuaho13389b62016-10-16 11:48:18 +01001968 TIntermDeclaration *declaration = new TIntermDeclaration();
1969 declaration->setLine(identifierLocation);
1970
Olli Etuahoe7847b02015-03-16 11:56:12 +02001971 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001972 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001973 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001974 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001975 declaration->appendDeclarator(symbol);
1976 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001977
Olli Etuaho13389b62016-10-16 11:48:18 +01001978 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001979}
1980
Olli Etuaho13389b62016-10-16 11:48:18 +01001981TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1982 const TSourceLoc &identifierLocation,
1983 const TString &identifier,
1984 const TSourceLoc &initLocation,
1985 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001986{
Olli Etuahofa33d582015-04-09 14:33:12 +03001987 mDeferredSingleDeclarationErrorCheck = false;
1988
Olli Etuaho383b7912016-08-05 11:22:59 +03001989 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001990
Olli Etuaho13389b62016-10-16 11:48:18 +01001991 TIntermDeclaration *declaration = new TIntermDeclaration();
1992 declaration->setLine(identifierLocation);
1993
1994 TIntermBinary *initNode = nullptr;
1995 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001996 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001997 if (initNode)
1998 {
1999 declaration->appendDeclarator(initNode);
2000 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002001 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002002 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002003}
2004
Olli Etuaho13389b62016-10-16 11:48:18 +01002005TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002006 TPublicType &publicType,
2007 const TSourceLoc &identifierLocation,
2008 const TString &identifier,
2009 const TSourceLoc &indexLocation,
2010 TIntermTyped *indexExpression,
2011 const TSourceLoc &initLocation,
2012 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002013{
2014 mDeferredSingleDeclarationErrorCheck = false;
2015
Olli Etuaho383b7912016-08-05 11:22:59 +03002016 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002017
Olli Etuaho8a176262016-08-16 14:23:01 +03002018 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002019
2020 TPublicType arrayType(publicType);
2021
Olli Etuaho856c4972016-08-08 11:38:39 +03002022 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002023 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2024 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002025 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002026 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002027 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002028 }
2029 // Make the type an array even if size check failed.
2030 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2031 arrayType.setArraySize(size);
2032
Olli Etuaho13389b62016-10-16 11:48:18 +01002033 TIntermDeclaration *declaration = new TIntermDeclaration();
2034 declaration->setLine(identifierLocation);
2035
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002036 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002037 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002038 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2039 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002040 if (initNode)
2041 {
2042 declaration->appendDeclarator(initNode);
2043 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002044 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002045
2046 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002047}
2048
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002049TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002050 const TTypeQualifierBuilder &typeQualifierBuilder,
2051 const TSourceLoc &identifierLoc,
2052 const TString *identifier,
2053 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002054{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002055 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002056
Martin Radev70866b82016-07-22 15:27:42 +03002057 if (!typeQualifier.invariant)
2058 {
2059 error(identifierLoc, "Expected invariant", identifier->c_str());
2060 return nullptr;
2061 }
2062 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2063 {
2064 return nullptr;
2065 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002066 if (!symbol)
2067 {
2068 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002069 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002070 }
Martin Radev70866b82016-07-22 15:27:42 +03002071 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002072 {
Martin Radev70866b82016-07-22 15:27:42 +03002073 error(identifierLoc, "invariant declaration specifies qualifier",
2074 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002075 }
Martin Radev70866b82016-07-22 15:27:42 +03002076 if (typeQualifier.precision != EbpUndefined)
2077 {
2078 error(identifierLoc, "invariant declaration specifies precision",
2079 getPrecisionString(typeQualifier.precision));
2080 }
2081 if (!typeQualifier.layoutQualifier.isEmpty())
2082 {
2083 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2084 }
2085
2086 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2087 ASSERT(variable);
2088 const TType &type = variable->getType();
2089
2090 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2091 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002092 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002093
2094 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2095
2096 TIntermSymbol *intermSymbol =
2097 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2098
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002099 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002100}
2101
Olli Etuaho13389b62016-10-16 11:48:18 +01002102void TParseContext::parseDeclarator(TPublicType &publicType,
2103 const TSourceLoc &identifierLocation,
2104 const TString &identifier,
2105 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002106{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002107 // If the declaration starting this declarator list was empty (example: int,), some checks were
2108 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002109 if (mDeferredSingleDeclarationErrorCheck)
2110 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002111 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002112 mDeferredSingleDeclarationErrorCheck = false;
2113 }
2114
Olli Etuaho856c4972016-08-08 11:38:39 +03002115 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002116
Olli Etuaho856c4972016-08-08 11:38:39 +03002117 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002118
Olli Etuaho2935c582015-04-08 14:32:06 +03002119 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002120 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002121
Jamie Madillb98c3a82015-07-23 14:26:04 -04002122 TIntermSymbol *symbol =
2123 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002124 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002125 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002126 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002127 declarationOut->appendDeclarator(symbol);
2128 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002129}
2130
Olli Etuaho13389b62016-10-16 11:48:18 +01002131void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2132 const TSourceLoc &identifierLocation,
2133 const TString &identifier,
2134 const TSourceLoc &arrayLocation,
2135 TIntermTyped *indexExpression,
2136 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002137{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002138 // If the declaration starting this declarator list was empty (example: int,), some checks were
2139 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002140 if (mDeferredSingleDeclarationErrorCheck)
2141 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002142 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002143 mDeferredSingleDeclarationErrorCheck = false;
2144 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002145
Olli Etuaho856c4972016-08-08 11:38:39 +03002146 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002147
Olli Etuaho856c4972016-08-08 11:38:39 +03002148 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002149
Olli Etuaho8a176262016-08-16 14:23:01 +03002150 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002151 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002152 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002153 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002154 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002155
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002156 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002157 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002158
Jamie Madillb98c3a82015-07-23 14:26:04 -04002159 TIntermSymbol *symbol =
2160 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002161 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002162 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002163
Olli Etuaho13389b62016-10-16 11:48:18 +01002164 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002165 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002166}
2167
Olli Etuaho13389b62016-10-16 11:48:18 +01002168void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2169 const TSourceLoc &identifierLocation,
2170 const TString &identifier,
2171 const TSourceLoc &initLocation,
2172 TIntermTyped *initializer,
2173 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002174{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002175 // If the declaration starting this declarator list was empty (example: int,), some checks were
2176 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002177 if (mDeferredSingleDeclarationErrorCheck)
2178 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002179 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002180 mDeferredSingleDeclarationErrorCheck = false;
2181 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002182
Olli Etuaho856c4972016-08-08 11:38:39 +03002183 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002184
Olli Etuaho13389b62016-10-16 11:48:18 +01002185 TIntermBinary *initNode = nullptr;
2186 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002187 {
2188 //
2189 // build the intermediate representation
2190 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002191 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002192 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002193 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002194 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002195 }
2196}
2197
Olli Etuaho13389b62016-10-16 11:48:18 +01002198void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2199 const TSourceLoc &identifierLocation,
2200 const TString &identifier,
2201 const TSourceLoc &indexLocation,
2202 TIntermTyped *indexExpression,
2203 const TSourceLoc &initLocation,
2204 TIntermTyped *initializer,
2205 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002206{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002207 // If the declaration starting this declarator list was empty (example: int,), some checks were
2208 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002209 if (mDeferredSingleDeclarationErrorCheck)
2210 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002211 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002212 mDeferredSingleDeclarationErrorCheck = false;
2213 }
2214
Olli Etuaho856c4972016-08-08 11:38:39 +03002215 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002216
Olli Etuaho8a176262016-08-16 14:23:01 +03002217 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002218
2219 TPublicType arrayType(publicType);
2220
Olli Etuaho856c4972016-08-08 11:38:39 +03002221 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002222 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2223 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002224 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002225 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002226 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002227 }
2228 // Make the type an array even if size check failed.
2229 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2230 arrayType.setArraySize(size);
2231
2232 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002233 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002234 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2235 {
2236 if (initNode)
2237 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002238 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002239 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002240 }
2241}
2242
Martin Radev70866b82016-07-22 15:27:42 +03002243void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002244{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002245 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002246 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002247
Martin Radev70866b82016-07-22 15:27:42 +03002248 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2249 typeQualifier.line);
2250
Jamie Madillc2128ff2016-07-04 10:26:17 -04002251 // It should never be the case, but some strange parser errors can send us here.
2252 if (layoutQualifier.isEmpty())
2253 {
2254 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002255 return;
2256 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002257
Martin Radev802abe02016-08-04 17:48:32 +03002258 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002259 {
Martin Radev802abe02016-08-04 17:48:32 +03002260 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002261 return;
2262 }
2263
Martin Radev2cc85b32016-08-05 16:22:53 +03002264 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2265
2266 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2267
Martin Radev802abe02016-08-04 17:48:32 +03002268 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002269 {
Martin Radev802abe02016-08-04 17:48:32 +03002270 if (mComputeShaderLocalSizeDeclared &&
2271 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2272 {
2273 error(typeQualifier.line, "Work group size does not match the previous declaration",
2274 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002275 return;
2276 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002277
Martin Radev802abe02016-08-04 17:48:32 +03002278 if (mShaderVersion < 310)
2279 {
2280 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002281 return;
2282 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002283
Martin Radev4c4c8e72016-08-04 12:25:34 +03002284 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002285 {
2286 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002287 return;
2288 }
2289
2290 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2291 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2292
2293 const TConstantUnion *maxComputeWorkGroupSizeData =
2294 maxComputeWorkGroupSize->getConstPointer();
2295
2296 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2297 {
2298 if (layoutQualifier.localSize[i] != -1)
2299 {
2300 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2301 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2302 if (mComputeShaderLocalSize[i] < 1 ||
2303 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2304 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002305 std::stringstream reasonStream;
2306 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2307 << maxComputeWorkGroupSizeValue;
2308 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002309
Olli Etuaho4de340a2016-12-16 09:32:03 +00002310 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002311 return;
2312 }
2313 }
2314 }
2315
2316 mComputeShaderLocalSizeDeclared = true;
2317 }
2318 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002319 {
Martin Radev802abe02016-08-04 17:48:32 +03002320
Olli Etuaho8a176262016-08-16 14:23:01 +03002321 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002322 {
Martin Radev802abe02016-08-04 17:48:32 +03002323 return;
2324 }
2325
2326 if (typeQualifier.qualifier != EvqUniform)
2327 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002328 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2329 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002330 return;
2331 }
2332
2333 if (mShaderVersion < 300)
2334 {
2335 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2336 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002337 return;
2338 }
2339
Olli Etuaho856c4972016-08-08 11:38:39 +03002340 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002341
2342 if (layoutQualifier.matrixPacking != EmpUnspecified)
2343 {
2344 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2345 }
2346
2347 if (layoutQualifier.blockStorage != EbsUnspecified)
2348 {
2349 mDefaultBlockStorage = layoutQualifier.blockStorage;
2350 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002351 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002352}
2353
Olli Etuaho476197f2016-10-11 13:59:08 +01002354TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002355 const TSourceLoc &location)
2356{
Olli Etuaho476197f2016-10-11 13:59:08 +01002357 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2358 // first declaration. Either way the instance in the symbol table is used to track whether the
2359 // function is declared multiple times.
2360 TFunction *function = static_cast<TFunction *>(
2361 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2362 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002363 {
2364 // ESSL 1.00.17 section 4.2.7.
2365 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2366 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002367 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002368 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002369
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002370 TIntermAggregate *prototype = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002371 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2372 // point to the data that already exists in the symbol table.
2373 prototype->setType(function->getReturnType());
Olli Etuahobd674552016-10-06 13:28:42 +01002374 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002375
Olli Etuaho476197f2016-10-11 13:59:08 +01002376 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002377 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002378 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002379 if (param.name != 0)
2380 {
2381 TVariable variable(param.name, *param.type);
2382
2383 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2384 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2385 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2386 }
2387 else
2388 {
2389 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002390 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002391 }
2392 }
2393
2394 prototype->setOp(EOpPrototype);
2395
2396 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002397
2398 if (!symbolTable.atGlobalLevel())
2399 {
2400 // ESSL 3.00.4 section 4.2.4.
2401 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002402 }
2403
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002404 return prototype;
2405}
2406
Olli Etuaho336b1472016-10-05 16:37:55 +01002407TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2408 const TFunction &function,
2409 TIntermAggregate *functionParameters,
2410 TIntermBlock *functionBody,
2411 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002412{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002413 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002414 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2415 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002416 error(location, "function does not return a value:", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002417 }
2418
Olli Etuahof51fdd22016-10-03 10:03:40 +01002419 if (functionBody == nullptr)
2420 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002421 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002422 functionBody->setLine(location);
2423 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002424 TIntermFunctionDefinition *functionNode =
2425 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2426 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002427
Olli Etuahobd674552016-10-06 13:28:42 +01002428 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002429
2430 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002431 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002432}
2433
Olli Etuaho476197f2016-10-11 13:59:08 +01002434void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2435 TFunction **function,
2436 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002437{
Olli Etuaho476197f2016-10-11 13:59:08 +01002438 ASSERT(function);
2439 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002440 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002441 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002442
2443 if (builtIn)
2444 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002445 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002446 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002447 else
Jamie Madill185fb402015-06-12 15:48:48 -04002448 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002449 TFunction *prevDec = static_cast<TFunction *>(
2450 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2451
2452 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2453 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2454 // occurance.
2455 if (*function != prevDec)
2456 {
2457 // Swap the parameters of the previous declaration to the parameters of the function
2458 // definition (parameter names may differ).
2459 prevDec->swapParameters(**function);
2460
2461 // The function definition will share the same symbol as any previous declaration.
2462 *function = prevDec;
2463 }
2464
2465 if ((*function)->isDefined())
2466 {
2467 error(location, "function already has a body", (*function)->getName().c_str());
2468 }
2469
2470 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002471 }
Jamie Madill185fb402015-06-12 15:48:48 -04002472
2473 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002474 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002475 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002476 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002477 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002478 error(location, "function cannot take any parameter(s)",
2479 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002480 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002481 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002482 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002483 error(location, "main function cannot return a value",
2484 (*function)->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002485 }
2486 }
2487
2488 //
2489 // Remember the return type for later checking for RETURN statements.
2490 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002491 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002492 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002493
2494 //
2495 // Insert parameters into the symbol table.
2496 // If the parameter has no name, it's not an error, just don't insert it
2497 // (could be used for unused args).
2498 //
2499 // Also, accumulate the list of parameters into the HIL, so lower level code
2500 // knows where to find parameters.
2501 //
2502 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002503 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002504 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002505 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002506 if (param.name != 0)
2507 {
2508 TVariable *variable = new TVariable(param.name, *param.type);
2509 //
2510 // Insert the parameters with name in the symbol table.
2511 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002512 if (!symbolTable.declare(variable))
2513 {
Jamie Madill185fb402015-06-12 15:48:48 -04002514 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002515 paramNodes = intermediate.growAggregate(
2516 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2517 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002518 }
2519
2520 //
2521 // Add the parameter to the HIL
2522 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002523 TIntermSymbol *symbol = intermediate.addSymbol(
2524 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002525
2526 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2527 }
2528 else
2529 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002530 paramNodes = intermediate.growAggregate(
2531 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002532 }
2533 }
2534 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2535 *aggregateOut = paramNodes;
2536 setLoopNestingLevel(0);
2537}
2538
Jamie Madillb98c3a82015-07-23 14:26:04 -04002539TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002540{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002541 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002542 // We don't know at this point whether this is a function definition or a prototype.
2543 // The definition production code will check for redefinitions.
2544 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002545 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002546 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2547 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002548 //
2549 TFunction *prevDec =
2550 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302551
Martin Radevda6254b2016-12-14 17:00:36 +02002552 if (getShaderVersion() >= 300 &&
2553 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2554 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302555 {
Martin Radevda6254b2016-12-14 17:00:36 +02002556 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302557 // Therefore overloading or redefining builtin functions is an error.
2558 error(location, "Name of a built-in function cannot be redeclared as function",
2559 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302560 }
2561 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002562 {
2563 if (prevDec->getReturnType() != function->getReturnType())
2564 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002565 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002566 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002567 }
2568 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2569 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002570 if (prevDec->getParam(i).type->getQualifier() !=
2571 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002572 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002573 error(location,
2574 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002575 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002576 }
2577 }
2578 }
2579
2580 //
2581 // Check for previously declared variables using the same name.
2582 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002583 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002584 if (prevSym)
2585 {
2586 if (!prevSym->isFunction())
2587 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002588 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002589 }
2590 }
2591 else
2592 {
2593 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002594 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002595 }
2596
2597 // We're at the inner scope level of the function's arguments and body statement.
2598 // Add the function prototype to the surrounding scope instead.
2599 symbolTable.getOuterLevel()->insert(function);
2600
2601 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002602 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2603 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002604 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2605 //
2606 return function;
2607}
2608
Olli Etuaho9de84a52016-06-14 17:36:01 +03002609TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2610 const TString *name,
2611 const TSourceLoc &location)
2612{
2613 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2614 {
2615 error(location, "no qualifiers allowed for function return",
2616 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002617 }
2618 if (!type.layoutQualifier.isEmpty())
2619 {
2620 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002621 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002622 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002623 checkIsNotSampler(location, type.typeSpecifierNonArray,
2624 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002625 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002626 if (mShaderVersion < 300)
2627 {
2628 // Array return values are forbidden, but there's also no valid syntax for declaring array
2629 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002630 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002631
2632 if (type.isStructureContainingArrays())
2633 {
2634 // ESSL 1.00.17 section 6.1 Function Definitions
2635 error(location, "structures containing arrays can't be function return values",
2636 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002637 }
2638 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002639
2640 // Add the function as a prototype after parsing it (we do not support recursion)
2641 return new TFunction(name, new TType(type));
2642}
2643
Jamie Madill06145232015-05-13 13:10:01 -04002644TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002645{
Jamie Madill06145232015-05-13 13:10:01 -04002646 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002647 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002648 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002649 error(publicType.getLine(), "constructor can't be a structure definition",
2650 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002651 }
2652
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002653 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002654 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002655 {
2656 op = EOpConstructStruct;
2657 }
2658 else
2659 {
Geoff Lang156d7192016-07-21 16:11:00 -04002660 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002661 if (op == EOpNull)
2662 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002663 error(publicType.getLine(), "cannot construct this type",
2664 getBasicString(publicType.getBasicType()));
2665 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002666 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002667 }
2668 }
2669
2670 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002671 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002672 return new TFunction(&tempString, type, op);
2673}
2674
Jamie Madillb98c3a82015-07-23 14:26:04 -04002675// This function is used to test for the correctness of the parameters passed to various constructor
2676// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002677//
Olli Etuaho856c4972016-08-08 11:38:39 +03002678// 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 +00002679//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002680TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002681 TOperator op,
2682 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302683 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002684{
Olli Etuaho856c4972016-08-08 11:38:39 +03002685 TType type = fnCall->getReturnType();
2686 if (type.isUnsizedArray())
2687 {
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002688 if (fnCall->getParamCount() == 0)
2689 {
2690 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2691 type.setArraySize(1u);
2692 return TIntermTyped::CreateZero(type);
2693 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002694 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2695 }
2696 bool constType = true;
2697 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2698 {
2699 const TConstParameter &param = fnCall->getParam(i);
2700 if (param.type->getQualifier() != EvqConst)
2701 constType = false;
2702 }
2703 if (constType)
2704 type.setQualifier(EvqConst);
2705
Olli Etuaho8a176262016-08-16 14:23:01 +03002706 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002707 {
2708 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2709 dummyNode->setType(type);
2710 return dummyNode;
2711 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002712 TIntermAggregate *constructor = arguments->getAsAggregate();
2713 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002714
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002715 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002716 constructor->setOp(op);
2717 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002718 ASSERT(constructor->isConstructor());
2719
2720 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002721 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002722
Olli Etuaho21203702014-11-13 16:16:21 +02002723 // Structs should not be precision qualified, the individual members may be.
2724 // Built-in types on the other hand should be precision qualified.
2725 if (op != EOpConstructStruct)
2726 {
2727 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002728 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002729 }
2730
Olli Etuaho856c4972016-08-08 11:38:39 +03002731 constructor->setType(type);
2732
Olli Etuaho77ba4082016-12-16 12:01:18 +00002733 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002734 if (constConstructor)
2735 {
2736 return constConstructor;
2737 }
2738
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002739 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740}
2741
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002742//
2743// Interface/uniform blocks
2744//
Olli Etuaho13389b62016-10-16 11:48:18 +01002745TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002746 const TTypeQualifierBuilder &typeQualifierBuilder,
2747 const TSourceLoc &nameLine,
2748 const TString &blockName,
2749 TFieldList *fieldList,
2750 const TString *instanceName,
2751 const TSourceLoc &instanceLine,
2752 TIntermTyped *arrayIndex,
2753 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002754{
Olli Etuaho856c4972016-08-08 11:38:39 +03002755 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002756
Olli Etuaho77ba4082016-12-16 12:01:18 +00002757 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002758
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002759 if (typeQualifier.qualifier != EvqUniform)
2760 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002761 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
2762 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002763 }
2764
Martin Radev70866b82016-07-22 15:27:42 +03002765 if (typeQualifier.invariant)
2766 {
2767 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2768 }
2769
Martin Radev2cc85b32016-08-05 16:22:53 +03002770 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2771
Jamie Madill099c0f32013-06-20 11:55:52 -04002772 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002773 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002774
Jamie Madill099c0f32013-06-20 11:55:52 -04002775 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2776 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002777 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002778 }
2779
Jamie Madill1566ef72013-06-20 11:55:54 -04002780 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2781 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002782 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002783 }
2784
Olli Etuaho856c4972016-08-08 11:38:39 +03002785 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002786
Martin Radev2cc85b32016-08-05 16:22:53 +03002787 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2788
Arun Patole7e7e68d2015-05-22 12:02:25 +05302789 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2790 if (!symbolTable.declare(blockNameSymbol))
2791 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002792 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002793 }
2794
Jamie Madill98493dd2013-07-08 14:39:03 -04002795 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302796 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2797 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002798 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302799 TType *fieldType = field->type();
2800 if (IsSampler(fieldType->getBasicType()))
2801 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002802 error(field->line(),
2803 "unsupported type - sampler types are not allowed in interface blocks",
2804 fieldType->getBasicString());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002805 }
2806
Martin Radev2cc85b32016-08-05 16:22:53 +03002807 if (IsImage(fieldType->getBasicType()))
2808 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002809 error(field->line(),
2810 "unsupported type - image types are not allowed in interface blocks",
2811 fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002812 }
2813
Jamie Madill98493dd2013-07-08 14:39:03 -04002814 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002815 switch (qualifier)
2816 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002817 case EvqGlobal:
2818 case EvqUniform:
2819 break;
2820 default:
2821 error(field->line(), "invalid qualifier on interface block member",
2822 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002823 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002824 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002825
Martin Radev70866b82016-07-22 15:27:42 +03002826 if (fieldType->isInvariant())
2827 {
2828 error(field->line(), "invalid qualifier on interface block member", "invariant");
2829 }
2830
Jamie Madilla5efff92013-06-06 11:56:47 -04002831 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002832 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002833 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002834
Jamie Madill98493dd2013-07-08 14:39:03 -04002835 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002836 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002837 error(field->line(), "invalid layout qualifier: cannot be used here",
2838 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04002839 }
2840
Jamie Madill98493dd2013-07-08 14:39:03 -04002841 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002842 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002843 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002844 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002845 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002846 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002847 warning(field->line(),
2848 "extraneous layout qualifier: only has an effect on matrix types",
2849 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04002850 }
2851
Jamie Madill98493dd2013-07-08 14:39:03 -04002852 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002853 }
2854
Jamie Madill98493dd2013-07-08 14:39:03 -04002855 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002856 unsigned int arraySize = 0;
2857 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002858 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002859 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002860 }
2861
Jamie Madillb98c3a82015-07-23 14:26:04 -04002862 TInterfaceBlock *interfaceBlock =
2863 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2864 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2865 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002866
2867 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002868 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002869
Jamie Madill98493dd2013-07-08 14:39:03 -04002870 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002871 {
2872 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002873 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2874 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002875 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302876 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002877
2878 // set parent pointer of the field variable
2879 fieldType->setInterfaceBlock(interfaceBlock);
2880
Arun Patole7e7e68d2015-05-22 12:02:25 +05302881 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002882 fieldVariable->setQualifier(typeQualifier.qualifier);
2883
Arun Patole7e7e68d2015-05-22 12:02:25 +05302884 if (!symbolTable.declare(fieldVariable))
2885 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002886 error(field->line(), "redefinition of an interface block member name",
2887 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002888 }
2889 }
2890 }
2891 else
2892 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002893 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002894
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002895 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302896 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002897 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002898
Arun Patole7e7e68d2015-05-22 12:02:25 +05302899 if (!symbolTable.declare(instanceTypeDef))
2900 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002901 error(instanceLine, "redefinition of an interface block instance name",
2902 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002903 }
2904
Jamie Madillb98c3a82015-07-23 14:26:04 -04002905 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002906 symbolName = instanceTypeDef->getName();
2907 }
2908
Olli Etuaho13389b62016-10-16 11:48:18 +01002909 TIntermSymbol *blockSymbol =
2910 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2911 TIntermDeclaration *declaration = new TIntermDeclaration();
2912 declaration->appendDeclarator(blockSymbol);
2913 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002914
2915 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002916 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002917}
2918
Olli Etuaho383b7912016-08-05 11:22:59 +03002919void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002920{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002921 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002922
2923 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00002924 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302925 if (mStructNestingLevel > 1)
2926 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002927 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002928 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002929}
2930
2931void TParseContext::exitStructDeclaration()
2932{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002933 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002934}
2935
Olli Etuaho8a176262016-08-16 14:23:01 +03002936void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002937{
Jamie Madillacb4b812016-11-07 13:50:29 -05002938 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302939 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002940 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002941 }
2942
Arun Patole7e7e68d2015-05-22 12:02:25 +05302943 if (field.type()->getBasicType() != EbtStruct)
2944 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002945 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002946 }
2947
2948 // We're already inside a structure definition at this point, so add
2949 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302950 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2951 {
Jamie Madill41a49272014-03-18 16:10:13 -04002952 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002953 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2954 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002955 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00002956 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03002957 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002958 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002959}
2960
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002961//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002962// Parse an array index expression
2963//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002964TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2965 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302966 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002967{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002968 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2969 {
2970 if (baseExpression->getAsSymbolNode())
2971 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302972 error(location, " left of '[' is not of type array, matrix, or vector ",
2973 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002974 }
2975 else
2976 {
2977 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2978 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002979
2980 TConstantUnion *unionArray = new TConstantUnion[1];
2981 unionArray->setFConst(0.0f);
2982 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2983 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002984 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002985
Jamie Madill21c1e452014-12-29 11:33:41 -05002986 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2987
Olli Etuaho36b05142015-11-12 13:10:42 +02002988 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2989 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2990 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2991 // index is a constant expression.
2992 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2993 {
2994 if (baseExpression->isInterfaceBlock())
2995 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002996 error(location,
2997 "array indexes for interface blocks arrays must be constant integral expressions",
2998 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02002999 }
3000 else if (baseExpression->getQualifier() == EvqFragmentOut)
3001 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003002 error(location,
3003 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003004 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003005 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3006 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003007 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003008 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003009 }
3010
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003011 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003012 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003013 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3014 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3015 // constant fold expressions that are not constant expressions). The most compatible way to
3016 // handle this case is to report a warning instead of an error and force the index to be in
3017 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003018 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003019 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003020
3021 int safeIndex = -1;
3022
3023 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003024 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003025 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003026 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003027 if (mShaderSpec == SH_WEBGL2_SPEC)
3028 {
3029 // Error has been already generated if index is not const.
3030 if (indexExpression->getQualifier() == EvqConst)
3031 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003032 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003033 }
3034 safeIndex = 0;
3035 }
3036 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3037 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003038 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003039 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003040 "GL_EXT_draw_buffers is disabled",
3041 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003042 safeIndex = 0;
3043 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003044 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003045 // Only do generic out-of-range check if similar error hasn't already been reported.
3046 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003047 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003048 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3049 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003050 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003051 }
3052 }
3053 else if (baseExpression->isMatrix())
3054 {
3055 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003056 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003057 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003058 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003059 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003060 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003061 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3062 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003063 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003064 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003065
3066 ASSERT(safeIndex >= 0);
3067 // Data of constant unions can't be changed, because it may be shared with other
3068 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3069 // sanitized object.
3070 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003071 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003072 TConstantUnion *safeConstantUnion = new TConstantUnion();
3073 safeConstantUnion->setIConst(safeIndex);
3074 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003075 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003076
3077 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003078 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003079 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003080 else
3081 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003082 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003083 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003084 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003085}
3086
Olli Etuaho90892fb2016-07-14 14:44:51 +03003087int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3088 const TSourceLoc &location,
3089 int index,
3090 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003091 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003092{
3093 if (index >= arraySize || index < 0)
3094 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003095 std::stringstream reasonStream;
3096 reasonStream << reason << " '" << index << "'";
3097 std::string token = reasonStream.str();
3098 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003099 if (index < 0)
3100 {
3101 return 0;
3102 }
3103 else
3104 {
3105 return arraySize - 1;
3106 }
3107 }
3108 return index;
3109}
3110
Jamie Madillb98c3a82015-07-23 14:26:04 -04003111TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3112 const TSourceLoc &dotLocation,
3113 const TString &fieldString,
3114 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003115{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003116 if (baseExpression->isArray())
3117 {
3118 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003119 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003120 }
3121
3122 if (baseExpression->isVector())
3123 {
3124 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003125 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3126 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003127 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003128 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003129 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003130 }
3131
Olli Etuahob6fa0432016-09-28 16:28:05 +01003132 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003133 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003134 else if (baseExpression->getBasicType() == EbtStruct)
3135 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303136 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003137 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003138 {
3139 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003140 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003141 }
3142 else
3143 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003144 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003145 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003146 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003147 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003148 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003149 {
3150 fieldFound = true;
3151 break;
3152 }
3153 }
3154 if (fieldFound)
3155 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003156 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3157 index->setLine(fieldLocation);
3158 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003159 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003160 }
3161 else
3162 {
3163 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003164 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003165 }
3166 }
3167 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003168 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003169 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303170 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003171 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003172 {
3173 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003174 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003175 }
3176 else
3177 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003178 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003179 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003180 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003181 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003182 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003183 {
3184 fieldFound = true;
3185 break;
3186 }
3187 }
3188 if (fieldFound)
3189 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003190 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3191 index->setLine(fieldLocation);
3192 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003193 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003194 }
3195 else
3196 {
3197 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003198 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003199 }
3200 }
3201 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003202 else
3203 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003204 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003205 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003206 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303207 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003208 }
3209 else
3210 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303211 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003212 " field selection requires structure, vector, or interface block on left hand "
3213 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303214 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003215 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003216 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003217 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003218}
3219
Jamie Madillb98c3a82015-07-23 14:26:04 -04003220TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3221 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003222{
Martin Radev802abe02016-08-04 17:48:32 +03003223 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003224
3225 if (qualifierType == "shared")
3226 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003227 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003228 {
3229 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3230 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003231 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003232 }
3233 else if (qualifierType == "packed")
3234 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003235 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003236 {
3237 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3238 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003239 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003240 }
3241 else if (qualifierType == "std140")
3242 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003243 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003244 }
3245 else if (qualifierType == "row_major")
3246 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003247 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003248 }
3249 else if (qualifierType == "column_major")
3250 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003251 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003252 }
3253 else if (qualifierType == "location")
3254 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003255 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3256 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003257 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003258 else if (qualifierType == "rgba32f")
3259 {
3260 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3261 qualifier.imageInternalFormat = EiifRGBA32F;
3262 }
3263 else if (qualifierType == "rgba16f")
3264 {
3265 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3266 qualifier.imageInternalFormat = EiifRGBA16F;
3267 }
3268 else if (qualifierType == "r32f")
3269 {
3270 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3271 qualifier.imageInternalFormat = EiifR32F;
3272 }
3273 else if (qualifierType == "rgba8")
3274 {
3275 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3276 qualifier.imageInternalFormat = EiifRGBA8;
3277 }
3278 else if (qualifierType == "rgba8_snorm")
3279 {
3280 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3281 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3282 }
3283 else if (qualifierType == "rgba32i")
3284 {
3285 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3286 qualifier.imageInternalFormat = EiifRGBA32I;
3287 }
3288 else if (qualifierType == "rgba16i")
3289 {
3290 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3291 qualifier.imageInternalFormat = EiifRGBA16I;
3292 }
3293 else if (qualifierType == "rgba8i")
3294 {
3295 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3296 qualifier.imageInternalFormat = EiifRGBA8I;
3297 }
3298 else if (qualifierType == "r32i")
3299 {
3300 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3301 qualifier.imageInternalFormat = EiifR32I;
3302 }
3303 else if (qualifierType == "rgba32ui")
3304 {
3305 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3306 qualifier.imageInternalFormat = EiifRGBA32UI;
3307 }
3308 else if (qualifierType == "rgba16ui")
3309 {
3310 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3311 qualifier.imageInternalFormat = EiifRGBA16UI;
3312 }
3313 else if (qualifierType == "rgba8ui")
3314 {
3315 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3316 qualifier.imageInternalFormat = EiifRGBA8UI;
3317 }
3318 else if (qualifierType == "r32ui")
3319 {
3320 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3321 qualifier.imageInternalFormat = EiifR32UI;
3322 }
3323
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003324 else
3325 {
3326 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003327 }
3328
Jamie Madilla5efff92013-06-06 11:56:47 -04003329 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003330}
3331
Martin Radev802abe02016-08-04 17:48:32 +03003332void TParseContext::parseLocalSize(const TString &qualifierType,
3333 const TSourceLoc &qualifierTypeLine,
3334 int intValue,
3335 const TSourceLoc &intValueLine,
3336 const std::string &intValueString,
3337 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003338 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003339{
Olli Etuaho856c4972016-08-08 11:38:39 +03003340 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003341 if (intValue < 1)
3342 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003343 std::stringstream reasonStream;
3344 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3345 std::string reason = reasonStream.str();
3346 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003347 }
3348 (*localSize)[index] = intValue;
3349}
3350
Jamie Madillb98c3a82015-07-23 14:26:04 -04003351TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3352 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003353 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303354 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003355{
Martin Radev802abe02016-08-04 17:48:32 +03003356 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003357
Martin Radev802abe02016-08-04 17:48:32 +03003358 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003359
Martin Radev802abe02016-08-04 17:48:32 +03003360 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003361 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003362 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003363 if (intValue < 0)
3364 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003365 error(intValueLine, "out of range: location must be non-negative",
3366 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003367 }
3368 else
3369 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003370 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003371 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003372 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003373 }
Martin Radev802abe02016-08-04 17:48:32 +03003374 else if (qualifierType == "local_size_x")
3375 {
3376 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3377 &qualifier.localSize);
3378 }
3379 else if (qualifierType == "local_size_y")
3380 {
3381 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3382 &qualifier.localSize);
3383 }
3384 else if (qualifierType == "local_size_z")
3385 {
3386 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3387 &qualifier.localSize);
3388 }
3389 else
3390 {
3391 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003392 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003393
Jamie Madilla5efff92013-06-06 11:56:47 -04003394 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003395}
3396
Olli Etuaho613b9592016-09-05 12:05:53 +03003397TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3398{
3399 return new TTypeQualifierBuilder(
3400 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3401 mShaderVersion);
3402}
3403
Jamie Madillb98c3a82015-07-23 14:26:04 -04003404TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003405 TLayoutQualifier rightQualifier,
3406 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003407{
Martin Radevc28888b2016-07-22 15:27:42 +03003408 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003409 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003410}
3411
Olli Etuaho4de340a2016-12-16 09:32:03 +00003412TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3413 const TFieldList *newlyAddedFields,
3414 const TSourceLoc &location)
3415{
3416 for (TField *field : *newlyAddedFields)
3417 {
3418 for (TField *oldField : *processedFields)
3419 {
3420 if (oldField->name() == field->name())
3421 {
3422 error(location, "duplicate field name in structure", field->name().c_str());
3423 }
3424 }
3425 processedFields->push_back(field);
3426 }
3427 return processedFields;
3428}
3429
Martin Radev70866b82016-07-22 15:27:42 +03003430TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3431 const TTypeQualifierBuilder &typeQualifierBuilder,
3432 TPublicType *typeSpecifier,
3433 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003434{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003435 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003436
Martin Radev70866b82016-07-22 15:27:42 +03003437 typeSpecifier->qualifier = typeQualifier.qualifier;
3438 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003439 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003440 typeSpecifier->invariant = typeQualifier.invariant;
3441 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303442 {
Martin Radev70866b82016-07-22 15:27:42 +03003443 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003444 }
Martin Radev70866b82016-07-22 15:27:42 +03003445 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003446}
3447
Jamie Madillb98c3a82015-07-23 14:26:04 -04003448TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3449 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003450{
Martin Radev4a9cd802016-09-01 16:51:51 +03003451 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3452 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003453
Martin Radev4a9cd802016-09-01 16:51:51 +03003454 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003455
Martin Radev4a9cd802016-09-01 16:51:51 +03003456 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003457
Arun Patole7e7e68d2015-05-22 12:02:25 +05303458 for (unsigned int i = 0; i < fieldList->size(); ++i)
3459 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003460 //
3461 // Careful not to replace already known aspects of type, like array-ness
3462 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303463 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003464 type->setBasicType(typeSpecifier.getBasicType());
3465 type->setPrimarySize(typeSpecifier.getPrimarySize());
3466 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003467 type->setPrecision(typeSpecifier.precision);
3468 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003469 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003470 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003471 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003472
3473 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303474 if (type->isArray())
3475 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003476 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003477 }
3478 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003479 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003480 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303481 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003482 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003483 }
3484
Martin Radev4a9cd802016-09-01 16:51:51 +03003485 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003486 }
3487
Jamie Madill98493dd2013-07-08 14:39:03 -04003488 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003489}
3490
Martin Radev4a9cd802016-09-01 16:51:51 +03003491TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3492 const TSourceLoc &nameLine,
3493 const TString *structName,
3494 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003495{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303496 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003497 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003498
Jamie Madill9b820842015-02-12 10:40:10 -05003499 // Store a bool in the struct if we're at global scope, to allow us to
3500 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003501 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003502
Jamie Madill98493dd2013-07-08 14:39:03 -04003503 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003504 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003505 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303506 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3507 if (!symbolTable.declare(userTypeDef))
3508 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003509 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003510 }
3511 }
3512
3513 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003514 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003515 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003516 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003517 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003518 switch (qualifier)
3519 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003520 case EvqGlobal:
3521 case EvqTemporary:
3522 break;
3523 default:
3524 error(field.line(), "invalid qualifier on struct member",
3525 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003526 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003527 }
Martin Radev70866b82016-07-22 15:27:42 +03003528 if (field.type()->isInvariant())
3529 {
3530 error(field.line(), "invalid qualifier on struct member", "invariant");
3531 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003532 if (IsImage(field.type()->getBasicType()))
3533 {
3534 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3535 }
3536
3537 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003538
3539 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003540 }
3541
Martin Radev4a9cd802016-09-01 16:51:51 +03003542 TTypeSpecifierNonArray typeSpecifierNonArray;
3543 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3544 typeSpecifierNonArray.userDef = structureType;
3545 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003546 exitStructDeclaration();
3547
Martin Radev4a9cd802016-09-01 16:51:51 +03003548 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003549}
3550
Jamie Madillb98c3a82015-07-23 14:26:04 -04003551TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003552 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003553 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003554{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003555 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003556 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003557 init->isVector())
3558 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003559 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3560 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003561 return nullptr;
3562 }
3563
Olli Etuahoac5274d2015-02-20 10:19:08 +02003564 if (statementList)
3565 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00003566 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02003567 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003568 return nullptr;
3569 }
3570 }
3571
Olli Etuahoa3a36662015-02-17 13:46:51 +02003572 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3573 if (node == nullptr)
3574 {
3575 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003576 return nullptr;
3577 }
3578 return node;
3579}
3580
3581TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3582{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003583 if (mSwitchNestingLevel == 0)
3584 {
3585 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003586 return nullptr;
3587 }
3588 if (condition == nullptr)
3589 {
3590 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003591 return nullptr;
3592 }
3593 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003594 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003595 {
3596 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003597 }
3598 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003599 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3600 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3601 // fold in case labels.
3602 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003603 {
3604 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003605 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003606 TIntermCase *node = intermediate.addCase(condition, loc);
3607 if (node == nullptr)
3608 {
3609 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003610 return nullptr;
3611 }
3612 return node;
3613}
3614
3615TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3616{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003617 if (mSwitchNestingLevel == 0)
3618 {
3619 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003620 return nullptr;
3621 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003622 TIntermCase *node = intermediate.addCase(nullptr, loc);
3623 if (node == nullptr)
3624 {
3625 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003626 return nullptr;
3627 }
3628 return node;
3629}
3630
Jamie Madillb98c3a82015-07-23 14:26:04 -04003631TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3632 TIntermTyped *child,
3633 const TSourceLoc &loc,
3634 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003635{
3636 if (child == nullptr)
3637 {
3638 return nullptr;
3639 }
3640
3641 switch (op)
3642 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003643 case EOpLogicalNot:
3644 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3645 child->isVector())
3646 {
3647 return nullptr;
3648 }
3649 break;
3650 case EOpBitwiseNot:
3651 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3652 child->isMatrix() || child->isArray())
3653 {
3654 return nullptr;
3655 }
3656 break;
3657 case EOpPostIncrement:
3658 case EOpPreIncrement:
3659 case EOpPostDecrement:
3660 case EOpPreDecrement:
3661 case EOpNegative:
3662 case EOpPositive:
3663 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003664 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003665 {
3666 return nullptr;
3667 }
3668 // Operators for built-ins are already type checked against their prototype.
3669 default:
3670 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003671 }
3672
Olli Etuahof119a262016-08-19 15:54:22 +03003673 TIntermUnary *node = new TIntermUnary(op, child);
3674 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003675
Olli Etuaho77ba4082016-12-16 12:01:18 +00003676 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03003677 if (foldedNode)
3678 return foldedNode;
3679
3680 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003681}
3682
Olli Etuaho09b22472015-02-11 11:47:26 +02003683TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3684{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003685 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003686 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003687 {
3688 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003689 return child;
3690 }
3691 return node;
3692}
3693
Jamie Madillb98c3a82015-07-23 14:26:04 -04003694TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3695 TIntermTyped *child,
3696 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003697{
Olli Etuaho856c4972016-08-08 11:38:39 +03003698 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003699 return addUnaryMath(op, child, loc);
3700}
3701
Jamie Madillb98c3a82015-07-23 14:26:04 -04003702bool TParseContext::binaryOpCommonCheck(TOperator op,
3703 TIntermTyped *left,
3704 TIntermTyped *right,
3705 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003706{
Olli Etuaho244be012016-08-18 15:26:02 +03003707 if (left->getType().getStruct() || right->getType().getStruct())
3708 {
3709 switch (op)
3710 {
3711 case EOpIndexDirectStruct:
3712 ASSERT(left->getType().getStruct());
3713 break;
3714 case EOpEqual:
3715 case EOpNotEqual:
3716 case EOpAssign:
3717 case EOpInitialize:
3718 if (left->getType() != right->getType())
3719 {
3720 return false;
3721 }
3722 break;
3723 default:
3724 error(loc, "Invalid operation for structs", GetOperatorString(op));
3725 return false;
3726 }
3727 }
3728
Olli Etuahod6b14282015-03-17 14:31:35 +02003729 if (left->isArray() || right->isArray())
3730 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003731 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003732 {
3733 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3734 return false;
3735 }
3736
3737 if (left->isArray() != right->isArray())
3738 {
3739 error(loc, "array / non-array mismatch", GetOperatorString(op));
3740 return false;
3741 }
3742
3743 switch (op)
3744 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003745 case EOpEqual:
3746 case EOpNotEqual:
3747 case EOpAssign:
3748 case EOpInitialize:
3749 break;
3750 default:
3751 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3752 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003753 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003754 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003755 if (left->getArraySize() != right->getArraySize())
3756 {
3757 error(loc, "array size mismatch", GetOperatorString(op));
3758 return false;
3759 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003760 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003761
3762 // Check ops which require integer / ivec parameters
3763 bool isBitShift = false;
3764 switch (op)
3765 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003766 case EOpBitShiftLeft:
3767 case EOpBitShiftRight:
3768 case EOpBitShiftLeftAssign:
3769 case EOpBitShiftRightAssign:
3770 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3771 // check that the basic type is an integer type.
3772 isBitShift = true;
3773 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3774 {
3775 return false;
3776 }
3777 break;
3778 case EOpBitwiseAnd:
3779 case EOpBitwiseXor:
3780 case EOpBitwiseOr:
3781 case EOpBitwiseAndAssign:
3782 case EOpBitwiseXorAssign:
3783 case EOpBitwiseOrAssign:
3784 // It is enough to check the type of only one operand, since later it
3785 // is checked that the operand types match.
3786 if (!IsInteger(left->getBasicType()))
3787 {
3788 return false;
3789 }
3790 break;
3791 default:
3792 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003793 }
3794
3795 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3796 // So the basic type should usually match.
3797 if (!isBitShift && left->getBasicType() != right->getBasicType())
3798 {
3799 return false;
3800 }
3801
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003802 // Check that:
3803 // 1. Type sizes match exactly on ops that require that.
3804 // 2. Restrictions for structs that contain arrays or samplers are respected.
3805 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003806 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003807 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003808 case EOpAssign:
3809 case EOpInitialize:
3810 case EOpEqual:
3811 case EOpNotEqual:
3812 // ESSL 1.00 sections 5.7, 5.8, 5.9
3813 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3814 {
3815 error(loc, "undefined operation for structs containing arrays",
3816 GetOperatorString(op));
3817 return false;
3818 }
3819 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3820 // we interpret the spec so that this extends to structs containing samplers,
3821 // similarly to ESSL 1.00 spec.
3822 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3823 left->getType().isStructureContainingSamplers())
3824 {
3825 error(loc, "undefined operation for structs containing samplers",
3826 GetOperatorString(op));
3827 return false;
3828 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003829
3830 if ((op == EOpAssign || op == EOpInitialize) &&
3831 left->getType().isStructureContainingImages())
3832 {
3833 error(loc, "undefined operation for structs containing images",
3834 GetOperatorString(op));
3835 return false;
3836 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003837 case EOpLessThan:
3838 case EOpGreaterThan:
3839 case EOpLessThanEqual:
3840 case EOpGreaterThanEqual:
3841 if ((left->getNominalSize() != right->getNominalSize()) ||
3842 (left->getSecondarySize() != right->getSecondarySize()))
3843 {
3844 return false;
3845 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003846 break;
3847 case EOpAdd:
3848 case EOpSub:
3849 case EOpDiv:
3850 case EOpIMod:
3851 case EOpBitShiftLeft:
3852 case EOpBitShiftRight:
3853 case EOpBitwiseAnd:
3854 case EOpBitwiseXor:
3855 case EOpBitwiseOr:
3856 case EOpAddAssign:
3857 case EOpSubAssign:
3858 case EOpDivAssign:
3859 case EOpIModAssign:
3860 case EOpBitShiftLeftAssign:
3861 case EOpBitShiftRightAssign:
3862 case EOpBitwiseAndAssign:
3863 case EOpBitwiseXorAssign:
3864 case EOpBitwiseOrAssign:
3865 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3866 {
3867 return false;
3868 }
3869
3870 // Are the sizes compatible?
3871 if (left->getNominalSize() != right->getNominalSize() ||
3872 left->getSecondarySize() != right->getSecondarySize())
3873 {
3874 // If the nominal sizes of operands do not match:
3875 // One of them must be a scalar.
3876 if (!left->isScalar() && !right->isScalar())
3877 return false;
3878
3879 // In the case of compound assignment other than multiply-assign,
3880 // the right side needs to be a scalar. Otherwise a vector/matrix
3881 // would be assigned to a scalar. A scalar can't be shifted by a
3882 // vector either.
3883 if (!right->isScalar() &&
3884 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3885 return false;
3886 }
3887 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003888 default:
3889 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003890 }
3891
Olli Etuahod6b14282015-03-17 14:31:35 +02003892 return true;
3893}
3894
Olli Etuaho1dded802016-08-18 18:13:13 +03003895bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3896 const TType &left,
3897 const TType &right)
3898{
3899 switch (op)
3900 {
3901 case EOpMul:
3902 case EOpMulAssign:
3903 return left.getNominalSize() == right.getNominalSize() &&
3904 left.getSecondarySize() == right.getSecondarySize();
3905 case EOpVectorTimesScalar:
3906 return true;
3907 case EOpVectorTimesScalarAssign:
3908 ASSERT(!left.isMatrix() && !right.isMatrix());
3909 return left.isVector() && !right.isVector();
3910 case EOpVectorTimesMatrix:
3911 return left.getNominalSize() == right.getRows();
3912 case EOpVectorTimesMatrixAssign:
3913 ASSERT(!left.isMatrix() && right.isMatrix());
3914 return left.isVector() && left.getNominalSize() == right.getRows() &&
3915 left.getNominalSize() == right.getCols();
3916 case EOpMatrixTimesVector:
3917 return left.getCols() == right.getNominalSize();
3918 case EOpMatrixTimesScalar:
3919 return true;
3920 case EOpMatrixTimesScalarAssign:
3921 ASSERT(left.isMatrix() && !right.isMatrix());
3922 return !right.isVector();
3923 case EOpMatrixTimesMatrix:
3924 return left.getCols() == right.getRows();
3925 case EOpMatrixTimesMatrixAssign:
3926 ASSERT(left.isMatrix() && right.isMatrix());
3927 // We need to check two things:
3928 // 1. The matrix multiplication step is valid.
3929 // 2. The result will have the same number of columns as the lvalue.
3930 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3931
3932 default:
3933 UNREACHABLE();
3934 return false;
3935 }
3936}
3937
Jamie Madillb98c3a82015-07-23 14:26:04 -04003938TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3939 TIntermTyped *left,
3940 TIntermTyped *right,
3941 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003942{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003943 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003944 return nullptr;
3945
Olli Etuahofc1806e2015-03-17 13:03:11 +02003946 switch (op)
3947 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003948 case EOpEqual:
3949 case EOpNotEqual:
3950 break;
3951 case EOpLessThan:
3952 case EOpGreaterThan:
3953 case EOpLessThanEqual:
3954 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003955 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3956 !right->getType().getStruct());
3957 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003958 {
3959 return nullptr;
3960 }
3961 break;
3962 case EOpLogicalOr:
3963 case EOpLogicalXor:
3964 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003965 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3966 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003967 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003968 {
3969 return nullptr;
3970 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003971 // Basic types matching should have been already checked.
3972 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003973 break;
3974 case EOpAdd:
3975 case EOpSub:
3976 case EOpDiv:
3977 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003978 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3979 !right->getType().getStruct());
3980 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003981 {
3982 return nullptr;
3983 }
3984 break;
3985 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003986 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3987 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003988 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003989 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003990 {
3991 return nullptr;
3992 }
3993 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003994 default:
3995 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003996 }
3997
Olli Etuaho1dded802016-08-18 18:13:13 +03003998 if (op == EOpMul)
3999 {
4000 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4001 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4002 {
4003 return nullptr;
4004 }
4005 }
4006
Olli Etuaho3fdec912016-08-18 15:08:06 +03004007 TIntermBinary *node = new TIntermBinary(op, left, right);
4008 node->setLine(loc);
4009
Olli Etuaho3fdec912016-08-18 15:08:06 +03004010 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004011 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004012 if (foldedNode)
4013 return foldedNode;
4014
4015 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004016}
4017
Jamie Madillb98c3a82015-07-23 14:26:04 -04004018TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4019 TIntermTyped *left,
4020 TIntermTyped *right,
4021 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004022{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004023 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004024 if (node == 0)
4025 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004026 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4027 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004028 return left;
4029 }
4030 return node;
4031}
4032
Jamie Madillb98c3a82015-07-23 14:26:04 -04004033TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4034 TIntermTyped *left,
4035 TIntermTyped *right,
4036 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004037{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004038 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004039 if (node == 0)
4040 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004041 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4042 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004043 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004044 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004045 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4046 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004047 }
4048 return node;
4049}
4050
Olli Etuaho13389b62016-10-16 11:48:18 +01004051TIntermBinary *TParseContext::createAssign(TOperator op,
4052 TIntermTyped *left,
4053 TIntermTyped *right,
4054 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004055{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004056 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004057 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004058 if (op == EOpMulAssign)
4059 {
4060 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4061 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4062 {
4063 return nullptr;
4064 }
4065 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004066 TIntermBinary *node = new TIntermBinary(op, left, right);
4067 node->setLine(loc);
4068
Olli Etuaho3fdec912016-08-18 15:08:06 +03004069 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004070 }
4071 return nullptr;
4072}
4073
Jamie Madillb98c3a82015-07-23 14:26:04 -04004074TIntermTyped *TParseContext::addAssign(TOperator op,
4075 TIntermTyped *left,
4076 TIntermTyped *right,
4077 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004078{
4079 TIntermTyped *node = createAssign(op, left, right, loc);
4080 if (node == nullptr)
4081 {
4082 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004083 return left;
4084 }
4085 return node;
4086}
4087
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004088TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4089 TIntermTyped *right,
4090 const TSourceLoc &loc)
4091{
Corentin Wallez0d959252016-07-12 17:26:32 -04004092 // WebGL2 section 5.26, the following results in an error:
4093 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004094 if (mShaderSpec == SH_WEBGL2_SPEC &&
4095 (left->isArray() || left->getBasicType() == EbtVoid ||
4096 left->getType().isStructureContainingArrays() || right->isArray() ||
4097 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004098 {
4099 error(loc,
4100 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4101 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004102 }
4103
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004104 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004105}
4106
Olli Etuaho49300862015-02-20 14:54:49 +02004107TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4108{
4109 switch (op)
4110 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004111 case EOpContinue:
4112 if (mLoopNestingLevel <= 0)
4113 {
4114 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004115 }
4116 break;
4117 case EOpBreak:
4118 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4119 {
4120 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004121 }
4122 break;
4123 case EOpReturn:
4124 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4125 {
4126 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004127 }
4128 break;
4129 default:
4130 // No checks for discard
4131 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004132 }
4133 return intermediate.addBranch(op, loc);
4134}
4135
Jamie Madillb98c3a82015-07-23 14:26:04 -04004136TIntermBranch *TParseContext::addBranch(TOperator op,
4137 TIntermTyped *returnValue,
4138 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004139{
4140 ASSERT(op == EOpReturn);
4141 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004142 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004143 {
4144 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004145 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004146 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004147 {
4148 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004149 }
4150 return intermediate.addBranch(op, returnValue, loc);
4151}
4152
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004153void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4154{
4155 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004156 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004157 TIntermNode *offset = nullptr;
4158 TIntermSequence *arguments = functionCall->getSequence();
4159 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4160 name.compare(0, 16, "textureLodOffset") == 0 ||
4161 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4162 name.compare(0, 17, "textureGradOffset") == 0 ||
4163 name.compare(0, 21, "textureProjGradOffset") == 0)
4164 {
4165 offset = arguments->back();
4166 }
4167 else if (name.compare(0, 13, "textureOffset") == 0 ||
4168 name.compare(0, 17, "textureProjOffset") == 0)
4169 {
4170 // A bias parameter might follow the offset parameter.
4171 ASSERT(arguments->size() >= 3);
4172 offset = (*arguments)[2];
4173 }
4174 if (offset != nullptr)
4175 {
4176 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4177 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4178 {
4179 TString unmangledName = TFunction::unmangleName(name);
4180 error(functionCall->getLine(), "Texture offset must be a constant expression",
4181 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004182 }
4183 else
4184 {
4185 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4186 size_t size = offsetConstantUnion->getType().getObjectSize();
4187 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4188 for (size_t i = 0u; i < size; ++i)
4189 {
4190 int offsetValue = values[i].getIConst();
4191 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4192 {
4193 std::stringstream tokenStream;
4194 tokenStream << offsetValue;
4195 std::string token = tokenStream.str();
4196 error(offset->getLine(), "Texture offset value out of valid range",
4197 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004198 }
4199 }
4200 }
4201 }
4202}
4203
Martin Radev2cc85b32016-08-05 16:22:53 +03004204// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4205void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4206{
4207 ASSERT(!functionCall->isUserDefined());
4208 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4209
4210 if (name.compare(0, 5, "image") == 0)
4211 {
4212 TIntermSequence *arguments = functionCall->getSequence();
4213 TIntermNode *imageNode = (*arguments)[0];
4214 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4215
4216 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4217
4218 if (name.compare(5, 5, "Store") == 0)
4219 {
4220 if (memoryQualifier.readonly)
4221 {
4222 error(imageNode->getLine(),
4223 "'imageStore' cannot be used with images qualified as 'readonly'",
4224 imageSymbol->getSymbol().c_str());
4225 }
4226 }
4227 else if (name.compare(5, 4, "Load") == 0)
4228 {
4229 if (memoryQualifier.writeonly)
4230 {
4231 error(imageNode->getLine(),
4232 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4233 imageSymbol->getSymbol().c_str());
4234 }
4235 }
4236 }
4237}
4238
4239// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4240void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4241 const TFunction *functionDefinition,
4242 const TIntermAggregate *functionCall)
4243{
4244 ASSERT(functionCall->isUserDefined());
4245
4246 const TIntermSequence &arguments = *functionCall->getSequence();
4247
4248 ASSERT(functionDefinition->getParamCount() == arguments.size());
4249
4250 for (size_t i = 0; i < arguments.size(); ++i)
4251 {
4252 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4253 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4254 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4255
4256 if (IsImage(functionArgumentType.getBasicType()))
4257 {
4258 const TMemoryQualifier &functionArgumentMemoryQualifier =
4259 functionArgumentType.getMemoryQualifier();
4260 const TMemoryQualifier &functionParameterMemoryQualifier =
4261 functionParameterType.getMemoryQualifier();
4262 if (functionArgumentMemoryQualifier.readonly &&
4263 !functionParameterMemoryQualifier.readonly)
4264 {
4265 error(functionCall->getLine(),
4266 "Function call discards the 'readonly' qualifier from image",
4267 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4268 }
4269
4270 if (functionArgumentMemoryQualifier.writeonly &&
4271 !functionParameterMemoryQualifier.writeonly)
4272 {
4273 error(functionCall->getLine(),
4274 "Function call discards the 'writeonly' qualifier from image",
4275 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4276 }
Martin Radev049edfa2016-11-11 14:35:37 +02004277
4278 if (functionArgumentMemoryQualifier.coherent &&
4279 !functionParameterMemoryQualifier.coherent)
4280 {
4281 error(functionCall->getLine(),
4282 "Function call discards the 'coherent' qualifier from image",
4283 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4284 }
4285
4286 if (functionArgumentMemoryQualifier.volatileQualifier &&
4287 !functionParameterMemoryQualifier.volatileQualifier)
4288 {
4289 error(functionCall->getLine(),
4290 "Function call discards the 'volatile' qualifier from image",
4291 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4292 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004293 }
4294 }
4295}
4296
Jamie Madillb98c3a82015-07-23 14:26:04 -04004297TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4298 TIntermNode *paramNode,
4299 TIntermNode *thisNode,
4300 const TSourceLoc &loc,
4301 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004302{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004303 *fatalError = false;
4304 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004305 TIntermTyped *callNode = nullptr;
4306
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004307 if (thisNode != nullptr)
4308 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004309 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004310 int arraySize = 0;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004311 TIntermTyped *typedThis = thisNode->getAsTyped();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004312 if (fnCall->getName() != "length")
4313 {
4314 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004315 }
4316 else if (paramNode != nullptr)
4317 {
4318 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004319 }
4320 else if (typedThis == nullptr || !typedThis->isArray())
4321 {
4322 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004323 }
4324 else
4325 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004326 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004327 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004328 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004329 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004330 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004331 // (func()).length()
4332 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004333 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4334 // expression.
4335 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4336 // spec section 5.9 which allows "An array, vector or matrix expression with the
4337 // length method applied".
4338 error(loc, "length can only be called on array names, not on array expressions",
4339 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004340 }
4341 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004342 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004343 callNode =
4344 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004345 }
4346 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004347 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004348 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004349 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004350 }
4351 else
4352 {
4353 //
4354 // Not a constructor. Find it in the symbol table.
4355 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304356 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004357 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004358 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004359 if (fnCandidate)
4360 {
4361 //
4362 // A declared function.
4363 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004364 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004365 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004366 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004367 }
4368 op = fnCandidate->getBuiltInOp();
4369 if (builtIn && op != EOpNull)
4370 {
4371 //
4372 // A function call mapped to a built-in operation.
4373 //
4374 if (fnCandidate->getParamCount() == 1)
4375 {
4376 //
4377 // Treat it like a built-in unary operator.
4378 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004379 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4380 paramNode = paramAgg->getSequence()->front();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004381 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004382 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004383 if (callNode == nullptr)
4384 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004385 std::stringstream reasonStream;
4386 reasonStream << "wrong operand type for built in unary function: "
4387 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
4388 std::string reason = reasonStream.str();
4389 error(paramNode->getLine(), reason.c_str(), "Internal Error");
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004390 *fatalError = true;
4391 return nullptr;
4392 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004393 }
4394 else
4395 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004396 TIntermAggregate *aggregate =
4397 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004398 aggregate->setType(fnCandidate->getReturnType());
4399 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004400 if (aggregate->areChildrenConstQualified())
4401 {
4402 aggregate->getTypePointer()->setQualifier(EvqConst);
4403 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004404
4405 // Some built-in functions have out parameters too.
4406 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304407
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004408 // See if we can constant fold a built-in. Note that this may be possible even
4409 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004410 TIntermTyped *foldedNode =
Olli Etuaho77ba4082016-12-16 12:01:18 +00004411 intermediate.foldAggregateBuiltIn(aggregate, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304412 if (foldedNode)
4413 {
Arun Patole274f0702015-05-05 13:33:30 +05304414 callNode = foldedNode;
4415 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004416 else
4417 {
4418 callNode = aggregate;
4419 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004420 }
4421 }
4422 else
4423 {
4424 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004425 TIntermAggregate *aggregate =
4426 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004427 aggregate->setType(fnCandidate->getReturnType());
4428
Jamie Madillb98c3a82015-07-23 14:26:04 -04004429 // this is how we know whether the given function is a builtIn function or a user
4430 // defined function
4431 // if builtIn == false, it's a userDefined -> could be an overloaded
4432 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004433 // if builtIn == true, it's definitely a builtIn function with EOpNull
4434 if (!builtIn)
4435 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004436 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004437
Olli Etuahobd674552016-10-06 13:28:42 +01004438 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004439 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004440 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004441 aggregate->setBuiltInFunctionPrecision();
4442
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004443 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004444
4445 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4446 }
4447 else
4448 {
4449 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004450 }
4451
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004452 callNode = aggregate;
4453
4454 functionCallLValueErrorCheck(fnCandidate, aggregate);
4455 }
4456 }
4457 else
4458 {
4459 // error message was put out by findFunction()
4460 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004461 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004462 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004463 callNode = intermediate.addConstantUnion(unionArray,
4464 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004465 }
4466 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004467 return callNode;
4468}
4469
Jamie Madillb98c3a82015-07-23 14:26:04 -04004470TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004471 TIntermTyped *trueExpression,
4472 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004473 const TSourceLoc &loc)
4474{
Olli Etuaho856c4972016-08-08 11:38:39 +03004475 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004476
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004477 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004478 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004479 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4480 falseExpression->getCompleteString());
4481 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004482 }
Olli Etuahode318b22016-10-25 16:18:25 +01004483 if (IsOpaqueType(trueExpression->getBasicType()))
4484 {
4485 // ESSL 1.00 section 4.1.7
4486 // ESSL 3.00 section 4.1.7
4487 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4488 // Note that structs containing opaque types don't need to be checked as structs are
4489 // forbidden below.
4490 error(loc, "ternary operator is not allowed for opaque types", ":");
4491 return falseExpression;
4492 }
4493
Olli Etuahoa2d53032015-04-15 14:14:44 +03004494 // ESSL1 sections 5.2 and 5.7:
4495 // ESSL3 section 5.7:
4496 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004497 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004498 {
4499 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004500 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004501 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004502 // WebGL2 section 5.26, the following results in an error:
4503 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004504 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004505 {
4506 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004507 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004508 }
4509
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004510 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004511}
Olli Etuaho49300862015-02-20 14:54:49 +02004512
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004513//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004514// Parse an array of strings using yyparse.
4515//
4516// Returns 0 for success.
4517//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004518int PaParseStrings(size_t count,
4519 const char *const string[],
4520 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304521 TParseContext *context)
4522{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004523 if ((count == 0) || (string == NULL))
4524 return 1;
4525
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004526 if (glslang_initialize(context))
4527 return 1;
4528
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004529 int error = glslang_scan(count, string, length, context);
4530 if (!error)
4531 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004532
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004533 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004534
alokp@chromium.org6b495712012-06-29 00:06:58 +00004535 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004536}
Jamie Madill45bcc782016-11-07 13:58:48 -05004537
4538} // namespace sh