blob: 4ca1e53a5764b34fd0a81f981fb12e1be1e1f342 [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,
77 TInfoSink &is,
78 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),
96 mDiagnostics(is),
97 mDirectiveHandler(ext,
98 mDiagnostics,
99 mShaderVersion,
100 mShaderType,
101 resources.WEBGL_debug_shader_precision == 1),
102 mPreprocessor(&mDiagnostics, &mDirectiveHandler),
103 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//
227// Used by flex/bison to output all syntax and parsing errors.
228//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530229void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400230 const char *reason,
231 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530232 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300234 mDiagnostics.error(loc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235}
236
Arun Patole7e7e68d2015-05-22 12:02:25 +0530237void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400238 const char *reason,
239 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530240 const char *extraInfo)
241{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300242 mDiagnostics.warning(loc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000243}
244
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200245void TParseContext::outOfRangeError(bool isError,
246 const TSourceLoc &loc,
247 const char *reason,
248 const char *token,
249 const char *extraInfo)
250{
251 if (isError)
252 {
253 error(loc, reason, token, extraInfo);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200254 }
255 else
256 {
257 warning(loc, reason, token, extraInfo);
258 }
259}
260
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261//
262// Same error message for all places assignments don't work.
263//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530264void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000266 std::stringstream extraInfoStream;
267 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
268 std::string extraInfo = extraInfoStream.str();
269 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270}
271
272//
273// Same error message for all places unary operations don't work.
274//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530275void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000276{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000277 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400278 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
279 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000280 std::string extraInfo = extraInfoStream.str();
281 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282}
283
284//
285// Same error message for all binary operations don't work.
286//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400287void TParseContext::binaryOpError(const TSourceLoc &line,
288 const char *op,
289 TString left,
290 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000292 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400293 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
294 << left << "' and a right operand of type '" << right
295 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000296 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530297 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298}
299
Olli Etuaho856c4972016-08-08 11:38:39 +0300300void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
301 TPrecision precision,
302 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530303{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400304 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300305 return;
Martin Radev70866b82016-07-22 15:27:42 +0300306
307 if (precision != EbpUndefined && !SupportsPrecision(type))
308 {
309 error(line, "illegal type for precision qualifier", getBasicString(type));
310 }
311
Olli Etuaho183d7e22015-11-20 15:59:09 +0200312 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530313 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200314 switch (type)
315 {
316 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400317 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300318 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200319 case EbtInt:
320 case EbtUInt:
321 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400322 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300323 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200324 default:
325 if (IsSampler(type))
326 {
327 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300328 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200329 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300330 if (IsImage(type))
331 {
332 error(line, "No precision specified (image)", "");
333 return;
334 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200335 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000336 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000337}
338
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339// Both test and if necessary, spit out an error, to see if the node is really
340// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300341bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400343 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530344 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100345 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
346
347 if (swizzleNode)
348 {
349 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
350 if (ok && swizzleNode->hasDuplicateOffsets())
351 {
352 error(line, " l-value of swizzle cannot have duplicate components", op);
353 return false;
354 }
355 return ok;
356 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357
Arun Patole7e7e68d2015-05-22 12:02:25 +0530358 if (binaryNode)
359 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400360 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530361 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400362 case EOpIndexDirect:
363 case EOpIndexIndirect:
364 case EOpIndexDirectStruct:
365 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300366 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400367 default:
368 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000370 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300371 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000372 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373
Arun Patole7e7e68d2015-05-22 12:02:25 +0530374 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000375 if (symNode != 0)
376 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377
Arun Patole7e7e68d2015-05-22 12:02:25 +0530378 const char *message = 0;
379 switch (node->getQualifier())
380 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400381 case EvqConst:
382 message = "can't modify a const";
383 break;
384 case EvqConstReadOnly:
385 message = "can't modify a const";
386 break;
387 case EvqAttribute:
388 message = "can't modify an attribute";
389 break;
390 case EvqFragmentIn:
391 message = "can't modify an input";
392 break;
393 case EvqVertexIn:
394 message = "can't modify an input";
395 break;
396 case EvqUniform:
397 message = "can't modify a uniform";
398 break;
399 case EvqVaryingIn:
400 message = "can't modify a varying";
401 break;
402 case EvqFragCoord:
403 message = "can't modify gl_FragCoord";
404 break;
405 case EvqFrontFacing:
406 message = "can't modify gl_FrontFacing";
407 break;
408 case EvqPointCoord:
409 message = "can't modify gl_PointCoord";
410 break;
Martin Radevb0883602016-08-04 17:48:58 +0300411 case EvqNumWorkGroups:
412 message = "can't modify gl_NumWorkGroups";
413 break;
414 case EvqWorkGroupSize:
415 message = "can't modify gl_WorkGroupSize";
416 break;
417 case EvqWorkGroupID:
418 message = "can't modify gl_WorkGroupID";
419 break;
420 case EvqLocalInvocationID:
421 message = "can't modify gl_LocalInvocationID";
422 break;
423 case EvqGlobalInvocationID:
424 message = "can't modify gl_GlobalInvocationID";
425 break;
426 case EvqLocalInvocationIndex:
427 message = "can't modify gl_LocalInvocationIndex";
428 break;
Martin Radev802abe02016-08-04 17:48:32 +0300429 case EvqComputeIn:
430 message = "can't modify work group size variable";
431 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400432 default:
433 //
434 // Type that can't be written to?
435 //
436 if (node->getBasicType() == EbtVoid)
437 {
438 message = "can't modify void";
439 }
440 if (IsSampler(node->getBasicType()))
441 {
442 message = "can't modify a sampler";
443 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300444 if (IsImage(node->getBasicType()))
445 {
446 message = "can't modify an image";
447 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000448 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449
Arun Patole7e7e68d2015-05-22 12:02:25 +0530450 if (message == 0 && binaryNode == 0 && symNode == 0)
451 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000452 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453
Olli Etuaho8a176262016-08-16 14:23:01 +0300454 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000457 //
458 // Everything else is okay, no error.
459 //
460 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300461 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000462
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000463 //
464 // If we get here, we have an error and a message.
465 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530466 if (symNode)
467 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000468 std::stringstream extraInfoStream;
469 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
470 std::string extraInfo = extraInfoStream.str();
471 error(line, " l-value required", op, extraInfo.c_str());
472 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530473 else
474 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000475 std::stringstream extraInfoStream;
476 extraInfoStream << "(" << message << ")";
477 std::string extraInfo = extraInfoStream.str();
478 error(line, " l-value required", op, extraInfo.c_str());
479 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480
Olli Etuaho8a176262016-08-16 14:23:01 +0300481 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482}
483
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484// Both test, and if necessary spit out an error, to see if the node is really
485// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300486void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000487{
Olli Etuaho383b7912016-08-05 11:22:59 +0300488 if (node->getQualifier() != EvqConst)
489 {
490 error(node->getLine(), "constant expression required", "");
491 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000492}
493
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494// Both test, and if necessary spit out an error, to see if the node is really
495// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300496void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000497{
Olli Etuaho383b7912016-08-05 11:22:59 +0300498 if (!node->isScalarInt())
499 {
500 error(node->getLine(), "integer expression required", token);
501 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502}
503
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000504// Both test, and if necessary spit out an error, to see if we are currently
505// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800506bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507{
Olli Etuaho856c4972016-08-08 11:38:39 +0300508 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300509 {
510 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800511 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300512 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800513 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514}
515
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516// For now, keep it simple: if it starts "gl_", it's reserved, independent
517// of scope. Except, if the symbol table is at the built-in push-level,
518// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000519// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
520// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300521bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530523 static const char *reservedErrMsg = "reserved built-in name";
524 if (!symbolTable.atBuiltInLevel())
525 {
526 if (identifier.compare(0, 3, "gl_") == 0)
527 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000528 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300529 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000530 }
Jamie Madillacb4b812016-11-07 13:50:29 -0500531 if (sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530532 {
533 if (identifier.compare(0, 6, "webgl_") == 0)
534 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000535 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300536 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000537 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530538 if (identifier.compare(0, 7, "_webgl_") == 0)
539 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000540 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300541 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000542 }
543 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530544 if (identifier.find("__") != TString::npos)
545 {
546 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400547 "identifiers containing two consecutive underscores (__) are reserved as "
548 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530549 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300550 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 }
552 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000553
Olli Etuaho8a176262016-08-16 14:23:01 +0300554 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000555}
556
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557// Make sure there is enough data provided to the constructor to build
558// something of the type of the constructor. Also returns the type of
559// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300560bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
561 TIntermNode *argumentsNode,
562 const TFunction &function,
563 TOperator op,
564 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000566 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400567 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530568 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400569 case EOpConstructMat2:
570 case EOpConstructMat2x3:
571 case EOpConstructMat2x4:
572 case EOpConstructMat3x2:
573 case EOpConstructMat3:
574 case EOpConstructMat3x4:
575 case EOpConstructMat4x2:
576 case EOpConstructMat4x3:
577 case EOpConstructMat4:
578 constructingMatrix = true;
579 break;
580 default:
581 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000582 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 //
585 // Note: It's okay to have too many components available, but not okay to have unused
586 // arguments. 'full' will go to true when enough args have been seen. If we loop
587 // again, there is an extra argument, so 'overfull' will become true.
588 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000589
Jamie Madillb98c3a82015-07-23 14:26:04 -0400590 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400591 bool full = false;
592 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 bool matrixInMatrix = false;
594 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530595 for (size_t i = 0; i < function.getParamCount(); ++i)
596 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700597 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000598 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530599
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000600 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 matrixInMatrix = true;
602 if (full)
603 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300604 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000606 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 arrayArg = true;
608 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530609
Olli Etuaho856c4972016-08-08 11:38:39 +0300610 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300611 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300612 // The size of an unsized constructor should already have been determined.
613 ASSERT(!type.isUnsizedArray());
614 if (static_cast<size_t>(type.getArraySize()) != function.getParamCount())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300615 {
616 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300617 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300618 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000619 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620
Arun Patole7e7e68d2015-05-22 12:02:25 +0530621 if (arrayArg && op != EOpConstructStruct)
622 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000623 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300624 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000625 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626
Olli Etuaho856c4972016-08-08 11:38:39 +0300627 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530628 {
629 if (function.getParamCount() != 1)
630 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400631 error(line, "constructing matrix from matrix can only take one argument",
632 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300633 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000634 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000635 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636
Arun Patole7e7e68d2015-05-22 12:02:25 +0530637 if (overFull)
638 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000639 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300640 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000641 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530642
Olli Etuaho856c4972016-08-08 11:38:39 +0300643 if (op == EOpConstructStruct && !type.isArray() &&
644 type.getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530645 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400646 error(line,
647 "Number of constructor parameters does not match the number of structure fields",
648 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300649 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651
Olli Etuaho856c4972016-08-08 11:38:39 +0300652 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530653 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300654 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
655 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000657 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300658 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000659 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000660 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200662 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200664 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300665 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000666 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200667
668 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
669 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530670 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200671 TIntermTyped *argTyped = argNode->getAsTyped();
672 ASSERT(argTyped != nullptr);
673 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
674 {
675 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300676 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200677 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300678 if (op != EOpConstructStruct && IsImage(argTyped->getBasicType()))
679 {
680 error(line, "cannot convert an image", "constructor");
681 return false;
682 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200683 if (argTyped->getBasicType() == EbtVoid)
684 {
685 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300686 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200687 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000688 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000689
Olli Etuaho856c4972016-08-08 11:38:39 +0300690 if (type.isArray())
691 {
692 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
693 // the array.
694 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
695 {
696 const TType &argType = argNode->getAsTyped()->getType();
697 // It has already been checked that the argument is not an array.
698 ASSERT(!argType.isArray());
699 if (!argType.sameElementType(type))
700 {
701 error(line, "Array constructor argument has an incorrect type", "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300702 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300703 }
704 }
705 }
706 else if (op == EOpConstructStruct)
707 {
708 const TFieldList &fields = type.getStruct()->fields();
709 TIntermSequence *args = argumentsAgg->getSequence();
710
711 for (size_t i = 0; i < fields.size(); i++)
712 {
713 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
714 {
715 error(line, "Structure constructor arguments do not match structure fields",
716 "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300717 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300718 }
719 }
720 }
721
Olli Etuaho8a176262016-08-16 14:23:01 +0300722 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723}
724
Jamie Madillb98c3a82015-07-23 14:26:04 -0400725// This function checks to see if a void variable has been declared and raise an error message for
726// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727//
728// returns true in case of an error
729//
Olli Etuaho856c4972016-08-08 11:38:39 +0300730bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400731 const TString &identifier,
732 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000733{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300734 if (type == EbtVoid)
735 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000736 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300737 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300738 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739
Olli Etuaho8a176262016-08-16 14:23:01 +0300740 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741}
742
Jamie Madillb98c3a82015-07-23 14:26:04 -0400743// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300744// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300745void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000746{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530747 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
748 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000749 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530750 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000751}
752
Jamie Madillb98c3a82015-07-23 14:26:04 -0400753// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300754// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300755void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756{
Martin Radev4a9cd802016-09-01 16:51:51 +0300757 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530758 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000759 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761}
762
Olli Etuaho856c4972016-08-08 11:38:39 +0300763bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300764 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400765 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000766{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530767 if (pType.type == EbtStruct)
768 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300769 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530770 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000771 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Olli Etuaho8a176262016-08-16 14:23:01 +0300772 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000773 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530774
Olli Etuaho8a176262016-08-16 14:23:01 +0300775 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530776 }
777 else if (IsSampler(pType.type))
778 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000779 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300780 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000781 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000782
Olli Etuaho8a176262016-08-16 14:23:01 +0300783 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784}
785
Martin Radev2cc85b32016-08-05 16:22:53 +0300786bool TParseContext::checkIsNotImage(const TSourceLoc &line,
787 const TTypeSpecifierNonArray &pType,
788 const char *reason)
789{
790 if (pType.type == EbtStruct)
791 {
792 if (ContainsImage(*pType.userDef))
793 {
794 error(line, reason, getBasicString(pType.type), "(structure contains an image)");
795
796 return false;
797 }
798
799 return true;
800 }
801 else if (IsImage(pType.type))
802 {
803 error(line, reason, getBasicString(pType.type));
804
805 return false;
806 }
807
808 return true;
809}
810
Olli Etuaho856c4972016-08-08 11:38:39 +0300811void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
812 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400813{
814 if (pType.layoutQualifier.location != -1)
815 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400816 error(line, "location must only be specified for a single input or output variable",
817 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400818 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400819}
820
Olli Etuaho856c4972016-08-08 11:38:39 +0300821void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
822 const TLayoutQualifier &layoutQualifier)
823{
824 if (layoutQualifier.location != -1)
825 {
826 error(location, "invalid layout qualifier:", "location",
827 "only valid on program inputs and outputs");
828 }
829}
830
Martin Radev2cc85b32016-08-05 16:22:53 +0300831void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
832 TQualifier qualifier,
833 const TType &type)
834{
835 checkOutParameterIsNotSampler(line, qualifier, type);
836 checkOutParameterIsNotImage(line, qualifier, type);
837}
838
Olli Etuaho856c4972016-08-08 11:38:39 +0300839void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
840 TQualifier qualifier,
841 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842{
Martin Radev2cc85b32016-08-05 16:22:53 +0300843 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
844 if (IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530845 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000846 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000847 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848}
849
Martin Radev2cc85b32016-08-05 16:22:53 +0300850void TParseContext::checkOutParameterIsNotImage(const TSourceLoc &line,
851 TQualifier qualifier,
852 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853{
Martin Radev2cc85b32016-08-05 16:22:53 +0300854 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
855 if (IsImage(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530856 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300857 error(line, "images cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000858 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300862unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530864 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000865
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200866 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
867 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
868 // fold as array size.
869 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000870 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000871 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300872 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000873 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874
Olli Etuaho856c4972016-08-08 11:38:39 +0300875 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400876
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000877 if (constant->getBasicType() == EbtUInt)
878 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300879 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000880 }
881 else
882 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300883 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000884
Olli Etuaho856c4972016-08-08 11:38:39 +0300885 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000886 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400887 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300888 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000889 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400890
Olli Etuaho856c4972016-08-08 11:38:39 +0300891 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400892 }
893
Olli Etuaho856c4972016-08-08 11:38:39 +0300894 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400895 {
896 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300897 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400898 }
899
900 // The size of arrays is restricted here to prevent issues further down the
901 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
902 // 4096 registers so this should be reasonable even for aggressively optimizable code.
903 const unsigned int sizeLimit = 65536;
904
Olli Etuaho856c4972016-08-08 11:38:39 +0300905 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400906 {
907 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300908 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000909 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300910
911 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912}
913
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300915bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
916 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917{
Olli Etuaho8a176262016-08-16 14:23:01 +0300918 if ((elementQualifier.qualifier == EvqAttribute) ||
919 (elementQualifier.qualifier == EvqVertexIn) ||
920 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300921 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400922 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300923 TType(elementQualifier).getQualifierString());
924 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000925 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926
Olli Etuaho8a176262016-08-16 14:23:01 +0300927 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928}
929
Olli Etuaho8a176262016-08-16 14:23:01 +0300930// See if this element type can be formed into an array.
931bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000933 //
934 // Can the type be an array?
935 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300936 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400937 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300938 error(line, "cannot declare arrays of arrays",
939 TType(elementType).getCompleteString().c_str());
940 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000941 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300942 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
943 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
944 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300945 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300946 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300947 {
948 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300949 TType(elementType).getCompleteString().c_str());
950 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300951 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952
Olli Etuaho8a176262016-08-16 14:23:01 +0300953 return true;
954}
955
956// Check if this qualified element type can be formed into an array.
957bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
958 const TPublicType &elementType)
959{
960 if (checkIsValidTypeForArray(indexLocation, elementType))
961 {
962 return checkIsValidQualifierForArray(indexLocation, elementType);
963 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000964 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965}
966
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300968void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
969 const TString &identifier,
970 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971{
Olli Etuaho3739d232015-04-08 12:23:44 +0300972 ASSERT(type != nullptr);
973 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000974 {
975 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300976 type->qualifier = EvqTemporary;
977
978 // Generate informative error messages for ESSL1.
979 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400980 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000981 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530982 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400983 "structures containing arrays may not be declared constant since they cannot be "
984 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530985 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000986 }
987 else
988 {
989 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
990 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300991 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000992 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300993 if (type->isUnsizedArray())
994 {
995 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300996 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
Olli Etuaho2935c582015-04-08 14:32:06 +0300999// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000// and update the symbol table.
1001//
Olli Etuaho2935c582015-04-08 14:32:06 +03001002// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001004bool TParseContext::declareVariable(const TSourceLoc &line,
1005 const TString &identifier,
1006 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001007 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008{
Olli Etuaho2935c582015-04-08 14:32:06 +03001009 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010
Olli Etuaho856c4972016-08-08 11:38:39 +03001011 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012
Olli Etuaho2935c582015-04-08 14:32:06 +03001013 // gl_LastFragData may be redeclared with a new precision qualifier
1014 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1015 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001016 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1017 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001018 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001019 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001020 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001021 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001022 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001023 }
1024 }
1025 else
1026 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001027 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1028 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001029 return false;
1030 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001031 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032
Olli Etuaho8a176262016-08-16 14:23:01 +03001033 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001034 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035
Olli Etuaho2935c582015-04-08 14:32:06 +03001036 (*variable) = new TVariable(&identifier, type);
1037 if (!symbolTable.declare(*variable))
1038 {
1039 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001040 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001041 return false;
1042 }
1043
Olli Etuaho8a176262016-08-16 14:23:01 +03001044 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001045 return false;
1046
1047 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048}
1049
Martin Radev70866b82016-07-22 15:27:42 +03001050void TParseContext::checkIsParameterQualifierValid(
1051 const TSourceLoc &line,
1052 const TTypeQualifierBuilder &typeQualifierBuilder,
1053 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301054{
Olli Etuaho613b9592016-09-05 12:05:53 +03001055 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001056
1057 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301058 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001059 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1060 }
1061
1062 if (!IsImage(type->getBasicType()))
1063 {
1064 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, line);
1065 }
1066 else
1067 {
1068 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001069 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070
Martin Radev70866b82016-07-22 15:27:42 +03001071 type->setQualifier(typeQualifier.qualifier);
1072
1073 if (typeQualifier.precision != EbpUndefined)
1074 {
1075 type->setPrecision(typeQualifier.precision);
1076 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
Olli Etuaho856c4972016-08-08 11:38:39 +03001079bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001080{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001081 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001082 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301083 if (iter == extBehavior.end())
1084 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001085 error(line, "extension", extension.c_str(), "is not supported");
Olli Etuaho8a176262016-08-16 14:23:01 +03001086 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001087 }
zmo@google.comf5450912011-09-09 01:37:19 +00001088 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301089 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1090 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001091 error(line, "extension", extension.c_str(), "is disabled");
Olli Etuaho8a176262016-08-16 14:23:01 +03001092 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001093 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301094 if (iter->second == EBhWarn)
1095 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001096 warning(line, "extension", extension.c_str(), "is being used");
Olli Etuaho8a176262016-08-16 14:23:01 +03001097 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001098 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099
Olli Etuaho8a176262016-08-16 14:23:01 +03001100 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101}
1102
Jamie Madillb98c3a82015-07-23 14:26:04 -04001103// These checks are common for all declarations starting a declarator list, and declarators that
1104// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +03001105void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001106 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001107{
Olli Etuahofa33d582015-04-09 14:33:12 +03001108 switch (publicType.qualifier)
1109 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001110 case EvqVaryingIn:
1111 case EvqVaryingOut:
1112 case EvqAttribute:
1113 case EvqVertexIn:
1114 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001115 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001116 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001117 {
1118 error(identifierLocation, "cannot be used with a structure",
1119 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001120 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001121 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001122
Jamie Madillb98c3a82015-07-23 14:26:04 -04001123 default:
1124 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001125 }
1126
Jamie Madillb98c3a82015-07-23 14:26:04 -04001127 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001128 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1129 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001130 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001131 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001132 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001133 if (publicType.qualifier != EvqUniform &&
1134 !checkIsNotImage(identifierLocation, publicType.typeSpecifierNonArray,
1135 "images must be uniform"))
1136 {
1137 return;
1138 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001139
1140 // check for layout qualifier issues
1141 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1142
1143 if (layoutQualifier.matrixPacking != EmpUnspecified)
1144 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001145 error(identifierLocation, "layout qualifier",
1146 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001147 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001148 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001149 }
1150
1151 if (layoutQualifier.blockStorage != EbsUnspecified)
1152 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001153 error(identifierLocation, "layout qualifier",
1154 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001155 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001156 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001157 }
1158
Olli Etuaho383b7912016-08-05 11:22:59 +03001159 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001160 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001161 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001162 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001163
1164 if (IsImage(publicType.getBasicType()))
1165 {
1166
1167 switch (layoutQualifier.imageInternalFormat)
1168 {
1169 case EiifRGBA32F:
1170 case EiifRGBA16F:
1171 case EiifR32F:
1172 case EiifRGBA8:
1173 case EiifRGBA8_SNORM:
1174 if (!IsFloatImage(publicType.getBasicType()))
1175 {
1176 error(identifierLocation,
1177 "internal image format requires a floating image type",
1178 getBasicString(publicType.getBasicType()));
1179 return;
1180 }
1181 break;
1182 case EiifRGBA32I:
1183 case EiifRGBA16I:
1184 case EiifRGBA8I:
1185 case EiifR32I:
1186 if (!IsIntegerImage(publicType.getBasicType()))
1187 {
1188 error(identifierLocation,
1189 "internal image format requires an integer image type",
1190 getBasicString(publicType.getBasicType()));
1191 return;
1192 }
1193 break;
1194 case EiifRGBA32UI:
1195 case EiifRGBA16UI:
1196 case EiifRGBA8UI:
1197 case EiifR32UI:
1198 if (!IsUnsignedImage(publicType.getBasicType()))
1199 {
1200 error(identifierLocation,
1201 "internal image format requires an unsigned image type",
1202 getBasicString(publicType.getBasicType()));
1203 return;
1204 }
1205 break;
1206 case EiifUnspecified:
1207 error(identifierLocation, "layout qualifier", "No image internal format specified");
1208 return;
1209 default:
1210 error(identifierLocation, "layout qualifier", "unrecognized token");
1211 return;
1212 }
1213
1214 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1215 switch (layoutQualifier.imageInternalFormat)
1216 {
1217 case EiifR32F:
1218 case EiifR32I:
1219 case EiifR32UI:
1220 break;
1221 default:
1222 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1223 {
1224 error(identifierLocation, "layout qualifier",
1225 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1226 "image variables must be qualified readonly and/or writeonly");
1227 return;
1228 }
1229 break;
1230 }
1231 }
1232 else
1233 {
1234
1235 if (!checkInternalFormatIsNotSpecified(identifierLocation,
1236 layoutQualifier.imageInternalFormat))
1237 {
1238 return;
1239 }
1240
1241 if (!checkIsMemoryQualifierNotSpecified(publicType.memoryQualifier, identifierLocation))
1242 {
1243 return;
1244 }
1245 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001246}
1247
Olli Etuaho856c4972016-08-08 11:38:39 +03001248void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1249 const TString &layoutQualifierName,
1250 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001251{
1252
1253 if (mShaderVersion < versionRequired)
1254 {
1255 error(location, "invalid layout qualifier:", layoutQualifierName.c_str(), "not supported");
Martin Radev802abe02016-08-04 17:48:32 +03001256 }
1257}
1258
Olli Etuaho856c4972016-08-08 11:38:39 +03001259bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1260 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001261{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001262 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001263 for (size_t i = 0u; i < localSize.size(); ++i)
1264 {
1265 if (localSize[i] != -1)
1266 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001267 error(location, "invalid layout qualifier:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03001268 "only valid when used with 'in' in a compute shader global layout declaration");
Olli Etuaho8a176262016-08-16 14:23:01 +03001269 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001270 }
1271 }
1272
Olli Etuaho8a176262016-08-16 14:23:01 +03001273 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001274}
1275
Martin Radev2cc85b32016-08-05 16:22:53 +03001276bool TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
1277 TLayoutImageInternalFormat internalFormat)
1278{
1279 if (internalFormat != EiifUnspecified)
1280 {
1281 error(location, "invalid layout qualifier:", getImageInternalFormatString(internalFormat),
1282 "only valid when used with images");
1283 return false;
1284 }
1285 return true;
1286}
1287
Olli Etuaho383b7912016-08-05 11:22:59 +03001288void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001289 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001290{
1291 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1292 {
1293 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1294 if (qual == EvqOut || qual == EvqInOut)
1295 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001296 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001297 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001298 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001299 error(argument->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001300 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03001301 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001302 }
1303 }
1304 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001305}
1306
Martin Radev70866b82016-07-22 15:27:42 +03001307void TParseContext::checkInvariantVariableQualifier(bool invariant,
1308 const TQualifier qualifier,
1309 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001310{
Martin Radev70866b82016-07-22 15:27:42 +03001311 if (!invariant)
1312 return;
1313
1314 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001315 {
Martin Radev70866b82016-07-22 15:27:42 +03001316 // input variables in the fragment shader can be also qualified as invariant
1317 if (!sh::CanBeInvariantESSL1(qualifier))
1318 {
1319 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1320 }
1321 }
1322 else
1323 {
1324 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1325 {
1326 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1327 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001328 }
1329}
1330
Arun Patole7e7e68d2015-05-22 12:02:25 +05301331bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001332{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001333 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001334 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1335 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001336}
1337
Arun Patole7e7e68d2015-05-22 12:02:25 +05301338bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001339{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001340 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001341}
1342
Jamie Madillb98c3a82015-07-23 14:26:04 -04001343void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1344 const char *extName,
1345 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001346{
1347 pp::SourceLocation srcLoc;
1348 srcLoc.file = loc.first_file;
1349 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001350 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001351}
1352
Jamie Madillb98c3a82015-07-23 14:26:04 -04001353void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1354 const char *name,
1355 const char *value,
1356 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001357{
1358 pp::SourceLocation srcLoc;
1359 srcLoc.file = loc.first_file;
1360 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001361 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001362}
1363
Martin Radev4c4c8e72016-08-04 12:25:34 +03001364sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001365{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001366 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001367 for (size_t i = 0u; i < result.size(); ++i)
1368 {
1369 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1370 {
1371 result[i] = 1;
1372 }
1373 else
1374 {
1375 result[i] = mComputeShaderLocalSize[i];
1376 }
1377 }
1378 return result;
1379}
1380
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001381/////////////////////////////////////////////////////////////////////////////////
1382//
1383// Non-Errors.
1384//
1385/////////////////////////////////////////////////////////////////////////////////
1386
Jamie Madill5c097022014-08-20 16:38:32 -04001387const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1388 const TString *name,
1389 const TSymbol *symbol)
1390{
1391 const TVariable *variable = NULL;
1392
1393 if (!symbol)
1394 {
1395 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001396 }
1397 else if (!symbol->isVariable())
1398 {
1399 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001400 }
1401 else
1402 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001403 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001404
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001405 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001406 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001407 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001408 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001409 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001410
1411 // Reject shaders using both gl_FragData and gl_FragColor
1412 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001413 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001414 {
1415 mUsesFragData = true;
1416 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001417 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001418 {
1419 mUsesFragColor = true;
1420 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001421 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1422 {
1423 mUsesSecondaryOutputs = true;
1424 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001425
1426 // This validation is not quite correct - it's only an error to write to
1427 // both FragData and FragColor. For simplicity, and because users shouldn't
1428 // be rewarded for reading from undefined varaibles, return an error
1429 // if they are both referenced, rather than assigned.
1430 if (mUsesFragData && mUsesFragColor)
1431 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001432 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1433 if (mUsesSecondaryOutputs)
1434 {
1435 errorMessage =
1436 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1437 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1438 }
1439 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001440 }
Martin Radevb0883602016-08-04 17:48:58 +03001441
1442 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1443 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1444 qualifier == EvqWorkGroupSize)
1445 {
1446 error(location,
1447 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1448 "gl_WorkGroupSize");
1449 }
Jamie Madill5c097022014-08-20 16:38:32 -04001450 }
1451
1452 if (!variable)
1453 {
1454 TType type(EbtFloat, EbpUndefined);
1455 TVariable *fakeVariable = new TVariable(name, type);
1456 symbolTable.declare(fakeVariable);
1457 variable = fakeVariable;
1458 }
1459
1460 return variable;
1461}
1462
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001463TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1464 const TString *name,
1465 const TSymbol *symbol)
1466{
1467 const TVariable *variable = getNamedVariable(location, name, symbol);
1468
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001469 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001470 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001471 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001472 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001473 }
1474 else
1475 {
1476 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1477 variable->getType(), location);
1478 }
1479}
1480
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001481//
1482// Look up a function name in the symbol table, and make sure it is a function.
1483//
1484// Return the function symbol if found, otherwise 0.
1485//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001486const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1487 TFunction *call,
1488 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301489 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001490{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001491 // First find by unmangled name to check whether the function name has been
1492 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001493 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301494 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1495 if (symbol == 0 || symbol->isFunction())
1496 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001497 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001498 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001499
Arun Patole7e7e68d2015-05-22 12:02:25 +05301500 if (symbol == 0)
1501 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001502 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001503 return 0;
1504 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001505
Arun Patole7e7e68d2015-05-22 12:02:25 +05301506 if (!symbol->isFunction())
1507 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001508 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001509 return 0;
1510 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001511
Jamie Madillb98c3a82015-07-23 14:26:04 -04001512 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001513}
1514
1515//
1516// Initializers show up in several places in the grammar. Have one set of
1517// code to handle them here.
1518//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001519// Returns true on error, false if no error
1520//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001521bool TParseContext::executeInitializer(const TSourceLoc &line,
1522 const TString &identifier,
1523 const TPublicType &pType,
1524 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001525 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001526{
Olli Etuaho13389b62016-10-16 11:48:18 +01001527 ASSERT(initNode != nullptr);
1528 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001529 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530
Olli Etuaho2935c582015-04-08 14:32:06 +03001531 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001532 if (type.isUnsizedArray())
1533 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001534 // We have not checked yet whether the initializer actually is an array or not.
1535 if (initializer->isArray())
1536 {
1537 type.setArraySize(initializer->getArraySize());
1538 }
1539 else
1540 {
1541 // Having a non-array initializer for an unsized array will result in an error later,
1542 // so we don't generate an error message here.
1543 type.setArraySize(1u);
1544 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001545 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001546 if (!declareVariable(line, identifier, type, &variable))
1547 {
1548 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001550
Olli Etuahob0c645e2015-05-12 14:25:36 +03001551 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001552 if (symbolTable.atGlobalLevel() &&
1553 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001554 {
1555 // Error message does not completely match behavior with ESSL 1.00, but
1556 // we want to steer developers towards only using constant expressions.
1557 error(line, "global variable initializers must be constant expressions", "=");
1558 return true;
1559 }
1560 if (globalInitWarning)
1561 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001562 warning(
1563 line,
1564 "global variable initializers should be constant expressions "
1565 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1566 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001567 }
1568
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001569 //
1570 // identifier must be of type constant, a global, or a temporary
1571 //
1572 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301573 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1574 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001575 error(line, " cannot initialize this type of qualifier ",
1576 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001577 return true;
1578 }
1579 //
1580 // test for and propagate constant
1581 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001582
Arun Patole7e7e68d2015-05-22 12:02:25 +05301583 if (qualifier == EvqConst)
1584 {
1585 if (qualifier != initializer->getType().getQualifier())
1586 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001587 std::stringstream extraInfoStream;
1588 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1589 std::string extraInfo = extraInfoStream.str();
1590 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001591 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001592 return true;
1593 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301594 if (type != initializer->getType())
1595 {
1596 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001597 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001598 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001599 return true;
1600 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001601
1602 // Save the constant folded value to the variable if possible. For example array
1603 // initializers are not folded, since that way copying the array literal to multiple places
1604 // in the shader is avoided.
1605 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1606 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301607 if (initializer->getAsConstantUnion())
1608 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001609 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001610 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001611 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301612 }
1613 else if (initializer->getAsSymbolNode())
1614 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001615 const TSymbol *symbol =
1616 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1617 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001619 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001620 if (constArray)
1621 {
1622 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001623 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001624 return false;
1625 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001626 }
1627 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001628
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001629 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1630 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001631 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1632 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001633 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001634 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1635 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001636 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001637
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001638 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639}
1640
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001641void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1642{
1643 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1644 typeSpecifier->getBasicType());
1645
1646 if (mShaderVersion < 300 && typeSpecifier->array)
1647 {
1648 error(typeSpecifier->getLine(), "not supported", "first-class array");
1649 typeSpecifier->clearArrayness();
1650 }
1651}
1652
Martin Radev70866b82016-07-22 15:27:42 +03001653TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301654 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001655{
Olli Etuaho613b9592016-09-05 12:05:53 +03001656 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001657
Martin Radev70866b82016-07-22 15:27:42 +03001658 TPublicType returnType = typeSpecifier;
1659 returnType.qualifier = typeQualifier.qualifier;
1660 returnType.invariant = typeQualifier.invariant;
1661 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001662 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001663 returnType.precision = typeSpecifier.precision;
1664
1665 if (typeQualifier.precision != EbpUndefined)
1666 {
1667 returnType.precision = typeQualifier.precision;
1668 }
1669
Martin Radev4a9cd802016-09-01 16:51:51 +03001670 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1671 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001672
Martin Radev4a9cd802016-09-01 16:51:51 +03001673 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1674 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001675
Martin Radev4a9cd802016-09-01 16:51:51 +03001676 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001677
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001678 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001679 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001680 if (typeSpecifier.array)
1681 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001682 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001683 returnType.clearArrayness();
1684 }
1685
Martin Radev70866b82016-07-22 15:27:42 +03001686 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001687 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001688 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001689 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001690 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001691 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001692
Martin Radev70866b82016-07-22 15:27:42 +03001693 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001694 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001695 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001696 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001697 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001698 }
1699 }
1700 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001701 {
Martin Radev70866b82016-07-22 15:27:42 +03001702 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001703 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001704 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001705 }
Martin Radev70866b82016-07-22 15:27:42 +03001706 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1707 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001708 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001709 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1710 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001711 }
Martin Radev70866b82016-07-22 15:27:42 +03001712 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001713 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001714 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001715 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001716 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001717 }
1718
1719 return returnType;
1720}
1721
Olli Etuaho856c4972016-08-08 11:38:39 +03001722void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1723 const TPublicType &type,
1724 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001725{
1726 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001727 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001728 {
1729 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001730 }
1731
1732 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1733 switch (qualifier)
1734 {
1735 case EvqVertexIn:
1736 // ESSL 3.00 section 4.3.4
1737 if (type.array)
1738 {
1739 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001740 }
1741 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1742 return;
1743 case EvqFragmentOut:
1744 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001745 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001746 {
1747 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001748 }
1749 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1750 return;
1751 default:
1752 break;
1753 }
1754
1755 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1756 // restrictions.
1757 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001758 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1759 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001760 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1761 {
1762 error(qualifierLocation, "must use 'flat' interpolation here",
1763 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001764 }
1765
Martin Radev4a9cd802016-09-01 16:51:51 +03001766 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001767 {
1768 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1769 // These restrictions are only implied by the ESSL 3.00 spec, but
1770 // the ESSL 3.10 spec lists these restrictions explicitly.
1771 if (type.array)
1772 {
1773 error(qualifierLocation, "cannot be an array of structures",
1774 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001775 }
1776 if (type.isStructureContainingArrays())
1777 {
1778 error(qualifierLocation, "cannot be a structure containing an array",
1779 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001780 }
1781 if (type.isStructureContainingType(EbtStruct))
1782 {
1783 error(qualifierLocation, "cannot be a structure containing a structure",
1784 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001785 }
1786 if (type.isStructureContainingType(EbtBool))
1787 {
1788 error(qualifierLocation, "cannot be a structure containing a bool",
1789 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001790 }
1791 }
1792}
1793
Martin Radev2cc85b32016-08-05 16:22:53 +03001794void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1795{
1796 if (qualifier.getType() == QtStorage)
1797 {
1798 const TStorageQualifierWrapper &storageQualifier =
1799 static_cast<const TStorageQualifierWrapper &>(qualifier);
1800 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1801 !symbolTable.atGlobalLevel())
1802 {
1803 error(storageQualifier.getLine(),
1804 "Local variables can only use the const storage qualifier.",
1805 storageQualifier.getQualifierString().c_str());
1806 }
1807 }
1808}
1809
1810bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1811 const TSourceLoc &location)
1812{
1813 if (memoryQualifier.readonly)
1814 {
1815 error(location, "Only allowed with images.", "readonly");
1816 return false;
1817 }
1818 if (memoryQualifier.writeonly)
1819 {
1820 error(location, "Only allowed with images.", "writeonly");
1821 return false;
1822 }
1823 return true;
1824}
1825
Olli Etuaho13389b62016-10-16 11:48:18 +01001826TIntermDeclaration *TParseContext::parseSingleDeclaration(
1827 TPublicType &publicType,
1828 const TSourceLoc &identifierOrTypeLocation,
1829 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001830{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001831 TType type(publicType);
1832 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1833 mDirectiveHandler.pragma().stdgl.invariantAll)
1834 {
1835 TQualifier qualifier = type.getQualifier();
1836
1837 // The directive handler has already taken care of rejecting invalid uses of this pragma
1838 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1839 // affected variable declarations:
1840 //
1841 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1842 // elsewhere, in TranslatorGLSL.)
1843 //
1844 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1845 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1846 // the way this is currently implemented we have to enable this compiler option before
1847 // parsing the shader and determining the shading language version it uses. If this were
1848 // implemented as a post-pass, the workaround could be more targeted.
1849 //
1850 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1851 // the specification, but there are desktop OpenGL drivers that expect that this is the
1852 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1853 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1854 {
1855 type.setInvariant(true);
1856 }
1857 }
1858
1859 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001860
Olli Etuahobab4c082015-04-24 16:38:49 +03001861 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001862
Olli Etuahobab4c082015-04-24 16:38:49 +03001863 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1864
Olli Etuaho13389b62016-10-16 11:48:18 +01001865 TIntermDeclaration *declaration = new TIntermDeclaration();
1866 declaration->setLine(identifierOrTypeLocation);
1867
Olli Etuahobab4c082015-04-24 16:38:49 +03001868 if (emptyDeclaration)
1869 {
1870 if (publicType.isUnsizedArray())
1871 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001872 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1873 // error. It is assumed that this applies to empty declarations as well.
1874 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1875 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001876 }
1877 }
1878 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001879 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001880 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001881
Olli Etuaho856c4972016-08-08 11:38:39 +03001882 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001883
Olli Etuaho2935c582015-04-08 14:32:06 +03001884 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001885 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001886
1887 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001888 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001889 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001890 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001891 }
1892
Olli Etuaho13389b62016-10-16 11:48:18 +01001893 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1894 // that may just declare a type.
1895 declaration->appendDeclarator(symbol);
1896
1897 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001898}
1899
Olli Etuaho13389b62016-10-16 11:48:18 +01001900TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1901 const TSourceLoc &identifierLocation,
1902 const TString &identifier,
1903 const TSourceLoc &indexLocation,
1904 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001905{
Olli Etuahofa33d582015-04-09 14:33:12 +03001906 mDeferredSingleDeclarationErrorCheck = false;
1907
Olli Etuaho383b7912016-08-05 11:22:59 +03001908 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001909
Olli Etuaho856c4972016-08-08 11:38:39 +03001910 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001911
Olli Etuaho8a176262016-08-16 14:23:01 +03001912 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001913
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001914 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001915
Olli Etuaho856c4972016-08-08 11:38:39 +03001916 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001917 // Make the type an array even if size check failed.
1918 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1919 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001920
Olli Etuaho2935c582015-04-08 14:32:06 +03001921 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001922 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001923
Olli Etuaho13389b62016-10-16 11:48:18 +01001924 TIntermDeclaration *declaration = new TIntermDeclaration();
1925 declaration->setLine(identifierLocation);
1926
Olli Etuahoe7847b02015-03-16 11:56:12 +02001927 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001928 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001929 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001930 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001931 declaration->appendDeclarator(symbol);
1932 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001933
Olli Etuaho13389b62016-10-16 11:48:18 +01001934 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001935}
1936
Olli Etuaho13389b62016-10-16 11:48:18 +01001937TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1938 const TSourceLoc &identifierLocation,
1939 const TString &identifier,
1940 const TSourceLoc &initLocation,
1941 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001942{
Olli Etuahofa33d582015-04-09 14:33:12 +03001943 mDeferredSingleDeclarationErrorCheck = false;
1944
Olli Etuaho383b7912016-08-05 11:22:59 +03001945 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001946
Olli Etuaho13389b62016-10-16 11:48:18 +01001947 TIntermDeclaration *declaration = new TIntermDeclaration();
1948 declaration->setLine(identifierLocation);
1949
1950 TIntermBinary *initNode = nullptr;
1951 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001952 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001953 if (initNode)
1954 {
1955 declaration->appendDeclarator(initNode);
1956 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001957 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001958 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001959}
1960
Olli Etuaho13389b62016-10-16 11:48:18 +01001961TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04001962 TPublicType &publicType,
1963 const TSourceLoc &identifierLocation,
1964 const TString &identifier,
1965 const TSourceLoc &indexLocation,
1966 TIntermTyped *indexExpression,
1967 const TSourceLoc &initLocation,
1968 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001969{
1970 mDeferredSingleDeclarationErrorCheck = false;
1971
Olli Etuaho383b7912016-08-05 11:22:59 +03001972 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001973
Olli Etuaho8a176262016-08-16 14:23:01 +03001974 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001975
1976 TPublicType arrayType(publicType);
1977
Olli Etuaho856c4972016-08-08 11:38:39 +03001978 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001979 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1980 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001981 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001982 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001983 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001984 }
1985 // Make the type an array even if size check failed.
1986 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1987 arrayType.setArraySize(size);
1988
Olli Etuaho13389b62016-10-16 11:48:18 +01001989 TIntermDeclaration *declaration = new TIntermDeclaration();
1990 declaration->setLine(identifierLocation);
1991
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001992 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01001993 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001994 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1995 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001996 if (initNode)
1997 {
1998 declaration->appendDeclarator(initNode);
1999 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002000 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002001
2002 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002003}
2004
Martin Radev70866b82016-07-22 15:27:42 +03002005TIntermAggregate *TParseContext::parseInvariantDeclaration(
2006 const TTypeQualifierBuilder &typeQualifierBuilder,
2007 const TSourceLoc &identifierLoc,
2008 const TString *identifier,
2009 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002010{
Olli Etuaho613b9592016-09-05 12:05:53 +03002011 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002012
Martin Radev70866b82016-07-22 15:27:42 +03002013 if (!typeQualifier.invariant)
2014 {
2015 error(identifierLoc, "Expected invariant", identifier->c_str());
2016 return nullptr;
2017 }
2018 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2019 {
2020 return nullptr;
2021 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002022 if (!symbol)
2023 {
2024 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002025 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002026 }
Martin Radev70866b82016-07-22 15:27:42 +03002027 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002028 {
Martin Radev70866b82016-07-22 15:27:42 +03002029 error(identifierLoc, "invariant declaration specifies qualifier",
2030 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002031 }
Martin Radev70866b82016-07-22 15:27:42 +03002032 if (typeQualifier.precision != EbpUndefined)
2033 {
2034 error(identifierLoc, "invariant declaration specifies precision",
2035 getPrecisionString(typeQualifier.precision));
2036 }
2037 if (!typeQualifier.layoutQualifier.isEmpty())
2038 {
2039 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2040 }
2041
2042 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2043 ASSERT(variable);
2044 const TType &type = variable->getType();
2045
2046 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2047 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002048 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002049
2050 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2051
2052 TIntermSymbol *intermSymbol =
2053 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2054
Olli Etuaho32db19b2016-10-04 14:43:16 +01002055 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(intermSymbol, identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002056 aggregate->setOp(EOpInvariantDeclaration);
2057 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002058}
2059
Olli Etuaho13389b62016-10-16 11:48:18 +01002060void TParseContext::parseDeclarator(TPublicType &publicType,
2061 const TSourceLoc &identifierLocation,
2062 const TString &identifier,
2063 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002064{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002065 // If the declaration starting this declarator list was empty (example: int,), some checks were
2066 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002067 if (mDeferredSingleDeclarationErrorCheck)
2068 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002069 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002070 mDeferredSingleDeclarationErrorCheck = false;
2071 }
2072
Olli Etuaho856c4972016-08-08 11:38:39 +03002073 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002074
Olli Etuaho856c4972016-08-08 11:38:39 +03002075 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002076
Olli Etuaho2935c582015-04-08 14:32:06 +03002077 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002078 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002079
Jamie Madillb98c3a82015-07-23 14:26:04 -04002080 TIntermSymbol *symbol =
2081 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002082 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002083 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002084 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002085 declarationOut->appendDeclarator(symbol);
2086 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002087}
2088
Olli Etuaho13389b62016-10-16 11:48:18 +01002089void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2090 const TSourceLoc &identifierLocation,
2091 const TString &identifier,
2092 const TSourceLoc &arrayLocation,
2093 TIntermTyped *indexExpression,
2094 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002095{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002096 // If the declaration starting this declarator list was empty (example: int,), some checks were
2097 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002098 if (mDeferredSingleDeclarationErrorCheck)
2099 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002100 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002101 mDeferredSingleDeclarationErrorCheck = false;
2102 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002103
Olli Etuaho856c4972016-08-08 11:38:39 +03002104 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002105
Olli Etuaho856c4972016-08-08 11:38:39 +03002106 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002107
Olli Etuaho8a176262016-08-16 14:23:01 +03002108 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002109 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02002110 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002111 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002112 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002113
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002114 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002115 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002116
Jamie Madillb98c3a82015-07-23 14:26:04 -04002117 TIntermSymbol *symbol =
2118 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002119 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002120 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002121
Olli Etuaho13389b62016-10-16 11:48:18 +01002122 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002123 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002124}
2125
Olli Etuaho13389b62016-10-16 11:48:18 +01002126void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2127 const TSourceLoc &identifierLocation,
2128 const TString &identifier,
2129 const TSourceLoc &initLocation,
2130 TIntermTyped *initializer,
2131 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002132{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002133 // If the declaration starting this declarator list was empty (example: int,), some checks were
2134 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002135 if (mDeferredSingleDeclarationErrorCheck)
2136 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002137 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002138 mDeferredSingleDeclarationErrorCheck = false;
2139 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002140
Olli Etuaho856c4972016-08-08 11:38:39 +03002141 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002142
Olli Etuaho13389b62016-10-16 11:48:18 +01002143 TIntermBinary *initNode = nullptr;
2144 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002145 {
2146 //
2147 // build the intermediate representation
2148 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002149 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002150 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002151 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002152 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002153 }
2154}
2155
Olli Etuaho13389b62016-10-16 11:48:18 +01002156void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2157 const TSourceLoc &identifierLocation,
2158 const TString &identifier,
2159 const TSourceLoc &indexLocation,
2160 TIntermTyped *indexExpression,
2161 const TSourceLoc &initLocation,
2162 TIntermTyped *initializer,
2163 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002164{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002165 // If the declaration starting this declarator list was empty (example: int,), some checks were
2166 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002167 if (mDeferredSingleDeclarationErrorCheck)
2168 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002169 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002170 mDeferredSingleDeclarationErrorCheck = false;
2171 }
2172
Olli Etuaho856c4972016-08-08 11:38:39 +03002173 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002174
Olli Etuaho8a176262016-08-16 14:23:01 +03002175 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002176
2177 TPublicType arrayType(publicType);
2178
Olli Etuaho856c4972016-08-08 11:38:39 +03002179 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002180 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2181 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002182 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002183 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002184 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002185 }
2186 // Make the type an array even if size check failed.
2187 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2188 arrayType.setArraySize(size);
2189
2190 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002191 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002192 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2193 {
2194 if (initNode)
2195 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002196 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002197 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002198 }
2199}
2200
Martin Radev70866b82016-07-22 15:27:42 +03002201void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002202{
Olli Etuaho613b9592016-09-05 12:05:53 +03002203 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002204 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002205
Martin Radev70866b82016-07-22 15:27:42 +03002206 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2207 typeQualifier.line);
2208
Jamie Madillc2128ff2016-07-04 10:26:17 -04002209 // It should never be the case, but some strange parser errors can send us here.
2210 if (layoutQualifier.isEmpty())
2211 {
2212 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002213 return;
2214 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002215
Martin Radev802abe02016-08-04 17:48:32 +03002216 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002217 {
Martin Radev802abe02016-08-04 17:48:32 +03002218 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002219 return;
2220 }
2221
Martin Radev2cc85b32016-08-05 16:22:53 +03002222 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2223
2224 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2225
Martin Radev802abe02016-08-04 17:48:32 +03002226 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002227 {
Martin Radev802abe02016-08-04 17:48:32 +03002228 if (mComputeShaderLocalSizeDeclared &&
2229 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2230 {
2231 error(typeQualifier.line, "Work group size does not match the previous declaration",
2232 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002233 return;
2234 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002235
Martin Radev802abe02016-08-04 17:48:32 +03002236 if (mShaderVersion < 310)
2237 {
2238 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002239 return;
2240 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002241
Martin Radev4c4c8e72016-08-04 12:25:34 +03002242 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002243 {
2244 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002245 return;
2246 }
2247
2248 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2249 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2250
2251 const TConstantUnion *maxComputeWorkGroupSizeData =
2252 maxComputeWorkGroupSize->getConstPointer();
2253
2254 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2255 {
2256 if (layoutQualifier.localSize[i] != -1)
2257 {
2258 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2259 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2260 if (mComputeShaderLocalSize[i] < 1 ||
2261 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2262 {
2263 std::stringstream errorMessageStream;
2264 errorMessageStream << "Value must be at least 1 and no greater than "
2265 << maxComputeWorkGroupSizeValue;
2266 const std::string &errorMessage = errorMessageStream.str();
2267
Martin Radev4c4c8e72016-08-04 12:25:34 +03002268 error(typeQualifier.line, "invalid value:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03002269 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002270 return;
2271 }
2272 }
2273 }
2274
2275 mComputeShaderLocalSizeDeclared = true;
2276 }
2277 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002278 {
Martin Radev802abe02016-08-04 17:48:32 +03002279
Olli Etuaho8a176262016-08-16 14:23:01 +03002280 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002281 {
Martin Radev802abe02016-08-04 17:48:32 +03002282 return;
2283 }
2284
2285 if (typeQualifier.qualifier != EvqUniform)
2286 {
2287 error(typeQualifier.line, "invalid qualifier:",
2288 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03002289 return;
2290 }
2291
2292 if (mShaderVersion < 300)
2293 {
2294 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2295 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002296 return;
2297 }
2298
Olli Etuaho856c4972016-08-08 11:38:39 +03002299 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002300
2301 if (layoutQualifier.matrixPacking != EmpUnspecified)
2302 {
2303 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2304 }
2305
2306 if (layoutQualifier.blockStorage != EbsUnspecified)
2307 {
2308 mDefaultBlockStorage = layoutQualifier.blockStorage;
2309 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002310 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002311}
2312
Olli Etuaho476197f2016-10-11 13:59:08 +01002313TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002314 const TSourceLoc &location)
2315{
Olli Etuaho476197f2016-10-11 13:59:08 +01002316 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2317 // first declaration. Either way the instance in the symbol table is used to track whether the
2318 // function is declared multiple times.
2319 TFunction *function = static_cast<TFunction *>(
2320 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2321 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002322 {
2323 // ESSL 1.00.17 section 4.2.7.
2324 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2325 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002326 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002327 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002328
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002329 TIntermAggregate *prototype = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002330 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2331 // point to the data that already exists in the symbol table.
2332 prototype->setType(function->getReturnType());
Olli Etuahobd674552016-10-06 13:28:42 +01002333 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002334
Olli Etuaho476197f2016-10-11 13:59:08 +01002335 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002336 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002337 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002338 if (param.name != 0)
2339 {
2340 TVariable variable(param.name, *param.type);
2341
2342 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2343 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2344 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2345 }
2346 else
2347 {
2348 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2349 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2350 }
2351 }
2352
2353 prototype->setOp(EOpPrototype);
2354
2355 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002356
2357 if (!symbolTable.atGlobalLevel())
2358 {
2359 // ESSL 3.00.4 section 4.2.4.
2360 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002361 }
2362
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002363 return prototype;
2364}
2365
Olli Etuaho336b1472016-10-05 16:37:55 +01002366TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2367 const TFunction &function,
2368 TIntermAggregate *functionParameters,
2369 TIntermBlock *functionBody,
2370 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002371{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002372 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002373 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2374 {
2375 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002376 }
2377
Olli Etuahof51fdd22016-10-03 10:03:40 +01002378 if (functionBody == nullptr)
2379 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002380 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002381 functionBody->setLine(location);
2382 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002383 TIntermFunctionDefinition *functionNode =
2384 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2385 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002386
Olli Etuahobd674552016-10-06 13:28:42 +01002387 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002388
2389 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002390 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002391}
2392
Olli Etuaho476197f2016-10-11 13:59:08 +01002393void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2394 TFunction **function,
2395 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002396{
Olli Etuaho476197f2016-10-11 13:59:08 +01002397 ASSERT(function);
2398 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002399 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002400 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002401
2402 if (builtIn)
2403 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002404 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002405 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002406 else
Jamie Madill185fb402015-06-12 15:48:48 -04002407 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002408 TFunction *prevDec = static_cast<TFunction *>(
2409 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2410
2411 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2412 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2413 // occurance.
2414 if (*function != prevDec)
2415 {
2416 // Swap the parameters of the previous declaration to the parameters of the function
2417 // definition (parameter names may differ).
2418 prevDec->swapParameters(**function);
2419
2420 // The function definition will share the same symbol as any previous declaration.
2421 *function = prevDec;
2422 }
2423
2424 if ((*function)->isDefined())
2425 {
2426 error(location, "function already has a body", (*function)->getName().c_str());
2427 }
2428
2429 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002430 }
Jamie Madill185fb402015-06-12 15:48:48 -04002431
2432 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002433 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002434 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002435 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002436 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002437 error(location, "function cannot take any parameter(s)",
2438 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002439 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002440 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002441 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002442 error(location, "", (*function)->getReturnType().getBasicString(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04002443 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002444 }
2445 }
2446
2447 //
2448 // Remember the return type for later checking for RETURN statements.
2449 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002450 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002451 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002452
2453 //
2454 // Insert parameters into the symbol table.
2455 // If the parameter has no name, it's not an error, just don't insert it
2456 // (could be used for unused args).
2457 //
2458 // Also, accumulate the list of parameters into the HIL, so lower level code
2459 // knows where to find parameters.
2460 //
2461 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002462 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002463 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002464 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002465 if (param.name != 0)
2466 {
2467 TVariable *variable = new TVariable(param.name, *param.type);
2468 //
2469 // Insert the parameters with name in the symbol table.
2470 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002471 if (!symbolTable.declare(variable))
2472 {
Jamie Madill185fb402015-06-12 15:48:48 -04002473 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002474 paramNodes = intermediate.growAggregate(
2475 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2476 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002477 }
2478
2479 //
2480 // Add the parameter to the HIL
2481 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002482 TIntermSymbol *symbol = intermediate.addSymbol(
2483 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002484
2485 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2486 }
2487 else
2488 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002489 paramNodes = intermediate.growAggregate(
2490 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002491 }
2492 }
2493 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2494 *aggregateOut = paramNodes;
2495 setLoopNestingLevel(0);
2496}
2497
Jamie Madillb98c3a82015-07-23 14:26:04 -04002498TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002499{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002500 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002501 // We don't know at this point whether this is a function definition or a prototype.
2502 // The definition production code will check for redefinitions.
2503 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002504 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002505 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2506 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002507 //
2508 TFunction *prevDec =
2509 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302510
2511 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2512 {
2513 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2514 // Therefore overloading or redefining builtin functions is an error.
2515 error(location, "Name of a built-in function cannot be redeclared as function",
2516 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302517 }
2518 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002519 {
2520 if (prevDec->getReturnType() != function->getReturnType())
2521 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002522 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002523 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002524 }
2525 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2526 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002527 if (prevDec->getParam(i).type->getQualifier() !=
2528 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002529 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002530 error(location,
2531 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002532 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002533 }
2534 }
2535 }
2536
2537 //
2538 // Check for previously declared variables using the same name.
2539 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002540 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002541 if (prevSym)
2542 {
2543 if (!prevSym->isFunction())
2544 {
2545 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002546 }
2547 }
2548 else
2549 {
2550 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002551 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002552 }
2553
2554 // We're at the inner scope level of the function's arguments and body statement.
2555 // Add the function prototype to the surrounding scope instead.
2556 symbolTable.getOuterLevel()->insert(function);
2557
2558 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002559 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2560 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002561 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2562 //
2563 return function;
2564}
2565
Olli Etuaho9de84a52016-06-14 17:36:01 +03002566TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2567 const TString *name,
2568 const TSourceLoc &location)
2569{
2570 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2571 {
2572 error(location, "no qualifiers allowed for function return",
2573 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002574 }
2575 if (!type.layoutQualifier.isEmpty())
2576 {
2577 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002578 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002579 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002580 checkIsNotSampler(location, type.typeSpecifierNonArray,
2581 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002582 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002583 if (mShaderVersion < 300)
2584 {
2585 // Array return values are forbidden, but there's also no valid syntax for declaring array
2586 // return values in ESSL 1.00.
2587 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2588
2589 if (type.isStructureContainingArrays())
2590 {
2591 // ESSL 1.00.17 section 6.1 Function Definitions
2592 error(location, "structures containing arrays can't be function return values",
2593 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002594 }
2595 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002596
2597 // Add the function as a prototype after parsing it (we do not support recursion)
2598 return new TFunction(name, new TType(type));
2599}
2600
Jamie Madill06145232015-05-13 13:10:01 -04002601TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002602{
Jamie Madill06145232015-05-13 13:10:01 -04002603 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002604 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002605 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002606 error(publicType.getLine(), "constructor can't be a structure definition",
2607 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002608 }
2609
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002610 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002611 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002612 {
2613 op = EOpConstructStruct;
2614 }
2615 else
2616 {
Geoff Lang156d7192016-07-21 16:11:00 -04002617 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002618 if (op == EOpNull)
2619 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002620 error(publicType.getLine(), "cannot construct this type",
2621 getBasicString(publicType.getBasicType()));
2622 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002623 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002624 }
2625 }
2626
2627 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002628 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002629 return new TFunction(&tempString, type, op);
2630}
2631
Jamie Madillb98c3a82015-07-23 14:26:04 -04002632// This function is used to test for the correctness of the parameters passed to various constructor
2633// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634//
Olli Etuaho856c4972016-08-08 11:38:39 +03002635// 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 +00002636//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002637TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002638 TOperator op,
2639 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302640 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641{
Olli Etuaho856c4972016-08-08 11:38:39 +03002642 TType type = fnCall->getReturnType();
2643 if (type.isUnsizedArray())
2644 {
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002645 if (fnCall->getParamCount() == 0)
2646 {
2647 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2648 type.setArraySize(1u);
2649 return TIntermTyped::CreateZero(type);
2650 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002651 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2652 }
2653 bool constType = true;
2654 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2655 {
2656 const TConstParameter &param = fnCall->getParam(i);
2657 if (param.type->getQualifier() != EvqConst)
2658 constType = false;
2659 }
2660 if (constType)
2661 type.setQualifier(EvqConst);
2662
Olli Etuaho8a176262016-08-16 14:23:01 +03002663 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002664 {
2665 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2666 dummyNode->setType(type);
2667 return dummyNode;
2668 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002669 TIntermAggregate *constructor = arguments->getAsAggregate();
2670 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002671
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002672 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002673 constructor->setOp(op);
2674 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002675 ASSERT(constructor->isConstructor());
2676
2677 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002678 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679
Olli Etuaho21203702014-11-13 16:16:21 +02002680 // Structs should not be precision qualified, the individual members may be.
2681 // Built-in types on the other hand should be precision qualified.
2682 if (op != EOpConstructStruct)
2683 {
2684 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002685 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002686 }
2687
Olli Etuaho856c4972016-08-08 11:38:39 +03002688 constructor->setType(type);
2689
Olli Etuahof119a262016-08-19 15:54:22 +03002690 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002691 if (constConstructor)
2692 {
2693 return constConstructor;
2694 }
2695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002696 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002697}
2698
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002699//
2700// Interface/uniform blocks
2701//
Olli Etuaho13389b62016-10-16 11:48:18 +01002702TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002703 const TTypeQualifierBuilder &typeQualifierBuilder,
2704 const TSourceLoc &nameLine,
2705 const TString &blockName,
2706 TFieldList *fieldList,
2707 const TString *instanceName,
2708 const TSourceLoc &instanceLine,
2709 TIntermTyped *arrayIndex,
2710 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002711{
Olli Etuaho856c4972016-08-08 11:38:39 +03002712 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002713
Olli Etuaho613b9592016-09-05 12:05:53 +03002714 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002715
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002716 if (typeQualifier.qualifier != EvqUniform)
2717 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302718 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2719 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002720 }
2721
Martin Radev70866b82016-07-22 15:27:42 +03002722 if (typeQualifier.invariant)
2723 {
2724 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2725 }
2726
Martin Radev2cc85b32016-08-05 16:22:53 +03002727 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2728
Jamie Madill099c0f32013-06-20 11:55:52 -04002729 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002730 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002731
Jamie Madill099c0f32013-06-20 11:55:52 -04002732 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2733 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002734 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002735 }
2736
Jamie Madill1566ef72013-06-20 11:55:54 -04002737 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2738 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002739 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002740 }
2741
Olli Etuaho856c4972016-08-08 11:38:39 +03002742 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002743
Martin Radev2cc85b32016-08-05 16:22:53 +03002744 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2745
Arun Patole7e7e68d2015-05-22 12:02:25 +05302746 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2747 if (!symbolTable.declare(blockNameSymbol))
2748 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002749 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002750 }
2751
Jamie Madill98493dd2013-07-08 14:39:03 -04002752 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302753 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2754 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002755 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302756 TType *fieldType = field->type();
2757 if (IsSampler(fieldType->getBasicType()))
2758 {
2759 error(field->line(), "unsupported type", fieldType->getBasicString(),
2760 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002761 }
2762
Martin Radev2cc85b32016-08-05 16:22:53 +03002763 if (IsImage(fieldType->getBasicType()))
2764 {
2765 error(field->line(), "unsupported type", fieldType->getBasicString(),
2766 "image types are not allowed in interface blocks");
2767 }
2768
Jamie Madill98493dd2013-07-08 14:39:03 -04002769 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002770 switch (qualifier)
2771 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002772 case EvqGlobal:
2773 case EvqUniform:
2774 break;
2775 default:
2776 error(field->line(), "invalid qualifier on interface block member",
2777 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002778 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002779 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002780
Martin Radev70866b82016-07-22 15:27:42 +03002781 if (fieldType->isInvariant())
2782 {
2783 error(field->line(), "invalid qualifier on interface block member", "invariant");
2784 }
2785
Jamie Madilla5efff92013-06-06 11:56:47 -04002786 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002787 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002788 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002789
Jamie Madill98493dd2013-07-08 14:39:03 -04002790 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002791 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002792 error(field->line(), "invalid layout qualifier:",
2793 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002794 }
2795
Jamie Madill98493dd2013-07-08 14:39:03 -04002796 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002797 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002798 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002799 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002800 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002801 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002802 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002803 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2804 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002805 }
2806
Jamie Madill98493dd2013-07-08 14:39:03 -04002807 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002808 }
2809
Jamie Madill98493dd2013-07-08 14:39:03 -04002810 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002811 unsigned int arraySize = 0;
2812 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002813 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002814 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002815 }
2816
Jamie Madillb98c3a82015-07-23 14:26:04 -04002817 TInterfaceBlock *interfaceBlock =
2818 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2819 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2820 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002821
2822 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002823 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002824
Jamie Madill98493dd2013-07-08 14:39:03 -04002825 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002826 {
2827 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002828 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2829 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002830 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302831 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002832
2833 // set parent pointer of the field variable
2834 fieldType->setInterfaceBlock(interfaceBlock);
2835
Arun Patole7e7e68d2015-05-22 12:02:25 +05302836 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002837 fieldVariable->setQualifier(typeQualifier.qualifier);
2838
Arun Patole7e7e68d2015-05-22 12:02:25 +05302839 if (!symbolTable.declare(fieldVariable))
2840 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002841 error(field->line(), "redefinition", field->name().c_str(),
2842 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002843 }
2844 }
2845 }
2846 else
2847 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002848 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002849
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002850 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302851 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002852 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002853
Arun Patole7e7e68d2015-05-22 12:02:25 +05302854 if (!symbolTable.declare(instanceTypeDef))
2855 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002856 error(instanceLine, "redefinition", instanceName->c_str(),
2857 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002858 }
2859
Jamie Madillb98c3a82015-07-23 14:26:04 -04002860 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002861 symbolName = instanceTypeDef->getName();
2862 }
2863
Olli Etuaho13389b62016-10-16 11:48:18 +01002864 TIntermSymbol *blockSymbol =
2865 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2866 TIntermDeclaration *declaration = new TIntermDeclaration();
2867 declaration->appendDeclarator(blockSymbol);
2868 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002869
2870 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002871 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002872}
2873
Olli Etuaho383b7912016-08-05 11:22:59 +03002874void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002875{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002876 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002877
2878 // Embedded structure definitions are not supported per GLSL ES spec.
2879 // They aren't allowed in GLSL either, but we need to detect this here
2880 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302881 if (mStructNestingLevel > 1)
2882 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002883 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002884 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002885}
2886
2887void TParseContext::exitStructDeclaration()
2888{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002889 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002890}
2891
Olli Etuaho8a176262016-08-16 14:23:01 +03002892void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002893{
Jamie Madillacb4b812016-11-07 13:50:29 -05002894 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302895 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002896 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002897 }
2898
Arun Patole7e7e68d2015-05-22 12:02:25 +05302899 if (field.type()->getBasicType() != EbtStruct)
2900 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002901 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002902 }
2903
2904 // We're already inside a structure definition at this point, so add
2905 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302906 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2907 {
Jamie Madill41a49272014-03-18 16:10:13 -04002908 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002909 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2910 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002911 std::string reason = reasonStream.str();
2912 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002913 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002914 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002915}
2916
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002917//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002918// Parse an array index expression
2919//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002920TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2921 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302922 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002923{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002924 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2925 {
2926 if (baseExpression->getAsSymbolNode())
2927 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302928 error(location, " left of '[' is not of type array, matrix, or vector ",
2929 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002930 }
2931 else
2932 {
2933 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2934 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002935
2936 TConstantUnion *unionArray = new TConstantUnion[1];
2937 unionArray->setFConst(0.0f);
2938 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2939 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002940 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002941
Jamie Madill21c1e452014-12-29 11:33:41 -05002942 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2943
Olli Etuaho36b05142015-11-12 13:10:42 +02002944 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2945 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2946 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2947 // index is a constant expression.
2948 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2949 {
2950 if (baseExpression->isInterfaceBlock())
2951 {
2952 error(
2953 location, "", "[",
2954 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002955 }
2956 else if (baseExpression->getQualifier() == EvqFragmentOut)
2957 {
2958 error(location, "", "[",
2959 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002960 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002961 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2962 {
2963 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002964 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002965 }
2966
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002967 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002968 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002969 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2970 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2971 // constant fold expressions that are not constant expressions). The most compatible way to
2972 // handle this case is to report a warning instead of an error and force the index to be in
2973 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002974 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002975 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002976
2977 int safeIndex = -1;
2978
2979 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002980 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002981 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002982 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002983 if (mShaderSpec == SH_WEBGL2_SPEC)
2984 {
2985 // Error has been already generated if index is not const.
2986 if (indexExpression->getQualifier() == EvqConst)
2987 {
2988 error(location, "", "[",
2989 "array index for gl_FragData must be constant zero");
2990 }
2991 safeIndex = 0;
2992 }
2993 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2994 {
2995 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2996 "array index for gl_FragData must be zero when "
2997 "GL_EXT_draw_buffers is disabled");
2998 safeIndex = 0;
2999 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003000 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003001 // Only do generic out-of-range check if similar error hasn't already been reported.
3002 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003003 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003004 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3005 baseExpression->getArraySize(),
3006 "array index out of range", "[]");
3007 }
3008 }
3009 else if (baseExpression->isMatrix())
3010 {
3011 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003012 baseExpression->getType().getCols(),
3013 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04003014 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003015 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003016 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003017 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3018 baseExpression->getType().getNominalSize(),
3019 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003020 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003021
3022 ASSERT(safeIndex >= 0);
3023 // Data of constant unions can't be changed, because it may be shared with other
3024 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3025 // sanitized object.
3026 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003027 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003028 TConstantUnion *safeConstantUnion = new TConstantUnion();
3029 safeConstantUnion->setIConst(safeIndex);
3030 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003031 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003032
3033 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
3034 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003035 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003036 else
3037 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003038 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
3039 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003040 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003041}
3042
Olli Etuaho90892fb2016-07-14 14:44:51 +03003043int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3044 const TSourceLoc &location,
3045 int index,
3046 int arraySize,
3047 const char *reason,
3048 const char *token)
3049{
3050 if (index >= arraySize || index < 0)
3051 {
3052 std::stringstream extraInfoStream;
3053 extraInfoStream << "'" << index << "'";
3054 std::string extraInfo = extraInfoStream.str();
3055 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
3056 if (index < 0)
3057 {
3058 return 0;
3059 }
3060 else
3061 {
3062 return arraySize - 1;
3063 }
3064 }
3065 return index;
3066}
3067
Jamie Madillb98c3a82015-07-23 14:26:04 -04003068TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3069 const TSourceLoc &dotLocation,
3070 const TString &fieldString,
3071 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003072{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003073 if (baseExpression->isArray())
3074 {
3075 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003076 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003077 }
3078
3079 if (baseExpression->isVector())
3080 {
3081 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003082 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3083 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003084 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003085 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003086 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003087 }
3088
Olli Etuahob6fa0432016-09-28 16:28:05 +01003089 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003090 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003091 else if (baseExpression->getBasicType() == EbtStruct)
3092 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303093 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003094 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003095 {
3096 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003097 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003098 }
3099 else
3100 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003101 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003102 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003103 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003104 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003105 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003106 {
3107 fieldFound = true;
3108 break;
3109 }
3110 }
3111 if (fieldFound)
3112 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003113 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3114 index->setLine(fieldLocation);
3115 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
3116 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003117 }
3118 else
3119 {
3120 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003121 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003122 }
3123 }
3124 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003125 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003126 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303127 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003128 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003129 {
3130 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003131 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003132 }
3133 else
3134 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003135 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003136 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003137 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003138 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003139 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003140 {
3141 fieldFound = true;
3142 break;
3143 }
3144 }
3145 if (fieldFound)
3146 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003147 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3148 index->setLine(fieldLocation);
3149 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
3150 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003151 }
3152 else
3153 {
3154 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003155 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003156 }
3157 }
3158 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003159 else
3160 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003161 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003162 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003163 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303164 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003165 }
3166 else
3167 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303168 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003169 " field selection requires structure, vector, or interface block on left hand "
3170 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303171 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003172 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003173 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003174 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003175}
3176
Jamie Madillb98c3a82015-07-23 14:26:04 -04003177TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3178 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003179{
Martin Radev802abe02016-08-04 17:48:32 +03003180 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003181
3182 if (qualifierType == "shared")
3183 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003184 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003185 {
3186 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3187 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003188 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003189 }
3190 else if (qualifierType == "packed")
3191 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003192 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003193 {
3194 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3195 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003196 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003197 }
3198 else if (qualifierType == "std140")
3199 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003200 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003201 }
3202 else if (qualifierType == "row_major")
3203 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003204 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003205 }
3206 else if (qualifierType == "column_major")
3207 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003208 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003209 }
3210 else if (qualifierType == "location")
3211 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003212 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3213 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003214 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003215 else if (qualifierType == "rgba32f")
3216 {
3217 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3218 qualifier.imageInternalFormat = EiifRGBA32F;
3219 }
3220 else if (qualifierType == "rgba16f")
3221 {
3222 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3223 qualifier.imageInternalFormat = EiifRGBA16F;
3224 }
3225 else if (qualifierType == "r32f")
3226 {
3227 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3228 qualifier.imageInternalFormat = EiifR32F;
3229 }
3230 else if (qualifierType == "rgba8")
3231 {
3232 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3233 qualifier.imageInternalFormat = EiifRGBA8;
3234 }
3235 else if (qualifierType == "rgba8_snorm")
3236 {
3237 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3238 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3239 }
3240 else if (qualifierType == "rgba32i")
3241 {
3242 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3243 qualifier.imageInternalFormat = EiifRGBA32I;
3244 }
3245 else if (qualifierType == "rgba16i")
3246 {
3247 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3248 qualifier.imageInternalFormat = EiifRGBA16I;
3249 }
3250 else if (qualifierType == "rgba8i")
3251 {
3252 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3253 qualifier.imageInternalFormat = EiifRGBA8I;
3254 }
3255 else if (qualifierType == "r32i")
3256 {
3257 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3258 qualifier.imageInternalFormat = EiifR32I;
3259 }
3260 else if (qualifierType == "rgba32ui")
3261 {
3262 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3263 qualifier.imageInternalFormat = EiifRGBA32UI;
3264 }
3265 else if (qualifierType == "rgba16ui")
3266 {
3267 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3268 qualifier.imageInternalFormat = EiifRGBA16UI;
3269 }
3270 else if (qualifierType == "rgba8ui")
3271 {
3272 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3273 qualifier.imageInternalFormat = EiifRGBA8UI;
3274 }
3275 else if (qualifierType == "r32ui")
3276 {
3277 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3278 qualifier.imageInternalFormat = EiifR32UI;
3279 }
3280
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003281 else
3282 {
3283 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003284 }
3285
Jamie Madilla5efff92013-06-06 11:56:47 -04003286 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003287}
3288
Martin Radev802abe02016-08-04 17:48:32 +03003289void TParseContext::parseLocalSize(const TString &qualifierType,
3290 const TSourceLoc &qualifierTypeLine,
3291 int intValue,
3292 const TSourceLoc &intValueLine,
3293 const std::string &intValueString,
3294 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003295 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003296{
Olli Etuaho856c4972016-08-08 11:38:39 +03003297 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003298 if (intValue < 1)
3299 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003300 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03003301 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003302 }
3303 (*localSize)[index] = intValue;
3304}
3305
Jamie Madillb98c3a82015-07-23 14:26:04 -04003306TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3307 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003308 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303309 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003310{
Martin Radev802abe02016-08-04 17:48:32 +03003311 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003312
Martin Radev802abe02016-08-04 17:48:32 +03003313 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003314
Martin Radev802abe02016-08-04 17:48:32 +03003315 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003316 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003317 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003318 if (intValue < 0)
3319 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003320 error(intValueLine, "out of range:", intValueString.c_str(),
3321 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003322 }
3323 else
3324 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003325 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003326 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003327 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003328 }
Martin Radev802abe02016-08-04 17:48:32 +03003329 else if (qualifierType == "local_size_x")
3330 {
3331 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3332 &qualifier.localSize);
3333 }
3334 else if (qualifierType == "local_size_y")
3335 {
3336 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3337 &qualifier.localSize);
3338 }
3339 else if (qualifierType == "local_size_z")
3340 {
3341 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3342 &qualifier.localSize);
3343 }
3344 else
3345 {
3346 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003347 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003348
Jamie Madilla5efff92013-06-06 11:56:47 -04003349 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003350}
3351
Olli Etuaho613b9592016-09-05 12:05:53 +03003352TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3353{
3354 return new TTypeQualifierBuilder(
3355 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3356 mShaderVersion);
3357}
3358
Jamie Madillb98c3a82015-07-23 14:26:04 -04003359TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003360 TLayoutQualifier rightQualifier,
3361 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003362{
Martin Radevc28888b2016-07-22 15:27:42 +03003363 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
3364 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003365}
3366
Martin Radev70866b82016-07-22 15:27:42 +03003367TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3368 const TTypeQualifierBuilder &typeQualifierBuilder,
3369 TPublicType *typeSpecifier,
3370 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003371{
Olli Etuaho613b9592016-09-05 12:05:53 +03003372 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003373
Martin Radev70866b82016-07-22 15:27:42 +03003374 typeSpecifier->qualifier = typeQualifier.qualifier;
3375 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003376 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003377 typeSpecifier->invariant = typeQualifier.invariant;
3378 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303379 {
Martin Radev70866b82016-07-22 15:27:42 +03003380 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003381 }
Martin Radev70866b82016-07-22 15:27:42 +03003382 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003383}
3384
Jamie Madillb98c3a82015-07-23 14:26:04 -04003385TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3386 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003387{
Martin Radev4a9cd802016-09-01 16:51:51 +03003388 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3389 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003390
Martin Radev4a9cd802016-09-01 16:51:51 +03003391 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003392
Martin Radev4a9cd802016-09-01 16:51:51 +03003393 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003394
Arun Patole7e7e68d2015-05-22 12:02:25 +05303395 for (unsigned int i = 0; i < fieldList->size(); ++i)
3396 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003397 //
3398 // Careful not to replace already known aspects of type, like array-ness
3399 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303400 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003401 type->setBasicType(typeSpecifier.getBasicType());
3402 type->setPrimarySize(typeSpecifier.getPrimarySize());
3403 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003404 type->setPrecision(typeSpecifier.precision);
3405 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003406 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003407 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003408 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003409
3410 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303411 if (type->isArray())
3412 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003413 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003414 }
3415 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003416 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003417 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303418 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003419 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003420 }
3421
Martin Radev4a9cd802016-09-01 16:51:51 +03003422 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003423 }
3424
Jamie Madill98493dd2013-07-08 14:39:03 -04003425 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003426}
3427
Martin Radev4a9cd802016-09-01 16:51:51 +03003428TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3429 const TSourceLoc &nameLine,
3430 const TString *structName,
3431 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003432{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303433 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003434 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003435
Jamie Madill9b820842015-02-12 10:40:10 -05003436 // Store a bool in the struct if we're at global scope, to allow us to
3437 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003438 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003439
Jamie Madill98493dd2013-07-08 14:39:03 -04003440 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003441 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003442 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303443 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3444 if (!symbolTable.declare(userTypeDef))
3445 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003446 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003447 }
3448 }
3449
3450 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003451 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003452 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003453 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003454 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003455 switch (qualifier)
3456 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003457 case EvqGlobal:
3458 case EvqTemporary:
3459 break;
3460 default:
3461 error(field.line(), "invalid qualifier on struct member",
3462 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003463 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003464 }
Martin Radev70866b82016-07-22 15:27:42 +03003465 if (field.type()->isInvariant())
3466 {
3467 error(field.line(), "invalid qualifier on struct member", "invariant");
3468 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003469 if (IsImage(field.type()->getBasicType()))
3470 {
3471 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3472 }
3473
3474 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003475
3476 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003477 }
3478
Martin Radev4a9cd802016-09-01 16:51:51 +03003479 TTypeSpecifierNonArray typeSpecifierNonArray;
3480 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3481 typeSpecifierNonArray.userDef = structureType;
3482 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003483 exitStructDeclaration();
3484
Martin Radev4a9cd802016-09-01 16:51:51 +03003485 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003486}
3487
Jamie Madillb98c3a82015-07-23 14:26:04 -04003488TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003489 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003490 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003491{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003492 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003493 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003494 init->isVector())
3495 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003496 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3497 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003498 return nullptr;
3499 }
3500
Olli Etuahoac5274d2015-02-20 10:19:08 +02003501 if (statementList)
3502 {
3503 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3504 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003505 return nullptr;
3506 }
3507 }
3508
Olli Etuahoa3a36662015-02-17 13:46:51 +02003509 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3510 if (node == nullptr)
3511 {
3512 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003513 return nullptr;
3514 }
3515 return node;
3516}
3517
3518TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3519{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003520 if (mSwitchNestingLevel == 0)
3521 {
3522 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003523 return nullptr;
3524 }
3525 if (condition == nullptr)
3526 {
3527 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003528 return nullptr;
3529 }
3530 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003531 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003532 {
3533 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003534 }
3535 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003536 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3537 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3538 // fold in case labels.
3539 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003540 {
3541 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003542 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003543 TIntermCase *node = intermediate.addCase(condition, loc);
3544 if (node == nullptr)
3545 {
3546 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003547 return nullptr;
3548 }
3549 return node;
3550}
3551
3552TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3553{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003554 if (mSwitchNestingLevel == 0)
3555 {
3556 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003557 return nullptr;
3558 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003559 TIntermCase *node = intermediate.addCase(nullptr, loc);
3560 if (node == nullptr)
3561 {
3562 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003563 return nullptr;
3564 }
3565 return node;
3566}
3567
Jamie Madillb98c3a82015-07-23 14:26:04 -04003568TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3569 TIntermTyped *child,
3570 const TSourceLoc &loc,
3571 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003572{
3573 if (child == nullptr)
3574 {
3575 return nullptr;
3576 }
3577
3578 switch (op)
3579 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003580 case EOpLogicalNot:
3581 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3582 child->isVector())
3583 {
3584 return nullptr;
3585 }
3586 break;
3587 case EOpBitwiseNot:
3588 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3589 child->isMatrix() || child->isArray())
3590 {
3591 return nullptr;
3592 }
3593 break;
3594 case EOpPostIncrement:
3595 case EOpPreIncrement:
3596 case EOpPostDecrement:
3597 case EOpPreDecrement:
3598 case EOpNegative:
3599 case EOpPositive:
3600 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003601 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003602 {
3603 return nullptr;
3604 }
3605 // Operators for built-ins are already type checked against their prototype.
3606 default:
3607 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003608 }
3609
Olli Etuahof119a262016-08-19 15:54:22 +03003610 TIntermUnary *node = new TIntermUnary(op, child);
3611 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003612
3613 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3614 if (foldedNode)
3615 return foldedNode;
3616
3617 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003618}
3619
Olli Etuaho09b22472015-02-11 11:47:26 +02003620TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3621{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003622 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003623 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003624 {
3625 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003626 return child;
3627 }
3628 return node;
3629}
3630
Jamie Madillb98c3a82015-07-23 14:26:04 -04003631TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3632 TIntermTyped *child,
3633 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003634{
Olli Etuaho856c4972016-08-08 11:38:39 +03003635 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003636 return addUnaryMath(op, child, loc);
3637}
3638
Jamie Madillb98c3a82015-07-23 14:26:04 -04003639bool TParseContext::binaryOpCommonCheck(TOperator op,
3640 TIntermTyped *left,
3641 TIntermTyped *right,
3642 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003643{
Olli Etuaho244be012016-08-18 15:26:02 +03003644 if (left->getType().getStruct() || right->getType().getStruct())
3645 {
3646 switch (op)
3647 {
3648 case EOpIndexDirectStruct:
3649 ASSERT(left->getType().getStruct());
3650 break;
3651 case EOpEqual:
3652 case EOpNotEqual:
3653 case EOpAssign:
3654 case EOpInitialize:
3655 if (left->getType() != right->getType())
3656 {
3657 return false;
3658 }
3659 break;
3660 default:
3661 error(loc, "Invalid operation for structs", GetOperatorString(op));
3662 return false;
3663 }
3664 }
3665
Olli Etuahod6b14282015-03-17 14:31:35 +02003666 if (left->isArray() || right->isArray())
3667 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003668 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003669 {
3670 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3671 return false;
3672 }
3673
3674 if (left->isArray() != right->isArray())
3675 {
3676 error(loc, "array / non-array mismatch", GetOperatorString(op));
3677 return false;
3678 }
3679
3680 switch (op)
3681 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003682 case EOpEqual:
3683 case EOpNotEqual:
3684 case EOpAssign:
3685 case EOpInitialize:
3686 break;
3687 default:
3688 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3689 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003690 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003691 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003692 if (left->getArraySize() != right->getArraySize())
3693 {
3694 error(loc, "array size mismatch", GetOperatorString(op));
3695 return false;
3696 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003697 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003698
3699 // Check ops which require integer / ivec parameters
3700 bool isBitShift = false;
3701 switch (op)
3702 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003703 case EOpBitShiftLeft:
3704 case EOpBitShiftRight:
3705 case EOpBitShiftLeftAssign:
3706 case EOpBitShiftRightAssign:
3707 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3708 // check that the basic type is an integer type.
3709 isBitShift = true;
3710 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3711 {
3712 return false;
3713 }
3714 break;
3715 case EOpBitwiseAnd:
3716 case EOpBitwiseXor:
3717 case EOpBitwiseOr:
3718 case EOpBitwiseAndAssign:
3719 case EOpBitwiseXorAssign:
3720 case EOpBitwiseOrAssign:
3721 // It is enough to check the type of only one operand, since later it
3722 // is checked that the operand types match.
3723 if (!IsInteger(left->getBasicType()))
3724 {
3725 return false;
3726 }
3727 break;
3728 default:
3729 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003730 }
3731
3732 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3733 // So the basic type should usually match.
3734 if (!isBitShift && left->getBasicType() != right->getBasicType())
3735 {
3736 return false;
3737 }
3738
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003739 // Check that:
3740 // 1. Type sizes match exactly on ops that require that.
3741 // 2. Restrictions for structs that contain arrays or samplers are respected.
3742 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003743 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003744 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003745 case EOpAssign:
3746 case EOpInitialize:
3747 case EOpEqual:
3748 case EOpNotEqual:
3749 // ESSL 1.00 sections 5.7, 5.8, 5.9
3750 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3751 {
3752 error(loc, "undefined operation for structs containing arrays",
3753 GetOperatorString(op));
3754 return false;
3755 }
3756 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3757 // we interpret the spec so that this extends to structs containing samplers,
3758 // similarly to ESSL 1.00 spec.
3759 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3760 left->getType().isStructureContainingSamplers())
3761 {
3762 error(loc, "undefined operation for structs containing samplers",
3763 GetOperatorString(op));
3764 return false;
3765 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003766
3767 if ((op == EOpAssign || op == EOpInitialize) &&
3768 left->getType().isStructureContainingImages())
3769 {
3770 error(loc, "undefined operation for structs containing images",
3771 GetOperatorString(op));
3772 return false;
3773 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003774 case EOpLessThan:
3775 case EOpGreaterThan:
3776 case EOpLessThanEqual:
3777 case EOpGreaterThanEqual:
3778 if ((left->getNominalSize() != right->getNominalSize()) ||
3779 (left->getSecondarySize() != right->getSecondarySize()))
3780 {
3781 return false;
3782 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003783 break;
3784 case EOpAdd:
3785 case EOpSub:
3786 case EOpDiv:
3787 case EOpIMod:
3788 case EOpBitShiftLeft:
3789 case EOpBitShiftRight:
3790 case EOpBitwiseAnd:
3791 case EOpBitwiseXor:
3792 case EOpBitwiseOr:
3793 case EOpAddAssign:
3794 case EOpSubAssign:
3795 case EOpDivAssign:
3796 case EOpIModAssign:
3797 case EOpBitShiftLeftAssign:
3798 case EOpBitShiftRightAssign:
3799 case EOpBitwiseAndAssign:
3800 case EOpBitwiseXorAssign:
3801 case EOpBitwiseOrAssign:
3802 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3803 {
3804 return false;
3805 }
3806
3807 // Are the sizes compatible?
3808 if (left->getNominalSize() != right->getNominalSize() ||
3809 left->getSecondarySize() != right->getSecondarySize())
3810 {
3811 // If the nominal sizes of operands do not match:
3812 // One of them must be a scalar.
3813 if (!left->isScalar() && !right->isScalar())
3814 return false;
3815
3816 // In the case of compound assignment other than multiply-assign,
3817 // the right side needs to be a scalar. Otherwise a vector/matrix
3818 // would be assigned to a scalar. A scalar can't be shifted by a
3819 // vector either.
3820 if (!right->isScalar() &&
3821 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3822 return false;
3823 }
3824 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003825 default:
3826 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003827 }
3828
Olli Etuahod6b14282015-03-17 14:31:35 +02003829 return true;
3830}
3831
Olli Etuaho1dded802016-08-18 18:13:13 +03003832bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3833 const TType &left,
3834 const TType &right)
3835{
3836 switch (op)
3837 {
3838 case EOpMul:
3839 case EOpMulAssign:
3840 return left.getNominalSize() == right.getNominalSize() &&
3841 left.getSecondarySize() == right.getSecondarySize();
3842 case EOpVectorTimesScalar:
3843 return true;
3844 case EOpVectorTimesScalarAssign:
3845 ASSERT(!left.isMatrix() && !right.isMatrix());
3846 return left.isVector() && !right.isVector();
3847 case EOpVectorTimesMatrix:
3848 return left.getNominalSize() == right.getRows();
3849 case EOpVectorTimesMatrixAssign:
3850 ASSERT(!left.isMatrix() && right.isMatrix());
3851 return left.isVector() && left.getNominalSize() == right.getRows() &&
3852 left.getNominalSize() == right.getCols();
3853 case EOpMatrixTimesVector:
3854 return left.getCols() == right.getNominalSize();
3855 case EOpMatrixTimesScalar:
3856 return true;
3857 case EOpMatrixTimesScalarAssign:
3858 ASSERT(left.isMatrix() && !right.isMatrix());
3859 return !right.isVector();
3860 case EOpMatrixTimesMatrix:
3861 return left.getCols() == right.getRows();
3862 case EOpMatrixTimesMatrixAssign:
3863 ASSERT(left.isMatrix() && right.isMatrix());
3864 // We need to check two things:
3865 // 1. The matrix multiplication step is valid.
3866 // 2. The result will have the same number of columns as the lvalue.
3867 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3868
3869 default:
3870 UNREACHABLE();
3871 return false;
3872 }
3873}
3874
Jamie Madillb98c3a82015-07-23 14:26:04 -04003875TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3876 TIntermTyped *left,
3877 TIntermTyped *right,
3878 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003879{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003880 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003881 return nullptr;
3882
Olli Etuahofc1806e2015-03-17 13:03:11 +02003883 switch (op)
3884 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003885 case EOpEqual:
3886 case EOpNotEqual:
3887 break;
3888 case EOpLessThan:
3889 case EOpGreaterThan:
3890 case EOpLessThanEqual:
3891 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003892 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3893 !right->getType().getStruct());
3894 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003895 {
3896 return nullptr;
3897 }
3898 break;
3899 case EOpLogicalOr:
3900 case EOpLogicalXor:
3901 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003902 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3903 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003904 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003905 {
3906 return nullptr;
3907 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003908 // Basic types matching should have been already checked.
3909 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003910 break;
3911 case EOpAdd:
3912 case EOpSub:
3913 case EOpDiv:
3914 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003915 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3916 !right->getType().getStruct());
3917 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003918 {
3919 return nullptr;
3920 }
3921 break;
3922 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003923 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3924 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003925 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003926 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003927 {
3928 return nullptr;
3929 }
3930 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003931 default:
3932 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003933 }
3934
Olli Etuaho1dded802016-08-18 18:13:13 +03003935 if (op == EOpMul)
3936 {
3937 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3938 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3939 {
3940 return nullptr;
3941 }
3942 }
3943
Olli Etuaho3fdec912016-08-18 15:08:06 +03003944 TIntermBinary *node = new TIntermBinary(op, left, right);
3945 node->setLine(loc);
3946
Olli Etuaho3fdec912016-08-18 15:08:06 +03003947 // See if we can fold constants.
3948 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3949 if (foldedNode)
3950 return foldedNode;
3951
3952 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003953}
3954
Jamie Madillb98c3a82015-07-23 14:26:04 -04003955TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3956 TIntermTyped *left,
3957 TIntermTyped *right,
3958 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003959{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003960 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003961 if (node == 0)
3962 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003963 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3964 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003965 return left;
3966 }
3967 return node;
3968}
3969
Jamie Madillb98c3a82015-07-23 14:26:04 -04003970TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3971 TIntermTyped *left,
3972 TIntermTyped *right,
3973 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003974{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003975 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003976 if (node == 0)
3977 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003978 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3979 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003980 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003981 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003982 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3983 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003984 }
3985 return node;
3986}
3987
Olli Etuaho13389b62016-10-16 11:48:18 +01003988TIntermBinary *TParseContext::createAssign(TOperator op,
3989 TIntermTyped *left,
3990 TIntermTyped *right,
3991 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003992{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003993 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003994 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003995 if (op == EOpMulAssign)
3996 {
3997 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3998 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3999 {
4000 return nullptr;
4001 }
4002 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004003 TIntermBinary *node = new TIntermBinary(op, left, right);
4004 node->setLine(loc);
4005
Olli Etuaho3fdec912016-08-18 15:08:06 +03004006 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004007 }
4008 return nullptr;
4009}
4010
Jamie Madillb98c3a82015-07-23 14:26:04 -04004011TIntermTyped *TParseContext::addAssign(TOperator op,
4012 TIntermTyped *left,
4013 TIntermTyped *right,
4014 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004015{
4016 TIntermTyped *node = createAssign(op, left, right, loc);
4017 if (node == nullptr)
4018 {
4019 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004020 return left;
4021 }
4022 return node;
4023}
4024
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004025TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4026 TIntermTyped *right,
4027 const TSourceLoc &loc)
4028{
Corentin Wallez0d959252016-07-12 17:26:32 -04004029 // WebGL2 section 5.26, the following results in an error:
4030 // "Sequence operator applied to void, arrays, or structs containing arrays"
4031 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
4032 left->getType().isStructureContainingArrays() ||
4033 right->isArray() || right->getBasicType() == EbtVoid ||
4034 right->getType().isStructureContainingArrays()))
4035 {
4036 error(loc,
4037 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4038 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004039 }
4040
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004041 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004042}
4043
Olli Etuaho49300862015-02-20 14:54:49 +02004044TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4045{
4046 switch (op)
4047 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004048 case EOpContinue:
4049 if (mLoopNestingLevel <= 0)
4050 {
4051 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004052 }
4053 break;
4054 case EOpBreak:
4055 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4056 {
4057 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004058 }
4059 break;
4060 case EOpReturn:
4061 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4062 {
4063 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004064 }
4065 break;
4066 default:
4067 // No checks for discard
4068 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004069 }
4070 return intermediate.addBranch(op, loc);
4071}
4072
Jamie Madillb98c3a82015-07-23 14:26:04 -04004073TIntermBranch *TParseContext::addBranch(TOperator op,
4074 TIntermTyped *returnValue,
4075 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004076{
4077 ASSERT(op == EOpReturn);
4078 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004079 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004080 {
4081 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004082 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004083 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004084 {
4085 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004086 }
4087 return intermediate.addBranch(op, returnValue, loc);
4088}
4089
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004090void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4091{
4092 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004093 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004094 TIntermNode *offset = nullptr;
4095 TIntermSequence *arguments = functionCall->getSequence();
4096 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4097 name.compare(0, 16, "textureLodOffset") == 0 ||
4098 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4099 name.compare(0, 17, "textureGradOffset") == 0 ||
4100 name.compare(0, 21, "textureProjGradOffset") == 0)
4101 {
4102 offset = arguments->back();
4103 }
4104 else if (name.compare(0, 13, "textureOffset") == 0 ||
4105 name.compare(0, 17, "textureProjOffset") == 0)
4106 {
4107 // A bias parameter might follow the offset parameter.
4108 ASSERT(arguments->size() >= 3);
4109 offset = (*arguments)[2];
4110 }
4111 if (offset != nullptr)
4112 {
4113 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4114 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4115 {
4116 TString unmangledName = TFunction::unmangleName(name);
4117 error(functionCall->getLine(), "Texture offset must be a constant expression",
4118 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004119 }
4120 else
4121 {
4122 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4123 size_t size = offsetConstantUnion->getType().getObjectSize();
4124 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4125 for (size_t i = 0u; i < size; ++i)
4126 {
4127 int offsetValue = values[i].getIConst();
4128 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4129 {
4130 std::stringstream tokenStream;
4131 tokenStream << offsetValue;
4132 std::string token = tokenStream.str();
4133 error(offset->getLine(), "Texture offset value out of valid range",
4134 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004135 }
4136 }
4137 }
4138 }
4139}
4140
Martin Radev2cc85b32016-08-05 16:22:53 +03004141// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4142void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4143{
4144 ASSERT(!functionCall->isUserDefined());
4145 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4146
4147 if (name.compare(0, 5, "image") == 0)
4148 {
4149 TIntermSequence *arguments = functionCall->getSequence();
4150 TIntermNode *imageNode = (*arguments)[0];
4151 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4152
4153 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4154
4155 if (name.compare(5, 5, "Store") == 0)
4156 {
4157 if (memoryQualifier.readonly)
4158 {
4159 error(imageNode->getLine(),
4160 "'imageStore' cannot be used with images qualified as 'readonly'",
4161 imageSymbol->getSymbol().c_str());
4162 }
4163 }
4164 else if (name.compare(5, 4, "Load") == 0)
4165 {
4166 if (memoryQualifier.writeonly)
4167 {
4168 error(imageNode->getLine(),
4169 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4170 imageSymbol->getSymbol().c_str());
4171 }
4172 }
4173 }
4174}
4175
4176// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4177void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4178 const TFunction *functionDefinition,
4179 const TIntermAggregate *functionCall)
4180{
4181 ASSERT(functionCall->isUserDefined());
4182
4183 const TIntermSequence &arguments = *functionCall->getSequence();
4184
4185 ASSERT(functionDefinition->getParamCount() == arguments.size());
4186
4187 for (size_t i = 0; i < arguments.size(); ++i)
4188 {
4189 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4190 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4191 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4192
4193 if (IsImage(functionArgumentType.getBasicType()))
4194 {
4195 const TMemoryQualifier &functionArgumentMemoryQualifier =
4196 functionArgumentType.getMemoryQualifier();
4197 const TMemoryQualifier &functionParameterMemoryQualifier =
4198 functionParameterType.getMemoryQualifier();
4199 if (functionArgumentMemoryQualifier.readonly &&
4200 !functionParameterMemoryQualifier.readonly)
4201 {
4202 error(functionCall->getLine(),
4203 "Function call discards the 'readonly' qualifier from image",
4204 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4205 }
4206
4207 if (functionArgumentMemoryQualifier.writeonly &&
4208 !functionParameterMemoryQualifier.writeonly)
4209 {
4210 error(functionCall->getLine(),
4211 "Function call discards the 'writeonly' qualifier from image",
4212 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4213 }
4214 }
4215 }
4216}
4217
Jamie Madillb98c3a82015-07-23 14:26:04 -04004218TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4219 TIntermNode *paramNode,
4220 TIntermNode *thisNode,
4221 const TSourceLoc &loc,
4222 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004223{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004224 *fatalError = false;
4225 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004226 TIntermTyped *callNode = nullptr;
4227
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004228 if (thisNode != nullptr)
4229 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004230 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004231 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004232 TIntermTyped *typedThis = thisNode->getAsTyped();
4233 if (fnCall->getName() != "length")
4234 {
4235 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004236 }
4237 else if (paramNode != nullptr)
4238 {
4239 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004240 }
4241 else if (typedThis == nullptr || !typedThis->isArray())
4242 {
4243 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004244 }
4245 else
4246 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004247 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004248 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004249 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004250 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004251 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004252 // (func()).length()
4253 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004254 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4255 // expression.
4256 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4257 // spec section 5.9 which allows "An array, vector or matrix expression with the
4258 // length method applied".
4259 error(loc, "length can only be called on array names, not on array expressions",
4260 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004261 }
4262 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004263 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004264 callNode =
4265 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004266 }
4267 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004268 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004269 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004270 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004271 }
4272 else
4273 {
4274 //
4275 // Not a constructor. Find it in the symbol table.
4276 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304277 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004278 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004279 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004280 if (fnCandidate)
4281 {
4282 //
4283 // A declared function.
4284 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004285 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004286 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004287 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004288 }
4289 op = fnCandidate->getBuiltInOp();
4290 if (builtIn && op != EOpNull)
4291 {
4292 //
4293 // A function call mapped to a built-in operation.
4294 //
4295 if (fnCandidate->getParamCount() == 1)
4296 {
4297 //
4298 // Treat it like a built-in unary operator.
4299 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004300 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4301 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004302 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4303 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004304 if (callNode == nullptr)
4305 {
4306 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004307 extraInfoStream
4308 << "built in unary operator function. Type: "
4309 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004310 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004311 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4312 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004313 *fatalError = true;
4314 return nullptr;
4315 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004316 }
4317 else
4318 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004319 TIntermAggregate *aggregate =
4320 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004321 aggregate->setType(fnCandidate->getReturnType());
4322 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004323 if (aggregate->areChildrenConstQualified())
4324 {
4325 aggregate->getTypePointer()->setQualifier(EvqConst);
4326 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004327
4328 // Some built-in functions have out parameters too.
4329 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304330
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004331 // See if we can constant fold a built-in. Note that this may be possible even
4332 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004333 TIntermTyped *foldedNode =
4334 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304335 if (foldedNode)
4336 {
Arun Patole274f0702015-05-05 13:33:30 +05304337 callNode = foldedNode;
4338 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004339 else
4340 {
4341 callNode = aggregate;
4342 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004343 }
4344 }
4345 else
4346 {
4347 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004348 TIntermAggregate *aggregate =
4349 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004350 aggregate->setType(fnCandidate->getReturnType());
4351
Jamie Madillb98c3a82015-07-23 14:26:04 -04004352 // this is how we know whether the given function is a builtIn function or a user
4353 // defined function
4354 // if builtIn == false, it's a userDefined -> could be an overloaded
4355 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004356 // if builtIn == true, it's definitely a builtIn function with EOpNull
4357 if (!builtIn)
4358 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004359 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004360
Olli Etuahobd674552016-10-06 13:28:42 +01004361 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004362 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004363 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004364 aggregate->setBuiltInFunctionPrecision();
4365
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004366 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004367
4368 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4369 }
4370 else
4371 {
4372 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004373 }
4374
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004375 callNode = aggregate;
4376
4377 functionCallLValueErrorCheck(fnCandidate, aggregate);
4378 }
4379 }
4380 else
4381 {
4382 // error message was put out by findFunction()
4383 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004384 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004385 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004386 callNode = intermediate.addConstantUnion(unionArray,
4387 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004388 }
4389 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004390 return callNode;
4391}
4392
Jamie Madillb98c3a82015-07-23 14:26:04 -04004393TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004394 TIntermTyped *trueExpression,
4395 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004396 const TSourceLoc &loc)
4397{
Olli Etuaho856c4972016-08-08 11:38:39 +03004398 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004399
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004400 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004401 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004402 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4403 falseExpression->getCompleteString());
4404 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004405 }
Olli Etuahode318b22016-10-25 16:18:25 +01004406 if (IsOpaqueType(trueExpression->getBasicType()))
4407 {
4408 // ESSL 1.00 section 4.1.7
4409 // ESSL 3.00 section 4.1.7
4410 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4411 // Note that structs containing opaque types don't need to be checked as structs are
4412 // forbidden below.
4413 error(loc, "ternary operator is not allowed for opaque types", ":");
4414 return falseExpression;
4415 }
4416
Olli Etuahoa2d53032015-04-15 14:14:44 +03004417 // ESSL1 sections 5.2 and 5.7:
4418 // ESSL3 section 5.7:
4419 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004420 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004421 {
4422 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004423 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004424 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004425 // WebGL2 section 5.26, the following results in an error:
4426 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004427 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004428 {
4429 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004430 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004431 }
4432
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004433 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004434}
Olli Etuaho49300862015-02-20 14:54:49 +02004435
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004436//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004437// Parse an array of strings using yyparse.
4438//
4439// Returns 0 for success.
4440//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004441int PaParseStrings(size_t count,
4442 const char *const string[],
4443 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304444 TParseContext *context)
4445{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004446 if ((count == 0) || (string == NULL))
4447 return 1;
4448
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004449 if (glslang_initialize(context))
4450 return 1;
4451
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004452 int error = glslang_scan(count, string, length, context);
4453 if (!error)
4454 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004455
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004456 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004457
alokp@chromium.org6b495712012-06-29 00:06:58 +00004458 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004459}
Jamie Madill45bcc782016-11-07 13:58:48 -05004460
4461} // namespace sh