blob: eb96d2accbf6e32d9c8330399bcc53b866b5b079 [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
alokp@chromium.org8b851c62012-06-15 16:25:11 +000019///////////////////////////////////////////////////////////////////////
20//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021// Sub- vector and matrix fields
22//
23////////////////////////////////////////////////////////////////////////
24
Martin Radev2cc85b32016-08-05 16:22:53 +030025namespace
26{
27
28const int kWebGLMaxStructNesting = 4;
29
30bool ContainsSampler(const TType &type)
31{
32 if (IsSampler(type.getBasicType()))
33 return true;
34
35 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
36 {
37 const TFieldList &fields = type.getStruct()->fields();
38 for (unsigned int i = 0; i < fields.size(); ++i)
39 {
40 if (ContainsSampler(*fields[i]->type()))
41 return true;
42 }
43 }
44
45 return false;
46}
47
48bool ContainsImage(const TType &type)
49{
50 if (IsImage(type.getBasicType()))
51 return true;
52
53 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
54 {
55 const TFieldList &fields = type.getStruct()->fields();
56 for (unsigned int i = 0; i < fields.size(); ++i)
57 {
58 if (ContainsImage(*fields[i]->type()))
59 return true;
60 }
61 }
62
63 return false;
64}
65
66} // namespace
67
Jamie Madillacb4b812016-11-07 13:50:29 -050068TParseContext::TParseContext(TSymbolTable &symt,
69 TExtensionBehavior &ext,
70 sh::GLenum type,
71 ShShaderSpec spec,
72 ShCompileOptions options,
73 bool checksPrecErrors,
74 TInfoSink &is,
75 const ShBuiltInResources &resources)
76 : intermediate(),
77 symbolTable(symt),
78 mDeferredSingleDeclarationErrorCheck(false),
79 mShaderType(type),
80 mShaderSpec(spec),
81 mCompileOptions(options),
82 mShaderVersion(100),
83 mTreeRoot(nullptr),
84 mLoopNestingLevel(0),
85 mStructNestingLevel(0),
86 mSwitchNestingLevel(0),
87 mCurrentFunctionType(nullptr),
88 mFunctionReturnsValue(false),
89 mChecksPrecisionErrors(checksPrecErrors),
90 mFragmentPrecisionHighOnESSL1(false),
91 mDefaultMatrixPacking(EmpColumnMajor),
92 mDefaultBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
93 mDiagnostics(is),
94 mDirectiveHandler(ext,
95 mDiagnostics,
96 mShaderVersion,
97 mShaderType,
98 resources.WEBGL_debug_shader_precision == 1),
99 mPreprocessor(&mDiagnostics, &mDirectiveHandler),
100 mScanner(nullptr),
101 mUsesFragData(false),
102 mUsesFragColor(false),
103 mUsesSecondaryOutputs(false),
104 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
105 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
106 mComputeShaderLocalSizeDeclared(false),
107 mDeclaringFunction(false)
108{
109 mComputeShaderLocalSize.fill(-1);
110}
111
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112//
113// Look at a '.' field selector string and change it into offsets
114// for a vector.
115//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400116bool TParseContext::parseVectorFields(const TString &compString,
117 int vecSize,
118 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530119 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400121 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530122 if (fields.num > 4)
123 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000124 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000125 return false;
126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
Jamie Madillb98c3a82015-07-23 14:26:04 -0400128 enum
129 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000130 exyzw,
131 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000132 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000133 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134
Arun Patole7e7e68d2015-05-22 12:02:25 +0530135 for (int i = 0; i < fields.num; ++i)
136 {
137 switch (compString[i])
138 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400139 case 'x':
140 fields.offsets[i] = 0;
141 fieldSet[i] = exyzw;
142 break;
143 case 'r':
144 fields.offsets[i] = 0;
145 fieldSet[i] = ergba;
146 break;
147 case 's':
148 fields.offsets[i] = 0;
149 fieldSet[i] = estpq;
150 break;
151 case 'y':
152 fields.offsets[i] = 1;
153 fieldSet[i] = exyzw;
154 break;
155 case 'g':
156 fields.offsets[i] = 1;
157 fieldSet[i] = ergba;
158 break;
159 case 't':
160 fields.offsets[i] = 1;
161 fieldSet[i] = estpq;
162 break;
163 case 'z':
164 fields.offsets[i] = 2;
165 fieldSet[i] = exyzw;
166 break;
167 case 'b':
168 fields.offsets[i] = 2;
169 fieldSet[i] = ergba;
170 break;
171 case 'p':
172 fields.offsets[i] = 2;
173 fieldSet[i] = estpq;
174 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530175
Jamie Madillb98c3a82015-07-23 14:26:04 -0400176 case 'w':
177 fields.offsets[i] = 3;
178 fieldSet[i] = exyzw;
179 break;
180 case 'a':
181 fields.offsets[i] = 3;
182 fieldSet[i] = ergba;
183 break;
184 case 'q':
185 fields.offsets[i] = 3;
186 fieldSet[i] = estpq;
187 break;
188 default:
189 error(line, "illegal vector field selection", compString.c_str());
190 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000191 }
192 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193
Arun Patole7e7e68d2015-05-22 12:02:25 +0530194 for (int i = 0; i < fields.num; ++i)
195 {
196 if (fields.offsets[i] >= vecSize)
197 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400198 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000199 return false;
200 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201
Arun Patole7e7e68d2015-05-22 12:02:25 +0530202 if (i > 0)
203 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400204 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530205 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400206 error(line, "illegal - vector component fields not from the same set",
207 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000208 return false;
209 }
210 }
211 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000212
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000213 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214}
215
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216///////////////////////////////////////////////////////////////////////
217//
218// Errors
219//
220////////////////////////////////////////////////////////////////////////
221
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222
223//
224// Used by flex/bison to output all syntax and parsing errors.
225//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530226void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400227 const char *reason,
228 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530229 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300231 mDiagnostics.error(loc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232}
233
Arun Patole7e7e68d2015-05-22 12:02:25 +0530234void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400235 const char *reason,
236 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530237 const char *extraInfo)
238{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300239 mDiagnostics.warning(loc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000240}
241
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200242void TParseContext::outOfRangeError(bool isError,
243 const TSourceLoc &loc,
244 const char *reason,
245 const char *token,
246 const char *extraInfo)
247{
248 if (isError)
249 {
250 error(loc, reason, token, extraInfo);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200251 }
252 else
253 {
254 warning(loc, reason, token, extraInfo);
255 }
256}
257
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258//
259// Same error message for all places assignments don't work.
260//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530261void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000263 std::stringstream extraInfoStream;
264 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
265 std::string extraInfo = extraInfoStream.str();
266 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000267}
268
269//
270// Same error message for all places unary operations don't work.
271//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530272void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000273{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000274 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400275 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
276 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000277 std::string extraInfo = extraInfoStream.str();
278 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279}
280
281//
282// Same error message for all binary operations don't work.
283//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400284void TParseContext::binaryOpError(const TSourceLoc &line,
285 const char *op,
286 TString left,
287 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000289 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400290 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
291 << left << "' and a right operand of type '" << right
292 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000293 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530294 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295}
296
Olli Etuaho856c4972016-08-08 11:38:39 +0300297void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
298 TPrecision precision,
299 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530300{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400301 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300302 return;
Martin Radev70866b82016-07-22 15:27:42 +0300303
304 if (precision != EbpUndefined && !SupportsPrecision(type))
305 {
306 error(line, "illegal type for precision qualifier", getBasicString(type));
307 }
308
Olli Etuaho183d7e22015-11-20 15:59:09 +0200309 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530310 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200311 switch (type)
312 {
313 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400314 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300315 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200316 case EbtInt:
317 case EbtUInt:
318 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400319 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300320 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200321 default:
322 if (IsSampler(type))
323 {
324 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300325 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200326 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300327 if (IsImage(type))
328 {
329 error(line, "No precision specified (image)", "");
330 return;
331 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200332 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000333 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000334}
335
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336// Both test and if necessary, spit out an error, to see if the node is really
337// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300338bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400340 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530341 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100342 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
343
344 if (swizzleNode)
345 {
346 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
347 if (ok && swizzleNode->hasDuplicateOffsets())
348 {
349 error(line, " l-value of swizzle cannot have duplicate components", op);
350 return false;
351 }
352 return ok;
353 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354
Arun Patole7e7e68d2015-05-22 12:02:25 +0530355 if (binaryNode)
356 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400357 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530358 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400359 case EOpIndexDirect:
360 case EOpIndexIndirect:
361 case EOpIndexDirectStruct:
362 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300363 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400364 default:
365 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000366 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000367 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300368 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370
Arun Patole7e7e68d2015-05-22 12:02:25 +0530371 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000372 if (symNode != 0)
373 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000374
Arun Patole7e7e68d2015-05-22 12:02:25 +0530375 const char *message = 0;
376 switch (node->getQualifier())
377 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400378 case EvqConst:
379 message = "can't modify a const";
380 break;
381 case EvqConstReadOnly:
382 message = "can't modify a const";
383 break;
384 case EvqAttribute:
385 message = "can't modify an attribute";
386 break;
387 case EvqFragmentIn:
388 message = "can't modify an input";
389 break;
390 case EvqVertexIn:
391 message = "can't modify an input";
392 break;
393 case EvqUniform:
394 message = "can't modify a uniform";
395 break;
396 case EvqVaryingIn:
397 message = "can't modify a varying";
398 break;
399 case EvqFragCoord:
400 message = "can't modify gl_FragCoord";
401 break;
402 case EvqFrontFacing:
403 message = "can't modify gl_FrontFacing";
404 break;
405 case EvqPointCoord:
406 message = "can't modify gl_PointCoord";
407 break;
Martin Radevb0883602016-08-04 17:48:58 +0300408 case EvqNumWorkGroups:
409 message = "can't modify gl_NumWorkGroups";
410 break;
411 case EvqWorkGroupSize:
412 message = "can't modify gl_WorkGroupSize";
413 break;
414 case EvqWorkGroupID:
415 message = "can't modify gl_WorkGroupID";
416 break;
417 case EvqLocalInvocationID:
418 message = "can't modify gl_LocalInvocationID";
419 break;
420 case EvqGlobalInvocationID:
421 message = "can't modify gl_GlobalInvocationID";
422 break;
423 case EvqLocalInvocationIndex:
424 message = "can't modify gl_LocalInvocationIndex";
425 break;
Martin Radev802abe02016-08-04 17:48:32 +0300426 case EvqComputeIn:
427 message = "can't modify work group size variable";
428 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400429 default:
430 //
431 // Type that can't be written to?
432 //
433 if (node->getBasicType() == EbtVoid)
434 {
435 message = "can't modify void";
436 }
437 if (IsSampler(node->getBasicType()))
438 {
439 message = "can't modify a sampler";
440 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300441 if (IsImage(node->getBasicType()))
442 {
443 message = "can't modify an image";
444 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000445 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000446
Arun Patole7e7e68d2015-05-22 12:02:25 +0530447 if (message == 0 && binaryNode == 0 && symNode == 0)
448 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000449 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450
Olli Etuaho8a176262016-08-16 14:23:01 +0300451 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000452 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 //
455 // Everything else is okay, no error.
456 //
457 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300458 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000460 //
461 // If we get here, we have an error and a message.
462 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530463 if (symNode)
464 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000465 std::stringstream extraInfoStream;
466 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
467 std::string extraInfo = extraInfoStream.str();
468 error(line, " l-value required", op, extraInfo.c_str());
469 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530470 else
471 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000472 std::stringstream extraInfoStream;
473 extraInfoStream << "(" << message << ")";
474 std::string extraInfo = extraInfoStream.str();
475 error(line, " l-value required", op, extraInfo.c_str());
476 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000477
Olli Etuaho8a176262016-08-16 14:23:01 +0300478 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000479}
480
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481// Both test, and if necessary spit out an error, to see if the node is really
482// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300483void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484{
Olli Etuaho383b7912016-08-05 11:22:59 +0300485 if (node->getQualifier() != EvqConst)
486 {
487 error(node->getLine(), "constant expression required", "");
488 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489}
490
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491// Both test, and if necessary spit out an error, to see if the node is really
492// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300493void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494{
Olli Etuaho383b7912016-08-05 11:22:59 +0300495 if (!node->isScalarInt())
496 {
497 error(node->getLine(), "integer expression required", token);
498 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000499}
500
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000501// Both test, and if necessary spit out an error, to see if we are currently
502// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800503bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000504{
Olli Etuaho856c4972016-08-08 11:38:39 +0300505 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300506 {
507 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800508 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300509 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800510 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000511}
512
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000513// For now, keep it simple: if it starts "gl_", it's reserved, independent
514// of scope. Except, if the symbol table is at the built-in push-level,
515// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000516// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
517// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300518bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530520 static const char *reservedErrMsg = "reserved built-in name";
521 if (!symbolTable.atBuiltInLevel())
522 {
523 if (identifier.compare(0, 3, "gl_") == 0)
524 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000525 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300526 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000527 }
Jamie Madillacb4b812016-11-07 13:50:29 -0500528 if (sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530529 {
530 if (identifier.compare(0, 6, "webgl_") == 0)
531 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000532 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300533 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000534 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530535 if (identifier.compare(0, 7, "_webgl_") == 0)
536 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000537 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300538 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000539 }
540 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530541 if (identifier.find("__") != TString::npos)
542 {
543 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400544 "identifiers containing two consecutive underscores (__) are reserved as "
545 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530546 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300547 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000548 }
549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000550
Olli Etuaho8a176262016-08-16 14:23:01 +0300551 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552}
553
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000554// Make sure there is enough data provided to the constructor to build
555// something of the type of the constructor. Also returns the type of
556// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300557bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
558 TIntermNode *argumentsNode,
559 const TFunction &function,
560 TOperator op,
561 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000563 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400564 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530565 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400566 case EOpConstructMat2:
567 case EOpConstructMat2x3:
568 case EOpConstructMat2x4:
569 case EOpConstructMat3x2:
570 case EOpConstructMat3:
571 case EOpConstructMat3x4:
572 case EOpConstructMat4x2:
573 case EOpConstructMat4x3:
574 case EOpConstructMat4:
575 constructingMatrix = true;
576 break;
577 default:
578 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000579 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000580
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000581 //
582 // Note: It's okay to have too many components available, but not okay to have unused
583 // arguments. 'full' will go to true when enough args have been seen. If we loop
584 // again, there is an extra argument, so 'overfull' will become true.
585 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000586
Jamie Madillb98c3a82015-07-23 14:26:04 -0400587 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400588 bool full = false;
589 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000590 bool matrixInMatrix = false;
591 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530592 for (size_t i = 0; i < function.getParamCount(); ++i)
593 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700594 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000595 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530596
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000597 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 matrixInMatrix = true;
599 if (full)
600 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300601 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000602 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000603 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 arrayArg = true;
605 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530606
Olli Etuaho856c4972016-08-08 11:38:39 +0300607 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300608 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300609 // The size of an unsized constructor should already have been determined.
610 ASSERT(!type.isUnsizedArray());
611 if (static_cast<size_t>(type.getArraySize()) != function.getParamCount())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300612 {
613 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300614 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300615 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617
Arun Patole7e7e68d2015-05-22 12:02:25 +0530618 if (arrayArg && op != EOpConstructStruct)
619 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000620 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300621 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000622 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623
Olli Etuaho856c4972016-08-08 11:38:39 +0300624 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530625 {
626 if (function.getParamCount() != 1)
627 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400628 error(line, "constructing matrix from matrix can only take one argument",
629 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300630 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000631 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000632 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000633
Arun Patole7e7e68d2015-05-22 12:02:25 +0530634 if (overFull)
635 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000636 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300637 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000638 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530639
Olli Etuaho856c4972016-08-08 11:38:39 +0300640 if (op == EOpConstructStruct && !type.isArray() &&
641 type.getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530642 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400643 error(line,
644 "Number of constructor parameters does not match the number of structure fields",
645 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300646 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000647 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648
Olli Etuaho856c4972016-08-08 11:38:39 +0300649 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530650 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300651 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
652 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530653 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000654 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300655 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000656 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000657 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000658
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200659 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530660 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200661 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300662 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000663 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200664
665 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
666 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530667 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200668 TIntermTyped *argTyped = argNode->getAsTyped();
669 ASSERT(argTyped != nullptr);
670 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
671 {
672 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300673 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200674 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300675 if (op != EOpConstructStruct && IsImage(argTyped->getBasicType()))
676 {
677 error(line, "cannot convert an image", "constructor");
678 return false;
679 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200680 if (argTyped->getBasicType() == EbtVoid)
681 {
682 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300683 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200684 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000685 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686
Olli Etuaho856c4972016-08-08 11:38:39 +0300687 if (type.isArray())
688 {
689 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
690 // the array.
691 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
692 {
693 const TType &argType = argNode->getAsTyped()->getType();
694 // It has already been checked that the argument is not an array.
695 ASSERT(!argType.isArray());
696 if (!argType.sameElementType(type))
697 {
698 error(line, "Array constructor argument has an incorrect type", "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300699 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300700 }
701 }
702 }
703 else if (op == EOpConstructStruct)
704 {
705 const TFieldList &fields = type.getStruct()->fields();
706 TIntermSequence *args = argumentsAgg->getSequence();
707
708 for (size_t i = 0; i < fields.size(); i++)
709 {
710 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
711 {
712 error(line, "Structure constructor arguments do not match structure fields",
713 "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300714 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300715 }
716 }
717 }
718
Olli Etuaho8a176262016-08-16 14:23:01 +0300719 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000720}
721
Jamie Madillb98c3a82015-07-23 14:26:04 -0400722// This function checks to see if a void variable has been declared and raise an error message for
723// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724//
725// returns true in case of an error
726//
Olli Etuaho856c4972016-08-08 11:38:39 +0300727bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400728 const TString &identifier,
729 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000730{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300731 if (type == EbtVoid)
732 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000733 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300734 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300735 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736
Olli Etuaho8a176262016-08-16 14:23:01 +0300737 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738}
739
Jamie Madillb98c3a82015-07-23 14:26:04 -0400740// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300741// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300742void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530744 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
745 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000746 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000748}
749
Jamie Madillb98c3a82015-07-23 14:26:04 -0400750// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300751// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300752void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753{
Martin Radev4a9cd802016-09-01 16:51:51 +0300754 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530755 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000756 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758}
759
Olli Etuaho856c4972016-08-08 11:38:39 +0300760bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300761 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400762 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530764 if (pType.type == EbtStruct)
765 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300766 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530767 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000768 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Olli Etuaho8a176262016-08-16 14:23:01 +0300769 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000770 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530771
Olli Etuaho8a176262016-08-16 14:23:01 +0300772 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530773 }
774 else if (IsSampler(pType.type))
775 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000776 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300777 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000778 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779
Olli Etuaho8a176262016-08-16 14:23:01 +0300780 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781}
782
Martin Radev2cc85b32016-08-05 16:22:53 +0300783bool TParseContext::checkIsNotImage(const TSourceLoc &line,
784 const TTypeSpecifierNonArray &pType,
785 const char *reason)
786{
787 if (pType.type == EbtStruct)
788 {
789 if (ContainsImage(*pType.userDef))
790 {
791 error(line, reason, getBasicString(pType.type), "(structure contains an image)");
792
793 return false;
794 }
795
796 return true;
797 }
798 else if (IsImage(pType.type))
799 {
800 error(line, reason, getBasicString(pType.type));
801
802 return false;
803 }
804
805 return true;
806}
807
Olli Etuaho856c4972016-08-08 11:38:39 +0300808void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
809 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400810{
811 if (pType.layoutQualifier.location != -1)
812 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400813 error(line, "location must only be specified for a single input or output variable",
814 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400815 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400816}
817
Olli Etuaho856c4972016-08-08 11:38:39 +0300818void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
819 const TLayoutQualifier &layoutQualifier)
820{
821 if (layoutQualifier.location != -1)
822 {
823 error(location, "invalid layout qualifier:", "location",
824 "only valid on program inputs and outputs");
825 }
826}
827
Martin Radev2cc85b32016-08-05 16:22:53 +0300828void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
829 TQualifier qualifier,
830 const TType &type)
831{
832 checkOutParameterIsNotSampler(line, qualifier, type);
833 checkOutParameterIsNotImage(line, qualifier, type);
834}
835
Olli Etuaho856c4972016-08-08 11:38:39 +0300836void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
837 TQualifier qualifier,
838 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839{
Martin Radev2cc85b32016-08-05 16:22:53 +0300840 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
841 if (IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530842 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000843 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000844 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845}
846
Martin Radev2cc85b32016-08-05 16:22:53 +0300847void TParseContext::checkOutParameterIsNotImage(const TSourceLoc &line,
848 TQualifier qualifier,
849 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850{
Martin Radev2cc85b32016-08-05 16:22:53 +0300851 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
852 if (IsImage(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530853 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300854 error(line, "images cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000855 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300859unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530861 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000862
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200863 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
864 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
865 // fold as array size.
866 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000867 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000868 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300869 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000870 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871
Olli Etuaho856c4972016-08-08 11:38:39 +0300872 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400873
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000874 if (constant->getBasicType() == EbtUInt)
875 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300876 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000877 }
878 else
879 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300880 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000881
Olli Etuaho856c4972016-08-08 11:38:39 +0300882 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000883 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400884 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300885 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000886 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400887
Olli Etuaho856c4972016-08-08 11:38:39 +0300888 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400889 }
890
Olli Etuaho856c4972016-08-08 11:38:39 +0300891 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400892 {
893 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300894 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400895 }
896
897 // The size of arrays is restricted here to prevent issues further down the
898 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
899 // 4096 registers so this should be reasonable even for aggressively optimizable code.
900 const unsigned int sizeLimit = 65536;
901
Olli Etuaho856c4972016-08-08 11:38:39 +0300902 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400903 {
904 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300905 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300907
908 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909}
910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300912bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
913 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914{
Olli Etuaho8a176262016-08-16 14:23:01 +0300915 if ((elementQualifier.qualifier == EvqAttribute) ||
916 (elementQualifier.qualifier == EvqVertexIn) ||
917 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300918 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400919 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300920 TType(elementQualifier).getQualifierString());
921 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000922 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923
Olli Etuaho8a176262016-08-16 14:23:01 +0300924 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925}
926
Olli Etuaho8a176262016-08-16 14:23:01 +0300927// See if this element type can be formed into an array.
928bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000930 //
931 // Can the type be an array?
932 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300933 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400934 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300935 error(line, "cannot declare arrays of arrays",
936 TType(elementType).getCompleteString().c_str());
937 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000938 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300939 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
940 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
941 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300942 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300943 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300944 {
945 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300946 TType(elementType).getCompleteString().c_str());
947 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300948 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949
Olli Etuaho8a176262016-08-16 14:23:01 +0300950 return true;
951}
952
953// Check if this qualified element type can be formed into an array.
954bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
955 const TPublicType &elementType)
956{
957 if (checkIsValidTypeForArray(indexLocation, elementType))
958 {
959 return checkIsValidQualifierForArray(indexLocation, elementType);
960 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000961 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962}
963
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300965void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
966 const TString &identifier,
967 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968{
Olli Etuaho3739d232015-04-08 12:23:44 +0300969 ASSERT(type != nullptr);
970 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000971 {
972 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300973 type->qualifier = EvqTemporary;
974
975 // Generate informative error messages for ESSL1.
976 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400977 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000978 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530979 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400980 "structures containing arrays may not be declared constant since they cannot be "
981 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530982 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000983 }
984 else
985 {
986 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
987 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300988 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000989 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300990 if (type->isUnsizedArray())
991 {
992 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300993 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994}
995
Olli Etuaho2935c582015-04-08 14:32:06 +0300996// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997// and update the symbol table.
998//
Olli Etuaho2935c582015-04-08 14:32:06 +0300999// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001001bool TParseContext::declareVariable(const TSourceLoc &line,
1002 const TString &identifier,
1003 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001004 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005{
Olli Etuaho2935c582015-04-08 14:32:06 +03001006 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007
Olli Etuaho856c4972016-08-08 11:38:39 +03001008 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009
Olli Etuaho2935c582015-04-08 14:32:06 +03001010 // gl_LastFragData may be redeclared with a new precision qualifier
1011 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1012 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001013 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1014 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001015 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001016 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001017 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001018 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001019 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001020 }
1021 }
1022 else
1023 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001024 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1025 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001026 return false;
1027 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001028 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029
Olli Etuaho8a176262016-08-16 14:23:01 +03001030 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001031 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032
Olli Etuaho2935c582015-04-08 14:32:06 +03001033 (*variable) = new TVariable(&identifier, type);
1034 if (!symbolTable.declare(*variable))
1035 {
1036 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001037 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001038 return false;
1039 }
1040
Olli Etuaho8a176262016-08-16 14:23:01 +03001041 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001042 return false;
1043
1044 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045}
1046
Martin Radev70866b82016-07-22 15:27:42 +03001047void TParseContext::checkIsParameterQualifierValid(
1048 const TSourceLoc &line,
1049 const TTypeQualifierBuilder &typeQualifierBuilder,
1050 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301051{
Olli Etuaho613b9592016-09-05 12:05:53 +03001052 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001053
1054 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301055 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001056 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1057 }
1058
1059 if (!IsImage(type->getBasicType()))
1060 {
1061 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, line);
1062 }
1063 else
1064 {
1065 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001066 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067
Martin Radev70866b82016-07-22 15:27:42 +03001068 type->setQualifier(typeQualifier.qualifier);
1069
1070 if (typeQualifier.precision != EbpUndefined)
1071 {
1072 type->setPrecision(typeQualifier.precision);
1073 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074}
1075
Olli Etuaho856c4972016-08-08 11:38:39 +03001076bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001077{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001078 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001079 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301080 if (iter == extBehavior.end())
1081 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001082 error(line, "extension", extension.c_str(), "is not supported");
Olli Etuaho8a176262016-08-16 14:23:01 +03001083 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001084 }
zmo@google.comf5450912011-09-09 01:37:19 +00001085 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301086 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1087 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001088 error(line, "extension", extension.c_str(), "is disabled");
Olli Etuaho8a176262016-08-16 14:23:01 +03001089 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001090 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301091 if (iter->second == EBhWarn)
1092 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001093 warning(line, "extension", extension.c_str(), "is being used");
Olli Etuaho8a176262016-08-16 14:23:01 +03001094 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001095 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096
Olli Etuaho8a176262016-08-16 14:23:01 +03001097 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098}
1099
Jamie Madillb98c3a82015-07-23 14:26:04 -04001100// These checks are common for all declarations starting a declarator list, and declarators that
1101// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +03001102void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001103 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001104{
Olli Etuahofa33d582015-04-09 14:33:12 +03001105 switch (publicType.qualifier)
1106 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001107 case EvqVaryingIn:
1108 case EvqVaryingOut:
1109 case EvqAttribute:
1110 case EvqVertexIn:
1111 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001112 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001113 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001114 {
1115 error(identifierLocation, "cannot be used with a structure",
1116 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001117 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001118 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001119
Jamie Madillb98c3a82015-07-23 14:26:04 -04001120 default:
1121 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001122 }
1123
Jamie Madillb98c3a82015-07-23 14:26:04 -04001124 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001125 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1126 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001127 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001128 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001129 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001130 if (publicType.qualifier != EvqUniform &&
1131 !checkIsNotImage(identifierLocation, publicType.typeSpecifierNonArray,
1132 "images must be uniform"))
1133 {
1134 return;
1135 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001136
1137 // check for layout qualifier issues
1138 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1139
1140 if (layoutQualifier.matrixPacking != EmpUnspecified)
1141 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001142 error(identifierLocation, "layout qualifier",
1143 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001144 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001145 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001146 }
1147
1148 if (layoutQualifier.blockStorage != EbsUnspecified)
1149 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001150 error(identifierLocation, "layout qualifier",
1151 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001152 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001153 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001154 }
1155
Olli Etuaho383b7912016-08-05 11:22:59 +03001156 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001157 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001158 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001159 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001160
1161 if (IsImage(publicType.getBasicType()))
1162 {
1163
1164 switch (layoutQualifier.imageInternalFormat)
1165 {
1166 case EiifRGBA32F:
1167 case EiifRGBA16F:
1168 case EiifR32F:
1169 case EiifRGBA8:
1170 case EiifRGBA8_SNORM:
1171 if (!IsFloatImage(publicType.getBasicType()))
1172 {
1173 error(identifierLocation,
1174 "internal image format requires a floating image type",
1175 getBasicString(publicType.getBasicType()));
1176 return;
1177 }
1178 break;
1179 case EiifRGBA32I:
1180 case EiifRGBA16I:
1181 case EiifRGBA8I:
1182 case EiifR32I:
1183 if (!IsIntegerImage(publicType.getBasicType()))
1184 {
1185 error(identifierLocation,
1186 "internal image format requires an integer image type",
1187 getBasicString(publicType.getBasicType()));
1188 return;
1189 }
1190 break;
1191 case EiifRGBA32UI:
1192 case EiifRGBA16UI:
1193 case EiifRGBA8UI:
1194 case EiifR32UI:
1195 if (!IsUnsignedImage(publicType.getBasicType()))
1196 {
1197 error(identifierLocation,
1198 "internal image format requires an unsigned image type",
1199 getBasicString(publicType.getBasicType()));
1200 return;
1201 }
1202 break;
1203 case EiifUnspecified:
1204 error(identifierLocation, "layout qualifier", "No image internal format specified");
1205 return;
1206 default:
1207 error(identifierLocation, "layout qualifier", "unrecognized token");
1208 return;
1209 }
1210
1211 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1212 switch (layoutQualifier.imageInternalFormat)
1213 {
1214 case EiifR32F:
1215 case EiifR32I:
1216 case EiifR32UI:
1217 break;
1218 default:
1219 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1220 {
1221 error(identifierLocation, "layout qualifier",
1222 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1223 "image variables must be qualified readonly and/or writeonly");
1224 return;
1225 }
1226 break;
1227 }
1228 }
1229 else
1230 {
1231
1232 if (!checkInternalFormatIsNotSpecified(identifierLocation,
1233 layoutQualifier.imageInternalFormat))
1234 {
1235 return;
1236 }
1237
1238 if (!checkIsMemoryQualifierNotSpecified(publicType.memoryQualifier, identifierLocation))
1239 {
1240 return;
1241 }
1242 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001243}
1244
Olli Etuaho856c4972016-08-08 11:38:39 +03001245void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1246 const TString &layoutQualifierName,
1247 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001248{
1249
1250 if (mShaderVersion < versionRequired)
1251 {
1252 error(location, "invalid layout qualifier:", layoutQualifierName.c_str(), "not supported");
Martin Radev802abe02016-08-04 17:48:32 +03001253 }
1254}
1255
Olli Etuaho856c4972016-08-08 11:38:39 +03001256bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1257 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001258{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001259 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001260 for (size_t i = 0u; i < localSize.size(); ++i)
1261 {
1262 if (localSize[i] != -1)
1263 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001264 error(location, "invalid layout qualifier:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03001265 "only valid when used with 'in' in a compute shader global layout declaration");
Olli Etuaho8a176262016-08-16 14:23:01 +03001266 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001267 }
1268 }
1269
Olli Etuaho8a176262016-08-16 14:23:01 +03001270 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001271}
1272
Martin Radev2cc85b32016-08-05 16:22:53 +03001273bool TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
1274 TLayoutImageInternalFormat internalFormat)
1275{
1276 if (internalFormat != EiifUnspecified)
1277 {
1278 error(location, "invalid layout qualifier:", getImageInternalFormatString(internalFormat),
1279 "only valid when used with images");
1280 return false;
1281 }
1282 return true;
1283}
1284
Olli Etuaho383b7912016-08-05 11:22:59 +03001285void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001286 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001287{
1288 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1289 {
1290 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1291 if (qual == EvqOut || qual == EvqInOut)
1292 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001293 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001294 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001295 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001296 error(argument->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001297 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03001298 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001299 }
1300 }
1301 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001302}
1303
Martin Radev70866b82016-07-22 15:27:42 +03001304void TParseContext::checkInvariantVariableQualifier(bool invariant,
1305 const TQualifier qualifier,
1306 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001307{
Martin Radev70866b82016-07-22 15:27:42 +03001308 if (!invariant)
1309 return;
1310
1311 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001312 {
Martin Radev70866b82016-07-22 15:27:42 +03001313 // input variables in the fragment shader can be also qualified as invariant
1314 if (!sh::CanBeInvariantESSL1(qualifier))
1315 {
1316 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1317 }
1318 }
1319 else
1320 {
1321 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1322 {
1323 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1324 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001325 }
1326}
1327
Arun Patole7e7e68d2015-05-22 12:02:25 +05301328bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001329{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001330 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001331 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1332 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001333}
1334
Arun Patole7e7e68d2015-05-22 12:02:25 +05301335bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001336{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001337 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001338}
1339
Jamie Madillb98c3a82015-07-23 14:26:04 -04001340void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1341 const char *extName,
1342 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001343{
1344 pp::SourceLocation srcLoc;
1345 srcLoc.file = loc.first_file;
1346 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001347 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001348}
1349
Jamie Madillb98c3a82015-07-23 14:26:04 -04001350void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1351 const char *name,
1352 const char *value,
1353 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001354{
1355 pp::SourceLocation srcLoc;
1356 srcLoc.file = loc.first_file;
1357 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001358 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001359}
1360
Martin Radev4c4c8e72016-08-04 12:25:34 +03001361sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001362{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001363 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001364 for (size_t i = 0u; i < result.size(); ++i)
1365 {
1366 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1367 {
1368 result[i] = 1;
1369 }
1370 else
1371 {
1372 result[i] = mComputeShaderLocalSize[i];
1373 }
1374 }
1375 return result;
1376}
1377
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001378/////////////////////////////////////////////////////////////////////////////////
1379//
1380// Non-Errors.
1381//
1382/////////////////////////////////////////////////////////////////////////////////
1383
Jamie Madill5c097022014-08-20 16:38:32 -04001384const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1385 const TString *name,
1386 const TSymbol *symbol)
1387{
1388 const TVariable *variable = NULL;
1389
1390 if (!symbol)
1391 {
1392 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001393 }
1394 else if (!symbol->isVariable())
1395 {
1396 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001397 }
1398 else
1399 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001400 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001401
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001402 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001403 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001404 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001405 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001406 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001407
1408 // Reject shaders using both gl_FragData and gl_FragColor
1409 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001410 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001411 {
1412 mUsesFragData = true;
1413 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001414 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001415 {
1416 mUsesFragColor = true;
1417 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001418 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1419 {
1420 mUsesSecondaryOutputs = true;
1421 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001422
1423 // This validation is not quite correct - it's only an error to write to
1424 // both FragData and FragColor. For simplicity, and because users shouldn't
1425 // be rewarded for reading from undefined varaibles, return an error
1426 // if they are both referenced, rather than assigned.
1427 if (mUsesFragData && mUsesFragColor)
1428 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001429 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1430 if (mUsesSecondaryOutputs)
1431 {
1432 errorMessage =
1433 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1434 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1435 }
1436 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001437 }
Martin Radevb0883602016-08-04 17:48:58 +03001438
1439 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1440 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1441 qualifier == EvqWorkGroupSize)
1442 {
1443 error(location,
1444 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1445 "gl_WorkGroupSize");
1446 }
Jamie Madill5c097022014-08-20 16:38:32 -04001447 }
1448
1449 if (!variable)
1450 {
1451 TType type(EbtFloat, EbpUndefined);
1452 TVariable *fakeVariable = new TVariable(name, type);
1453 symbolTable.declare(fakeVariable);
1454 variable = fakeVariable;
1455 }
1456
1457 return variable;
1458}
1459
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001460TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1461 const TString *name,
1462 const TSymbol *symbol)
1463{
1464 const TVariable *variable = getNamedVariable(location, name, symbol);
1465
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001466 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001467 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001468 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001469 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001470 }
1471 else
1472 {
1473 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1474 variable->getType(), location);
1475 }
1476}
1477
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001478//
1479// Look up a function name in the symbol table, and make sure it is a function.
1480//
1481// Return the function symbol if found, otherwise 0.
1482//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001483const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1484 TFunction *call,
1485 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301486 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001487{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001488 // First find by unmangled name to check whether the function name has been
1489 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001490 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301491 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1492 if (symbol == 0 || symbol->isFunction())
1493 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001494 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001495 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001496
Arun Patole7e7e68d2015-05-22 12:02:25 +05301497 if (symbol == 0)
1498 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001499 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001500 return 0;
1501 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001502
Arun Patole7e7e68d2015-05-22 12:02:25 +05301503 if (!symbol->isFunction())
1504 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001505 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001506 return 0;
1507 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001508
Jamie Madillb98c3a82015-07-23 14:26:04 -04001509 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001510}
1511
1512//
1513// Initializers show up in several places in the grammar. Have one set of
1514// code to handle them here.
1515//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001516// Returns true on error, false if no error
1517//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001518bool TParseContext::executeInitializer(const TSourceLoc &line,
1519 const TString &identifier,
1520 const TPublicType &pType,
1521 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001522 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001523{
Olli Etuaho13389b62016-10-16 11:48:18 +01001524 ASSERT(initNode != nullptr);
1525 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001526 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001527
Olli Etuaho2935c582015-04-08 14:32:06 +03001528 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001529 if (type.isUnsizedArray())
1530 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001531 // We have not checked yet whether the initializer actually is an array or not.
1532 if (initializer->isArray())
1533 {
1534 type.setArraySize(initializer->getArraySize());
1535 }
1536 else
1537 {
1538 // Having a non-array initializer for an unsized array will result in an error later,
1539 // so we don't generate an error message here.
1540 type.setArraySize(1u);
1541 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001542 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001543 if (!declareVariable(line, identifier, type, &variable))
1544 {
1545 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001546 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001547
Olli Etuahob0c645e2015-05-12 14:25:36 +03001548 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001549 if (symbolTable.atGlobalLevel() &&
1550 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001551 {
1552 // Error message does not completely match behavior with ESSL 1.00, but
1553 // we want to steer developers towards only using constant expressions.
1554 error(line, "global variable initializers must be constant expressions", "=");
1555 return true;
1556 }
1557 if (globalInitWarning)
1558 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001559 warning(
1560 line,
1561 "global variable initializers should be constant expressions "
1562 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1563 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001564 }
1565
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001566 //
1567 // identifier must be of type constant, a global, or a temporary
1568 //
1569 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301570 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1571 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001572 error(line, " cannot initialize this type of qualifier ",
1573 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001574 return true;
1575 }
1576 //
1577 // test for and propagate constant
1578 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001579
Arun Patole7e7e68d2015-05-22 12:02:25 +05301580 if (qualifier == EvqConst)
1581 {
1582 if (qualifier != initializer->getType().getQualifier())
1583 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001584 std::stringstream extraInfoStream;
1585 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1586 std::string extraInfo = extraInfoStream.str();
1587 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001588 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001589 return true;
1590 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301591 if (type != initializer->getType())
1592 {
1593 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001594 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001595 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001596 return true;
1597 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001598
1599 // Save the constant folded value to the variable if possible. For example array
1600 // initializers are not folded, since that way copying the array literal to multiple places
1601 // in the shader is avoided.
1602 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1603 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301604 if (initializer->getAsConstantUnion())
1605 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001606 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001607 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001608 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301609 }
1610 else if (initializer->getAsSymbolNode())
1611 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001612 const TSymbol *symbol =
1613 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1614 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001615
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001616 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001617 if (constArray)
1618 {
1619 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001620 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001621 return false;
1622 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001623 }
1624 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001625
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001626 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1627 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001628 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1629 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001630 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001631 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1632 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001635 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001636}
1637
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001638void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1639{
1640 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1641 typeSpecifier->getBasicType());
1642
1643 if (mShaderVersion < 300 && typeSpecifier->array)
1644 {
1645 error(typeSpecifier->getLine(), "not supported", "first-class array");
1646 typeSpecifier->clearArrayness();
1647 }
1648}
1649
Martin Radev70866b82016-07-22 15:27:42 +03001650TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301651 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001652{
Olli Etuaho613b9592016-09-05 12:05:53 +03001653 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001654
Martin Radev70866b82016-07-22 15:27:42 +03001655 TPublicType returnType = typeSpecifier;
1656 returnType.qualifier = typeQualifier.qualifier;
1657 returnType.invariant = typeQualifier.invariant;
1658 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001659 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001660 returnType.precision = typeSpecifier.precision;
1661
1662 if (typeQualifier.precision != EbpUndefined)
1663 {
1664 returnType.precision = typeQualifier.precision;
1665 }
1666
Martin Radev4a9cd802016-09-01 16:51:51 +03001667 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1668 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001669
Martin Radev4a9cd802016-09-01 16:51:51 +03001670 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1671 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001672
Martin Radev4a9cd802016-09-01 16:51:51 +03001673 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001674
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001675 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001676 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001677 if (typeSpecifier.array)
1678 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001679 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001680 returnType.clearArrayness();
1681 }
1682
Martin Radev70866b82016-07-22 15:27:42 +03001683 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001684 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001685 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001686 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001687 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001688 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001689
Martin Radev70866b82016-07-22 15:27:42 +03001690 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001691 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001692 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001693 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001694 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001695 }
1696 }
1697 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001698 {
Martin Radev70866b82016-07-22 15:27:42 +03001699 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001700 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001701 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001702 }
Martin Radev70866b82016-07-22 15:27:42 +03001703 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1704 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001705 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001706 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1707 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001708 }
Martin Radev70866b82016-07-22 15:27:42 +03001709 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001710 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001711 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001712 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001713 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001714 }
1715
1716 return returnType;
1717}
1718
Olli Etuaho856c4972016-08-08 11:38:39 +03001719void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1720 const TPublicType &type,
1721 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001722{
1723 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001724 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001725 {
1726 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001727 }
1728
1729 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1730 switch (qualifier)
1731 {
1732 case EvqVertexIn:
1733 // ESSL 3.00 section 4.3.4
1734 if (type.array)
1735 {
1736 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001737 }
1738 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1739 return;
1740 case EvqFragmentOut:
1741 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001742 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001743 {
1744 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001745 }
1746 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1747 return;
1748 default:
1749 break;
1750 }
1751
1752 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1753 // restrictions.
1754 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001755 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1756 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001757 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1758 {
1759 error(qualifierLocation, "must use 'flat' interpolation here",
1760 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001761 }
1762
Martin Radev4a9cd802016-09-01 16:51:51 +03001763 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001764 {
1765 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1766 // These restrictions are only implied by the ESSL 3.00 spec, but
1767 // the ESSL 3.10 spec lists these restrictions explicitly.
1768 if (type.array)
1769 {
1770 error(qualifierLocation, "cannot be an array of structures",
1771 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001772 }
1773 if (type.isStructureContainingArrays())
1774 {
1775 error(qualifierLocation, "cannot be a structure containing an array",
1776 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001777 }
1778 if (type.isStructureContainingType(EbtStruct))
1779 {
1780 error(qualifierLocation, "cannot be a structure containing a structure",
1781 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001782 }
1783 if (type.isStructureContainingType(EbtBool))
1784 {
1785 error(qualifierLocation, "cannot be a structure containing a bool",
1786 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001787 }
1788 }
1789}
1790
Martin Radev2cc85b32016-08-05 16:22:53 +03001791void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1792{
1793 if (qualifier.getType() == QtStorage)
1794 {
1795 const TStorageQualifierWrapper &storageQualifier =
1796 static_cast<const TStorageQualifierWrapper &>(qualifier);
1797 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1798 !symbolTable.atGlobalLevel())
1799 {
1800 error(storageQualifier.getLine(),
1801 "Local variables can only use the const storage qualifier.",
1802 storageQualifier.getQualifierString().c_str());
1803 }
1804 }
1805}
1806
1807bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1808 const TSourceLoc &location)
1809{
1810 if (memoryQualifier.readonly)
1811 {
1812 error(location, "Only allowed with images.", "readonly");
1813 return false;
1814 }
1815 if (memoryQualifier.writeonly)
1816 {
1817 error(location, "Only allowed with images.", "writeonly");
1818 return false;
1819 }
1820 return true;
1821}
1822
Olli Etuaho13389b62016-10-16 11:48:18 +01001823TIntermDeclaration *TParseContext::parseSingleDeclaration(
1824 TPublicType &publicType,
1825 const TSourceLoc &identifierOrTypeLocation,
1826 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001827{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001828 TType type(publicType);
1829 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1830 mDirectiveHandler.pragma().stdgl.invariantAll)
1831 {
1832 TQualifier qualifier = type.getQualifier();
1833
1834 // The directive handler has already taken care of rejecting invalid uses of this pragma
1835 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1836 // affected variable declarations:
1837 //
1838 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1839 // elsewhere, in TranslatorGLSL.)
1840 //
1841 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1842 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1843 // the way this is currently implemented we have to enable this compiler option before
1844 // parsing the shader and determining the shading language version it uses. If this were
1845 // implemented as a post-pass, the workaround could be more targeted.
1846 //
1847 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1848 // the specification, but there are desktop OpenGL drivers that expect that this is the
1849 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1850 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1851 {
1852 type.setInvariant(true);
1853 }
1854 }
1855
1856 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001857
Olli Etuahobab4c082015-04-24 16:38:49 +03001858 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001859
Olli Etuahobab4c082015-04-24 16:38:49 +03001860 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1861
Olli Etuaho13389b62016-10-16 11:48:18 +01001862 TIntermDeclaration *declaration = new TIntermDeclaration();
1863 declaration->setLine(identifierOrTypeLocation);
1864
Olli Etuahobab4c082015-04-24 16:38:49 +03001865 if (emptyDeclaration)
1866 {
1867 if (publicType.isUnsizedArray())
1868 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001869 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1870 // error. It is assumed that this applies to empty declarations as well.
1871 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1872 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001873 }
1874 }
1875 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001876 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001877 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001878
Olli Etuaho856c4972016-08-08 11:38:39 +03001879 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001880
Olli Etuaho2935c582015-04-08 14:32:06 +03001881 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001882 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001883
1884 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001885 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001886 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001887 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001888 }
1889
Olli Etuaho13389b62016-10-16 11:48:18 +01001890 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1891 // that may just declare a type.
1892 declaration->appendDeclarator(symbol);
1893
1894 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001895}
1896
Olli Etuaho13389b62016-10-16 11:48:18 +01001897TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1898 const TSourceLoc &identifierLocation,
1899 const TString &identifier,
1900 const TSourceLoc &indexLocation,
1901 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001902{
Olli Etuahofa33d582015-04-09 14:33:12 +03001903 mDeferredSingleDeclarationErrorCheck = false;
1904
Olli Etuaho383b7912016-08-05 11:22:59 +03001905 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001906
Olli Etuaho856c4972016-08-08 11:38:39 +03001907 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001908
Olli Etuaho8a176262016-08-16 14:23:01 +03001909 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001910
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001911 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001912
Olli Etuaho856c4972016-08-08 11:38:39 +03001913 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001914 // Make the type an array even if size check failed.
1915 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1916 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001917
Olli Etuaho2935c582015-04-08 14:32:06 +03001918 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001919 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001920
Olli Etuaho13389b62016-10-16 11:48:18 +01001921 TIntermDeclaration *declaration = new TIntermDeclaration();
1922 declaration->setLine(identifierLocation);
1923
Olli Etuahoe7847b02015-03-16 11:56:12 +02001924 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001925 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001926 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001927 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001928 declaration->appendDeclarator(symbol);
1929 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001930
Olli Etuaho13389b62016-10-16 11:48:18 +01001931 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001932}
1933
Olli Etuaho13389b62016-10-16 11:48:18 +01001934TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1935 const TSourceLoc &identifierLocation,
1936 const TString &identifier,
1937 const TSourceLoc &initLocation,
1938 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001939{
Olli Etuahofa33d582015-04-09 14:33:12 +03001940 mDeferredSingleDeclarationErrorCheck = false;
1941
Olli Etuaho383b7912016-08-05 11:22:59 +03001942 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001943
Olli Etuaho13389b62016-10-16 11:48:18 +01001944 TIntermDeclaration *declaration = new TIntermDeclaration();
1945 declaration->setLine(identifierLocation);
1946
1947 TIntermBinary *initNode = nullptr;
1948 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001949 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001950 if (initNode)
1951 {
1952 declaration->appendDeclarator(initNode);
1953 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001954 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001955 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001956}
1957
Olli Etuaho13389b62016-10-16 11:48:18 +01001958TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04001959 TPublicType &publicType,
1960 const TSourceLoc &identifierLocation,
1961 const TString &identifier,
1962 const TSourceLoc &indexLocation,
1963 TIntermTyped *indexExpression,
1964 const TSourceLoc &initLocation,
1965 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001966{
1967 mDeferredSingleDeclarationErrorCheck = false;
1968
Olli Etuaho383b7912016-08-05 11:22:59 +03001969 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001970
Olli Etuaho8a176262016-08-16 14:23:01 +03001971 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001972
1973 TPublicType arrayType(publicType);
1974
Olli Etuaho856c4972016-08-08 11:38:39 +03001975 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001976 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1977 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001978 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001979 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001980 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001981 }
1982 // Make the type an array even if size check failed.
1983 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1984 arrayType.setArraySize(size);
1985
Olli Etuaho13389b62016-10-16 11:48:18 +01001986 TIntermDeclaration *declaration = new TIntermDeclaration();
1987 declaration->setLine(identifierLocation);
1988
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001989 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01001990 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001991 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1992 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001993 if (initNode)
1994 {
1995 declaration->appendDeclarator(initNode);
1996 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001997 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001998
1999 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002000}
2001
Martin Radev70866b82016-07-22 15:27:42 +03002002TIntermAggregate *TParseContext::parseInvariantDeclaration(
2003 const TTypeQualifierBuilder &typeQualifierBuilder,
2004 const TSourceLoc &identifierLoc,
2005 const TString *identifier,
2006 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002007{
Olli Etuaho613b9592016-09-05 12:05:53 +03002008 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002009
Martin Radev70866b82016-07-22 15:27:42 +03002010 if (!typeQualifier.invariant)
2011 {
2012 error(identifierLoc, "Expected invariant", identifier->c_str());
2013 return nullptr;
2014 }
2015 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2016 {
2017 return nullptr;
2018 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002019 if (!symbol)
2020 {
2021 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002022 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002023 }
Martin Radev70866b82016-07-22 15:27:42 +03002024 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002025 {
Martin Radev70866b82016-07-22 15:27:42 +03002026 error(identifierLoc, "invariant declaration specifies qualifier",
2027 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002028 }
Martin Radev70866b82016-07-22 15:27:42 +03002029 if (typeQualifier.precision != EbpUndefined)
2030 {
2031 error(identifierLoc, "invariant declaration specifies precision",
2032 getPrecisionString(typeQualifier.precision));
2033 }
2034 if (!typeQualifier.layoutQualifier.isEmpty())
2035 {
2036 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2037 }
2038
2039 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2040 ASSERT(variable);
2041 const TType &type = variable->getType();
2042
2043 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2044 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002045 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002046
2047 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2048
2049 TIntermSymbol *intermSymbol =
2050 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2051
Olli Etuaho32db19b2016-10-04 14:43:16 +01002052 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(intermSymbol, identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002053 aggregate->setOp(EOpInvariantDeclaration);
2054 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002055}
2056
Olli Etuaho13389b62016-10-16 11:48:18 +01002057void TParseContext::parseDeclarator(TPublicType &publicType,
2058 const TSourceLoc &identifierLocation,
2059 const TString &identifier,
2060 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002061{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002062 // If the declaration starting this declarator list was empty (example: int,), some checks were
2063 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002064 if (mDeferredSingleDeclarationErrorCheck)
2065 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002066 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002067 mDeferredSingleDeclarationErrorCheck = false;
2068 }
2069
Olli Etuaho856c4972016-08-08 11:38:39 +03002070 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002071
Olli Etuaho856c4972016-08-08 11:38:39 +03002072 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002073
Olli Etuaho2935c582015-04-08 14:32:06 +03002074 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002075 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002076
Jamie Madillb98c3a82015-07-23 14:26:04 -04002077 TIntermSymbol *symbol =
2078 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002079 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002080 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002081 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002082 declarationOut->appendDeclarator(symbol);
2083 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002084}
2085
Olli Etuaho13389b62016-10-16 11:48:18 +01002086void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2087 const TSourceLoc &identifierLocation,
2088 const TString &identifier,
2089 const TSourceLoc &arrayLocation,
2090 TIntermTyped *indexExpression,
2091 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002092{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002093 // If the declaration starting this declarator list was empty (example: int,), some checks were
2094 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002095 if (mDeferredSingleDeclarationErrorCheck)
2096 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002097 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002098 mDeferredSingleDeclarationErrorCheck = false;
2099 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002100
Olli Etuaho856c4972016-08-08 11:38:39 +03002101 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002102
Olli Etuaho856c4972016-08-08 11:38:39 +03002103 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002104
Olli Etuaho8a176262016-08-16 14:23:01 +03002105 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002106 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02002107 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002108 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002109 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002110
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002111 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002112 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002113
Jamie Madillb98c3a82015-07-23 14:26:04 -04002114 TIntermSymbol *symbol =
2115 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002116 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002117 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002118
Olli Etuaho13389b62016-10-16 11:48:18 +01002119 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002120 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002121}
2122
Olli Etuaho13389b62016-10-16 11:48:18 +01002123void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2124 const TSourceLoc &identifierLocation,
2125 const TString &identifier,
2126 const TSourceLoc &initLocation,
2127 TIntermTyped *initializer,
2128 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002129{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002130 // If the declaration starting this declarator list was empty (example: int,), some checks were
2131 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002132 if (mDeferredSingleDeclarationErrorCheck)
2133 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002134 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002135 mDeferredSingleDeclarationErrorCheck = false;
2136 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002137
Olli Etuaho856c4972016-08-08 11:38:39 +03002138 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002139
Olli Etuaho13389b62016-10-16 11:48:18 +01002140 TIntermBinary *initNode = nullptr;
2141 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002142 {
2143 //
2144 // build the intermediate representation
2145 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002146 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002147 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002148 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002149 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002150 }
2151}
2152
Olli Etuaho13389b62016-10-16 11:48:18 +01002153void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2154 const TSourceLoc &identifierLocation,
2155 const TString &identifier,
2156 const TSourceLoc &indexLocation,
2157 TIntermTyped *indexExpression,
2158 const TSourceLoc &initLocation,
2159 TIntermTyped *initializer,
2160 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002161{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002162 // If the declaration starting this declarator list was empty (example: int,), some checks were
2163 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002164 if (mDeferredSingleDeclarationErrorCheck)
2165 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002166 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002167 mDeferredSingleDeclarationErrorCheck = false;
2168 }
2169
Olli Etuaho856c4972016-08-08 11:38:39 +03002170 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002171
Olli Etuaho8a176262016-08-16 14:23:01 +03002172 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002173
2174 TPublicType arrayType(publicType);
2175
Olli Etuaho856c4972016-08-08 11:38:39 +03002176 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002177 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2178 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002179 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002180 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002181 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002182 }
2183 // Make the type an array even if size check failed.
2184 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2185 arrayType.setArraySize(size);
2186
2187 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002188 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002189 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2190 {
2191 if (initNode)
2192 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002193 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002194 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002195 }
2196}
2197
Martin Radev70866b82016-07-22 15:27:42 +03002198void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002199{
Olli Etuaho613b9592016-09-05 12:05:53 +03002200 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002201 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002202
Martin Radev70866b82016-07-22 15:27:42 +03002203 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2204 typeQualifier.line);
2205
Jamie Madillc2128ff2016-07-04 10:26:17 -04002206 // It should never be the case, but some strange parser errors can send us here.
2207 if (layoutQualifier.isEmpty())
2208 {
2209 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002210 return;
2211 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002212
Martin Radev802abe02016-08-04 17:48:32 +03002213 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002214 {
Martin Radev802abe02016-08-04 17:48:32 +03002215 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002216 return;
2217 }
2218
Martin Radev2cc85b32016-08-05 16:22:53 +03002219 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2220
2221 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2222
Martin Radev802abe02016-08-04 17:48:32 +03002223 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002224 {
Martin Radev802abe02016-08-04 17:48:32 +03002225 if (mComputeShaderLocalSizeDeclared &&
2226 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2227 {
2228 error(typeQualifier.line, "Work group size does not match the previous declaration",
2229 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002230 return;
2231 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002232
Martin Radev802abe02016-08-04 17:48:32 +03002233 if (mShaderVersion < 310)
2234 {
2235 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002236 return;
2237 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002238
Martin Radev4c4c8e72016-08-04 12:25:34 +03002239 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002240 {
2241 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002242 return;
2243 }
2244
2245 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2246 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2247
2248 const TConstantUnion *maxComputeWorkGroupSizeData =
2249 maxComputeWorkGroupSize->getConstPointer();
2250
2251 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2252 {
2253 if (layoutQualifier.localSize[i] != -1)
2254 {
2255 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2256 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2257 if (mComputeShaderLocalSize[i] < 1 ||
2258 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2259 {
2260 std::stringstream errorMessageStream;
2261 errorMessageStream << "Value must be at least 1 and no greater than "
2262 << maxComputeWorkGroupSizeValue;
2263 const std::string &errorMessage = errorMessageStream.str();
2264
Martin Radev4c4c8e72016-08-04 12:25:34 +03002265 error(typeQualifier.line, "invalid value:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03002266 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002267 return;
2268 }
2269 }
2270 }
2271
2272 mComputeShaderLocalSizeDeclared = true;
2273 }
2274 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002275 {
Martin Radev802abe02016-08-04 17:48:32 +03002276
Olli Etuaho8a176262016-08-16 14:23:01 +03002277 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002278 {
Martin Radev802abe02016-08-04 17:48:32 +03002279 return;
2280 }
2281
2282 if (typeQualifier.qualifier != EvqUniform)
2283 {
2284 error(typeQualifier.line, "invalid qualifier:",
2285 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03002286 return;
2287 }
2288
2289 if (mShaderVersion < 300)
2290 {
2291 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2292 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002293 return;
2294 }
2295
Olli Etuaho856c4972016-08-08 11:38:39 +03002296 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002297
2298 if (layoutQualifier.matrixPacking != EmpUnspecified)
2299 {
2300 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2301 }
2302
2303 if (layoutQualifier.blockStorage != EbsUnspecified)
2304 {
2305 mDefaultBlockStorage = layoutQualifier.blockStorage;
2306 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002307 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002308}
2309
Olli Etuaho476197f2016-10-11 13:59:08 +01002310TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002311 const TSourceLoc &location)
2312{
Olli Etuaho476197f2016-10-11 13:59:08 +01002313 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2314 // first declaration. Either way the instance in the symbol table is used to track whether the
2315 // function is declared multiple times.
2316 TFunction *function = static_cast<TFunction *>(
2317 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2318 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002319 {
2320 // ESSL 1.00.17 section 4.2.7.
2321 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2322 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002323 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002324 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002325
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002326 TIntermAggregate *prototype = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002327 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2328 // point to the data that already exists in the symbol table.
2329 prototype->setType(function->getReturnType());
Olli Etuahobd674552016-10-06 13:28:42 +01002330 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002331
Olli Etuaho476197f2016-10-11 13:59:08 +01002332 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002333 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002334 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002335 if (param.name != 0)
2336 {
2337 TVariable variable(param.name, *param.type);
2338
2339 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2340 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2341 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2342 }
2343 else
2344 {
2345 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2346 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2347 }
2348 }
2349
2350 prototype->setOp(EOpPrototype);
2351
2352 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002353
2354 if (!symbolTable.atGlobalLevel())
2355 {
2356 // ESSL 3.00.4 section 4.2.4.
2357 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002358 }
2359
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002360 return prototype;
2361}
2362
Olli Etuaho336b1472016-10-05 16:37:55 +01002363TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2364 const TFunction &function,
2365 TIntermAggregate *functionParameters,
2366 TIntermBlock *functionBody,
2367 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002368{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002369 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002370 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2371 {
2372 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002373 }
2374
Olli Etuahof51fdd22016-10-03 10:03:40 +01002375 if (functionBody == nullptr)
2376 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002377 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002378 functionBody->setLine(location);
2379 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002380 TIntermFunctionDefinition *functionNode =
2381 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2382 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002383
Olli Etuahobd674552016-10-06 13:28:42 +01002384 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002385
2386 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002387 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002388}
2389
Olli Etuaho476197f2016-10-11 13:59:08 +01002390void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2391 TFunction **function,
2392 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002393{
Olli Etuaho476197f2016-10-11 13:59:08 +01002394 ASSERT(function);
2395 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002396 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002397 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002398
2399 if (builtIn)
2400 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002401 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002402 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002403 else
Jamie Madill185fb402015-06-12 15:48:48 -04002404 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002405 TFunction *prevDec = static_cast<TFunction *>(
2406 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2407
2408 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2409 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2410 // occurance.
2411 if (*function != prevDec)
2412 {
2413 // Swap the parameters of the previous declaration to the parameters of the function
2414 // definition (parameter names may differ).
2415 prevDec->swapParameters(**function);
2416
2417 // The function definition will share the same symbol as any previous declaration.
2418 *function = prevDec;
2419 }
2420
2421 if ((*function)->isDefined())
2422 {
2423 error(location, "function already has a body", (*function)->getName().c_str());
2424 }
2425
2426 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002427 }
Jamie Madill185fb402015-06-12 15:48:48 -04002428
2429 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002430 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002431 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002432 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002433 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002434 error(location, "function cannot take any parameter(s)",
2435 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002436 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002437 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002438 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002439 error(location, "", (*function)->getReturnType().getBasicString(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04002440 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002441 }
2442 }
2443
2444 //
2445 // Remember the return type for later checking for RETURN statements.
2446 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002447 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002448 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002449
2450 //
2451 // Insert parameters into the symbol table.
2452 // If the parameter has no name, it's not an error, just don't insert it
2453 // (could be used for unused args).
2454 //
2455 // Also, accumulate the list of parameters into the HIL, so lower level code
2456 // knows where to find parameters.
2457 //
2458 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002459 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002460 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002461 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002462 if (param.name != 0)
2463 {
2464 TVariable *variable = new TVariable(param.name, *param.type);
2465 //
2466 // Insert the parameters with name in the symbol table.
2467 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002468 if (!symbolTable.declare(variable))
2469 {
Jamie Madill185fb402015-06-12 15:48:48 -04002470 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002471 paramNodes = intermediate.growAggregate(
2472 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2473 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002474 }
2475
2476 //
2477 // Add the parameter to the HIL
2478 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002479 TIntermSymbol *symbol = intermediate.addSymbol(
2480 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002481
2482 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2483 }
2484 else
2485 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002486 paramNodes = intermediate.growAggregate(
2487 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002488 }
2489 }
2490 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2491 *aggregateOut = paramNodes;
2492 setLoopNestingLevel(0);
2493}
2494
Jamie Madillb98c3a82015-07-23 14:26:04 -04002495TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002496{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002497 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002498 // We don't know at this point whether this is a function definition or a prototype.
2499 // The definition production code will check for redefinitions.
2500 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002501 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002502 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2503 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002504 //
2505 TFunction *prevDec =
2506 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302507
2508 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2509 {
2510 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2511 // Therefore overloading or redefining builtin functions is an error.
2512 error(location, "Name of a built-in function cannot be redeclared as function",
2513 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302514 }
2515 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002516 {
2517 if (prevDec->getReturnType() != function->getReturnType())
2518 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002519 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002520 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002521 }
2522 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2523 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002524 if (prevDec->getParam(i).type->getQualifier() !=
2525 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002526 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002527 error(location,
2528 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002529 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002530 }
2531 }
2532 }
2533
2534 //
2535 // Check for previously declared variables using the same name.
2536 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002537 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002538 if (prevSym)
2539 {
2540 if (!prevSym->isFunction())
2541 {
2542 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002543 }
2544 }
2545 else
2546 {
2547 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002548 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002549 }
2550
2551 // We're at the inner scope level of the function's arguments and body statement.
2552 // Add the function prototype to the surrounding scope instead.
2553 symbolTable.getOuterLevel()->insert(function);
2554
2555 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002556 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2557 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002558 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2559 //
2560 return function;
2561}
2562
Olli Etuaho9de84a52016-06-14 17:36:01 +03002563TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2564 const TString *name,
2565 const TSourceLoc &location)
2566{
2567 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2568 {
2569 error(location, "no qualifiers allowed for function return",
2570 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002571 }
2572 if (!type.layoutQualifier.isEmpty())
2573 {
2574 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002575 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002576 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002577 checkIsNotSampler(location, type.typeSpecifierNonArray,
2578 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002579 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002580 if (mShaderVersion < 300)
2581 {
2582 // Array return values are forbidden, but there's also no valid syntax for declaring array
2583 // return values in ESSL 1.00.
2584 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2585
2586 if (type.isStructureContainingArrays())
2587 {
2588 // ESSL 1.00.17 section 6.1 Function Definitions
2589 error(location, "structures containing arrays can't be function return values",
2590 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002591 }
2592 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002593
2594 // Add the function as a prototype after parsing it (we do not support recursion)
2595 return new TFunction(name, new TType(type));
2596}
2597
Jamie Madill06145232015-05-13 13:10:01 -04002598TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002599{
Jamie Madill06145232015-05-13 13:10:01 -04002600 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002601 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002602 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002603 error(publicType.getLine(), "constructor can't be a structure definition",
2604 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002605 }
2606
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002607 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002608 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002609 {
2610 op = EOpConstructStruct;
2611 }
2612 else
2613 {
Geoff Lang156d7192016-07-21 16:11:00 -04002614 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002615 if (op == EOpNull)
2616 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002617 error(publicType.getLine(), "cannot construct this type",
2618 getBasicString(publicType.getBasicType()));
2619 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002620 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002621 }
2622 }
2623
2624 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002625 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002626 return new TFunction(&tempString, type, op);
2627}
2628
Jamie Madillb98c3a82015-07-23 14:26:04 -04002629// This function is used to test for the correctness of the parameters passed to various constructor
2630// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631//
Olli Etuaho856c4972016-08-08 11:38:39 +03002632// 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 +00002633//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002634TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002635 TOperator op,
2636 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302637 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002638{
Olli Etuaho856c4972016-08-08 11:38:39 +03002639 TType type = fnCall->getReturnType();
2640 if (type.isUnsizedArray())
2641 {
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002642 if (fnCall->getParamCount() == 0)
2643 {
2644 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2645 type.setArraySize(1u);
2646 return TIntermTyped::CreateZero(type);
2647 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002648 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2649 }
2650 bool constType = true;
2651 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2652 {
2653 const TConstParameter &param = fnCall->getParam(i);
2654 if (param.type->getQualifier() != EvqConst)
2655 constType = false;
2656 }
2657 if (constType)
2658 type.setQualifier(EvqConst);
2659
Olli Etuaho8a176262016-08-16 14:23:01 +03002660 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002661 {
2662 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2663 dummyNode->setType(type);
2664 return dummyNode;
2665 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002666 TIntermAggregate *constructor = arguments->getAsAggregate();
2667 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002668
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002669 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002670 constructor->setOp(op);
2671 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002672 ASSERT(constructor->isConstructor());
2673
2674 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002675 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002676
Olli Etuaho21203702014-11-13 16:16:21 +02002677 // Structs should not be precision qualified, the individual members may be.
2678 // Built-in types on the other hand should be precision qualified.
2679 if (op != EOpConstructStruct)
2680 {
2681 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002682 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002683 }
2684
Olli Etuaho856c4972016-08-08 11:38:39 +03002685 constructor->setType(type);
2686
Olli Etuahof119a262016-08-19 15:54:22 +03002687 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002688 if (constConstructor)
2689 {
2690 return constConstructor;
2691 }
2692
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002693 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694}
2695
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002696//
2697// Interface/uniform blocks
2698//
Olli Etuaho13389b62016-10-16 11:48:18 +01002699TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002700 const TTypeQualifierBuilder &typeQualifierBuilder,
2701 const TSourceLoc &nameLine,
2702 const TString &blockName,
2703 TFieldList *fieldList,
2704 const TString *instanceName,
2705 const TSourceLoc &instanceLine,
2706 TIntermTyped *arrayIndex,
2707 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002708{
Olli Etuaho856c4972016-08-08 11:38:39 +03002709 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002710
Olli Etuaho613b9592016-09-05 12:05:53 +03002711 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002712
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002713 if (typeQualifier.qualifier != EvqUniform)
2714 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302715 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2716 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002717 }
2718
Martin Radev70866b82016-07-22 15:27:42 +03002719 if (typeQualifier.invariant)
2720 {
2721 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2722 }
2723
Martin Radev2cc85b32016-08-05 16:22:53 +03002724 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2725
Jamie Madill099c0f32013-06-20 11:55:52 -04002726 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002727 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002728
Jamie Madill099c0f32013-06-20 11:55:52 -04002729 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2730 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002731 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002732 }
2733
Jamie Madill1566ef72013-06-20 11:55:54 -04002734 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2735 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002736 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002737 }
2738
Olli Etuaho856c4972016-08-08 11:38:39 +03002739 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002740
Martin Radev2cc85b32016-08-05 16:22:53 +03002741 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2742
Arun Patole7e7e68d2015-05-22 12:02:25 +05302743 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2744 if (!symbolTable.declare(blockNameSymbol))
2745 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002746 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002747 }
2748
Jamie Madill98493dd2013-07-08 14:39:03 -04002749 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302750 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2751 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002752 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302753 TType *fieldType = field->type();
2754 if (IsSampler(fieldType->getBasicType()))
2755 {
2756 error(field->line(), "unsupported type", fieldType->getBasicString(),
2757 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002758 }
2759
Martin Radev2cc85b32016-08-05 16:22:53 +03002760 if (IsImage(fieldType->getBasicType()))
2761 {
2762 error(field->line(), "unsupported type", fieldType->getBasicString(),
2763 "image types are not allowed in interface blocks");
2764 }
2765
Jamie Madill98493dd2013-07-08 14:39:03 -04002766 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002767 switch (qualifier)
2768 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002769 case EvqGlobal:
2770 case EvqUniform:
2771 break;
2772 default:
2773 error(field->line(), "invalid qualifier on interface block member",
2774 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002775 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002776 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002777
Martin Radev70866b82016-07-22 15:27:42 +03002778 if (fieldType->isInvariant())
2779 {
2780 error(field->line(), "invalid qualifier on interface block member", "invariant");
2781 }
2782
Jamie Madilla5efff92013-06-06 11:56:47 -04002783 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002784 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002785 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002786
Jamie Madill98493dd2013-07-08 14:39:03 -04002787 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002788 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002789 error(field->line(), "invalid layout qualifier:",
2790 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002791 }
2792
Jamie Madill98493dd2013-07-08 14:39:03 -04002793 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002794 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002795 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002796 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002797 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002798 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002799 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002800 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2801 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002802 }
2803
Jamie Madill98493dd2013-07-08 14:39:03 -04002804 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002805 }
2806
Jamie Madill98493dd2013-07-08 14:39:03 -04002807 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002808 unsigned int arraySize = 0;
2809 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002810 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002811 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002812 }
2813
Jamie Madillb98c3a82015-07-23 14:26:04 -04002814 TInterfaceBlock *interfaceBlock =
2815 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2816 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2817 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002818
2819 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002820 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002821
Jamie Madill98493dd2013-07-08 14:39:03 -04002822 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002823 {
2824 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002825 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2826 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002827 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302828 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002829
2830 // set parent pointer of the field variable
2831 fieldType->setInterfaceBlock(interfaceBlock);
2832
Arun Patole7e7e68d2015-05-22 12:02:25 +05302833 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002834 fieldVariable->setQualifier(typeQualifier.qualifier);
2835
Arun Patole7e7e68d2015-05-22 12:02:25 +05302836 if (!symbolTable.declare(fieldVariable))
2837 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002838 error(field->line(), "redefinition", field->name().c_str(),
2839 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002840 }
2841 }
2842 }
2843 else
2844 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002845 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002846
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002847 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302848 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002849 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002850
Arun Patole7e7e68d2015-05-22 12:02:25 +05302851 if (!symbolTable.declare(instanceTypeDef))
2852 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002853 error(instanceLine, "redefinition", instanceName->c_str(),
2854 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002855 }
2856
Jamie Madillb98c3a82015-07-23 14:26:04 -04002857 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002858 symbolName = instanceTypeDef->getName();
2859 }
2860
Olli Etuaho13389b62016-10-16 11:48:18 +01002861 TIntermSymbol *blockSymbol =
2862 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2863 TIntermDeclaration *declaration = new TIntermDeclaration();
2864 declaration->appendDeclarator(blockSymbol);
2865 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002866
2867 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002868 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002869}
2870
Olli Etuaho383b7912016-08-05 11:22:59 +03002871void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002872{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002873 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002874
2875 // Embedded structure definitions are not supported per GLSL ES spec.
2876 // They aren't allowed in GLSL either, but we need to detect this here
2877 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302878 if (mStructNestingLevel > 1)
2879 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002880 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002881 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002882}
2883
2884void TParseContext::exitStructDeclaration()
2885{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002886 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002887}
2888
Olli Etuaho8a176262016-08-16 14:23:01 +03002889void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002890{
Jamie Madillacb4b812016-11-07 13:50:29 -05002891 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302892 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002893 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002894 }
2895
Arun Patole7e7e68d2015-05-22 12:02:25 +05302896 if (field.type()->getBasicType() != EbtStruct)
2897 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002898 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002899 }
2900
2901 // We're already inside a structure definition at this point, so add
2902 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302903 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2904 {
Jamie Madill41a49272014-03-18 16:10:13 -04002905 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002906 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2907 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002908 std::string reason = reasonStream.str();
2909 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002910 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002911 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002912}
2913
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002914//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002915// Parse an array index expression
2916//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002917TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2918 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302919 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002920{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002921 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2922 {
2923 if (baseExpression->getAsSymbolNode())
2924 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302925 error(location, " left of '[' is not of type array, matrix, or vector ",
2926 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002927 }
2928 else
2929 {
2930 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2931 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002932
2933 TConstantUnion *unionArray = new TConstantUnion[1];
2934 unionArray->setFConst(0.0f);
2935 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2936 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002937 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002938
Jamie Madill21c1e452014-12-29 11:33:41 -05002939 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2940
Olli Etuaho36b05142015-11-12 13:10:42 +02002941 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2942 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2943 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2944 // index is a constant expression.
2945 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2946 {
2947 if (baseExpression->isInterfaceBlock())
2948 {
2949 error(
2950 location, "", "[",
2951 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002952 }
2953 else if (baseExpression->getQualifier() == EvqFragmentOut)
2954 {
2955 error(location, "", "[",
2956 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002957 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002958 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2959 {
2960 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002961 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002962 }
2963
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002964 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002965 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002966 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2967 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2968 // constant fold expressions that are not constant expressions). The most compatible way to
2969 // handle this case is to report a warning instead of an error and force the index to be in
2970 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002971 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002972 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002973
2974 int safeIndex = -1;
2975
2976 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002977 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002978 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002979 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002980 if (mShaderSpec == SH_WEBGL2_SPEC)
2981 {
2982 // Error has been already generated if index is not const.
2983 if (indexExpression->getQualifier() == EvqConst)
2984 {
2985 error(location, "", "[",
2986 "array index for gl_FragData must be constant zero");
2987 }
2988 safeIndex = 0;
2989 }
2990 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2991 {
2992 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2993 "array index for gl_FragData must be zero when "
2994 "GL_EXT_draw_buffers is disabled");
2995 safeIndex = 0;
2996 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002997 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002998 // Only do generic out-of-range check if similar error hasn't already been reported.
2999 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003000 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003001 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3002 baseExpression->getArraySize(),
3003 "array index out of range", "[]");
3004 }
3005 }
3006 else if (baseExpression->isMatrix())
3007 {
3008 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003009 baseExpression->getType().getCols(),
3010 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04003011 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003012 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003013 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003014 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3015 baseExpression->getType().getNominalSize(),
3016 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003017 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003018
3019 ASSERT(safeIndex >= 0);
3020 // Data of constant unions can't be changed, because it may be shared with other
3021 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3022 // sanitized object.
3023 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003024 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003025 TConstantUnion *safeConstantUnion = new TConstantUnion();
3026 safeConstantUnion->setIConst(safeIndex);
3027 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003028 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003029
3030 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
3031 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003032 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003033 else
3034 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003035 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
3036 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003037 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003038}
3039
Olli Etuaho90892fb2016-07-14 14:44:51 +03003040int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3041 const TSourceLoc &location,
3042 int index,
3043 int arraySize,
3044 const char *reason,
3045 const char *token)
3046{
3047 if (index >= arraySize || index < 0)
3048 {
3049 std::stringstream extraInfoStream;
3050 extraInfoStream << "'" << index << "'";
3051 std::string extraInfo = extraInfoStream.str();
3052 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
3053 if (index < 0)
3054 {
3055 return 0;
3056 }
3057 else
3058 {
3059 return arraySize - 1;
3060 }
3061 }
3062 return index;
3063}
3064
Jamie Madillb98c3a82015-07-23 14:26:04 -04003065TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3066 const TSourceLoc &dotLocation,
3067 const TString &fieldString,
3068 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003069{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003070 if (baseExpression->isArray())
3071 {
3072 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003073 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003074 }
3075
3076 if (baseExpression->isVector())
3077 {
3078 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003079 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3080 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003081 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003082 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003083 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003084 }
3085
Olli Etuahob6fa0432016-09-28 16:28:05 +01003086 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003087 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003088 else if (baseExpression->getBasicType() == EbtStruct)
3089 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303090 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003091 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003092 {
3093 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003094 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003095 }
3096 else
3097 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003098 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003099 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003100 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003101 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003102 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003103 {
3104 fieldFound = true;
3105 break;
3106 }
3107 }
3108 if (fieldFound)
3109 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003110 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3111 index->setLine(fieldLocation);
3112 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
3113 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003114 }
3115 else
3116 {
3117 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003118 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003119 }
3120 }
3121 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003122 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003123 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303124 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003125 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003126 {
3127 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003128 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003129 }
3130 else
3131 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003132 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003133 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003134 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003135 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003136 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003137 {
3138 fieldFound = true;
3139 break;
3140 }
3141 }
3142 if (fieldFound)
3143 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003144 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3145 index->setLine(fieldLocation);
3146 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
3147 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003148 }
3149 else
3150 {
3151 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003152 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003153 }
3154 }
3155 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003156 else
3157 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003158 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003159 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003160 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303161 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003162 }
3163 else
3164 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303165 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003166 " field selection requires structure, vector, or interface block on left hand "
3167 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303168 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003169 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003170 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003171 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003172}
3173
Jamie Madillb98c3a82015-07-23 14:26:04 -04003174TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3175 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003176{
Martin Radev802abe02016-08-04 17:48:32 +03003177 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003178
3179 if (qualifierType == "shared")
3180 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003181 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003182 {
3183 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3184 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003185 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003186 }
3187 else if (qualifierType == "packed")
3188 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003189 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003190 {
3191 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3192 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003193 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003194 }
3195 else if (qualifierType == "std140")
3196 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003197 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003198 }
3199 else if (qualifierType == "row_major")
3200 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003201 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003202 }
3203 else if (qualifierType == "column_major")
3204 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003205 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003206 }
3207 else if (qualifierType == "location")
3208 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003209 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3210 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003211 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003212 else if (qualifierType == "rgba32f")
3213 {
3214 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3215 qualifier.imageInternalFormat = EiifRGBA32F;
3216 }
3217 else if (qualifierType == "rgba16f")
3218 {
3219 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3220 qualifier.imageInternalFormat = EiifRGBA16F;
3221 }
3222 else if (qualifierType == "r32f")
3223 {
3224 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3225 qualifier.imageInternalFormat = EiifR32F;
3226 }
3227 else if (qualifierType == "rgba8")
3228 {
3229 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3230 qualifier.imageInternalFormat = EiifRGBA8;
3231 }
3232 else if (qualifierType == "rgba8_snorm")
3233 {
3234 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3235 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3236 }
3237 else if (qualifierType == "rgba32i")
3238 {
3239 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3240 qualifier.imageInternalFormat = EiifRGBA32I;
3241 }
3242 else if (qualifierType == "rgba16i")
3243 {
3244 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3245 qualifier.imageInternalFormat = EiifRGBA16I;
3246 }
3247 else if (qualifierType == "rgba8i")
3248 {
3249 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3250 qualifier.imageInternalFormat = EiifRGBA8I;
3251 }
3252 else if (qualifierType == "r32i")
3253 {
3254 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3255 qualifier.imageInternalFormat = EiifR32I;
3256 }
3257 else if (qualifierType == "rgba32ui")
3258 {
3259 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3260 qualifier.imageInternalFormat = EiifRGBA32UI;
3261 }
3262 else if (qualifierType == "rgba16ui")
3263 {
3264 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3265 qualifier.imageInternalFormat = EiifRGBA16UI;
3266 }
3267 else if (qualifierType == "rgba8ui")
3268 {
3269 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3270 qualifier.imageInternalFormat = EiifRGBA8UI;
3271 }
3272 else if (qualifierType == "r32ui")
3273 {
3274 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3275 qualifier.imageInternalFormat = EiifR32UI;
3276 }
3277
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003278 else
3279 {
3280 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003281 }
3282
Jamie Madilla5efff92013-06-06 11:56:47 -04003283 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003284}
3285
Martin Radev802abe02016-08-04 17:48:32 +03003286void TParseContext::parseLocalSize(const TString &qualifierType,
3287 const TSourceLoc &qualifierTypeLine,
3288 int intValue,
3289 const TSourceLoc &intValueLine,
3290 const std::string &intValueString,
3291 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003292 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003293{
Olli Etuaho856c4972016-08-08 11:38:39 +03003294 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003295 if (intValue < 1)
3296 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003297 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03003298 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003299 }
3300 (*localSize)[index] = intValue;
3301}
3302
Jamie Madillb98c3a82015-07-23 14:26:04 -04003303TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3304 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003305 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303306 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003307{
Martin Radev802abe02016-08-04 17:48:32 +03003308 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003309
Martin Radev802abe02016-08-04 17:48:32 +03003310 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003311
Martin Radev802abe02016-08-04 17:48:32 +03003312 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003313 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003314 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003315 if (intValue < 0)
3316 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003317 error(intValueLine, "out of range:", intValueString.c_str(),
3318 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003319 }
3320 else
3321 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003322 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003323 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003324 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003325 }
Martin Radev802abe02016-08-04 17:48:32 +03003326 else if (qualifierType == "local_size_x")
3327 {
3328 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3329 &qualifier.localSize);
3330 }
3331 else if (qualifierType == "local_size_y")
3332 {
3333 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3334 &qualifier.localSize);
3335 }
3336 else if (qualifierType == "local_size_z")
3337 {
3338 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3339 &qualifier.localSize);
3340 }
3341 else
3342 {
3343 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003344 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003345
Jamie Madilla5efff92013-06-06 11:56:47 -04003346 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003347}
3348
Olli Etuaho613b9592016-09-05 12:05:53 +03003349TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3350{
3351 return new TTypeQualifierBuilder(
3352 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3353 mShaderVersion);
3354}
3355
Jamie Madillb98c3a82015-07-23 14:26:04 -04003356TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003357 TLayoutQualifier rightQualifier,
3358 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003359{
Martin Radevc28888b2016-07-22 15:27:42 +03003360 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
3361 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003362}
3363
Martin Radev70866b82016-07-22 15:27:42 +03003364TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3365 const TTypeQualifierBuilder &typeQualifierBuilder,
3366 TPublicType *typeSpecifier,
3367 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003368{
Olli Etuaho613b9592016-09-05 12:05:53 +03003369 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003370
Martin Radev70866b82016-07-22 15:27:42 +03003371 typeSpecifier->qualifier = typeQualifier.qualifier;
3372 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003373 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003374 typeSpecifier->invariant = typeQualifier.invariant;
3375 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303376 {
Martin Radev70866b82016-07-22 15:27:42 +03003377 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003378 }
Martin Radev70866b82016-07-22 15:27:42 +03003379 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003380}
3381
Jamie Madillb98c3a82015-07-23 14:26:04 -04003382TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3383 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003384{
Martin Radev4a9cd802016-09-01 16:51:51 +03003385 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3386 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003387
Martin Radev4a9cd802016-09-01 16:51:51 +03003388 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003389
Martin Radev4a9cd802016-09-01 16:51:51 +03003390 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003391
Arun Patole7e7e68d2015-05-22 12:02:25 +05303392 for (unsigned int i = 0; i < fieldList->size(); ++i)
3393 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003394 //
3395 // Careful not to replace already known aspects of type, like array-ness
3396 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303397 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003398 type->setBasicType(typeSpecifier.getBasicType());
3399 type->setPrimarySize(typeSpecifier.getPrimarySize());
3400 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003401 type->setPrecision(typeSpecifier.precision);
3402 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003403 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003404 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003405 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003406
3407 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303408 if (type->isArray())
3409 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003410 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003411 }
3412 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003413 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003414 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303415 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003416 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003417 }
3418
Martin Radev4a9cd802016-09-01 16:51:51 +03003419 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003420 }
3421
Jamie Madill98493dd2013-07-08 14:39:03 -04003422 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003423}
3424
Martin Radev4a9cd802016-09-01 16:51:51 +03003425TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3426 const TSourceLoc &nameLine,
3427 const TString *structName,
3428 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003429{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303430 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003431 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003432
Jamie Madill9b820842015-02-12 10:40:10 -05003433 // Store a bool in the struct if we're at global scope, to allow us to
3434 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003435 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003436
Jamie Madill98493dd2013-07-08 14:39:03 -04003437 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003438 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003439 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303440 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3441 if (!symbolTable.declare(userTypeDef))
3442 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003443 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003444 }
3445 }
3446
3447 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003448 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003449 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003450 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003451 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003452 switch (qualifier)
3453 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003454 case EvqGlobal:
3455 case EvqTemporary:
3456 break;
3457 default:
3458 error(field.line(), "invalid qualifier on struct member",
3459 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003460 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003461 }
Martin Radev70866b82016-07-22 15:27:42 +03003462 if (field.type()->isInvariant())
3463 {
3464 error(field.line(), "invalid qualifier on struct member", "invariant");
3465 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003466 if (IsImage(field.type()->getBasicType()))
3467 {
3468 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3469 }
3470
3471 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003472
3473 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003474 }
3475
Martin Radev4a9cd802016-09-01 16:51:51 +03003476 TTypeSpecifierNonArray typeSpecifierNonArray;
3477 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3478 typeSpecifierNonArray.userDef = structureType;
3479 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003480 exitStructDeclaration();
3481
Martin Radev4a9cd802016-09-01 16:51:51 +03003482 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003483}
3484
Jamie Madillb98c3a82015-07-23 14:26:04 -04003485TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003486 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003487 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003488{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003489 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003490 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003491 init->isVector())
3492 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003493 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3494 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003495 return nullptr;
3496 }
3497
Olli Etuahoac5274d2015-02-20 10:19:08 +02003498 if (statementList)
3499 {
3500 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3501 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003502 return nullptr;
3503 }
3504 }
3505
Olli Etuahoa3a36662015-02-17 13:46:51 +02003506 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3507 if (node == nullptr)
3508 {
3509 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003510 return nullptr;
3511 }
3512 return node;
3513}
3514
3515TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3516{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003517 if (mSwitchNestingLevel == 0)
3518 {
3519 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003520 return nullptr;
3521 }
3522 if (condition == nullptr)
3523 {
3524 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003525 return nullptr;
3526 }
3527 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003528 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003529 {
3530 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003531 }
3532 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003533 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3534 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3535 // fold in case labels.
3536 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003537 {
3538 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003539 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003540 TIntermCase *node = intermediate.addCase(condition, loc);
3541 if (node == nullptr)
3542 {
3543 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003544 return nullptr;
3545 }
3546 return node;
3547}
3548
3549TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3550{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003551 if (mSwitchNestingLevel == 0)
3552 {
3553 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003554 return nullptr;
3555 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003556 TIntermCase *node = intermediate.addCase(nullptr, loc);
3557 if (node == nullptr)
3558 {
3559 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003560 return nullptr;
3561 }
3562 return node;
3563}
3564
Jamie Madillb98c3a82015-07-23 14:26:04 -04003565TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3566 TIntermTyped *child,
3567 const TSourceLoc &loc,
3568 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003569{
3570 if (child == nullptr)
3571 {
3572 return nullptr;
3573 }
3574
3575 switch (op)
3576 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003577 case EOpLogicalNot:
3578 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3579 child->isVector())
3580 {
3581 return nullptr;
3582 }
3583 break;
3584 case EOpBitwiseNot:
3585 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3586 child->isMatrix() || child->isArray())
3587 {
3588 return nullptr;
3589 }
3590 break;
3591 case EOpPostIncrement:
3592 case EOpPreIncrement:
3593 case EOpPostDecrement:
3594 case EOpPreDecrement:
3595 case EOpNegative:
3596 case EOpPositive:
3597 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003598 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003599 {
3600 return nullptr;
3601 }
3602 // Operators for built-ins are already type checked against their prototype.
3603 default:
3604 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003605 }
3606
Olli Etuahof119a262016-08-19 15:54:22 +03003607 TIntermUnary *node = new TIntermUnary(op, child);
3608 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003609
3610 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3611 if (foldedNode)
3612 return foldedNode;
3613
3614 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003615}
3616
Olli Etuaho09b22472015-02-11 11:47:26 +02003617TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3618{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003619 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003620 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003621 {
3622 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003623 return child;
3624 }
3625 return node;
3626}
3627
Jamie Madillb98c3a82015-07-23 14:26:04 -04003628TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3629 TIntermTyped *child,
3630 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003631{
Olli Etuaho856c4972016-08-08 11:38:39 +03003632 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003633 return addUnaryMath(op, child, loc);
3634}
3635
Jamie Madillb98c3a82015-07-23 14:26:04 -04003636bool TParseContext::binaryOpCommonCheck(TOperator op,
3637 TIntermTyped *left,
3638 TIntermTyped *right,
3639 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003640{
Olli Etuaho244be012016-08-18 15:26:02 +03003641 if (left->getType().getStruct() || right->getType().getStruct())
3642 {
3643 switch (op)
3644 {
3645 case EOpIndexDirectStruct:
3646 ASSERT(left->getType().getStruct());
3647 break;
3648 case EOpEqual:
3649 case EOpNotEqual:
3650 case EOpAssign:
3651 case EOpInitialize:
3652 if (left->getType() != right->getType())
3653 {
3654 return false;
3655 }
3656 break;
3657 default:
3658 error(loc, "Invalid operation for structs", GetOperatorString(op));
3659 return false;
3660 }
3661 }
3662
Olli Etuahod6b14282015-03-17 14:31:35 +02003663 if (left->isArray() || right->isArray())
3664 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003665 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003666 {
3667 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3668 return false;
3669 }
3670
3671 if (left->isArray() != right->isArray())
3672 {
3673 error(loc, "array / non-array mismatch", GetOperatorString(op));
3674 return false;
3675 }
3676
3677 switch (op)
3678 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003679 case EOpEqual:
3680 case EOpNotEqual:
3681 case EOpAssign:
3682 case EOpInitialize:
3683 break;
3684 default:
3685 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3686 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003687 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003688 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003689 if (left->getArraySize() != right->getArraySize())
3690 {
3691 error(loc, "array size mismatch", GetOperatorString(op));
3692 return false;
3693 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003694 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003695
3696 // Check ops which require integer / ivec parameters
3697 bool isBitShift = false;
3698 switch (op)
3699 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003700 case EOpBitShiftLeft:
3701 case EOpBitShiftRight:
3702 case EOpBitShiftLeftAssign:
3703 case EOpBitShiftRightAssign:
3704 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3705 // check that the basic type is an integer type.
3706 isBitShift = true;
3707 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3708 {
3709 return false;
3710 }
3711 break;
3712 case EOpBitwiseAnd:
3713 case EOpBitwiseXor:
3714 case EOpBitwiseOr:
3715 case EOpBitwiseAndAssign:
3716 case EOpBitwiseXorAssign:
3717 case EOpBitwiseOrAssign:
3718 // It is enough to check the type of only one operand, since later it
3719 // is checked that the operand types match.
3720 if (!IsInteger(left->getBasicType()))
3721 {
3722 return false;
3723 }
3724 break;
3725 default:
3726 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003727 }
3728
3729 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3730 // So the basic type should usually match.
3731 if (!isBitShift && left->getBasicType() != right->getBasicType())
3732 {
3733 return false;
3734 }
3735
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003736 // Check that:
3737 // 1. Type sizes match exactly on ops that require that.
3738 // 2. Restrictions for structs that contain arrays or samplers are respected.
3739 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003740 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003741 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003742 case EOpAssign:
3743 case EOpInitialize:
3744 case EOpEqual:
3745 case EOpNotEqual:
3746 // ESSL 1.00 sections 5.7, 5.8, 5.9
3747 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3748 {
3749 error(loc, "undefined operation for structs containing arrays",
3750 GetOperatorString(op));
3751 return false;
3752 }
3753 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3754 // we interpret the spec so that this extends to structs containing samplers,
3755 // similarly to ESSL 1.00 spec.
3756 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3757 left->getType().isStructureContainingSamplers())
3758 {
3759 error(loc, "undefined operation for structs containing samplers",
3760 GetOperatorString(op));
3761 return false;
3762 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003763
3764 if ((op == EOpAssign || op == EOpInitialize) &&
3765 left->getType().isStructureContainingImages())
3766 {
3767 error(loc, "undefined operation for structs containing images",
3768 GetOperatorString(op));
3769 return false;
3770 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003771 case EOpLessThan:
3772 case EOpGreaterThan:
3773 case EOpLessThanEqual:
3774 case EOpGreaterThanEqual:
3775 if ((left->getNominalSize() != right->getNominalSize()) ||
3776 (left->getSecondarySize() != right->getSecondarySize()))
3777 {
3778 return false;
3779 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003780 break;
3781 case EOpAdd:
3782 case EOpSub:
3783 case EOpDiv:
3784 case EOpIMod:
3785 case EOpBitShiftLeft:
3786 case EOpBitShiftRight:
3787 case EOpBitwiseAnd:
3788 case EOpBitwiseXor:
3789 case EOpBitwiseOr:
3790 case EOpAddAssign:
3791 case EOpSubAssign:
3792 case EOpDivAssign:
3793 case EOpIModAssign:
3794 case EOpBitShiftLeftAssign:
3795 case EOpBitShiftRightAssign:
3796 case EOpBitwiseAndAssign:
3797 case EOpBitwiseXorAssign:
3798 case EOpBitwiseOrAssign:
3799 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3800 {
3801 return false;
3802 }
3803
3804 // Are the sizes compatible?
3805 if (left->getNominalSize() != right->getNominalSize() ||
3806 left->getSecondarySize() != right->getSecondarySize())
3807 {
3808 // If the nominal sizes of operands do not match:
3809 // One of them must be a scalar.
3810 if (!left->isScalar() && !right->isScalar())
3811 return false;
3812
3813 // In the case of compound assignment other than multiply-assign,
3814 // the right side needs to be a scalar. Otherwise a vector/matrix
3815 // would be assigned to a scalar. A scalar can't be shifted by a
3816 // vector either.
3817 if (!right->isScalar() &&
3818 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3819 return false;
3820 }
3821 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003822 default:
3823 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003824 }
3825
Olli Etuahod6b14282015-03-17 14:31:35 +02003826 return true;
3827}
3828
Olli Etuaho1dded802016-08-18 18:13:13 +03003829bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3830 const TType &left,
3831 const TType &right)
3832{
3833 switch (op)
3834 {
3835 case EOpMul:
3836 case EOpMulAssign:
3837 return left.getNominalSize() == right.getNominalSize() &&
3838 left.getSecondarySize() == right.getSecondarySize();
3839 case EOpVectorTimesScalar:
3840 return true;
3841 case EOpVectorTimesScalarAssign:
3842 ASSERT(!left.isMatrix() && !right.isMatrix());
3843 return left.isVector() && !right.isVector();
3844 case EOpVectorTimesMatrix:
3845 return left.getNominalSize() == right.getRows();
3846 case EOpVectorTimesMatrixAssign:
3847 ASSERT(!left.isMatrix() && right.isMatrix());
3848 return left.isVector() && left.getNominalSize() == right.getRows() &&
3849 left.getNominalSize() == right.getCols();
3850 case EOpMatrixTimesVector:
3851 return left.getCols() == right.getNominalSize();
3852 case EOpMatrixTimesScalar:
3853 return true;
3854 case EOpMatrixTimesScalarAssign:
3855 ASSERT(left.isMatrix() && !right.isMatrix());
3856 return !right.isVector();
3857 case EOpMatrixTimesMatrix:
3858 return left.getCols() == right.getRows();
3859 case EOpMatrixTimesMatrixAssign:
3860 ASSERT(left.isMatrix() && right.isMatrix());
3861 // We need to check two things:
3862 // 1. The matrix multiplication step is valid.
3863 // 2. The result will have the same number of columns as the lvalue.
3864 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3865
3866 default:
3867 UNREACHABLE();
3868 return false;
3869 }
3870}
3871
Jamie Madillb98c3a82015-07-23 14:26:04 -04003872TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3873 TIntermTyped *left,
3874 TIntermTyped *right,
3875 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003876{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003877 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003878 return nullptr;
3879
Olli Etuahofc1806e2015-03-17 13:03:11 +02003880 switch (op)
3881 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003882 case EOpEqual:
3883 case EOpNotEqual:
3884 break;
3885 case EOpLessThan:
3886 case EOpGreaterThan:
3887 case EOpLessThanEqual:
3888 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003889 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3890 !right->getType().getStruct());
3891 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003892 {
3893 return nullptr;
3894 }
3895 break;
3896 case EOpLogicalOr:
3897 case EOpLogicalXor:
3898 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003899 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3900 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003901 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003902 {
3903 return nullptr;
3904 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003905 // Basic types matching should have been already checked.
3906 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003907 break;
3908 case EOpAdd:
3909 case EOpSub:
3910 case EOpDiv:
3911 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003912 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3913 !right->getType().getStruct());
3914 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003915 {
3916 return nullptr;
3917 }
3918 break;
3919 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003920 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3921 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003922 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003923 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003924 {
3925 return nullptr;
3926 }
3927 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003928 default:
3929 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003930 }
3931
Olli Etuaho1dded802016-08-18 18:13:13 +03003932 if (op == EOpMul)
3933 {
3934 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3935 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3936 {
3937 return nullptr;
3938 }
3939 }
3940
Olli Etuaho3fdec912016-08-18 15:08:06 +03003941 TIntermBinary *node = new TIntermBinary(op, left, right);
3942 node->setLine(loc);
3943
Olli Etuaho3fdec912016-08-18 15:08:06 +03003944 // See if we can fold constants.
3945 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3946 if (foldedNode)
3947 return foldedNode;
3948
3949 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003950}
3951
Jamie Madillb98c3a82015-07-23 14:26:04 -04003952TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3953 TIntermTyped *left,
3954 TIntermTyped *right,
3955 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003956{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003957 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003958 if (node == 0)
3959 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003960 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3961 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003962 return left;
3963 }
3964 return node;
3965}
3966
Jamie Madillb98c3a82015-07-23 14:26:04 -04003967TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3968 TIntermTyped *left,
3969 TIntermTyped *right,
3970 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003971{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003972 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003973 if (node == 0)
3974 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003975 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3976 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003977 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003978 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003979 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3980 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003981 }
3982 return node;
3983}
3984
Olli Etuaho13389b62016-10-16 11:48:18 +01003985TIntermBinary *TParseContext::createAssign(TOperator op,
3986 TIntermTyped *left,
3987 TIntermTyped *right,
3988 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003989{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003990 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003991 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003992 if (op == EOpMulAssign)
3993 {
3994 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3995 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3996 {
3997 return nullptr;
3998 }
3999 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004000 TIntermBinary *node = new TIntermBinary(op, left, right);
4001 node->setLine(loc);
4002
Olli Etuaho3fdec912016-08-18 15:08:06 +03004003 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004004 }
4005 return nullptr;
4006}
4007
Jamie Madillb98c3a82015-07-23 14:26:04 -04004008TIntermTyped *TParseContext::addAssign(TOperator op,
4009 TIntermTyped *left,
4010 TIntermTyped *right,
4011 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004012{
4013 TIntermTyped *node = createAssign(op, left, right, loc);
4014 if (node == nullptr)
4015 {
4016 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004017 return left;
4018 }
4019 return node;
4020}
4021
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004022TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4023 TIntermTyped *right,
4024 const TSourceLoc &loc)
4025{
Corentin Wallez0d959252016-07-12 17:26:32 -04004026 // WebGL2 section 5.26, the following results in an error:
4027 // "Sequence operator applied to void, arrays, or structs containing arrays"
4028 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
4029 left->getType().isStructureContainingArrays() ||
4030 right->isArray() || right->getBasicType() == EbtVoid ||
4031 right->getType().isStructureContainingArrays()))
4032 {
4033 error(loc,
4034 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4035 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004036 }
4037
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004038 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004039}
4040
Olli Etuaho49300862015-02-20 14:54:49 +02004041TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4042{
4043 switch (op)
4044 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004045 case EOpContinue:
4046 if (mLoopNestingLevel <= 0)
4047 {
4048 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004049 }
4050 break;
4051 case EOpBreak:
4052 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4053 {
4054 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004055 }
4056 break;
4057 case EOpReturn:
4058 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4059 {
4060 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004061 }
4062 break;
4063 default:
4064 // No checks for discard
4065 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004066 }
4067 return intermediate.addBranch(op, loc);
4068}
4069
Jamie Madillb98c3a82015-07-23 14:26:04 -04004070TIntermBranch *TParseContext::addBranch(TOperator op,
4071 TIntermTyped *returnValue,
4072 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004073{
4074 ASSERT(op == EOpReturn);
4075 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004076 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004077 {
4078 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004079 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004080 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004081 {
4082 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004083 }
4084 return intermediate.addBranch(op, returnValue, loc);
4085}
4086
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004087void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4088{
4089 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004090 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004091 TIntermNode *offset = nullptr;
4092 TIntermSequence *arguments = functionCall->getSequence();
4093 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4094 name.compare(0, 16, "textureLodOffset") == 0 ||
4095 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4096 name.compare(0, 17, "textureGradOffset") == 0 ||
4097 name.compare(0, 21, "textureProjGradOffset") == 0)
4098 {
4099 offset = arguments->back();
4100 }
4101 else if (name.compare(0, 13, "textureOffset") == 0 ||
4102 name.compare(0, 17, "textureProjOffset") == 0)
4103 {
4104 // A bias parameter might follow the offset parameter.
4105 ASSERT(arguments->size() >= 3);
4106 offset = (*arguments)[2];
4107 }
4108 if (offset != nullptr)
4109 {
4110 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4111 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4112 {
4113 TString unmangledName = TFunction::unmangleName(name);
4114 error(functionCall->getLine(), "Texture offset must be a constant expression",
4115 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004116 }
4117 else
4118 {
4119 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4120 size_t size = offsetConstantUnion->getType().getObjectSize();
4121 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4122 for (size_t i = 0u; i < size; ++i)
4123 {
4124 int offsetValue = values[i].getIConst();
4125 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4126 {
4127 std::stringstream tokenStream;
4128 tokenStream << offsetValue;
4129 std::string token = tokenStream.str();
4130 error(offset->getLine(), "Texture offset value out of valid range",
4131 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004132 }
4133 }
4134 }
4135 }
4136}
4137
Martin Radev2cc85b32016-08-05 16:22:53 +03004138// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4139void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4140{
4141 ASSERT(!functionCall->isUserDefined());
4142 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4143
4144 if (name.compare(0, 5, "image") == 0)
4145 {
4146 TIntermSequence *arguments = functionCall->getSequence();
4147 TIntermNode *imageNode = (*arguments)[0];
4148 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4149
4150 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4151
4152 if (name.compare(5, 5, "Store") == 0)
4153 {
4154 if (memoryQualifier.readonly)
4155 {
4156 error(imageNode->getLine(),
4157 "'imageStore' cannot be used with images qualified as 'readonly'",
4158 imageSymbol->getSymbol().c_str());
4159 }
4160 }
4161 else if (name.compare(5, 4, "Load") == 0)
4162 {
4163 if (memoryQualifier.writeonly)
4164 {
4165 error(imageNode->getLine(),
4166 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4167 imageSymbol->getSymbol().c_str());
4168 }
4169 }
4170 }
4171}
4172
4173// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4174void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4175 const TFunction *functionDefinition,
4176 const TIntermAggregate *functionCall)
4177{
4178 ASSERT(functionCall->isUserDefined());
4179
4180 const TIntermSequence &arguments = *functionCall->getSequence();
4181
4182 ASSERT(functionDefinition->getParamCount() == arguments.size());
4183
4184 for (size_t i = 0; i < arguments.size(); ++i)
4185 {
4186 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4187 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4188 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4189
4190 if (IsImage(functionArgumentType.getBasicType()))
4191 {
4192 const TMemoryQualifier &functionArgumentMemoryQualifier =
4193 functionArgumentType.getMemoryQualifier();
4194 const TMemoryQualifier &functionParameterMemoryQualifier =
4195 functionParameterType.getMemoryQualifier();
4196 if (functionArgumentMemoryQualifier.readonly &&
4197 !functionParameterMemoryQualifier.readonly)
4198 {
4199 error(functionCall->getLine(),
4200 "Function call discards the 'readonly' qualifier from image",
4201 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4202 }
4203
4204 if (functionArgumentMemoryQualifier.writeonly &&
4205 !functionParameterMemoryQualifier.writeonly)
4206 {
4207 error(functionCall->getLine(),
4208 "Function call discards the 'writeonly' qualifier from image",
4209 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4210 }
4211 }
4212 }
4213}
4214
Jamie Madillb98c3a82015-07-23 14:26:04 -04004215TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4216 TIntermNode *paramNode,
4217 TIntermNode *thisNode,
4218 const TSourceLoc &loc,
4219 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004220{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004221 *fatalError = false;
4222 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004223 TIntermTyped *callNode = nullptr;
4224
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004225 if (thisNode != nullptr)
4226 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004227 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004228 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004229 TIntermTyped *typedThis = thisNode->getAsTyped();
4230 if (fnCall->getName() != "length")
4231 {
4232 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004233 }
4234 else if (paramNode != nullptr)
4235 {
4236 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004237 }
4238 else if (typedThis == nullptr || !typedThis->isArray())
4239 {
4240 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004241 }
4242 else
4243 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004244 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004245 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004246 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004247 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004248 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004249 // (func()).length()
4250 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004251 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4252 // expression.
4253 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4254 // spec section 5.9 which allows "An array, vector or matrix expression with the
4255 // length method applied".
4256 error(loc, "length can only be called on array names, not on array expressions",
4257 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004258 }
4259 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004260 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004261 callNode =
4262 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004263 }
4264 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004265 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004266 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004267 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004268 }
4269 else
4270 {
4271 //
4272 // Not a constructor. Find it in the symbol table.
4273 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304274 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004275 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004276 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004277 if (fnCandidate)
4278 {
4279 //
4280 // A declared function.
4281 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004282 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004283 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004284 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004285 }
4286 op = fnCandidate->getBuiltInOp();
4287 if (builtIn && op != EOpNull)
4288 {
4289 //
4290 // A function call mapped to a built-in operation.
4291 //
4292 if (fnCandidate->getParamCount() == 1)
4293 {
4294 //
4295 // Treat it like a built-in unary operator.
4296 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004297 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4298 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004299 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4300 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004301 if (callNode == nullptr)
4302 {
4303 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004304 extraInfoStream
4305 << "built in unary operator function. Type: "
4306 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004307 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004308 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4309 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004310 *fatalError = true;
4311 return nullptr;
4312 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004313 }
4314 else
4315 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004316 TIntermAggregate *aggregate =
4317 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004318 aggregate->setType(fnCandidate->getReturnType());
4319 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004320 if (aggregate->areChildrenConstQualified())
4321 {
4322 aggregate->getTypePointer()->setQualifier(EvqConst);
4323 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004324
4325 // Some built-in functions have out parameters too.
4326 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304327
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004328 // See if we can constant fold a built-in. Note that this may be possible even
4329 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004330 TIntermTyped *foldedNode =
4331 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304332 if (foldedNode)
4333 {
Arun Patole274f0702015-05-05 13:33:30 +05304334 callNode = foldedNode;
4335 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004336 else
4337 {
4338 callNode = aggregate;
4339 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004340 }
4341 }
4342 else
4343 {
4344 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004345 TIntermAggregate *aggregate =
4346 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004347 aggregate->setType(fnCandidate->getReturnType());
4348
Jamie Madillb98c3a82015-07-23 14:26:04 -04004349 // this is how we know whether the given function is a builtIn function or a user
4350 // defined function
4351 // if builtIn == false, it's a userDefined -> could be an overloaded
4352 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004353 // if builtIn == true, it's definitely a builtIn function with EOpNull
4354 if (!builtIn)
4355 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004356 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004357
Olli Etuahobd674552016-10-06 13:28:42 +01004358 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004359 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004360 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004361 aggregate->setBuiltInFunctionPrecision();
4362
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004363 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004364
4365 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4366 }
4367 else
4368 {
4369 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004370 }
4371
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004372 callNode = aggregate;
4373
4374 functionCallLValueErrorCheck(fnCandidate, aggregate);
4375 }
4376 }
4377 else
4378 {
4379 // error message was put out by findFunction()
4380 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004381 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004382 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004383 callNode = intermediate.addConstantUnion(unionArray,
4384 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004385 }
4386 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004387 return callNode;
4388}
4389
Jamie Madillb98c3a82015-07-23 14:26:04 -04004390TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004391 TIntermTyped *trueExpression,
4392 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004393 const TSourceLoc &loc)
4394{
Olli Etuaho856c4972016-08-08 11:38:39 +03004395 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004396
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004397 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004398 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004399 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4400 falseExpression->getCompleteString());
4401 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004402 }
Olli Etuahode318b22016-10-25 16:18:25 +01004403 if (IsOpaqueType(trueExpression->getBasicType()))
4404 {
4405 // ESSL 1.00 section 4.1.7
4406 // ESSL 3.00 section 4.1.7
4407 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4408 // Note that structs containing opaque types don't need to be checked as structs are
4409 // forbidden below.
4410 error(loc, "ternary operator is not allowed for opaque types", ":");
4411 return falseExpression;
4412 }
4413
Olli Etuahoa2d53032015-04-15 14:14:44 +03004414 // ESSL1 sections 5.2 and 5.7:
4415 // ESSL3 section 5.7:
4416 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004417 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004418 {
4419 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004420 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004421 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004422 // WebGL2 section 5.26, the following results in an error:
4423 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004424 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004425 {
4426 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004427 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004428 }
4429
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004430 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004431}
Olli Etuaho49300862015-02-20 14:54:49 +02004432
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004433//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004434// Parse an array of strings using yyparse.
4435//
4436// Returns 0 for success.
4437//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004438int PaParseStrings(size_t count,
4439 const char *const string[],
4440 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304441 TParseContext *context)
4442{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004443 if ((count == 0) || (string == NULL))
4444 return 1;
4445
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004446 if (glslang_initialize(context))
4447 return 1;
4448
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004449 int error = glslang_scan(count, string, length, context);
4450 if (!error)
4451 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004452
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004453 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004454
alokp@chromium.org6b495712012-06-29 00:06:58 +00004455 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004456}