blob: 0be33391a6fafbf00dafa0991f5e0a9ee6991e1b [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040013#include "compiler/translator/Cache.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020014#include "compiler/translator/glslang.h"
15#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030017#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
Jamie Madill45bcc782016-11-07 13:58:48 -050019namespace sh
20{
21
alokp@chromium.org8b851c62012-06-15 16:25:11 +000022///////////////////////////////////////////////////////////////////////
23//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024// Sub- vector and matrix fields
25//
26////////////////////////////////////////////////////////////////////////
27
Martin Radev2cc85b32016-08-05 16:22:53 +030028namespace
29{
30
31const int kWebGLMaxStructNesting = 4;
32
33bool ContainsSampler(const TType &type)
34{
35 if (IsSampler(type.getBasicType()))
36 return true;
37
38 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
39 {
40 const TFieldList &fields = type.getStruct()->fields();
41 for (unsigned int i = 0; i < fields.size(); ++i)
42 {
43 if (ContainsSampler(*fields[i]->type()))
44 return true;
45 }
46 }
47
48 return false;
49}
50
51bool ContainsImage(const TType &type)
52{
53 if (IsImage(type.getBasicType()))
54 return true;
55
56 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
57 {
58 const TFieldList &fields = type.getStruct()->fields();
59 for (unsigned int i = 0; i < fields.size(); ++i)
60 {
61 if (ContainsImage(*fields[i]->type()))
62 return true;
63 }
64 }
65
66 return false;
67}
68
69} // namespace
70
Jamie Madillacb4b812016-11-07 13:50:29 -050071TParseContext::TParseContext(TSymbolTable &symt,
72 TExtensionBehavior &ext,
73 sh::GLenum type,
74 ShShaderSpec spec,
75 ShCompileOptions options,
76 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +000077 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -050078 const ShBuiltInResources &resources)
79 : intermediate(),
80 symbolTable(symt),
81 mDeferredSingleDeclarationErrorCheck(false),
82 mShaderType(type),
83 mShaderSpec(spec),
84 mCompileOptions(options),
85 mShaderVersion(100),
86 mTreeRoot(nullptr),
87 mLoopNestingLevel(0),
88 mStructNestingLevel(0),
89 mSwitchNestingLevel(0),
90 mCurrentFunctionType(nullptr),
91 mFunctionReturnsValue(false),
92 mChecksPrecisionErrors(checksPrecErrors),
93 mFragmentPrecisionHighOnESSL1(false),
94 mDefaultMatrixPacking(EmpColumnMajor),
95 mDefaultBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +000096 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -050097 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +000098 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -050099 mShaderVersion,
100 mShaderType,
101 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000102 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500103 mScanner(nullptr),
104 mUsesFragData(false),
105 mUsesFragColor(false),
106 mUsesSecondaryOutputs(false),
107 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
108 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000109 mMultiviewAvailable(resources.OVR_multiview == 1),
Jamie Madillacb4b812016-11-07 13:50:29 -0500110 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000111 mNumViews(-1),
112 mMaxNumViews(resources.MaxViewsOVR),
Jamie Madillacb4b812016-11-07 13:50:29 -0500113 mDeclaringFunction(false)
114{
115 mComputeShaderLocalSize.fill(-1);
116}
117
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118//
119// Look at a '.' field selector string and change it into offsets
120// for a vector.
121//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400122bool TParseContext::parseVectorFields(const TString &compString,
123 int vecSize,
124 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530125 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000126{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400127 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530128 if (fields.num > 4)
129 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000130 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000131 return false;
132 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133
Jamie Madillb98c3a82015-07-23 14:26:04 -0400134 enum
135 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000136 exyzw,
137 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000138 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000139 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140
Arun Patole7e7e68d2015-05-22 12:02:25 +0530141 for (int i = 0; i < fields.num; ++i)
142 {
143 switch (compString[i])
144 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400145 case 'x':
146 fields.offsets[i] = 0;
147 fieldSet[i] = exyzw;
148 break;
149 case 'r':
150 fields.offsets[i] = 0;
151 fieldSet[i] = ergba;
152 break;
153 case 's':
154 fields.offsets[i] = 0;
155 fieldSet[i] = estpq;
156 break;
157 case 'y':
158 fields.offsets[i] = 1;
159 fieldSet[i] = exyzw;
160 break;
161 case 'g':
162 fields.offsets[i] = 1;
163 fieldSet[i] = ergba;
164 break;
165 case 't':
166 fields.offsets[i] = 1;
167 fieldSet[i] = estpq;
168 break;
169 case 'z':
170 fields.offsets[i] = 2;
171 fieldSet[i] = exyzw;
172 break;
173 case 'b':
174 fields.offsets[i] = 2;
175 fieldSet[i] = ergba;
176 break;
177 case 'p':
178 fields.offsets[i] = 2;
179 fieldSet[i] = estpq;
180 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530181
Jamie Madillb98c3a82015-07-23 14:26:04 -0400182 case 'w':
183 fields.offsets[i] = 3;
184 fieldSet[i] = exyzw;
185 break;
186 case 'a':
187 fields.offsets[i] = 3;
188 fieldSet[i] = ergba;
189 break;
190 case 'q':
191 fields.offsets[i] = 3;
192 fieldSet[i] = estpq;
193 break;
194 default:
195 error(line, "illegal vector field selection", compString.c_str());
196 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000197 }
198 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199
Arun Patole7e7e68d2015-05-22 12:02:25 +0530200 for (int i = 0; i < fields.num; ++i)
201 {
202 if (fields.offsets[i] >= vecSize)
203 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400204 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000205 return false;
206 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207
Arun Patole7e7e68d2015-05-22 12:02:25 +0530208 if (i > 0)
209 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400210 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530211 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400212 error(line, "illegal - vector component fields not from the same set",
213 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000214 return false;
215 }
216 }
217 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000219 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220}
221
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222///////////////////////////////////////////////////////////////////////
223//
224// Errors
225//
226////////////////////////////////////////////////////////////////////////
227
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228//
229// Used by flex/bison to output all syntax and parsing errors.
230//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000231void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000233 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000234}
235
Olli Etuaho4de340a2016-12-16 09:32:03 +0000236void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530237{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000238 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000239}
240
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200241void TParseContext::outOfRangeError(bool isError,
242 const TSourceLoc &loc,
243 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000244 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200245{
246 if (isError)
247 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000248 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200249 }
250 else
251 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000252 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200253 }
254}
255
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256//
257// Same error message for all places assignments don't work.
258//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530259void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000261 std::stringstream reasonStream;
262 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
263 std::string reason = reasonStream.str();
264 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265}
266
267//
268// Same error message for all places unary operations don't work.
269//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530270void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000272 std::stringstream reasonStream;
273 reasonStream << "wrong operand type - no operation '" << op
274 << "' exists that takes an operand of type " << operand
275 << " (or there is no acceptable conversion)";
276 std::string reason = reasonStream.str();
277 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278}
279
280//
281// Same error message for all binary operations don't work.
282//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400283void TParseContext::binaryOpError(const TSourceLoc &line,
284 const char *op,
285 TString left,
286 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000288 std::stringstream reasonStream;
289 reasonStream << "wrong operand types - no operation '" << op
290 << "' exists that takes a left-hand operand of type '" << left
291 << "' and a right operand of type '" << right
292 << "' (or there is no acceptable conversion)";
293 std::string reason = reasonStream.str();
294 error(line, reason.c_str(), op);
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 Madilld7b1ab52016-12-12 14:42:19 -0500340 TIntermSymbol *symNode = node->getAsSymbolNode();
341 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 *message = 0;
372 switch (node->getQualifier())
373 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400374 case EvqConst:
375 message = "can't modify a const";
376 break;
377 case EvqConstReadOnly:
378 message = "can't modify a const";
379 break;
380 case EvqAttribute:
381 message = "can't modify an attribute";
382 break;
383 case EvqFragmentIn:
384 message = "can't modify an input";
385 break;
386 case EvqVertexIn:
387 message = "can't modify an input";
388 break;
389 case EvqUniform:
390 message = "can't modify a uniform";
391 break;
392 case EvqVaryingIn:
393 message = "can't modify a varying";
394 break;
395 case EvqFragCoord:
396 message = "can't modify gl_FragCoord";
397 break;
398 case EvqFrontFacing:
399 message = "can't modify gl_FrontFacing";
400 break;
401 case EvqPointCoord:
402 message = "can't modify gl_PointCoord";
403 break;
Martin Radevb0883602016-08-04 17:48:58 +0300404 case EvqNumWorkGroups:
405 message = "can't modify gl_NumWorkGroups";
406 break;
407 case EvqWorkGroupSize:
408 message = "can't modify gl_WorkGroupSize";
409 break;
410 case EvqWorkGroupID:
411 message = "can't modify gl_WorkGroupID";
412 break;
413 case EvqLocalInvocationID:
414 message = "can't modify gl_LocalInvocationID";
415 break;
416 case EvqGlobalInvocationID:
417 message = "can't modify gl_GlobalInvocationID";
418 break;
419 case EvqLocalInvocationIndex:
420 message = "can't modify gl_LocalInvocationIndex";
421 break;
Martin Radev802abe02016-08-04 17:48:32 +0300422 case EvqComputeIn:
423 message = "can't modify work group size variable";
424 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400425 default:
426 //
427 // Type that can't be written to?
428 //
429 if (node->getBasicType() == EbtVoid)
430 {
431 message = "can't modify void";
432 }
433 if (IsSampler(node->getBasicType()))
434 {
435 message = "can't modify a sampler";
436 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300437 if (IsImage(node->getBasicType()))
438 {
439 message = "can't modify an image";
440 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000441 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000442
Arun Patole7e7e68d2015-05-22 12:02:25 +0530443 if (message == 0 && binaryNode == 0 && symNode == 0)
444 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000445 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000446
Olli Etuaho8a176262016-08-16 14:23:01 +0300447 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000448 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000450 //
451 // Everything else is okay, no error.
452 //
453 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300454 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000456 //
457 // If we get here, we have an error and a message.
458 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530459 if (symNode)
460 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000461 const char *symbol = symNode->getSymbol().c_str();
462 std::stringstream reasonStream;
463 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
464 std::string reason = reasonStream.str();
465 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000466 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530467 else
468 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000469 std::stringstream reasonStream;
470 reasonStream << "l-value required (" << message << ")";
471 std::string reason = reasonStream.str();
472 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000473 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474
Olli Etuaho8a176262016-08-16 14:23:01 +0300475 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000476}
477
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000478// Both test, and if necessary spit out an error, to see if the node is really
479// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300480void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481{
Olli Etuaho383b7912016-08-05 11:22:59 +0300482 if (node->getQualifier() != EvqConst)
483 {
484 error(node->getLine(), "constant expression required", "");
485 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486}
487
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488// Both test, and if necessary spit out an error, to see if the node is really
489// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300490void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491{
Olli Etuaho383b7912016-08-05 11:22:59 +0300492 if (!node->isScalarInt())
493 {
494 error(node->getLine(), "integer expression required", token);
495 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496}
497
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000498// Both test, and if necessary spit out an error, to see if we are currently
499// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800500bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000501{
Olli Etuaho856c4972016-08-08 11:38:39 +0300502 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300503 {
504 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800505 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300506 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800507 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000508}
509
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510// For now, keep it simple: if it starts "gl_", it's reserved, independent
511// of scope. Except, if the symbol table is at the built-in push-level,
512// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000513// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
514// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300515bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530517 static const char *reservedErrMsg = "reserved built-in name";
518 if (!symbolTable.atBuiltInLevel())
519 {
520 if (identifier.compare(0, 3, "gl_") == 0)
521 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000522 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300523 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000524 }
Jamie Madillacb4b812016-11-07 13:50:29 -0500525 if (sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530526 {
527 if (identifier.compare(0, 6, "webgl_") == 0)
528 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000529 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300530 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000531 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530532 if (identifier.compare(0, 7, "_webgl_") == 0)
533 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000534 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300535 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000536 }
537 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530538 if (identifier.find("__") != TString::npos)
539 {
540 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400541 "identifiers containing two consecutive underscores (__) are reserved as "
542 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530543 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300544 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 }
546 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547
Olli Etuaho8a176262016-08-16 14:23:01 +0300548 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549}
550
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000551// Make sure there is enough data provided to the constructor to build
552// something of the type of the constructor. Also returns the type of
553// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300554bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
555 TIntermNode *argumentsNode,
556 const TFunction &function,
557 TOperator op,
558 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000559{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000560 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400561 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530562 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400563 case EOpConstructMat2:
564 case EOpConstructMat2x3:
565 case EOpConstructMat2x4:
566 case EOpConstructMat3x2:
567 case EOpConstructMat3:
568 case EOpConstructMat3x4:
569 case EOpConstructMat4x2:
570 case EOpConstructMat4x3:
571 case EOpConstructMat4:
572 constructingMatrix = true;
573 break;
574 default:
575 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000576 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000577
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000578 //
579 // Note: It's okay to have too many components available, but not okay to have unused
580 // arguments. 'full' will go to true when enough args have been seen. If we loop
581 // again, there is an extra argument, so 'overfull' will become true.
582 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583
Jamie Madillb98c3a82015-07-23 14:26:04 -0400584 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400585 bool full = false;
586 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000587 bool matrixInMatrix = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500588 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530589 for (size_t i = 0; i < function.getParamCount(); ++i)
590 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700591 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000592 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530593
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000594 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000595 matrixInMatrix = true;
596 if (full)
597 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300598 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000599 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000600 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 arrayArg = true;
602 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530603
Olli Etuaho856c4972016-08-08 11:38:39 +0300604 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300605 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300606 // The size of an unsized constructor should already have been determined.
607 ASSERT(!type.isUnsizedArray());
608 if (static_cast<size_t>(type.getArraySize()) != function.getParamCount())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300609 {
610 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300611 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300612 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000613 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 if (arrayArg && op != EOpConstructStruct)
616 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000617 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300618 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000619 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620
Olli Etuaho856c4972016-08-08 11:38:39 +0300621 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530622 {
623 if (function.getParamCount() != 1)
624 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400625 error(line, "constructing matrix from matrix can only take one argument",
626 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300627 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000628 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000629 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000630
Arun Patole7e7e68d2015-05-22 12:02:25 +0530631 if (overFull)
632 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000633 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300634 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000635 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530636
Olli Etuaho856c4972016-08-08 11:38:39 +0300637 if (op == EOpConstructStruct && !type.isArray() &&
638 type.getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530639 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400640 error(line,
641 "Number of constructor parameters does not match the number of structure fields",
642 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300643 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000644 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645
Olli Etuaho856c4972016-08-08 11:38:39 +0300646 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530647 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300648 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
649 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530650 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000651 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300652 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000653 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000654 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000655
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200656 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530657 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200658 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300659 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000660 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200661
662 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
663 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530664 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200665 TIntermTyped *argTyped = argNode->getAsTyped();
666 ASSERT(argTyped != nullptr);
667 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
668 {
669 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300670 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200671 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300672 if (op != EOpConstructStruct && IsImage(argTyped->getBasicType()))
673 {
674 error(line, "cannot convert an image", "constructor");
675 return false;
676 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200677 if (argTyped->getBasicType() == EbtVoid)
678 {
679 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300680 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200681 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000682 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683
Olli Etuaho856c4972016-08-08 11:38:39 +0300684 if (type.isArray())
685 {
686 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
687 // the array.
688 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
689 {
690 const TType &argType = argNode->getAsTyped()->getType();
691 // It has already been checked that the argument is not an array.
692 ASSERT(!argType.isArray());
693 if (!argType.sameElementType(type))
694 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000695 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300696 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300697 }
698 }
699 }
700 else if (op == EOpConstructStruct)
701 {
702 const TFieldList &fields = type.getStruct()->fields();
703 TIntermSequence *args = argumentsAgg->getSequence();
704
705 for (size_t i = 0; i < fields.size(); i++)
706 {
707 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
708 {
709 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000710 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300711 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300712 }
713 }
714 }
715
Olli Etuaho8a176262016-08-16 14:23:01 +0300716 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000717}
718
Jamie Madillb98c3a82015-07-23 14:26:04 -0400719// This function checks to see if a void variable has been declared and raise an error message for
720// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000721//
722// returns true in case of an error
723//
Olli Etuaho856c4972016-08-08 11:38:39 +0300724bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400725 const TString &identifier,
726 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300728 if (type == EbtVoid)
729 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000730 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300731 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300732 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000733
Olli Etuaho8a176262016-08-16 14:23:01 +0300734 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735}
736
Jamie Madillb98c3a82015-07-23 14:26:04 -0400737// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300738// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300739void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530741 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
742 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000743 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530744 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745}
746
Jamie Madillb98c3a82015-07-23 14:26:04 -0400747// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300748// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300749void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750{
Martin Radev4a9cd802016-09-01 16:51:51 +0300751 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530752 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000753 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530754 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000755}
756
Olli Etuaho856c4972016-08-08 11:38:39 +0300757bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300758 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400759 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530761 if (pType.type == EbtStruct)
762 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300763 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530764 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000765 std::stringstream reasonStream;
766 reasonStream << reason << " (structure contains a sampler)";
767 std::string reasonStr = reasonStream.str();
768 error(line, reasonStr.c_str(), getBasicString(pType.type));
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 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000791 std::stringstream reasonStream;
792 reasonStream << reason << " (structure contains an image)";
793 std::string reasonStr = reasonStream.str();
794 error(line, reasonStr.c_str(), getBasicString(pType.type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300795
796 return false;
797 }
798
799 return true;
800 }
801 else if (IsImage(pType.type))
802 {
803 error(line, reason, getBasicString(pType.type));
804
805 return false;
806 }
807
808 return true;
809}
810
Olli Etuaho856c4972016-08-08 11:38:39 +0300811void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
812 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400813{
814 if (pType.layoutQualifier.location != -1)
815 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400816 error(line, "location must only be specified for a single input or output variable",
817 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400818 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400819}
820
Olli Etuaho856c4972016-08-08 11:38:39 +0300821void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
822 const TLayoutQualifier &layoutQualifier)
823{
824 if (layoutQualifier.location != -1)
825 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000826 error(location, "invalid layout qualifier: only valid on program inputs and outputs",
827 "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300828 }
829}
830
Martin Radev2cc85b32016-08-05 16:22:53 +0300831void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
832 TQualifier qualifier,
833 const TType &type)
834{
835 checkOutParameterIsNotSampler(line, qualifier, type);
836 checkOutParameterIsNotImage(line, qualifier, type);
837}
838
Olli Etuaho856c4972016-08-08 11:38:39 +0300839void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
840 TQualifier qualifier,
841 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842{
Martin Radev2cc85b32016-08-05 16:22:53 +0300843 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
844 if (IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530845 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000846 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000847 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848}
849
Martin Radev2cc85b32016-08-05 16:22:53 +0300850void TParseContext::checkOutParameterIsNotImage(const TSourceLoc &line,
851 TQualifier qualifier,
852 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853{
Martin Radev2cc85b32016-08-05 16:22:53 +0300854 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
855 if (IsImage(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530856 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300857 error(line, "images cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000858 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300862unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530864 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000865
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200866 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
867 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
868 // fold as array size.
869 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000870 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000871 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300872 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000873 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874
Olli Etuaho856c4972016-08-08 11:38:39 +0300875 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400876
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000877 if (constant->getBasicType() == EbtUInt)
878 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300879 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000880 }
881 else
882 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300883 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000884
Olli Etuaho856c4972016-08-08 11:38:39 +0300885 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000886 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400887 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300888 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000889 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400890
Olli Etuaho856c4972016-08-08 11:38:39 +0300891 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400892 }
893
Olli Etuaho856c4972016-08-08 11:38:39 +0300894 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400895 {
896 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300897 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400898 }
899
900 // The size of arrays is restricted here to prevent issues further down the
901 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
902 // 4096 registers so this should be reasonable even for aggressively optimizable code.
903 const unsigned int sizeLimit = 65536;
904
Olli Etuaho856c4972016-08-08 11:38:39 +0300905 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400906 {
907 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300908 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000909 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300910
911 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912}
913
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300915bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
916 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917{
Olli Etuaho8a176262016-08-16 14:23:01 +0300918 if ((elementQualifier.qualifier == EvqAttribute) ||
919 (elementQualifier.qualifier == EvqVertexIn) ||
920 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300921 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400922 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300923 TType(elementQualifier).getQualifierString());
924 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000925 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926
Olli Etuaho8a176262016-08-16 14:23:01 +0300927 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928}
929
Olli Etuaho8a176262016-08-16 14:23:01 +0300930// See if this element type can be formed into an array.
931bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000933 //
934 // Can the type be an array?
935 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300936 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400937 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300938 error(line, "cannot declare arrays of arrays",
939 TType(elementType).getCompleteString().c_str());
940 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000941 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300942 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
943 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
944 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300945 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300946 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300947 {
948 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300949 TType(elementType).getCompleteString().c_str());
950 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300951 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952
Olli Etuaho8a176262016-08-16 14:23:01 +0300953 return true;
954}
955
956// Check if this qualified element type can be formed into an array.
957bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
958 const TPublicType &elementType)
959{
960 if (checkIsValidTypeForArray(indexLocation, elementType))
961 {
962 return checkIsValidQualifierForArray(indexLocation, elementType);
963 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000964 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965}
966
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300968void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
969 const TString &identifier,
970 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971{
Olli Etuaho3739d232015-04-08 12:23:44 +0300972 ASSERT(type != nullptr);
973 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000974 {
975 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300976 type->qualifier = EvqTemporary;
977
978 // Generate informative error messages for ESSL1.
979 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400980 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000981 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530982 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400983 "structures containing arrays may not be declared constant since they cannot be "
984 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530985 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000986 }
987 else
988 {
989 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
990 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300991 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000992 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300993 if (type->isUnsizedArray())
994 {
995 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300996 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
Olli Etuaho2935c582015-04-08 14:32:06 +0300999// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000// and update the symbol table.
1001//
Olli Etuaho2935c582015-04-08 14:32:06 +03001002// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001004bool TParseContext::declareVariable(const TSourceLoc &line,
1005 const TString &identifier,
1006 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001007 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008{
Olli Etuaho2935c582015-04-08 14:32:06 +03001009 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010
Olli Etuaho856c4972016-08-08 11:38:39 +03001011 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012
Olli Etuaho2935c582015-04-08 14:32:06 +03001013 // gl_LastFragData may be redeclared with a new precision qualifier
1014 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1015 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001016 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1017 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001018 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001019 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001020 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001021 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001022 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001023 }
1024 }
1025 else
1026 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001027 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1028 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001029 return false;
1030 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001031 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032
Olli Etuaho8a176262016-08-16 14:23:01 +03001033 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001034 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035
Olli Etuaho2935c582015-04-08 14:32:06 +03001036 (*variable) = new TVariable(&identifier, type);
1037 if (!symbolTable.declare(*variable))
1038 {
1039 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001040 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001041 return false;
1042 }
1043
Olli Etuaho8a176262016-08-16 14:23:01 +03001044 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001045 return false;
1046
1047 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048}
1049
Martin Radev70866b82016-07-22 15:27:42 +03001050void TParseContext::checkIsParameterQualifierValid(
1051 const TSourceLoc &line,
1052 const TTypeQualifierBuilder &typeQualifierBuilder,
1053 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301054{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001055 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001056
1057 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301058 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001059 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1060 }
1061
1062 if (!IsImage(type->getBasicType()))
1063 {
1064 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, line);
1065 }
1066 else
1067 {
1068 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001069 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070
Martin Radev70866b82016-07-22 15:27:42 +03001071 type->setQualifier(typeQualifier.qualifier);
1072
1073 if (typeQualifier.precision != EbpUndefined)
1074 {
1075 type->setPrecision(typeQualifier.precision);
1076 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
Olli Etuaho856c4972016-08-08 11:38:39 +03001079bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001080{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001081 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001082 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301083 if (iter == extBehavior.end())
1084 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001085 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001086 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001087 }
zmo@google.comf5450912011-09-09 01:37:19 +00001088 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301089 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1090 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001091 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1092 // associated with more than one extension.
1093 if (extension == "GL_OVR_multiview")
1094 {
1095 return checkCanUseExtension(line, "GL_OVR_multiview2");
1096 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001097 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001098 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001099 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301100 if (iter->second == EBhWarn)
1101 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001102 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001103 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001104 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105
Olli Etuaho8a176262016-08-16 14:23:01 +03001106 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107}
1108
Martin Radevb8b01222016-11-20 23:25:53 +02001109void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1110 const TSourceLoc &location)
1111{
1112 if (publicType.isUnsizedArray())
1113 {
1114 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1115 // error. It is assumed that this applies to empty declarations as well.
1116 error(location, "empty array declaration needs to specify a size", "");
1117 }
1118 if (publicType.qualifier == EvqShared && !publicType.layoutQualifier.isEmpty())
1119 {
1120 error(location, "Shared memory declarations cannot have layout specified", "layout");
1121 }
1122}
1123
Jamie Madillb98c3a82015-07-23 14:26:04 -04001124// These checks are common for all declarations starting a declarator list, and declarators that
1125// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +03001126void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001127 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001128{
Olli Etuahofa33d582015-04-09 14:33:12 +03001129 switch (publicType.qualifier)
1130 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001131 case EvqVaryingIn:
1132 case EvqVaryingOut:
1133 case EvqAttribute:
1134 case EvqVertexIn:
1135 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001136 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001137 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001138 {
1139 error(identifierLocation, "cannot be used with a structure",
1140 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001141 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001142 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001143
Jamie Madillb98c3a82015-07-23 14:26:04 -04001144 default:
1145 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001146 }
1147
Jamie Madillb98c3a82015-07-23 14:26:04 -04001148 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001149 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1150 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001151 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001152 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001153 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001154 if (publicType.qualifier != EvqUniform &&
1155 !checkIsNotImage(identifierLocation, publicType.typeSpecifierNonArray,
1156 "images must be uniform"))
1157 {
1158 return;
1159 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001160
1161 // check for layout qualifier issues
1162 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1163
1164 if (layoutQualifier.matrixPacking != EmpUnspecified)
1165 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001166 error(identifierLocation, "layout qualifier only valid for interface blocks",
1167 getMatrixPackingString(layoutQualifier.matrixPacking));
Olli Etuaho383b7912016-08-05 11:22:59 +03001168 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001169 }
1170
1171 if (layoutQualifier.blockStorage != EbsUnspecified)
1172 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001173 error(identifierLocation, "layout qualifier only valid for interface blocks",
1174 getBlockStorageString(layoutQualifier.blockStorage));
Olli Etuaho383b7912016-08-05 11:22:59 +03001175 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001176 }
1177
Olli Etuaho383b7912016-08-05 11:22:59 +03001178 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001179 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001180 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001181 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001182
1183 if (IsImage(publicType.getBasicType()))
1184 {
1185
1186 switch (layoutQualifier.imageInternalFormat)
1187 {
1188 case EiifRGBA32F:
1189 case EiifRGBA16F:
1190 case EiifR32F:
1191 case EiifRGBA8:
1192 case EiifRGBA8_SNORM:
1193 if (!IsFloatImage(publicType.getBasicType()))
1194 {
1195 error(identifierLocation,
1196 "internal image format requires a floating image type",
1197 getBasicString(publicType.getBasicType()));
1198 return;
1199 }
1200 break;
1201 case EiifRGBA32I:
1202 case EiifRGBA16I:
1203 case EiifRGBA8I:
1204 case EiifR32I:
1205 if (!IsIntegerImage(publicType.getBasicType()))
1206 {
1207 error(identifierLocation,
1208 "internal image format requires an integer image type",
1209 getBasicString(publicType.getBasicType()));
1210 return;
1211 }
1212 break;
1213 case EiifRGBA32UI:
1214 case EiifRGBA16UI:
1215 case EiifRGBA8UI:
1216 case EiifR32UI:
1217 if (!IsUnsignedImage(publicType.getBasicType()))
1218 {
1219 error(identifierLocation,
1220 "internal image format requires an unsigned image type",
1221 getBasicString(publicType.getBasicType()));
1222 return;
1223 }
1224 break;
1225 case EiifUnspecified:
1226 error(identifierLocation, "layout qualifier", "No image internal format specified");
1227 return;
1228 default:
1229 error(identifierLocation, "layout qualifier", "unrecognized token");
1230 return;
1231 }
1232
1233 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1234 switch (layoutQualifier.imageInternalFormat)
1235 {
1236 case EiifR32F:
1237 case EiifR32I:
1238 case EiifR32UI:
1239 break;
1240 default:
1241 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1242 {
1243 error(identifierLocation, "layout qualifier",
1244 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1245 "image variables must be qualified readonly and/or writeonly");
1246 return;
1247 }
1248 break;
1249 }
1250 }
1251 else
1252 {
1253
1254 if (!checkInternalFormatIsNotSpecified(identifierLocation,
1255 layoutQualifier.imageInternalFormat))
1256 {
1257 return;
1258 }
1259
1260 if (!checkIsMemoryQualifierNotSpecified(publicType.memoryQualifier, identifierLocation))
1261 {
1262 return;
1263 }
1264 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001265}
1266
Olli Etuaho856c4972016-08-08 11:38:39 +03001267void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1268 const TString &layoutQualifierName,
1269 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001270{
1271
1272 if (mShaderVersion < versionRequired)
1273 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001274 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001275 }
1276}
1277
Olli Etuaho856c4972016-08-08 11:38:39 +03001278bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1279 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001280{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001281 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001282 for (size_t i = 0u; i < localSize.size(); ++i)
1283 {
1284 if (localSize[i] != -1)
1285 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001286 error(location,
1287 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1288 "global layout declaration",
1289 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001290 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001291 }
1292 }
1293
Olli Etuaho8a176262016-08-16 14:23:01 +03001294 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001295}
1296
Martin Radev2cc85b32016-08-05 16:22:53 +03001297bool TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
1298 TLayoutImageInternalFormat internalFormat)
1299{
1300 if (internalFormat != EiifUnspecified)
1301 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001302 error(location, "invalid layout qualifier: only valid when used with images",
1303 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001304 return false;
1305 }
1306 return true;
1307}
1308
Olli Etuaho383b7912016-08-05 11:22:59 +03001309void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001310 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001311{
1312 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1313 {
1314 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1315 if (qual == EvqOut || qual == EvqInOut)
1316 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001317 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001318 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001319 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001320 TString unmangledName =
1321 TFunction::unmangleName(fnCall->getFunctionSymbolInfo()->getName());
Olli Etuaho856c4972016-08-08 11:38:39 +03001322 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001323 "Constant value cannot be passed for 'out' or 'inout' parameters.",
1324 unmangledName.c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001325 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001326 }
1327 }
1328 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001329}
1330
Martin Radev70866b82016-07-22 15:27:42 +03001331void TParseContext::checkInvariantVariableQualifier(bool invariant,
1332 const TQualifier qualifier,
1333 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001334{
Martin Radev70866b82016-07-22 15:27:42 +03001335 if (!invariant)
1336 return;
1337
1338 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001339 {
Martin Radev70866b82016-07-22 15:27:42 +03001340 // input variables in the fragment shader can be also qualified as invariant
1341 if (!sh::CanBeInvariantESSL1(qualifier))
1342 {
1343 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1344 }
1345 }
1346 else
1347 {
1348 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1349 {
1350 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1351 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001352 }
1353}
1354
Arun Patole7e7e68d2015-05-22 12:02:25 +05301355bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001356{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001357 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001358 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1359 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001360}
1361
Arun Patole7e7e68d2015-05-22 12:02:25 +05301362bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001363{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001364 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001365}
1366
Jamie Madillb98c3a82015-07-23 14:26:04 -04001367void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1368 const char *extName,
1369 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001370{
1371 pp::SourceLocation srcLoc;
1372 srcLoc.file = loc.first_file;
1373 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001374 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001375}
1376
Jamie Madillb98c3a82015-07-23 14:26:04 -04001377void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1378 const char *name,
1379 const char *value,
1380 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001381{
1382 pp::SourceLocation srcLoc;
1383 srcLoc.file = loc.first_file;
1384 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001385 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001386}
1387
Martin Radev4c4c8e72016-08-04 12:25:34 +03001388sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001389{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001390 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001391 for (size_t i = 0u; i < result.size(); ++i)
1392 {
1393 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1394 {
1395 result[i] = 1;
1396 }
1397 else
1398 {
1399 result[i] = mComputeShaderLocalSize[i];
1400 }
1401 }
1402 return result;
1403}
1404
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001405/////////////////////////////////////////////////////////////////////////////////
1406//
1407// Non-Errors.
1408//
1409/////////////////////////////////////////////////////////////////////////////////
1410
Jamie Madill5c097022014-08-20 16:38:32 -04001411const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1412 const TString *name,
1413 const TSymbol *symbol)
1414{
1415 const TVariable *variable = NULL;
1416
1417 if (!symbol)
1418 {
1419 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001420 }
1421 else if (!symbol->isVariable())
1422 {
1423 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001424 }
1425 else
1426 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001427 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001428
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001429 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001430 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001431 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001432 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001433 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001434
1435 // Reject shaders using both gl_FragData and gl_FragColor
1436 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001437 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001438 {
1439 mUsesFragData = true;
1440 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001441 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001442 {
1443 mUsesFragColor = true;
1444 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001445 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1446 {
1447 mUsesSecondaryOutputs = true;
1448 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001449
1450 // This validation is not quite correct - it's only an error to write to
1451 // both FragData and FragColor. For simplicity, and because users shouldn't
1452 // be rewarded for reading from undefined varaibles, return an error
1453 // if they are both referenced, rather than assigned.
1454 if (mUsesFragData && mUsesFragColor)
1455 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001456 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1457 if (mUsesSecondaryOutputs)
1458 {
1459 errorMessage =
1460 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1461 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1462 }
1463 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001464 }
Martin Radevb0883602016-08-04 17:48:58 +03001465
1466 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1467 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1468 qualifier == EvqWorkGroupSize)
1469 {
1470 error(location,
1471 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1472 "gl_WorkGroupSize");
1473 }
Jamie Madill5c097022014-08-20 16:38:32 -04001474 }
1475
1476 if (!variable)
1477 {
1478 TType type(EbtFloat, EbpUndefined);
1479 TVariable *fakeVariable = new TVariable(name, type);
1480 symbolTable.declare(fakeVariable);
1481 variable = fakeVariable;
1482 }
1483
1484 return variable;
1485}
1486
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001487TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1488 const TString *name,
1489 const TSymbol *symbol)
1490{
1491 const TVariable *variable = getNamedVariable(location, name, symbol);
1492
Olli Etuaho09b04a22016-12-15 13:30:26 +00001493 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1494 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1495 {
1496 // WEBGL_multiview spec
1497 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1498 "gl_ViewID_OVR");
1499 }
1500
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001501 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001502 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001503 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001504 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001505 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001506 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1507 mComputeShaderLocalSizeDeclared)
1508 {
1509 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1510 // needs to be added to the AST as a constant and not as a symbol.
1511 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1512 TConstantUnion *constArray = new TConstantUnion[3];
1513 for (size_t i = 0; i < 3; ++i)
1514 {
1515 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1516 }
1517
1518 ASSERT(variable->getType().getBasicType() == EbtUInt);
1519 ASSERT(variable->getType().getObjectSize() == 3);
1520
1521 TType type(variable->getType());
1522 type.setQualifier(EvqConst);
1523 return intermediate.addConstantUnion(constArray, type, location);
1524 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001525 else
1526 {
1527 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1528 variable->getType(), location);
1529 }
1530}
1531
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001532//
1533// Look up a function name in the symbol table, and make sure it is a function.
1534//
1535// Return the function symbol if found, otherwise 0.
1536//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001537const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1538 TFunction *call,
1539 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301540 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001541{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001542 // First find by unmangled name to check whether the function name has been
1543 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001544 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301545 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1546 if (symbol == 0 || symbol->isFunction())
1547 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001548 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001550
Arun Patole7e7e68d2015-05-22 12:02:25 +05301551 if (symbol == 0)
1552 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001553 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001554 return 0;
1555 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001556
Arun Patole7e7e68d2015-05-22 12:02:25 +05301557 if (!symbol->isFunction())
1558 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001559 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001560 return 0;
1561 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001562
Jamie Madillb98c3a82015-07-23 14:26:04 -04001563 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001564}
1565
1566//
1567// Initializers show up in several places in the grammar. Have one set of
1568// code to handle them here.
1569//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001570// Returns true on error, false if no error
1571//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001572bool TParseContext::executeInitializer(const TSourceLoc &line,
1573 const TString &identifier,
1574 const TPublicType &pType,
1575 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001576 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577{
Olli Etuaho13389b62016-10-16 11:48:18 +01001578 ASSERT(initNode != nullptr);
1579 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001580 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001581
Olli Etuaho2935c582015-04-08 14:32:06 +03001582 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001583 if (type.isUnsizedArray())
1584 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001585 // We have not checked yet whether the initializer actually is an array or not.
1586 if (initializer->isArray())
1587 {
1588 type.setArraySize(initializer->getArraySize());
1589 }
1590 else
1591 {
1592 // Having a non-array initializer for an unsized array will result in an error later,
1593 // so we don't generate an error message here.
1594 type.setArraySize(1u);
1595 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001596 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001597 if (!declareVariable(line, identifier, type, &variable))
1598 {
1599 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001600 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001601
Olli Etuahob0c645e2015-05-12 14:25:36 +03001602 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001603 if (symbolTable.atGlobalLevel() &&
1604 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001605 {
1606 // Error message does not completely match behavior with ESSL 1.00, but
1607 // we want to steer developers towards only using constant expressions.
1608 error(line, "global variable initializers must be constant expressions", "=");
1609 return true;
1610 }
1611 if (globalInitWarning)
1612 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001613 warning(
1614 line,
1615 "global variable initializers should be constant expressions "
1616 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1617 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001618 }
1619
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001620 //
1621 // identifier must be of type constant, a global, or a temporary
1622 //
1623 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301624 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1625 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001626 error(line, " cannot initialize this type of qualifier ",
1627 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001628 return true;
1629 }
1630 //
1631 // test for and propagate constant
1632 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001633
Arun Patole7e7e68d2015-05-22 12:02:25 +05301634 if (qualifier == EvqConst)
1635 {
1636 if (qualifier != initializer->getType().getQualifier())
1637 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001638 std::stringstream reasonStream;
1639 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1640 << "'";
1641 std::string reason = reasonStream.str();
1642 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001643 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001644 return true;
1645 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301646 if (type != initializer->getType())
1647 {
1648 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001649 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001650 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001651 return true;
1652 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001653
1654 // Save the constant folded value to the variable if possible. For example array
1655 // initializers are not folded, since that way copying the array literal to multiple places
1656 // in the shader is avoided.
1657 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1658 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301659 if (initializer->getAsConstantUnion())
1660 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001661 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001662 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001663 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301664 }
1665 else if (initializer->getAsSymbolNode())
1666 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001667 const TSymbol *symbol =
1668 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1669 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001671 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001672 if (constArray)
1673 {
1674 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001675 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001676 return false;
1677 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001678 }
1679 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001680
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001681 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1682 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001683 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1684 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001685 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001686 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1687 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001688 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001690 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691}
1692
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001693void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1694{
1695 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1696 typeSpecifier->getBasicType());
1697
1698 if (mShaderVersion < 300 && typeSpecifier->array)
1699 {
1700 error(typeSpecifier->getLine(), "not supported", "first-class array");
1701 typeSpecifier->clearArrayness();
1702 }
1703}
1704
Martin Radev70866b82016-07-22 15:27:42 +03001705TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301706 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001707{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001708 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001709
Martin Radev70866b82016-07-22 15:27:42 +03001710 TPublicType returnType = typeSpecifier;
1711 returnType.qualifier = typeQualifier.qualifier;
1712 returnType.invariant = typeQualifier.invariant;
1713 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001714 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001715 returnType.precision = typeSpecifier.precision;
1716
1717 if (typeQualifier.precision != EbpUndefined)
1718 {
1719 returnType.precision = typeQualifier.precision;
1720 }
1721
Martin Radev4a9cd802016-09-01 16:51:51 +03001722 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1723 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001724
Martin Radev4a9cd802016-09-01 16:51:51 +03001725 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1726 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001727
Martin Radev4a9cd802016-09-01 16:51:51 +03001728 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001729
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001730 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001731 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001732 if (typeSpecifier.array)
1733 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001734 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001735 returnType.clearArrayness();
1736 }
1737
Martin Radev70866b82016-07-22 15:27:42 +03001738 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001739 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001740 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001741 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001742 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001743 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001744
Martin Radev70866b82016-07-22 15:27:42 +03001745 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001746 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001747 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001748 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001749 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001750 }
1751 }
1752 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001753 {
Martin Radev70866b82016-07-22 15:27:42 +03001754 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001755 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001756 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001757 }
Martin Radev70866b82016-07-22 15:27:42 +03001758 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1759 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001760 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001761 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1762 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001763 }
Martin Radev70866b82016-07-22 15:27:42 +03001764 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001765 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001766 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001767 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001768 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001769 }
1770
1771 return returnType;
1772}
1773
Olli Etuaho856c4972016-08-08 11:38:39 +03001774void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1775 const TPublicType &type,
1776 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001777{
1778 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001779 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001780 {
1781 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001782 }
1783
1784 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1785 switch (qualifier)
1786 {
1787 case EvqVertexIn:
1788 // ESSL 3.00 section 4.3.4
1789 if (type.array)
1790 {
1791 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001792 }
1793 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1794 return;
1795 case EvqFragmentOut:
1796 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001797 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001798 {
1799 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001800 }
1801 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1802 return;
1803 default:
1804 break;
1805 }
1806
1807 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1808 // restrictions.
1809 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001810 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1811 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001812 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1813 {
1814 error(qualifierLocation, "must use 'flat' interpolation here",
1815 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001816 }
1817
Martin Radev4a9cd802016-09-01 16:51:51 +03001818 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001819 {
1820 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1821 // These restrictions are only implied by the ESSL 3.00 spec, but
1822 // the ESSL 3.10 spec lists these restrictions explicitly.
1823 if (type.array)
1824 {
1825 error(qualifierLocation, "cannot be an array of structures",
1826 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001827 }
1828 if (type.isStructureContainingArrays())
1829 {
1830 error(qualifierLocation, "cannot be a structure containing an array",
1831 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001832 }
1833 if (type.isStructureContainingType(EbtStruct))
1834 {
1835 error(qualifierLocation, "cannot be a structure containing a structure",
1836 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001837 }
1838 if (type.isStructureContainingType(EbtBool))
1839 {
1840 error(qualifierLocation, "cannot be a structure containing a bool",
1841 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001842 }
1843 }
1844}
1845
Martin Radev2cc85b32016-08-05 16:22:53 +03001846void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1847{
1848 if (qualifier.getType() == QtStorage)
1849 {
1850 const TStorageQualifierWrapper &storageQualifier =
1851 static_cast<const TStorageQualifierWrapper &>(qualifier);
1852 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1853 !symbolTable.atGlobalLevel())
1854 {
1855 error(storageQualifier.getLine(),
1856 "Local variables can only use the const storage qualifier.",
1857 storageQualifier.getQualifierString().c_str());
1858 }
1859 }
1860}
1861
1862bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1863 const TSourceLoc &location)
1864{
1865 if (memoryQualifier.readonly)
1866 {
1867 error(location, "Only allowed with images.", "readonly");
1868 return false;
1869 }
1870 if (memoryQualifier.writeonly)
1871 {
1872 error(location, "Only allowed with images.", "writeonly");
1873 return false;
1874 }
Martin Radev049edfa2016-11-11 14:35:37 +02001875 if (memoryQualifier.coherent)
1876 {
1877 error(location, "Only allowed with images.", "coherent");
1878 return false;
1879 }
1880 if (memoryQualifier.restrictQualifier)
1881 {
1882 error(location, "Only allowed with images.", "restrict");
1883 return false;
1884 }
1885 if (memoryQualifier.volatileQualifier)
1886 {
1887 error(location, "Only allowed with images.", "volatile");
1888 return false;
1889 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001890 return true;
1891}
1892
Olli Etuaho13389b62016-10-16 11:48:18 +01001893TIntermDeclaration *TParseContext::parseSingleDeclaration(
1894 TPublicType &publicType,
1895 const TSourceLoc &identifierOrTypeLocation,
1896 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001897{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001898 TType type(publicType);
1899 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1900 mDirectiveHandler.pragma().stdgl.invariantAll)
1901 {
1902 TQualifier qualifier = type.getQualifier();
1903
1904 // The directive handler has already taken care of rejecting invalid uses of this pragma
1905 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1906 // affected variable declarations:
1907 //
1908 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1909 // elsewhere, in TranslatorGLSL.)
1910 //
1911 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1912 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1913 // the way this is currently implemented we have to enable this compiler option before
1914 // parsing the shader and determining the shading language version it uses. If this were
1915 // implemented as a post-pass, the workaround could be more targeted.
1916 //
1917 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1918 // the specification, but there are desktop OpenGL drivers that expect that this is the
1919 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1920 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1921 {
1922 type.setInvariant(true);
1923 }
1924 }
1925
1926 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001927
Olli Etuahobab4c082015-04-24 16:38:49 +03001928 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001929
Olli Etuahobab4c082015-04-24 16:38:49 +03001930 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1931
Olli Etuaho13389b62016-10-16 11:48:18 +01001932 TIntermDeclaration *declaration = new TIntermDeclaration();
1933 declaration->setLine(identifierOrTypeLocation);
1934
Olli Etuahobab4c082015-04-24 16:38:49 +03001935 if (emptyDeclaration)
1936 {
Martin Radevb8b01222016-11-20 23:25:53 +02001937 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobab4c082015-04-24 16:38:49 +03001938 }
1939 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001940 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001941 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001942
Olli Etuaho856c4972016-08-08 11:38:39 +03001943 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001944
Olli Etuaho2935c582015-04-08 14:32:06 +03001945 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001946 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001947
1948 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001949 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001950 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001951 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001952 }
1953
Olli Etuaho13389b62016-10-16 11:48:18 +01001954 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1955 // that may just declare a type.
1956 declaration->appendDeclarator(symbol);
1957
1958 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001959}
1960
Olli Etuaho13389b62016-10-16 11:48:18 +01001961TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1962 const TSourceLoc &identifierLocation,
1963 const TString &identifier,
1964 const TSourceLoc &indexLocation,
1965 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001966{
Olli Etuahofa33d582015-04-09 14:33:12 +03001967 mDeferredSingleDeclarationErrorCheck = false;
1968
Olli Etuaho383b7912016-08-05 11:22:59 +03001969 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001970
Olli Etuaho856c4972016-08-08 11:38:39 +03001971 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001972
Olli Etuaho8a176262016-08-16 14:23:01 +03001973 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001974
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001975 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001976
Olli Etuaho856c4972016-08-08 11:38:39 +03001977 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001978 // Make the type an array even if size check failed.
1979 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1980 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001981
Olli Etuaho2935c582015-04-08 14:32:06 +03001982 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001983 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001984
Olli Etuaho13389b62016-10-16 11:48:18 +01001985 TIntermDeclaration *declaration = new TIntermDeclaration();
1986 declaration->setLine(identifierLocation);
1987
Olli Etuahoe7847b02015-03-16 11:56:12 +02001988 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001989 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001990 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001991 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001992 declaration->appendDeclarator(symbol);
1993 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001994
Olli Etuaho13389b62016-10-16 11:48:18 +01001995 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001996}
1997
Olli Etuaho13389b62016-10-16 11:48:18 +01001998TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1999 const TSourceLoc &identifierLocation,
2000 const TString &identifier,
2001 const TSourceLoc &initLocation,
2002 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002003{
Olli Etuahofa33d582015-04-09 14:33:12 +03002004 mDeferredSingleDeclarationErrorCheck = false;
2005
Olli Etuaho383b7912016-08-05 11:22:59 +03002006 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002007
Olli Etuaho13389b62016-10-16 11:48:18 +01002008 TIntermDeclaration *declaration = new TIntermDeclaration();
2009 declaration->setLine(identifierLocation);
2010
2011 TIntermBinary *initNode = nullptr;
2012 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002013 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002014 if (initNode)
2015 {
2016 declaration->appendDeclarator(initNode);
2017 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002018 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002019 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002020}
2021
Olli Etuaho13389b62016-10-16 11:48:18 +01002022TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002023 TPublicType &publicType,
2024 const TSourceLoc &identifierLocation,
2025 const TString &identifier,
2026 const TSourceLoc &indexLocation,
2027 TIntermTyped *indexExpression,
2028 const TSourceLoc &initLocation,
2029 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002030{
2031 mDeferredSingleDeclarationErrorCheck = false;
2032
Olli Etuaho383b7912016-08-05 11:22:59 +03002033 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002034
Olli Etuaho8a176262016-08-16 14:23:01 +03002035 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002036
2037 TPublicType arrayType(publicType);
2038
Olli Etuaho856c4972016-08-08 11:38:39 +03002039 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002040 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2041 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002042 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002043 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002044 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002045 }
2046 // Make the type an array even if size check failed.
2047 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2048 arrayType.setArraySize(size);
2049
Olli Etuaho13389b62016-10-16 11:48:18 +01002050 TIntermDeclaration *declaration = new TIntermDeclaration();
2051 declaration->setLine(identifierLocation);
2052
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002053 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002054 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002055 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2056 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002057 if (initNode)
2058 {
2059 declaration->appendDeclarator(initNode);
2060 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002061 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002062
2063 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002064}
2065
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002066TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002067 const TTypeQualifierBuilder &typeQualifierBuilder,
2068 const TSourceLoc &identifierLoc,
2069 const TString *identifier,
2070 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002071{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002072 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002073
Martin Radev70866b82016-07-22 15:27:42 +03002074 if (!typeQualifier.invariant)
2075 {
2076 error(identifierLoc, "Expected invariant", identifier->c_str());
2077 return nullptr;
2078 }
2079 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2080 {
2081 return nullptr;
2082 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002083 if (!symbol)
2084 {
2085 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002086 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002087 }
Martin Radev70866b82016-07-22 15:27:42 +03002088 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002089 {
Martin Radev70866b82016-07-22 15:27:42 +03002090 error(identifierLoc, "invariant declaration specifies qualifier",
2091 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002092 }
Martin Radev70866b82016-07-22 15:27:42 +03002093 if (typeQualifier.precision != EbpUndefined)
2094 {
2095 error(identifierLoc, "invariant declaration specifies precision",
2096 getPrecisionString(typeQualifier.precision));
2097 }
2098 if (!typeQualifier.layoutQualifier.isEmpty())
2099 {
2100 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2101 }
2102
2103 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2104 ASSERT(variable);
2105 const TType &type = variable->getType();
2106
2107 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2108 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002109 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002110
2111 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2112
2113 TIntermSymbol *intermSymbol =
2114 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2115
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002116 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002117}
2118
Olli Etuaho13389b62016-10-16 11:48:18 +01002119void TParseContext::parseDeclarator(TPublicType &publicType,
2120 const TSourceLoc &identifierLocation,
2121 const TString &identifier,
2122 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002123{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002124 // If the declaration starting this declarator list was empty (example: int,), some checks were
2125 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002126 if (mDeferredSingleDeclarationErrorCheck)
2127 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002128 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002129 mDeferredSingleDeclarationErrorCheck = false;
2130 }
2131
Olli Etuaho856c4972016-08-08 11:38:39 +03002132 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002133
Olli Etuaho856c4972016-08-08 11:38:39 +03002134 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002135
Olli Etuaho2935c582015-04-08 14:32:06 +03002136 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002137 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002138
Jamie Madillb98c3a82015-07-23 14:26:04 -04002139 TIntermSymbol *symbol =
2140 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002141 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002142 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002143 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002144 declarationOut->appendDeclarator(symbol);
2145 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002146}
2147
Olli Etuaho13389b62016-10-16 11:48:18 +01002148void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2149 const TSourceLoc &identifierLocation,
2150 const TString &identifier,
2151 const TSourceLoc &arrayLocation,
2152 TIntermTyped *indexExpression,
2153 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002154{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002155 // If the declaration starting this declarator list was empty (example: int,), some checks were
2156 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002157 if (mDeferredSingleDeclarationErrorCheck)
2158 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002159 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002160 mDeferredSingleDeclarationErrorCheck = false;
2161 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002162
Olli Etuaho856c4972016-08-08 11:38:39 +03002163 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002164
Olli Etuaho856c4972016-08-08 11:38:39 +03002165 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002166
Olli Etuaho8a176262016-08-16 14:23:01 +03002167 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002168 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002169 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002170 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002171 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002172
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002173 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002174 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002175
Jamie Madillb98c3a82015-07-23 14:26:04 -04002176 TIntermSymbol *symbol =
2177 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002178 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002179 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002180
Olli Etuaho13389b62016-10-16 11:48:18 +01002181 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002182 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002183}
2184
Olli Etuaho13389b62016-10-16 11:48:18 +01002185void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2186 const TSourceLoc &identifierLocation,
2187 const TString &identifier,
2188 const TSourceLoc &initLocation,
2189 TIntermTyped *initializer,
2190 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002191{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002192 // If the declaration starting this declarator list was empty (example: int,), some checks were
2193 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002194 if (mDeferredSingleDeclarationErrorCheck)
2195 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002196 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002197 mDeferredSingleDeclarationErrorCheck = false;
2198 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002199
Olli Etuaho856c4972016-08-08 11:38:39 +03002200 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002201
Olli Etuaho13389b62016-10-16 11:48:18 +01002202 TIntermBinary *initNode = nullptr;
2203 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002204 {
2205 //
2206 // build the intermediate representation
2207 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002208 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002209 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002210 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002211 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002212 }
2213}
2214
Olli Etuaho13389b62016-10-16 11:48:18 +01002215void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2216 const TSourceLoc &identifierLocation,
2217 const TString &identifier,
2218 const TSourceLoc &indexLocation,
2219 TIntermTyped *indexExpression,
2220 const TSourceLoc &initLocation,
2221 TIntermTyped *initializer,
2222 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002223{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002224 // If the declaration starting this declarator list was empty (example: int,), some checks were
2225 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002226 if (mDeferredSingleDeclarationErrorCheck)
2227 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002228 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002229 mDeferredSingleDeclarationErrorCheck = false;
2230 }
2231
Olli Etuaho856c4972016-08-08 11:38:39 +03002232 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002233
Olli Etuaho8a176262016-08-16 14:23:01 +03002234 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002235
2236 TPublicType arrayType(publicType);
2237
Olli Etuaho856c4972016-08-08 11:38:39 +03002238 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002239 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2240 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002241 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002242 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002243 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002244 }
2245 // Make the type an array even if size check failed.
2246 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2247 arrayType.setArraySize(size);
2248
2249 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002250 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002251 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2252 {
2253 if (initNode)
2254 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002255 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002256 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002257 }
2258}
2259
Martin Radev70866b82016-07-22 15:27:42 +03002260void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002261{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002262 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002263 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002264
Martin Radev70866b82016-07-22 15:27:42 +03002265 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2266 typeQualifier.line);
2267
Jamie Madillc2128ff2016-07-04 10:26:17 -04002268 // It should never be the case, but some strange parser errors can send us here.
2269 if (layoutQualifier.isEmpty())
2270 {
2271 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002272 return;
2273 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002274
Martin Radev802abe02016-08-04 17:48:32 +03002275 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002276 {
Martin Radev802abe02016-08-04 17:48:32 +03002277 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002278 return;
2279 }
2280
Martin Radev2cc85b32016-08-05 16:22:53 +03002281 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2282
2283 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2284
Martin Radev802abe02016-08-04 17:48:32 +03002285 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002286 {
Martin Radev802abe02016-08-04 17:48:32 +03002287 if (mComputeShaderLocalSizeDeclared &&
2288 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2289 {
2290 error(typeQualifier.line, "Work group size does not match the previous declaration",
2291 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002292 return;
2293 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002294
Martin Radev802abe02016-08-04 17:48:32 +03002295 if (mShaderVersion < 310)
2296 {
2297 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002298 return;
2299 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002300
Martin Radev4c4c8e72016-08-04 12:25:34 +03002301 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002302 {
2303 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002304 return;
2305 }
2306
2307 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2308 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2309
2310 const TConstantUnion *maxComputeWorkGroupSizeData =
2311 maxComputeWorkGroupSize->getConstPointer();
2312
2313 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2314 {
2315 if (layoutQualifier.localSize[i] != -1)
2316 {
2317 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2318 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2319 if (mComputeShaderLocalSize[i] < 1 ||
2320 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2321 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002322 std::stringstream reasonStream;
2323 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2324 << maxComputeWorkGroupSizeValue;
2325 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002326
Olli Etuaho4de340a2016-12-16 09:32:03 +00002327 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002328 return;
2329 }
2330 }
2331 }
2332
2333 mComputeShaderLocalSizeDeclared = true;
2334 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00002335 else if (mMultiviewAvailable &&
2336 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
2337 typeQualifier.qualifier == EvqVertexIn)
2338 {
2339 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2340 // specification.
2341 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2342 {
2343 error(typeQualifier.line, "Number of views does not match the previous declaration",
2344 "layout");
2345 return;
2346 }
2347
2348 if (layoutQualifier.numViews == -1)
2349 {
2350 error(typeQualifier.line, "No num_views specified", "layout");
2351 return;
2352 }
2353
2354 if (layoutQualifier.numViews > mMaxNumViews)
2355 {
2356 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2357 "layout");
2358 return;
2359 }
2360
2361 mNumViews = layoutQualifier.numViews;
2362 }
Martin Radev802abe02016-08-04 17:48:32 +03002363 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002364 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002365 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002366 {
Martin Radev802abe02016-08-04 17:48:32 +03002367 return;
2368 }
2369
2370 if (typeQualifier.qualifier != EvqUniform)
2371 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002372 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2373 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002374 return;
2375 }
2376
2377 if (mShaderVersion < 300)
2378 {
2379 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2380 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002381 return;
2382 }
2383
Olli Etuaho09b04a22016-12-15 13:30:26 +00002384 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002385
2386 if (layoutQualifier.matrixPacking != EmpUnspecified)
2387 {
2388 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2389 }
2390
2391 if (layoutQualifier.blockStorage != EbsUnspecified)
2392 {
2393 mDefaultBlockStorage = layoutQualifier.blockStorage;
2394 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002395 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002396}
2397
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002398TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2399 const TFunction &function,
2400 const TSourceLoc &location,
2401 bool insertParametersToSymbolTable)
2402{
2403 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(function.getReturnType());
2404 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2405 // point to the data that already exists in the symbol table.
2406 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2407 prototype->setLine(location);
2408
2409 for (size_t i = 0; i < function.getParamCount(); i++)
2410 {
2411 const TConstParameter &param = function.getParam(i);
2412
2413 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2414 // be used for unused args).
2415 if (param.name != nullptr)
2416 {
2417 TVariable *variable = new TVariable(param.name, *param.type);
2418
2419 // Insert the parameter in the symbol table.
2420 if (insertParametersToSymbolTable && !symbolTable.declare(variable))
2421 {
2422 error(location, "redefinition", variable->getName().c_str());
2423 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2424 continue;
2425 }
2426 TIntermSymbol *symbol = intermediate.addSymbol(
2427 variable->getUniqueId(), variable->getName(), variable->getType(), location);
2428 prototype->appendParameter(symbol);
2429 }
2430 else
2431 {
2432 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2433 }
2434 }
2435 return prototype;
2436}
2437
Olli Etuaho16c745a2017-01-16 17:02:27 +00002438TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2439 const TFunction &parsedFunction,
2440 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002441{
Olli Etuaho476197f2016-10-11 13:59:08 +01002442 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2443 // first declaration. Either way the instance in the symbol table is used to track whether the
2444 // function is declared multiple times.
2445 TFunction *function = static_cast<TFunction *>(
2446 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2447 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002448 {
2449 // ESSL 1.00.17 section 4.2.7.
2450 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2451 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002452 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002453 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002454
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002455 TIntermFunctionPrototype *prototype =
2456 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002457
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002458 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002459
2460 if (!symbolTable.atGlobalLevel())
2461 {
2462 // ESSL 3.00.4 section 4.2.4.
2463 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002464 }
2465
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002466 return prototype;
2467}
2468
Olli Etuaho336b1472016-10-05 16:37:55 +01002469TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002470 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002471 TIntermBlock *functionBody,
2472 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002473{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002474 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002475 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2476 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002477 error(location, "function does not return a value:",
2478 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002479 }
2480
Olli Etuahof51fdd22016-10-03 10:03:40 +01002481 if (functionBody == nullptr)
2482 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002483 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002484 functionBody->setLine(location);
2485 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002486 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002487 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002488 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002489
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002490 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002491 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002492}
2493
Olli Etuaho476197f2016-10-11 13:59:08 +01002494void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2495 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002496 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002497{
Olli Etuaho476197f2016-10-11 13:59:08 +01002498 ASSERT(function);
2499 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002500 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002501 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002502
2503 if (builtIn)
2504 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002505 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002506 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002507 else
Jamie Madill185fb402015-06-12 15:48:48 -04002508 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002509 TFunction *prevDec = static_cast<TFunction *>(
2510 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2511
2512 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2513 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2514 // occurance.
2515 if (*function != prevDec)
2516 {
2517 // Swap the parameters of the previous declaration to the parameters of the function
2518 // definition (parameter names may differ).
2519 prevDec->swapParameters(**function);
2520
2521 // The function definition will share the same symbol as any previous declaration.
2522 *function = prevDec;
2523 }
2524
2525 if ((*function)->isDefined())
2526 {
2527 error(location, "function already has a body", (*function)->getName().c_str());
2528 }
2529
2530 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002531 }
Jamie Madill185fb402015-06-12 15:48:48 -04002532
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002533 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002534 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002535 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002536
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002537 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002538 setLoopNestingLevel(0);
2539}
2540
Jamie Madillb98c3a82015-07-23 14:26:04 -04002541TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002542{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002543 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002544 // We don't know at this point whether this is a function definition or a prototype.
2545 // The definition production code will check for redefinitions.
2546 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002547 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002548 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2549 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002550 //
2551 TFunction *prevDec =
2552 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302553
Martin Radevda6254b2016-12-14 17:00:36 +02002554 if (getShaderVersion() >= 300 &&
2555 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2556 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302557 {
Martin Radevda6254b2016-12-14 17:00:36 +02002558 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302559 // Therefore overloading or redefining builtin functions is an error.
2560 error(location, "Name of a built-in function cannot be redeclared as function",
2561 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302562 }
2563 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002564 {
2565 if (prevDec->getReturnType() != function->getReturnType())
2566 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002567 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002568 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002569 }
2570 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2571 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002572 if (prevDec->getParam(i).type->getQualifier() !=
2573 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002574 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002575 error(location,
2576 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002577 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002578 }
2579 }
2580 }
2581
2582 //
2583 // Check for previously declared variables using the same name.
2584 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002585 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002586 if (prevSym)
2587 {
2588 if (!prevSym->isFunction())
2589 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002590 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002591 }
2592 }
2593 else
2594 {
2595 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002596 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002597 }
2598
2599 // We're at the inner scope level of the function's arguments and body statement.
2600 // Add the function prototype to the surrounding scope instead.
2601 symbolTable.getOuterLevel()->insert(function);
2602
Olli Etuaho78d13742017-01-18 13:06:10 +00002603 // Raise error message if main function takes any parameters or return anything other than void
2604 if (function->getName() == "main")
2605 {
2606 if (function->getParamCount() > 0)
2607 {
2608 error(location, "function cannot take any parameter(s)", "main");
2609 }
2610 if (function->getReturnType().getBasicType() != EbtVoid)
2611 {
2612 error(location, "main function cannot return a value",
2613 function->getReturnType().getBasicString());
2614 }
2615 }
2616
Jamie Madill185fb402015-06-12 15:48:48 -04002617 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002618 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2619 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002620 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2621 //
2622 return function;
2623}
2624
Olli Etuaho9de84a52016-06-14 17:36:01 +03002625TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2626 const TString *name,
2627 const TSourceLoc &location)
2628{
2629 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2630 {
2631 error(location, "no qualifiers allowed for function return",
2632 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002633 }
2634 if (!type.layoutQualifier.isEmpty())
2635 {
2636 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002637 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002638 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002639 checkIsNotSampler(location, type.typeSpecifierNonArray,
2640 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002641 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002642 if (mShaderVersion < 300)
2643 {
2644 // Array return values are forbidden, but there's also no valid syntax for declaring array
2645 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002646 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002647
2648 if (type.isStructureContainingArrays())
2649 {
2650 // ESSL 1.00.17 section 6.1 Function Definitions
2651 error(location, "structures containing arrays can't be function return values",
2652 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002653 }
2654 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002655
2656 // Add the function as a prototype after parsing it (we do not support recursion)
2657 return new TFunction(name, new TType(type));
2658}
2659
Jamie Madill06145232015-05-13 13:10:01 -04002660TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002661{
Jamie Madill06145232015-05-13 13:10:01 -04002662 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002663 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002664 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002665 error(publicType.getLine(), "constructor can't be a structure definition",
2666 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002667 }
2668
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002669 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002670 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002671 {
2672 op = EOpConstructStruct;
2673 }
2674 else
2675 {
Geoff Lang156d7192016-07-21 16:11:00 -04002676 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002677 if (op == EOpNull)
2678 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002679 error(publicType.getLine(), "cannot construct this type",
2680 getBasicString(publicType.getBasicType()));
2681 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002682 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002683 }
2684 }
2685
2686 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002687 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002688 return new TFunction(&tempString, type, op);
2689}
2690
Jamie Madillb98c3a82015-07-23 14:26:04 -04002691// This function is used to test for the correctness of the parameters passed to various constructor
2692// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002693//
Olli Etuaho856c4972016-08-08 11:38:39 +03002694// 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 +00002695//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002696TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002697 TOperator op,
2698 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302699 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700{
Olli Etuaho856c4972016-08-08 11:38:39 +03002701 TType type = fnCall->getReturnType();
2702 if (type.isUnsizedArray())
2703 {
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002704 if (fnCall->getParamCount() == 0)
2705 {
2706 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2707 type.setArraySize(1u);
2708 return TIntermTyped::CreateZero(type);
2709 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002710 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2711 }
2712 bool constType = true;
2713 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2714 {
2715 const TConstParameter &param = fnCall->getParam(i);
2716 if (param.type->getQualifier() != EvqConst)
2717 constType = false;
2718 }
2719 if (constType)
2720 type.setQualifier(EvqConst);
2721
Olli Etuaho8a176262016-08-16 14:23:01 +03002722 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002723 {
2724 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2725 dummyNode->setType(type);
2726 return dummyNode;
2727 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002728 TIntermAggregate *constructor = arguments->getAsAggregate();
2729 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002730
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002731 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002732 constructor->setOp(op);
2733 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002734 ASSERT(constructor->isConstructor());
2735
2736 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002737 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002738
Olli Etuaho21203702014-11-13 16:16:21 +02002739 // Structs should not be precision qualified, the individual members may be.
2740 // Built-in types on the other hand should be precision qualified.
2741 if (op != EOpConstructStruct)
2742 {
2743 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002744 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002745 }
2746
Olli Etuaho856c4972016-08-08 11:38:39 +03002747 constructor->setType(type);
2748
Olli Etuaho77ba4082016-12-16 12:01:18 +00002749 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002750 if (constConstructor)
2751 {
2752 return constConstructor;
2753 }
2754
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002755 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002756}
2757
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002758//
2759// Interface/uniform blocks
2760//
Olli Etuaho13389b62016-10-16 11:48:18 +01002761TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002762 const TTypeQualifierBuilder &typeQualifierBuilder,
2763 const TSourceLoc &nameLine,
2764 const TString &blockName,
2765 TFieldList *fieldList,
2766 const TString *instanceName,
2767 const TSourceLoc &instanceLine,
2768 TIntermTyped *arrayIndex,
2769 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002770{
Olli Etuaho856c4972016-08-08 11:38:39 +03002771 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002772
Olli Etuaho77ba4082016-12-16 12:01:18 +00002773 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002774
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002775 if (typeQualifier.qualifier != EvqUniform)
2776 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002777 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
2778 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002779 }
2780
Martin Radev70866b82016-07-22 15:27:42 +03002781 if (typeQualifier.invariant)
2782 {
2783 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2784 }
2785
Martin Radev2cc85b32016-08-05 16:22:53 +03002786 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2787
Jamie Madill099c0f32013-06-20 11:55:52 -04002788 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002789 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002790
Jamie Madill099c0f32013-06-20 11:55:52 -04002791 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2792 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002793 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002794 }
2795
Jamie Madill1566ef72013-06-20 11:55:54 -04002796 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2797 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002798 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002799 }
2800
Olli Etuaho856c4972016-08-08 11:38:39 +03002801 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002802
Martin Radev2cc85b32016-08-05 16:22:53 +03002803 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2804
Arun Patole7e7e68d2015-05-22 12:02:25 +05302805 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2806 if (!symbolTable.declare(blockNameSymbol))
2807 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002808 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002809 }
2810
Jamie Madill98493dd2013-07-08 14:39:03 -04002811 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302812 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2813 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002814 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302815 TType *fieldType = field->type();
2816 if (IsSampler(fieldType->getBasicType()))
2817 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002818 error(field->line(),
2819 "unsupported type - sampler types are not allowed in interface blocks",
2820 fieldType->getBasicString());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002821 }
2822
Martin Radev2cc85b32016-08-05 16:22:53 +03002823 if (IsImage(fieldType->getBasicType()))
2824 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002825 error(field->line(),
2826 "unsupported type - image types are not allowed in interface blocks",
2827 fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002828 }
2829
Jamie Madill98493dd2013-07-08 14:39:03 -04002830 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002831 switch (qualifier)
2832 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002833 case EvqGlobal:
2834 case EvqUniform:
2835 break;
2836 default:
2837 error(field->line(), "invalid qualifier on interface block member",
2838 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002839 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002840 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002841
Martin Radev70866b82016-07-22 15:27:42 +03002842 if (fieldType->isInvariant())
2843 {
2844 error(field->line(), "invalid qualifier on interface block member", "invariant");
2845 }
2846
Jamie Madilla5efff92013-06-06 11:56:47 -04002847 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002848 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002849 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002850
Jamie Madill98493dd2013-07-08 14:39:03 -04002851 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002852 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002853 error(field->line(), "invalid layout qualifier: cannot be used here",
2854 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04002855 }
2856
Jamie Madill98493dd2013-07-08 14:39:03 -04002857 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002858 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002859 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002860 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002861 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002862 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002863 warning(field->line(),
2864 "extraneous layout qualifier: only has an effect on matrix types",
2865 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04002866 }
2867
Jamie Madill98493dd2013-07-08 14:39:03 -04002868 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002869 }
2870
Jamie Madill98493dd2013-07-08 14:39:03 -04002871 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002872 unsigned int arraySize = 0;
2873 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002874 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002875 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002876 }
2877
Jamie Madillb98c3a82015-07-23 14:26:04 -04002878 TInterfaceBlock *interfaceBlock =
2879 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2880 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2881 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002882
2883 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002884 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002885
Jamie Madill98493dd2013-07-08 14:39:03 -04002886 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002887 {
2888 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002889 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2890 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002891 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302892 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002893
2894 // set parent pointer of the field variable
2895 fieldType->setInterfaceBlock(interfaceBlock);
2896
Arun Patole7e7e68d2015-05-22 12:02:25 +05302897 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002898 fieldVariable->setQualifier(typeQualifier.qualifier);
2899
Arun Patole7e7e68d2015-05-22 12:02:25 +05302900 if (!symbolTable.declare(fieldVariable))
2901 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002902 error(field->line(), "redefinition of an interface block member name",
2903 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002904 }
2905 }
2906 }
2907 else
2908 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002909 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002910
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002911 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302912 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002913 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002914
Arun Patole7e7e68d2015-05-22 12:02:25 +05302915 if (!symbolTable.declare(instanceTypeDef))
2916 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002917 error(instanceLine, "redefinition of an interface block instance name",
2918 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002919 }
2920
Jamie Madillb98c3a82015-07-23 14:26:04 -04002921 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002922 symbolName = instanceTypeDef->getName();
2923 }
2924
Olli Etuaho13389b62016-10-16 11:48:18 +01002925 TIntermSymbol *blockSymbol =
2926 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2927 TIntermDeclaration *declaration = new TIntermDeclaration();
2928 declaration->appendDeclarator(blockSymbol);
2929 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002930
2931 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002932 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002933}
2934
Olli Etuaho383b7912016-08-05 11:22:59 +03002935void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002936{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002937 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002938
2939 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00002940 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302941 if (mStructNestingLevel > 1)
2942 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002943 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002944 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002945}
2946
2947void TParseContext::exitStructDeclaration()
2948{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002949 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002950}
2951
Olli Etuaho8a176262016-08-16 14:23:01 +03002952void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002953{
Jamie Madillacb4b812016-11-07 13:50:29 -05002954 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302955 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002956 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002957 }
2958
Arun Patole7e7e68d2015-05-22 12:02:25 +05302959 if (field.type()->getBasicType() != EbtStruct)
2960 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002961 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002962 }
2963
2964 // We're already inside a structure definition at this point, so add
2965 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302966 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2967 {
Jamie Madill41a49272014-03-18 16:10:13 -04002968 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002969 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2970 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002971 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00002972 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03002973 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002974 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002975}
2976
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002977//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002978// Parse an array index expression
2979//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002980TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2981 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302982 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002983{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002984 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2985 {
2986 if (baseExpression->getAsSymbolNode())
2987 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302988 error(location, " left of '[' is not of type array, matrix, or vector ",
2989 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002990 }
2991 else
2992 {
2993 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2994 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002995
2996 TConstantUnion *unionArray = new TConstantUnion[1];
2997 unionArray->setFConst(0.0f);
2998 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2999 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003000 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003001
Jamie Madill21c1e452014-12-29 11:33:41 -05003002 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3003
Olli Etuaho36b05142015-11-12 13:10:42 +02003004 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3005 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3006 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3007 // index is a constant expression.
3008 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3009 {
3010 if (baseExpression->isInterfaceBlock())
3011 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003012 error(location,
3013 "array indexes for interface blocks arrays must be constant integral expressions",
3014 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003015 }
3016 else if (baseExpression->getQualifier() == EvqFragmentOut)
3017 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003018 error(location,
3019 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003020 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003021 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3022 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003023 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003024 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003025 }
3026
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003027 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003028 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003029 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3030 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3031 // constant fold expressions that are not constant expressions). The most compatible way to
3032 // handle this case is to report a warning instead of an error and force the index to be in
3033 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003034 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003035 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003036
3037 int safeIndex = -1;
3038
3039 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003040 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003041 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003042 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003043 if (mShaderSpec == SH_WEBGL2_SPEC)
3044 {
3045 // Error has been already generated if index is not const.
3046 if (indexExpression->getQualifier() == EvqConst)
3047 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003048 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003049 }
3050 safeIndex = 0;
3051 }
3052 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3053 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003054 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003055 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003056 "GL_EXT_draw_buffers is disabled",
3057 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003058 safeIndex = 0;
3059 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003060 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003061 // Only do generic out-of-range check if similar error hasn't already been reported.
3062 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003063 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003064 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3065 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003066 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003067 }
3068 }
3069 else if (baseExpression->isMatrix())
3070 {
3071 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003072 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003073 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003074 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003075 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003076 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003077 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3078 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003079 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003080 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003081
3082 ASSERT(safeIndex >= 0);
3083 // Data of constant unions can't be changed, because it may be shared with other
3084 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3085 // sanitized object.
3086 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003087 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003088 TConstantUnion *safeConstantUnion = new TConstantUnion();
3089 safeConstantUnion->setIConst(safeIndex);
3090 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003091 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003092
3093 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003094 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003095 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003096 else
3097 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003098 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003099 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003100 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003101}
3102
Olli Etuaho90892fb2016-07-14 14:44:51 +03003103int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3104 const TSourceLoc &location,
3105 int index,
3106 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003107 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003108{
3109 if (index >= arraySize || index < 0)
3110 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003111 std::stringstream reasonStream;
3112 reasonStream << reason << " '" << index << "'";
3113 std::string token = reasonStream.str();
3114 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003115 if (index < 0)
3116 {
3117 return 0;
3118 }
3119 else
3120 {
3121 return arraySize - 1;
3122 }
3123 }
3124 return index;
3125}
3126
Jamie Madillb98c3a82015-07-23 14:26:04 -04003127TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3128 const TSourceLoc &dotLocation,
3129 const TString &fieldString,
3130 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003131{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003132 if (baseExpression->isArray())
3133 {
3134 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003135 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003136 }
3137
3138 if (baseExpression->isVector())
3139 {
3140 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003141 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3142 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003143 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003144 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003145 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003146 }
3147
Olli Etuahob6fa0432016-09-28 16:28:05 +01003148 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003149 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003150 else if (baseExpression->getBasicType() == EbtStruct)
3151 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303152 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003153 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003154 {
3155 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003156 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003157 }
3158 else
3159 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003160 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003161 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003162 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003163 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003164 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003165 {
3166 fieldFound = true;
3167 break;
3168 }
3169 }
3170 if (fieldFound)
3171 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003172 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3173 index->setLine(fieldLocation);
3174 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003175 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003176 }
3177 else
3178 {
3179 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003180 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003181 }
3182 }
3183 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003184 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003185 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303186 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003187 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003188 {
3189 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003190 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003191 }
3192 else
3193 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003194 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003195 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003196 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003197 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003198 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003199 {
3200 fieldFound = true;
3201 break;
3202 }
3203 }
3204 if (fieldFound)
3205 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003206 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3207 index->setLine(fieldLocation);
3208 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003209 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003210 }
3211 else
3212 {
3213 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003214 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003215 }
3216 }
3217 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003218 else
3219 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003220 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003221 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003222 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303223 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003224 }
3225 else
3226 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303227 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003228 " field selection requires structure, vector, or interface block on left hand "
3229 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303230 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003231 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003232 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003233 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003234}
3235
Jamie Madillb98c3a82015-07-23 14:26:04 -04003236TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3237 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003238{
Martin Radev802abe02016-08-04 17:48:32 +03003239 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003240
3241 if (qualifierType == "shared")
3242 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003243 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003244 {
3245 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3246 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003247 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003248 }
3249 else if (qualifierType == "packed")
3250 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003251 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003252 {
3253 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3254 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003255 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003256 }
3257 else if (qualifierType == "std140")
3258 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003259 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003260 }
3261 else if (qualifierType == "row_major")
3262 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003263 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003264 }
3265 else if (qualifierType == "column_major")
3266 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003267 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003268 }
3269 else if (qualifierType == "location")
3270 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003271 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3272 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003273 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003274 else if (qualifierType == "rgba32f")
3275 {
3276 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3277 qualifier.imageInternalFormat = EiifRGBA32F;
3278 }
3279 else if (qualifierType == "rgba16f")
3280 {
3281 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3282 qualifier.imageInternalFormat = EiifRGBA16F;
3283 }
3284 else if (qualifierType == "r32f")
3285 {
3286 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3287 qualifier.imageInternalFormat = EiifR32F;
3288 }
3289 else if (qualifierType == "rgba8")
3290 {
3291 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3292 qualifier.imageInternalFormat = EiifRGBA8;
3293 }
3294 else if (qualifierType == "rgba8_snorm")
3295 {
3296 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3297 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3298 }
3299 else if (qualifierType == "rgba32i")
3300 {
3301 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3302 qualifier.imageInternalFormat = EiifRGBA32I;
3303 }
3304 else if (qualifierType == "rgba16i")
3305 {
3306 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3307 qualifier.imageInternalFormat = EiifRGBA16I;
3308 }
3309 else if (qualifierType == "rgba8i")
3310 {
3311 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3312 qualifier.imageInternalFormat = EiifRGBA8I;
3313 }
3314 else if (qualifierType == "r32i")
3315 {
3316 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3317 qualifier.imageInternalFormat = EiifR32I;
3318 }
3319 else if (qualifierType == "rgba32ui")
3320 {
3321 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3322 qualifier.imageInternalFormat = EiifRGBA32UI;
3323 }
3324 else if (qualifierType == "rgba16ui")
3325 {
3326 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3327 qualifier.imageInternalFormat = EiifRGBA16UI;
3328 }
3329 else if (qualifierType == "rgba8ui")
3330 {
3331 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3332 qualifier.imageInternalFormat = EiifRGBA8UI;
3333 }
3334 else if (qualifierType == "r32ui")
3335 {
3336 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3337 qualifier.imageInternalFormat = EiifR32UI;
3338 }
3339
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003340 else
3341 {
3342 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003343 }
3344
Jamie Madilla5efff92013-06-06 11:56:47 -04003345 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003346}
3347
Martin Radev802abe02016-08-04 17:48:32 +03003348void TParseContext::parseLocalSize(const TString &qualifierType,
3349 const TSourceLoc &qualifierTypeLine,
3350 int intValue,
3351 const TSourceLoc &intValueLine,
3352 const std::string &intValueString,
3353 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003354 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003355{
Olli Etuaho856c4972016-08-08 11:38:39 +03003356 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003357 if (intValue < 1)
3358 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003359 std::stringstream reasonStream;
3360 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3361 std::string reason = reasonStream.str();
3362 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003363 }
3364 (*localSize)[index] = intValue;
3365}
3366
Olli Etuaho09b04a22016-12-15 13:30:26 +00003367void TParseContext::parseNumViews(int intValue,
3368 const TSourceLoc &intValueLine,
3369 const std::string &intValueString,
3370 int *numViews)
3371{
3372 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3373 // specification.
3374 if (intValue < 1)
3375 {
3376 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3377 }
3378 *numViews = intValue;
3379}
3380
Jamie Madillb98c3a82015-07-23 14:26:04 -04003381TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3382 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003383 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303384 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003385{
Martin Radev802abe02016-08-04 17:48:32 +03003386 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003387
Martin Radev802abe02016-08-04 17:48:32 +03003388 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003389
Martin Radev802abe02016-08-04 17:48:32 +03003390 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003391 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003392 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003393 if (intValue < 0)
3394 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003395 error(intValueLine, "out of range: location must be non-negative",
3396 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003397 }
3398 else
3399 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003400 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003401 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003402 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003403 }
Martin Radev802abe02016-08-04 17:48:32 +03003404 else if (qualifierType == "local_size_x")
3405 {
3406 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3407 &qualifier.localSize);
3408 }
3409 else if (qualifierType == "local_size_y")
3410 {
3411 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3412 &qualifier.localSize);
3413 }
3414 else if (qualifierType == "local_size_z")
3415 {
3416 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3417 &qualifier.localSize);
3418 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00003419 else if (qualifierType == "num_views" && mMultiviewAvailable &&
3420 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
3421 mShaderType == GL_VERTEX_SHADER)
3422 {
3423 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3424 }
Martin Radev802abe02016-08-04 17:48:32 +03003425 else
3426 {
3427 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003428 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003429
Jamie Madilla5efff92013-06-06 11:56:47 -04003430 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003431}
3432
Olli Etuaho613b9592016-09-05 12:05:53 +03003433TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3434{
3435 return new TTypeQualifierBuilder(
3436 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3437 mShaderVersion);
3438}
3439
Jamie Madillb98c3a82015-07-23 14:26:04 -04003440TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003441 TLayoutQualifier rightQualifier,
3442 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003443{
Martin Radevc28888b2016-07-22 15:27:42 +03003444 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003445 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003446}
3447
Olli Etuaho4de340a2016-12-16 09:32:03 +00003448TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3449 const TFieldList *newlyAddedFields,
3450 const TSourceLoc &location)
3451{
3452 for (TField *field : *newlyAddedFields)
3453 {
3454 for (TField *oldField : *processedFields)
3455 {
3456 if (oldField->name() == field->name())
3457 {
3458 error(location, "duplicate field name in structure", field->name().c_str());
3459 }
3460 }
3461 processedFields->push_back(field);
3462 }
3463 return processedFields;
3464}
3465
Martin Radev70866b82016-07-22 15:27:42 +03003466TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3467 const TTypeQualifierBuilder &typeQualifierBuilder,
3468 TPublicType *typeSpecifier,
3469 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003470{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003471 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003472
Martin Radev70866b82016-07-22 15:27:42 +03003473 typeSpecifier->qualifier = typeQualifier.qualifier;
3474 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003475 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003476 typeSpecifier->invariant = typeQualifier.invariant;
3477 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303478 {
Martin Radev70866b82016-07-22 15:27:42 +03003479 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003480 }
Martin Radev70866b82016-07-22 15:27:42 +03003481 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003482}
3483
Jamie Madillb98c3a82015-07-23 14:26:04 -04003484TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3485 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003486{
Martin Radev4a9cd802016-09-01 16:51:51 +03003487 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3488 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003489
Martin Radev4a9cd802016-09-01 16:51:51 +03003490 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003491
Martin Radev4a9cd802016-09-01 16:51:51 +03003492 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003493
Arun Patole7e7e68d2015-05-22 12:02:25 +05303494 for (unsigned int i = 0; i < fieldList->size(); ++i)
3495 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003496 //
3497 // Careful not to replace already known aspects of type, like array-ness
3498 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303499 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003500 type->setBasicType(typeSpecifier.getBasicType());
3501 type->setPrimarySize(typeSpecifier.getPrimarySize());
3502 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003503 type->setPrecision(typeSpecifier.precision);
3504 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003505 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003506 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003507 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003508
3509 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303510 if (type->isArray())
3511 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003512 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003513 }
3514 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003515 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003516 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303517 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003518 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003519 }
3520
Martin Radev4a9cd802016-09-01 16:51:51 +03003521 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003522 }
3523
Jamie Madill98493dd2013-07-08 14:39:03 -04003524 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003525}
3526
Martin Radev4a9cd802016-09-01 16:51:51 +03003527TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3528 const TSourceLoc &nameLine,
3529 const TString *structName,
3530 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003531{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303532 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003533 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003534
Jamie Madill9b820842015-02-12 10:40:10 -05003535 // Store a bool in the struct if we're at global scope, to allow us to
3536 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003537 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003538
Jamie Madill98493dd2013-07-08 14:39:03 -04003539 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003540 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003541 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303542 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3543 if (!symbolTable.declare(userTypeDef))
3544 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003545 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003546 }
3547 }
3548
3549 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003550 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003551 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003552 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003553 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003554 switch (qualifier)
3555 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003556 case EvqGlobal:
3557 case EvqTemporary:
3558 break;
3559 default:
3560 error(field.line(), "invalid qualifier on struct member",
3561 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003562 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003563 }
Martin Radev70866b82016-07-22 15:27:42 +03003564 if (field.type()->isInvariant())
3565 {
3566 error(field.line(), "invalid qualifier on struct member", "invariant");
3567 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003568 if (IsImage(field.type()->getBasicType()))
3569 {
3570 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3571 }
3572
3573 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003574
3575 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003576 }
3577
Martin Radev4a9cd802016-09-01 16:51:51 +03003578 TTypeSpecifierNonArray typeSpecifierNonArray;
3579 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3580 typeSpecifierNonArray.userDef = structureType;
3581 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003582 exitStructDeclaration();
3583
Martin Radev4a9cd802016-09-01 16:51:51 +03003584 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003585}
3586
Jamie Madillb98c3a82015-07-23 14:26:04 -04003587TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003588 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003589 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003590{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003591 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003592 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003593 init->isVector())
3594 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003595 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3596 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003597 return nullptr;
3598 }
3599
Olli Etuahoac5274d2015-02-20 10:19:08 +02003600 if (statementList)
3601 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00003602 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02003603 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003604 return nullptr;
3605 }
3606 }
3607
Olli Etuahoa3a36662015-02-17 13:46:51 +02003608 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3609 if (node == nullptr)
3610 {
3611 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003612 return nullptr;
3613 }
3614 return node;
3615}
3616
3617TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3618{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003619 if (mSwitchNestingLevel == 0)
3620 {
3621 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003622 return nullptr;
3623 }
3624 if (condition == nullptr)
3625 {
3626 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003627 return nullptr;
3628 }
3629 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003630 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003631 {
3632 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003633 }
3634 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003635 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3636 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3637 // fold in case labels.
3638 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003639 {
3640 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003641 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003642 TIntermCase *node = intermediate.addCase(condition, loc);
3643 if (node == nullptr)
3644 {
3645 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003646 return nullptr;
3647 }
3648 return node;
3649}
3650
3651TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3652{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003653 if (mSwitchNestingLevel == 0)
3654 {
3655 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003656 return nullptr;
3657 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003658 TIntermCase *node = intermediate.addCase(nullptr, loc);
3659 if (node == nullptr)
3660 {
3661 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003662 return nullptr;
3663 }
3664 return node;
3665}
3666
Jamie Madillb98c3a82015-07-23 14:26:04 -04003667TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3668 TIntermTyped *child,
3669 const TSourceLoc &loc,
3670 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003671{
3672 if (child == nullptr)
3673 {
3674 return nullptr;
3675 }
3676
3677 switch (op)
3678 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003679 case EOpLogicalNot:
3680 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3681 child->isVector())
3682 {
3683 return nullptr;
3684 }
3685 break;
3686 case EOpBitwiseNot:
3687 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3688 child->isMatrix() || child->isArray())
3689 {
3690 return nullptr;
3691 }
3692 break;
3693 case EOpPostIncrement:
3694 case EOpPreIncrement:
3695 case EOpPostDecrement:
3696 case EOpPreDecrement:
3697 case EOpNegative:
3698 case EOpPositive:
3699 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003700 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003701 {
3702 return nullptr;
3703 }
3704 // Operators for built-ins are already type checked against their prototype.
3705 default:
3706 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003707 }
3708
Olli Etuahof119a262016-08-19 15:54:22 +03003709 TIntermUnary *node = new TIntermUnary(op, child);
3710 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003711
Olli Etuaho77ba4082016-12-16 12:01:18 +00003712 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03003713 if (foldedNode)
3714 return foldedNode;
3715
3716 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003717}
3718
Olli Etuaho09b22472015-02-11 11:47:26 +02003719TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3720{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003721 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003722 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003723 {
3724 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003725 return child;
3726 }
3727 return node;
3728}
3729
Jamie Madillb98c3a82015-07-23 14:26:04 -04003730TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3731 TIntermTyped *child,
3732 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003733{
Olli Etuaho856c4972016-08-08 11:38:39 +03003734 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003735 return addUnaryMath(op, child, loc);
3736}
3737
Jamie Madillb98c3a82015-07-23 14:26:04 -04003738bool TParseContext::binaryOpCommonCheck(TOperator op,
3739 TIntermTyped *left,
3740 TIntermTyped *right,
3741 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003742{
Olli Etuaho244be012016-08-18 15:26:02 +03003743 if (left->getType().getStruct() || right->getType().getStruct())
3744 {
3745 switch (op)
3746 {
3747 case EOpIndexDirectStruct:
3748 ASSERT(left->getType().getStruct());
3749 break;
3750 case EOpEqual:
3751 case EOpNotEqual:
3752 case EOpAssign:
3753 case EOpInitialize:
3754 if (left->getType() != right->getType())
3755 {
3756 return false;
3757 }
3758 break;
3759 default:
3760 error(loc, "Invalid operation for structs", GetOperatorString(op));
3761 return false;
3762 }
3763 }
3764
Olli Etuahod6b14282015-03-17 14:31:35 +02003765 if (left->isArray() || right->isArray())
3766 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003767 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003768 {
3769 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3770 return false;
3771 }
3772
3773 if (left->isArray() != right->isArray())
3774 {
3775 error(loc, "array / non-array mismatch", GetOperatorString(op));
3776 return false;
3777 }
3778
3779 switch (op)
3780 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003781 case EOpEqual:
3782 case EOpNotEqual:
3783 case EOpAssign:
3784 case EOpInitialize:
3785 break;
3786 default:
3787 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3788 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003789 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003790 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003791 if (left->getArraySize() != right->getArraySize())
3792 {
3793 error(loc, "array size mismatch", GetOperatorString(op));
3794 return false;
3795 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003796 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003797
3798 // Check ops which require integer / ivec parameters
3799 bool isBitShift = false;
3800 switch (op)
3801 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003802 case EOpBitShiftLeft:
3803 case EOpBitShiftRight:
3804 case EOpBitShiftLeftAssign:
3805 case EOpBitShiftRightAssign:
3806 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3807 // check that the basic type is an integer type.
3808 isBitShift = true;
3809 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3810 {
3811 return false;
3812 }
3813 break;
3814 case EOpBitwiseAnd:
3815 case EOpBitwiseXor:
3816 case EOpBitwiseOr:
3817 case EOpBitwiseAndAssign:
3818 case EOpBitwiseXorAssign:
3819 case EOpBitwiseOrAssign:
3820 // It is enough to check the type of only one operand, since later it
3821 // is checked that the operand types match.
3822 if (!IsInteger(left->getBasicType()))
3823 {
3824 return false;
3825 }
3826 break;
3827 default:
3828 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003829 }
3830
3831 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3832 // So the basic type should usually match.
3833 if (!isBitShift && left->getBasicType() != right->getBasicType())
3834 {
3835 return false;
3836 }
3837
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003838 // Check that:
3839 // 1. Type sizes match exactly on ops that require that.
3840 // 2. Restrictions for structs that contain arrays or samplers are respected.
3841 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003842 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003843 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003844 case EOpAssign:
3845 case EOpInitialize:
3846 case EOpEqual:
3847 case EOpNotEqual:
3848 // ESSL 1.00 sections 5.7, 5.8, 5.9
3849 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3850 {
3851 error(loc, "undefined operation for structs containing arrays",
3852 GetOperatorString(op));
3853 return false;
3854 }
3855 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3856 // we interpret the spec so that this extends to structs containing samplers,
3857 // similarly to ESSL 1.00 spec.
3858 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3859 left->getType().isStructureContainingSamplers())
3860 {
3861 error(loc, "undefined operation for structs containing samplers",
3862 GetOperatorString(op));
3863 return false;
3864 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003865
3866 if ((op == EOpAssign || op == EOpInitialize) &&
3867 left->getType().isStructureContainingImages())
3868 {
3869 error(loc, "undefined operation for structs containing images",
3870 GetOperatorString(op));
3871 return false;
3872 }
Olli Etuahoe1805592017-01-02 16:41:20 +00003873 if ((left->getNominalSize() != right->getNominalSize()) ||
3874 (left->getSecondarySize() != right->getSecondarySize()))
3875 {
3876 error(loc, "dimension mismatch", GetOperatorString(op));
3877 return false;
3878 }
3879 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003880 case EOpLessThan:
3881 case EOpGreaterThan:
3882 case EOpLessThanEqual:
3883 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00003884 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003885 {
Olli Etuahoe1805592017-01-02 16:41:20 +00003886 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003887 return false;
3888 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003889 break;
3890 case EOpAdd:
3891 case EOpSub:
3892 case EOpDiv:
3893 case EOpIMod:
3894 case EOpBitShiftLeft:
3895 case EOpBitShiftRight:
3896 case EOpBitwiseAnd:
3897 case EOpBitwiseXor:
3898 case EOpBitwiseOr:
3899 case EOpAddAssign:
3900 case EOpSubAssign:
3901 case EOpDivAssign:
3902 case EOpIModAssign:
3903 case EOpBitShiftLeftAssign:
3904 case EOpBitShiftRightAssign:
3905 case EOpBitwiseAndAssign:
3906 case EOpBitwiseXorAssign:
3907 case EOpBitwiseOrAssign:
3908 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3909 {
3910 return false;
3911 }
3912
3913 // Are the sizes compatible?
3914 if (left->getNominalSize() != right->getNominalSize() ||
3915 left->getSecondarySize() != right->getSecondarySize())
3916 {
3917 // If the nominal sizes of operands do not match:
3918 // One of them must be a scalar.
3919 if (!left->isScalar() && !right->isScalar())
3920 return false;
3921
3922 // In the case of compound assignment other than multiply-assign,
3923 // the right side needs to be a scalar. Otherwise a vector/matrix
3924 // would be assigned to a scalar. A scalar can't be shifted by a
3925 // vector either.
3926 if (!right->isScalar() &&
3927 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3928 return false;
3929 }
3930 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003931 default:
3932 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003933 }
3934
Olli Etuahod6b14282015-03-17 14:31:35 +02003935 return true;
3936}
3937
Olli Etuaho1dded802016-08-18 18:13:13 +03003938bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3939 const TType &left,
3940 const TType &right)
3941{
3942 switch (op)
3943 {
3944 case EOpMul:
3945 case EOpMulAssign:
3946 return left.getNominalSize() == right.getNominalSize() &&
3947 left.getSecondarySize() == right.getSecondarySize();
3948 case EOpVectorTimesScalar:
3949 return true;
3950 case EOpVectorTimesScalarAssign:
3951 ASSERT(!left.isMatrix() && !right.isMatrix());
3952 return left.isVector() && !right.isVector();
3953 case EOpVectorTimesMatrix:
3954 return left.getNominalSize() == right.getRows();
3955 case EOpVectorTimesMatrixAssign:
3956 ASSERT(!left.isMatrix() && right.isMatrix());
3957 return left.isVector() && left.getNominalSize() == right.getRows() &&
3958 left.getNominalSize() == right.getCols();
3959 case EOpMatrixTimesVector:
3960 return left.getCols() == right.getNominalSize();
3961 case EOpMatrixTimesScalar:
3962 return true;
3963 case EOpMatrixTimesScalarAssign:
3964 ASSERT(left.isMatrix() && !right.isMatrix());
3965 return !right.isVector();
3966 case EOpMatrixTimesMatrix:
3967 return left.getCols() == right.getRows();
3968 case EOpMatrixTimesMatrixAssign:
3969 ASSERT(left.isMatrix() && right.isMatrix());
3970 // We need to check two things:
3971 // 1. The matrix multiplication step is valid.
3972 // 2. The result will have the same number of columns as the lvalue.
3973 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3974
3975 default:
3976 UNREACHABLE();
3977 return false;
3978 }
3979}
3980
Jamie Madillb98c3a82015-07-23 14:26:04 -04003981TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3982 TIntermTyped *left,
3983 TIntermTyped *right,
3984 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003985{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003986 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003987 return nullptr;
3988
Olli Etuahofc1806e2015-03-17 13:03:11 +02003989 switch (op)
3990 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003991 case EOpEqual:
3992 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04003993 case EOpLessThan:
3994 case EOpGreaterThan:
3995 case EOpLessThanEqual:
3996 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04003997 break;
3998 case EOpLogicalOr:
3999 case EOpLogicalXor:
4000 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004001 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4002 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004003 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004004 {
4005 return nullptr;
4006 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004007 // Basic types matching should have been already checked.
4008 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004009 break;
4010 case EOpAdd:
4011 case EOpSub:
4012 case EOpDiv:
4013 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004014 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4015 !right->getType().getStruct());
4016 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004017 {
4018 return nullptr;
4019 }
4020 break;
4021 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004022 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4023 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004024 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004025 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004026 {
4027 return nullptr;
4028 }
4029 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004030 default:
4031 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004032 }
4033
Olli Etuaho1dded802016-08-18 18:13:13 +03004034 if (op == EOpMul)
4035 {
4036 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4037 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4038 {
4039 return nullptr;
4040 }
4041 }
4042
Olli Etuaho3fdec912016-08-18 15:08:06 +03004043 TIntermBinary *node = new TIntermBinary(op, left, right);
4044 node->setLine(loc);
4045
Olli Etuaho3fdec912016-08-18 15:08:06 +03004046 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004047 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004048 if (foldedNode)
4049 return foldedNode;
4050
4051 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004052}
4053
Jamie Madillb98c3a82015-07-23 14:26:04 -04004054TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4055 TIntermTyped *left,
4056 TIntermTyped *right,
4057 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004058{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004059 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004060 if (node == 0)
4061 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004062 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4063 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004064 return left;
4065 }
4066 return node;
4067}
4068
Jamie Madillb98c3a82015-07-23 14:26:04 -04004069TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4070 TIntermTyped *left,
4071 TIntermTyped *right,
4072 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004073{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004074 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004075 if (node == 0)
4076 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004077 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4078 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004079 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004080 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004081 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4082 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004083 }
4084 return node;
4085}
4086
Olli Etuaho13389b62016-10-16 11:48:18 +01004087TIntermBinary *TParseContext::createAssign(TOperator op,
4088 TIntermTyped *left,
4089 TIntermTyped *right,
4090 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004091{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004092 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004093 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004094 if (op == EOpMulAssign)
4095 {
4096 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4097 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4098 {
4099 return nullptr;
4100 }
4101 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004102 TIntermBinary *node = new TIntermBinary(op, left, right);
4103 node->setLine(loc);
4104
Olli Etuaho3fdec912016-08-18 15:08:06 +03004105 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004106 }
4107 return nullptr;
4108}
4109
Jamie Madillb98c3a82015-07-23 14:26:04 -04004110TIntermTyped *TParseContext::addAssign(TOperator op,
4111 TIntermTyped *left,
4112 TIntermTyped *right,
4113 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004114{
4115 TIntermTyped *node = createAssign(op, left, right, loc);
4116 if (node == nullptr)
4117 {
4118 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004119 return left;
4120 }
4121 return node;
4122}
4123
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004124TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4125 TIntermTyped *right,
4126 const TSourceLoc &loc)
4127{
Corentin Wallez0d959252016-07-12 17:26:32 -04004128 // WebGL2 section 5.26, the following results in an error:
4129 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004130 if (mShaderSpec == SH_WEBGL2_SPEC &&
4131 (left->isArray() || left->getBasicType() == EbtVoid ||
4132 left->getType().isStructureContainingArrays() || right->isArray() ||
4133 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004134 {
4135 error(loc,
4136 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4137 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004138 }
4139
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004140 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004141}
4142
Olli Etuaho49300862015-02-20 14:54:49 +02004143TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4144{
4145 switch (op)
4146 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004147 case EOpContinue:
4148 if (mLoopNestingLevel <= 0)
4149 {
4150 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004151 }
4152 break;
4153 case EOpBreak:
4154 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4155 {
4156 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004157 }
4158 break;
4159 case EOpReturn:
4160 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4161 {
4162 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004163 }
4164 break;
4165 default:
4166 // No checks for discard
4167 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004168 }
4169 return intermediate.addBranch(op, loc);
4170}
4171
Jamie Madillb98c3a82015-07-23 14:26:04 -04004172TIntermBranch *TParseContext::addBranch(TOperator op,
4173 TIntermTyped *returnValue,
4174 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004175{
4176 ASSERT(op == EOpReturn);
4177 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004178 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004179 {
4180 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004181 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004182 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004183 {
4184 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004185 }
4186 return intermediate.addBranch(op, returnValue, loc);
4187}
4188
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004189void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4190{
4191 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004192 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004193 TIntermNode *offset = nullptr;
4194 TIntermSequence *arguments = functionCall->getSequence();
4195 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4196 name.compare(0, 16, "textureLodOffset") == 0 ||
4197 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4198 name.compare(0, 17, "textureGradOffset") == 0 ||
4199 name.compare(0, 21, "textureProjGradOffset") == 0)
4200 {
4201 offset = arguments->back();
4202 }
4203 else if (name.compare(0, 13, "textureOffset") == 0 ||
4204 name.compare(0, 17, "textureProjOffset") == 0)
4205 {
4206 // A bias parameter might follow the offset parameter.
4207 ASSERT(arguments->size() >= 3);
4208 offset = (*arguments)[2];
4209 }
4210 if (offset != nullptr)
4211 {
4212 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4213 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4214 {
4215 TString unmangledName = TFunction::unmangleName(name);
4216 error(functionCall->getLine(), "Texture offset must be a constant expression",
4217 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004218 }
4219 else
4220 {
4221 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4222 size_t size = offsetConstantUnion->getType().getObjectSize();
4223 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4224 for (size_t i = 0u; i < size; ++i)
4225 {
4226 int offsetValue = values[i].getIConst();
4227 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4228 {
4229 std::stringstream tokenStream;
4230 tokenStream << offsetValue;
4231 std::string token = tokenStream.str();
4232 error(offset->getLine(), "Texture offset value out of valid range",
4233 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004234 }
4235 }
4236 }
4237 }
4238}
4239
Martin Radev2cc85b32016-08-05 16:22:53 +03004240// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4241void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4242{
4243 ASSERT(!functionCall->isUserDefined());
4244 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4245
4246 if (name.compare(0, 5, "image") == 0)
4247 {
4248 TIntermSequence *arguments = functionCall->getSequence();
4249 TIntermNode *imageNode = (*arguments)[0];
4250 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4251
4252 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4253
4254 if (name.compare(5, 5, "Store") == 0)
4255 {
4256 if (memoryQualifier.readonly)
4257 {
4258 error(imageNode->getLine(),
4259 "'imageStore' cannot be used with images qualified as 'readonly'",
4260 imageSymbol->getSymbol().c_str());
4261 }
4262 }
4263 else if (name.compare(5, 4, "Load") == 0)
4264 {
4265 if (memoryQualifier.writeonly)
4266 {
4267 error(imageNode->getLine(),
4268 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4269 imageSymbol->getSymbol().c_str());
4270 }
4271 }
4272 }
4273}
4274
4275// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4276void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4277 const TFunction *functionDefinition,
4278 const TIntermAggregate *functionCall)
4279{
4280 ASSERT(functionCall->isUserDefined());
4281
4282 const TIntermSequence &arguments = *functionCall->getSequence();
4283
4284 ASSERT(functionDefinition->getParamCount() == arguments.size());
4285
4286 for (size_t i = 0; i < arguments.size(); ++i)
4287 {
4288 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4289 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4290 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4291
4292 if (IsImage(functionArgumentType.getBasicType()))
4293 {
4294 const TMemoryQualifier &functionArgumentMemoryQualifier =
4295 functionArgumentType.getMemoryQualifier();
4296 const TMemoryQualifier &functionParameterMemoryQualifier =
4297 functionParameterType.getMemoryQualifier();
4298 if (functionArgumentMemoryQualifier.readonly &&
4299 !functionParameterMemoryQualifier.readonly)
4300 {
4301 error(functionCall->getLine(),
4302 "Function call discards the 'readonly' qualifier from image",
4303 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4304 }
4305
4306 if (functionArgumentMemoryQualifier.writeonly &&
4307 !functionParameterMemoryQualifier.writeonly)
4308 {
4309 error(functionCall->getLine(),
4310 "Function call discards the 'writeonly' qualifier from image",
4311 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4312 }
Martin Radev049edfa2016-11-11 14:35:37 +02004313
4314 if (functionArgumentMemoryQualifier.coherent &&
4315 !functionParameterMemoryQualifier.coherent)
4316 {
4317 error(functionCall->getLine(),
4318 "Function call discards the 'coherent' qualifier from image",
4319 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4320 }
4321
4322 if (functionArgumentMemoryQualifier.volatileQualifier &&
4323 !functionParameterMemoryQualifier.volatileQualifier)
4324 {
4325 error(functionCall->getLine(),
4326 "Function call discards the 'volatile' qualifier from image",
4327 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4328 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004329 }
4330 }
4331}
4332
Jamie Madillb98c3a82015-07-23 14:26:04 -04004333TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4334 TIntermNode *paramNode,
4335 TIntermNode *thisNode,
4336 const TSourceLoc &loc,
4337 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004338{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004339 *fatalError = false;
4340 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004341 TIntermTyped *callNode = nullptr;
4342
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004343 if (thisNode != nullptr)
4344 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004345 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004346 int arraySize = 0;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004347 TIntermTyped *typedThis = thisNode->getAsTyped();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004348 if (fnCall->getName() != "length")
4349 {
4350 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004351 }
4352 else if (paramNode != nullptr)
4353 {
4354 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004355 }
4356 else if (typedThis == nullptr || !typedThis->isArray())
4357 {
4358 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004359 }
4360 else
4361 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004362 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004363 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004364 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004365 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004366 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004367 // (func()).length()
4368 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004369 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4370 // expression.
4371 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4372 // spec section 5.9 which allows "An array, vector or matrix expression with the
4373 // length method applied".
4374 error(loc, "length can only be called on array names, not on array expressions",
4375 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004376 }
4377 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004378 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004379 callNode =
4380 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004381 }
4382 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004383 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004384 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004385 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004386 }
4387 else
4388 {
4389 //
4390 // Not a constructor. Find it in the symbol table.
4391 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304392 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004393 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004394 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004395 if (fnCandidate)
4396 {
4397 //
4398 // A declared function.
4399 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004400 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004401 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004402 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004403 }
4404 op = fnCandidate->getBuiltInOp();
4405 if (builtIn && op != EOpNull)
4406 {
4407 //
4408 // A function call mapped to a built-in operation.
4409 //
4410 if (fnCandidate->getParamCount() == 1)
4411 {
4412 //
4413 // Treat it like a built-in unary operator.
4414 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004415 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4416 paramNode = paramAgg->getSequence()->front();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004417 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004418 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004419 if (callNode == nullptr)
4420 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004421 std::stringstream reasonStream;
4422 reasonStream << "wrong operand type for built in unary function: "
4423 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
4424 std::string reason = reasonStream.str();
4425 error(paramNode->getLine(), reason.c_str(), "Internal Error");
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004426 *fatalError = true;
4427 return nullptr;
4428 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004429 }
4430 else
4431 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004432 TIntermAggregate *aggregate =
4433 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004434 aggregate->setType(fnCandidate->getReturnType());
Olli Etuaho9250cb22017-01-21 10:51:27 +00004435 aggregate->setPrecisionForBuiltInOp();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004436 if (aggregate->areChildrenConstQualified())
4437 {
4438 aggregate->getTypePointer()->setQualifier(EvqConst);
4439 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004440
4441 // Some built-in functions have out parameters too.
4442 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304443
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004444 // See if we can constant fold a built-in. Note that this may be possible even
4445 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004446 TIntermTyped *foldedNode =
Olli Etuaho77ba4082016-12-16 12:01:18 +00004447 intermediate.foldAggregateBuiltIn(aggregate, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304448 if (foldedNode)
4449 {
Arun Patole274f0702015-05-05 13:33:30 +05304450 callNode = foldedNode;
4451 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004452 else
4453 {
4454 callNode = aggregate;
4455 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004456 }
4457 }
4458 else
4459 {
4460 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004461 TIntermAggregate *aggregate =
4462 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004463 aggregate->setType(fnCandidate->getReturnType());
4464
Jamie Madillb98c3a82015-07-23 14:26:04 -04004465 // this is how we know whether the given function is a builtIn function or a user
4466 // defined function
4467 // if builtIn == false, it's a userDefined -> could be an overloaded
4468 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004469 // if builtIn == true, it's definitely a builtIn function with EOpNull
4470 if (!builtIn)
4471 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004472 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004473
Olli Etuahobd674552016-10-06 13:28:42 +01004474 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004475 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004476 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004477 aggregate->setBuiltInFunctionPrecision();
4478
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004479 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004480
4481 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4482 }
4483 else
4484 {
4485 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004486 }
4487
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004488 callNode = aggregate;
4489
4490 functionCallLValueErrorCheck(fnCandidate, aggregate);
4491 }
4492 }
4493 else
4494 {
4495 // error message was put out by findFunction()
4496 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004497 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004498 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004499 callNode = intermediate.addConstantUnion(unionArray,
4500 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004501 }
4502 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004503 return callNode;
4504}
4505
Jamie Madillb98c3a82015-07-23 14:26:04 -04004506TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004507 TIntermTyped *trueExpression,
4508 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004509 const TSourceLoc &loc)
4510{
Olli Etuaho856c4972016-08-08 11:38:39 +03004511 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004512
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004513 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004514 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004515 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4516 falseExpression->getCompleteString());
4517 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004518 }
Olli Etuahode318b22016-10-25 16:18:25 +01004519 if (IsOpaqueType(trueExpression->getBasicType()))
4520 {
4521 // ESSL 1.00 section 4.1.7
4522 // ESSL 3.00 section 4.1.7
4523 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4524 // Note that structs containing opaque types don't need to be checked as structs are
4525 // forbidden below.
4526 error(loc, "ternary operator is not allowed for opaque types", ":");
4527 return falseExpression;
4528 }
4529
Olli Etuahoa2d53032015-04-15 14:14:44 +03004530 // ESSL1 sections 5.2 and 5.7:
4531 // ESSL3 section 5.7:
4532 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004533 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004534 {
4535 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004536 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004537 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004538 // WebGL2 section 5.26, the following results in an error:
4539 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004540 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004541 {
4542 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004543 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004544 }
4545
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004546 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004547}
Olli Etuaho49300862015-02-20 14:54:49 +02004548
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004549//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004550// Parse an array of strings using yyparse.
4551//
4552// Returns 0 for success.
4553//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004554int PaParseStrings(size_t count,
4555 const char *const string[],
4556 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304557 TParseContext *context)
4558{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004559 if ((count == 0) || (string == NULL))
4560 return 1;
4561
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004562 if (glslang_initialize(context))
4563 return 1;
4564
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004565 int error = glslang_scan(count, string, length, context);
4566 if (!error)
4567 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004568
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004569 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004570
alokp@chromium.org6b495712012-06-29 00:06:58 +00004571 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004572}
Jamie Madill45bcc782016-11-07 13:58:48 -05004573
4574} // namespace sh