blob: 261669267caca580e076905c54819a437e878dd2 [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 Etuaho476197f2016-10-11 13:59:08 +01002398TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002399 const TSourceLoc &location)
2400{
Olli Etuaho476197f2016-10-11 13:59:08 +01002401 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2402 // first declaration. Either way the instance in the symbol table is used to track whether the
2403 // function is declared multiple times.
2404 TFunction *function = static_cast<TFunction *>(
2405 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2406 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002407 {
2408 // ESSL 1.00.17 section 4.2.7.
2409 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2410 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002411 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002412 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002413
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002414 TIntermAggregate *prototype = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002415 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2416 // point to the data that already exists in the symbol table.
2417 prototype->setType(function->getReturnType());
Olli Etuahobd674552016-10-06 13:28:42 +01002418 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002419
Olli Etuaho476197f2016-10-11 13:59:08 +01002420 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002421 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002422 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002423 if (param.name != 0)
2424 {
2425 TVariable variable(param.name, *param.type);
2426
2427 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2428 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2429 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2430 }
2431 else
2432 {
2433 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002434 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002435 }
2436 }
2437
2438 prototype->setOp(EOpPrototype);
2439
2440 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002441
2442 if (!symbolTable.atGlobalLevel())
2443 {
2444 // ESSL 3.00.4 section 4.2.4.
2445 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002446 }
2447
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002448 return prototype;
2449}
2450
Olli Etuaho336b1472016-10-05 16:37:55 +01002451TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2452 const TFunction &function,
2453 TIntermAggregate *functionParameters,
2454 TIntermBlock *functionBody,
2455 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002456{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002457 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002458 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2459 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002460 error(location, "function does not return a value:", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002461 }
2462
Olli Etuahof51fdd22016-10-03 10:03:40 +01002463 if (functionBody == nullptr)
2464 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002465 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002466 functionBody->setLine(location);
2467 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002468 TIntermFunctionDefinition *functionNode =
2469 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2470 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002471
Olli Etuahobd674552016-10-06 13:28:42 +01002472 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002473
2474 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002475 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002476}
2477
Olli Etuaho476197f2016-10-11 13:59:08 +01002478void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2479 TFunction **function,
2480 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002481{
Olli Etuaho476197f2016-10-11 13:59:08 +01002482 ASSERT(function);
2483 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002484 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002485 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002486
2487 if (builtIn)
2488 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002489 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002490 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002491 else
Jamie Madill185fb402015-06-12 15:48:48 -04002492 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002493 TFunction *prevDec = static_cast<TFunction *>(
2494 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2495
2496 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2497 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2498 // occurance.
2499 if (*function != prevDec)
2500 {
2501 // Swap the parameters of the previous declaration to the parameters of the function
2502 // definition (parameter names may differ).
2503 prevDec->swapParameters(**function);
2504
2505 // The function definition will share the same symbol as any previous declaration.
2506 *function = prevDec;
2507 }
2508
2509 if ((*function)->isDefined())
2510 {
2511 error(location, "function already has a body", (*function)->getName().c_str());
2512 }
2513
2514 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002515 }
Jamie Madill185fb402015-06-12 15:48:48 -04002516
2517 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002518 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002519 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002520 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002521 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002522 error(location, "function cannot take any parameter(s)",
2523 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002524 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002525 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002526 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002527 error(location, "main function cannot return a value",
2528 (*function)->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002529 }
2530 }
2531
2532 //
2533 // Remember the return type for later checking for RETURN statements.
2534 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002535 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002536 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002537
2538 //
2539 // Insert parameters into the symbol table.
2540 // If the parameter has no name, it's not an error, just don't insert it
2541 // (could be used for unused args).
2542 //
2543 // Also, accumulate the list of parameters into the HIL, so lower level code
2544 // knows where to find parameters.
2545 //
2546 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002547 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002548 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002549 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002550 if (param.name != 0)
2551 {
2552 TVariable *variable = new TVariable(param.name, *param.type);
2553 //
2554 // Insert the parameters with name in the symbol table.
2555 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002556 if (!symbolTable.declare(variable))
2557 {
Jamie Madill185fb402015-06-12 15:48:48 -04002558 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002559 paramNodes = intermediate.growAggregate(
2560 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2561 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002562 }
2563
2564 //
2565 // Add the parameter to the HIL
2566 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002567 TIntermSymbol *symbol = intermediate.addSymbol(
2568 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002569
2570 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2571 }
2572 else
2573 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002574 paramNodes = intermediate.growAggregate(
2575 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002576 }
2577 }
2578 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2579 *aggregateOut = paramNodes;
2580 setLoopNestingLevel(0);
2581}
2582
Jamie Madillb98c3a82015-07-23 14:26:04 -04002583TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002584{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002585 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002586 // We don't know at this point whether this is a function definition or a prototype.
2587 // The definition production code will check for redefinitions.
2588 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002589 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002590 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2591 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002592 //
2593 TFunction *prevDec =
2594 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302595
Martin Radevda6254b2016-12-14 17:00:36 +02002596 if (getShaderVersion() >= 300 &&
2597 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2598 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302599 {
Martin Radevda6254b2016-12-14 17:00:36 +02002600 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302601 // Therefore overloading or redefining builtin functions is an error.
2602 error(location, "Name of a built-in function cannot be redeclared as function",
2603 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302604 }
2605 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002606 {
2607 if (prevDec->getReturnType() != function->getReturnType())
2608 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002609 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002610 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002611 }
2612 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2613 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002614 if (prevDec->getParam(i).type->getQualifier() !=
2615 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002616 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002617 error(location,
2618 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002619 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002620 }
2621 }
2622 }
2623
2624 //
2625 // Check for previously declared variables using the same name.
2626 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002627 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002628 if (prevSym)
2629 {
2630 if (!prevSym->isFunction())
2631 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002632 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002633 }
2634 }
2635 else
2636 {
2637 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002638 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002639 }
2640
2641 // We're at the inner scope level of the function's arguments and body statement.
2642 // Add the function prototype to the surrounding scope instead.
2643 symbolTable.getOuterLevel()->insert(function);
2644
2645 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002646 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2647 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002648 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2649 //
2650 return function;
2651}
2652
Olli Etuaho9de84a52016-06-14 17:36:01 +03002653TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2654 const TString *name,
2655 const TSourceLoc &location)
2656{
2657 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2658 {
2659 error(location, "no qualifiers allowed for function return",
2660 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002661 }
2662 if (!type.layoutQualifier.isEmpty())
2663 {
2664 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002665 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002666 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002667 checkIsNotSampler(location, type.typeSpecifierNonArray,
2668 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002669 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002670 if (mShaderVersion < 300)
2671 {
2672 // Array return values are forbidden, but there's also no valid syntax for declaring array
2673 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002674 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002675
2676 if (type.isStructureContainingArrays())
2677 {
2678 // ESSL 1.00.17 section 6.1 Function Definitions
2679 error(location, "structures containing arrays can't be function return values",
2680 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002681 }
2682 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002683
2684 // Add the function as a prototype after parsing it (we do not support recursion)
2685 return new TFunction(name, new TType(type));
2686}
2687
Jamie Madill06145232015-05-13 13:10:01 -04002688TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002689{
Jamie Madill06145232015-05-13 13:10:01 -04002690 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002691 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002692 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002693 error(publicType.getLine(), "constructor can't be a structure definition",
2694 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002695 }
2696
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002697 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002698 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002699 {
2700 op = EOpConstructStruct;
2701 }
2702 else
2703 {
Geoff Lang156d7192016-07-21 16:11:00 -04002704 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002705 if (op == EOpNull)
2706 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002707 error(publicType.getLine(), "cannot construct this type",
2708 getBasicString(publicType.getBasicType()));
2709 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002710 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002711 }
2712 }
2713
2714 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002715 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002716 return new TFunction(&tempString, type, op);
2717}
2718
Jamie Madillb98c3a82015-07-23 14:26:04 -04002719// This function is used to test for the correctness of the parameters passed to various constructor
2720// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721//
Olli Etuaho856c4972016-08-08 11:38:39 +03002722// 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 +00002723//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002724TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002725 TOperator op,
2726 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302727 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002728{
Olli Etuaho856c4972016-08-08 11:38:39 +03002729 TType type = fnCall->getReturnType();
2730 if (type.isUnsizedArray())
2731 {
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002732 if (fnCall->getParamCount() == 0)
2733 {
2734 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2735 type.setArraySize(1u);
2736 return TIntermTyped::CreateZero(type);
2737 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002738 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2739 }
2740 bool constType = true;
2741 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2742 {
2743 const TConstParameter &param = fnCall->getParam(i);
2744 if (param.type->getQualifier() != EvqConst)
2745 constType = false;
2746 }
2747 if (constType)
2748 type.setQualifier(EvqConst);
2749
Olli Etuaho8a176262016-08-16 14:23:01 +03002750 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002751 {
2752 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2753 dummyNode->setType(type);
2754 return dummyNode;
2755 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002756 TIntermAggregate *constructor = arguments->getAsAggregate();
2757 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002758
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002759 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002760 constructor->setOp(op);
2761 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002762 ASSERT(constructor->isConstructor());
2763
2764 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002765 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766
Olli Etuaho21203702014-11-13 16:16:21 +02002767 // Structs should not be precision qualified, the individual members may be.
2768 // Built-in types on the other hand should be precision qualified.
2769 if (op != EOpConstructStruct)
2770 {
2771 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002772 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002773 }
2774
Olli Etuaho856c4972016-08-08 11:38:39 +03002775 constructor->setType(type);
2776
Olli Etuaho77ba4082016-12-16 12:01:18 +00002777 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002778 if (constConstructor)
2779 {
2780 return constConstructor;
2781 }
2782
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002783 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784}
2785
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002786//
2787// Interface/uniform blocks
2788//
Olli Etuaho13389b62016-10-16 11:48:18 +01002789TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002790 const TTypeQualifierBuilder &typeQualifierBuilder,
2791 const TSourceLoc &nameLine,
2792 const TString &blockName,
2793 TFieldList *fieldList,
2794 const TString *instanceName,
2795 const TSourceLoc &instanceLine,
2796 TIntermTyped *arrayIndex,
2797 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002798{
Olli Etuaho856c4972016-08-08 11:38:39 +03002799 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002800
Olli Etuaho77ba4082016-12-16 12:01:18 +00002801 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002802
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002803 if (typeQualifier.qualifier != EvqUniform)
2804 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002805 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
2806 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002807 }
2808
Martin Radev70866b82016-07-22 15:27:42 +03002809 if (typeQualifier.invariant)
2810 {
2811 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2812 }
2813
Martin Radev2cc85b32016-08-05 16:22:53 +03002814 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2815
Jamie Madill099c0f32013-06-20 11:55:52 -04002816 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002817 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002818
Jamie Madill099c0f32013-06-20 11:55:52 -04002819 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2820 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002821 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002822 }
2823
Jamie Madill1566ef72013-06-20 11:55:54 -04002824 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2825 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002826 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002827 }
2828
Olli Etuaho856c4972016-08-08 11:38:39 +03002829 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002830
Martin Radev2cc85b32016-08-05 16:22:53 +03002831 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2832
Arun Patole7e7e68d2015-05-22 12:02:25 +05302833 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2834 if (!symbolTable.declare(blockNameSymbol))
2835 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002836 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002837 }
2838
Jamie Madill98493dd2013-07-08 14:39:03 -04002839 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302840 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2841 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002842 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302843 TType *fieldType = field->type();
2844 if (IsSampler(fieldType->getBasicType()))
2845 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002846 error(field->line(),
2847 "unsupported type - sampler types are not allowed in interface blocks",
2848 fieldType->getBasicString());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002849 }
2850
Martin Radev2cc85b32016-08-05 16:22:53 +03002851 if (IsImage(fieldType->getBasicType()))
2852 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002853 error(field->line(),
2854 "unsupported type - image types are not allowed in interface blocks",
2855 fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002856 }
2857
Jamie Madill98493dd2013-07-08 14:39:03 -04002858 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002859 switch (qualifier)
2860 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002861 case EvqGlobal:
2862 case EvqUniform:
2863 break;
2864 default:
2865 error(field->line(), "invalid qualifier on interface block member",
2866 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002867 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002868 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002869
Martin Radev70866b82016-07-22 15:27:42 +03002870 if (fieldType->isInvariant())
2871 {
2872 error(field->line(), "invalid qualifier on interface block member", "invariant");
2873 }
2874
Jamie Madilla5efff92013-06-06 11:56:47 -04002875 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002876 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002877 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002878
Jamie Madill98493dd2013-07-08 14:39:03 -04002879 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002880 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002881 error(field->line(), "invalid layout qualifier: cannot be used here",
2882 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04002883 }
2884
Jamie Madill98493dd2013-07-08 14:39:03 -04002885 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002886 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002887 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002888 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002889 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002890 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002891 warning(field->line(),
2892 "extraneous layout qualifier: only has an effect on matrix types",
2893 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04002894 }
2895
Jamie Madill98493dd2013-07-08 14:39:03 -04002896 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002897 }
2898
Jamie Madill98493dd2013-07-08 14:39:03 -04002899 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002900 unsigned int arraySize = 0;
2901 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002902 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002903 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002904 }
2905
Jamie Madillb98c3a82015-07-23 14:26:04 -04002906 TInterfaceBlock *interfaceBlock =
2907 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2908 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2909 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002910
2911 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002912 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002913
Jamie Madill98493dd2013-07-08 14:39:03 -04002914 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002915 {
2916 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002917 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2918 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002919 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302920 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002921
2922 // set parent pointer of the field variable
2923 fieldType->setInterfaceBlock(interfaceBlock);
2924
Arun Patole7e7e68d2015-05-22 12:02:25 +05302925 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002926 fieldVariable->setQualifier(typeQualifier.qualifier);
2927
Arun Patole7e7e68d2015-05-22 12:02:25 +05302928 if (!symbolTable.declare(fieldVariable))
2929 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002930 error(field->line(), "redefinition of an interface block member name",
2931 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002932 }
2933 }
2934 }
2935 else
2936 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002937 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002938
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002939 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302940 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002941 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002942
Arun Patole7e7e68d2015-05-22 12:02:25 +05302943 if (!symbolTable.declare(instanceTypeDef))
2944 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002945 error(instanceLine, "redefinition of an interface block instance name",
2946 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002947 }
2948
Jamie Madillb98c3a82015-07-23 14:26:04 -04002949 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002950 symbolName = instanceTypeDef->getName();
2951 }
2952
Olli Etuaho13389b62016-10-16 11:48:18 +01002953 TIntermSymbol *blockSymbol =
2954 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2955 TIntermDeclaration *declaration = new TIntermDeclaration();
2956 declaration->appendDeclarator(blockSymbol);
2957 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002958
2959 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002960 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002961}
2962
Olli Etuaho383b7912016-08-05 11:22:59 +03002963void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002964{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002965 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002966
2967 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00002968 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302969 if (mStructNestingLevel > 1)
2970 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002971 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002972 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002973}
2974
2975void TParseContext::exitStructDeclaration()
2976{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002977 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002978}
2979
Olli Etuaho8a176262016-08-16 14:23:01 +03002980void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002981{
Jamie Madillacb4b812016-11-07 13:50:29 -05002982 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302983 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002984 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002985 }
2986
Arun Patole7e7e68d2015-05-22 12:02:25 +05302987 if (field.type()->getBasicType() != EbtStruct)
2988 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002989 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002990 }
2991
2992 // We're already inside a structure definition at this point, so add
2993 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302994 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2995 {
Jamie Madill41a49272014-03-18 16:10:13 -04002996 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002997 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2998 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002999 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003000 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003001 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003002 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003003}
3004
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003005//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003006// Parse an array index expression
3007//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003008TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3009 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303010 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003011{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003012 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3013 {
3014 if (baseExpression->getAsSymbolNode())
3015 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303016 error(location, " left of '[' is not of type array, matrix, or vector ",
3017 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003018 }
3019 else
3020 {
3021 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3022 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003023
3024 TConstantUnion *unionArray = new TConstantUnion[1];
3025 unionArray->setFConst(0.0f);
3026 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
3027 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003028 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003029
Jamie Madill21c1e452014-12-29 11:33:41 -05003030 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3031
Olli Etuaho36b05142015-11-12 13:10:42 +02003032 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3033 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3034 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3035 // index is a constant expression.
3036 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3037 {
3038 if (baseExpression->isInterfaceBlock())
3039 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003040 error(location,
3041 "array indexes for interface blocks arrays must be constant integral expressions",
3042 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003043 }
3044 else if (baseExpression->getQualifier() == EvqFragmentOut)
3045 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003046 error(location,
3047 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003048 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003049 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3050 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003051 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003052 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003053 }
3054
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003055 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003056 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003057 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3058 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3059 // constant fold expressions that are not constant expressions). The most compatible way to
3060 // handle this case is to report a warning instead of an error and force the index to be in
3061 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003062 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003063 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003064
3065 int safeIndex = -1;
3066
3067 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003068 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003069 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003070 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003071 if (mShaderSpec == SH_WEBGL2_SPEC)
3072 {
3073 // Error has been already generated if index is not const.
3074 if (indexExpression->getQualifier() == EvqConst)
3075 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003076 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003077 }
3078 safeIndex = 0;
3079 }
3080 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3081 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003082 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003083 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003084 "GL_EXT_draw_buffers is disabled",
3085 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003086 safeIndex = 0;
3087 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003088 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003089 // Only do generic out-of-range check if similar error hasn't already been reported.
3090 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003091 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003092 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3093 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003094 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003095 }
3096 }
3097 else if (baseExpression->isMatrix())
3098 {
3099 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003100 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003101 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003102 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003103 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003104 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003105 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3106 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003107 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003108 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003109
3110 ASSERT(safeIndex >= 0);
3111 // Data of constant unions can't be changed, because it may be shared with other
3112 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3113 // sanitized object.
3114 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003115 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003116 TConstantUnion *safeConstantUnion = new TConstantUnion();
3117 safeConstantUnion->setIConst(safeIndex);
3118 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003119 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003120
3121 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003122 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003123 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003124 else
3125 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003126 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003127 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003128 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003129}
3130
Olli Etuaho90892fb2016-07-14 14:44:51 +03003131int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3132 const TSourceLoc &location,
3133 int index,
3134 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003135 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003136{
3137 if (index >= arraySize || index < 0)
3138 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003139 std::stringstream reasonStream;
3140 reasonStream << reason << " '" << index << "'";
3141 std::string token = reasonStream.str();
3142 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003143 if (index < 0)
3144 {
3145 return 0;
3146 }
3147 else
3148 {
3149 return arraySize - 1;
3150 }
3151 }
3152 return index;
3153}
3154
Jamie Madillb98c3a82015-07-23 14:26:04 -04003155TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3156 const TSourceLoc &dotLocation,
3157 const TString &fieldString,
3158 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003159{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003160 if (baseExpression->isArray())
3161 {
3162 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003163 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003164 }
3165
3166 if (baseExpression->isVector())
3167 {
3168 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003169 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3170 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003171 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003172 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003173 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003174 }
3175
Olli Etuahob6fa0432016-09-28 16:28:05 +01003176 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003177 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003178 else if (baseExpression->getBasicType() == EbtStruct)
3179 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303180 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003181 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003182 {
3183 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003184 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003185 }
3186 else
3187 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003188 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003189 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003190 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003191 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003192 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003193 {
3194 fieldFound = true;
3195 break;
3196 }
3197 }
3198 if (fieldFound)
3199 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003200 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3201 index->setLine(fieldLocation);
3202 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003203 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003204 }
3205 else
3206 {
3207 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003208 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003209 }
3210 }
3211 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003212 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003213 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303214 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003215 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003216 {
3217 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003218 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003219 }
3220 else
3221 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003222 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003223 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003224 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003225 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003226 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003227 {
3228 fieldFound = true;
3229 break;
3230 }
3231 }
3232 if (fieldFound)
3233 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003234 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3235 index->setLine(fieldLocation);
3236 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003237 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003238 }
3239 else
3240 {
3241 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003242 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003243 }
3244 }
3245 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003246 else
3247 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003248 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003249 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003250 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303251 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003252 }
3253 else
3254 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303255 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003256 " field selection requires structure, vector, or interface block on left hand "
3257 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303258 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003259 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003260 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003261 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003262}
3263
Jamie Madillb98c3a82015-07-23 14:26:04 -04003264TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3265 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003266{
Martin Radev802abe02016-08-04 17:48:32 +03003267 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003268
3269 if (qualifierType == "shared")
3270 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003271 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003272 {
3273 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3274 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003275 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003276 }
3277 else if (qualifierType == "packed")
3278 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003279 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003280 {
3281 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3282 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003283 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003284 }
3285 else if (qualifierType == "std140")
3286 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003287 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003288 }
3289 else if (qualifierType == "row_major")
3290 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003291 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003292 }
3293 else if (qualifierType == "column_major")
3294 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003295 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003296 }
3297 else if (qualifierType == "location")
3298 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003299 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3300 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003301 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003302 else if (qualifierType == "rgba32f")
3303 {
3304 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3305 qualifier.imageInternalFormat = EiifRGBA32F;
3306 }
3307 else if (qualifierType == "rgba16f")
3308 {
3309 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3310 qualifier.imageInternalFormat = EiifRGBA16F;
3311 }
3312 else if (qualifierType == "r32f")
3313 {
3314 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3315 qualifier.imageInternalFormat = EiifR32F;
3316 }
3317 else if (qualifierType == "rgba8")
3318 {
3319 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3320 qualifier.imageInternalFormat = EiifRGBA8;
3321 }
3322 else if (qualifierType == "rgba8_snorm")
3323 {
3324 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3325 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3326 }
3327 else if (qualifierType == "rgba32i")
3328 {
3329 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3330 qualifier.imageInternalFormat = EiifRGBA32I;
3331 }
3332 else if (qualifierType == "rgba16i")
3333 {
3334 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3335 qualifier.imageInternalFormat = EiifRGBA16I;
3336 }
3337 else if (qualifierType == "rgba8i")
3338 {
3339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3340 qualifier.imageInternalFormat = EiifRGBA8I;
3341 }
3342 else if (qualifierType == "r32i")
3343 {
3344 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3345 qualifier.imageInternalFormat = EiifR32I;
3346 }
3347 else if (qualifierType == "rgba32ui")
3348 {
3349 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3350 qualifier.imageInternalFormat = EiifRGBA32UI;
3351 }
3352 else if (qualifierType == "rgba16ui")
3353 {
3354 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3355 qualifier.imageInternalFormat = EiifRGBA16UI;
3356 }
3357 else if (qualifierType == "rgba8ui")
3358 {
3359 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3360 qualifier.imageInternalFormat = EiifRGBA8UI;
3361 }
3362 else if (qualifierType == "r32ui")
3363 {
3364 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3365 qualifier.imageInternalFormat = EiifR32UI;
3366 }
3367
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003368 else
3369 {
3370 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003371 }
3372
Jamie Madilla5efff92013-06-06 11:56:47 -04003373 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003374}
3375
Martin Radev802abe02016-08-04 17:48:32 +03003376void TParseContext::parseLocalSize(const TString &qualifierType,
3377 const TSourceLoc &qualifierTypeLine,
3378 int intValue,
3379 const TSourceLoc &intValueLine,
3380 const std::string &intValueString,
3381 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003382 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003383{
Olli Etuaho856c4972016-08-08 11:38:39 +03003384 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003385 if (intValue < 1)
3386 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003387 std::stringstream reasonStream;
3388 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3389 std::string reason = reasonStream.str();
3390 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003391 }
3392 (*localSize)[index] = intValue;
3393}
3394
Olli Etuaho09b04a22016-12-15 13:30:26 +00003395void TParseContext::parseNumViews(int intValue,
3396 const TSourceLoc &intValueLine,
3397 const std::string &intValueString,
3398 int *numViews)
3399{
3400 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3401 // specification.
3402 if (intValue < 1)
3403 {
3404 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3405 }
3406 *numViews = intValue;
3407}
3408
Jamie Madillb98c3a82015-07-23 14:26:04 -04003409TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3410 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003411 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303412 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003413{
Martin Radev802abe02016-08-04 17:48:32 +03003414 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003415
Martin Radev802abe02016-08-04 17:48:32 +03003416 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003417
Martin Radev802abe02016-08-04 17:48:32 +03003418 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003419 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003420 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003421 if (intValue < 0)
3422 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003423 error(intValueLine, "out of range: location must be non-negative",
3424 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003425 }
3426 else
3427 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003428 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003429 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003430 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003431 }
Martin Radev802abe02016-08-04 17:48:32 +03003432 else if (qualifierType == "local_size_x")
3433 {
3434 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3435 &qualifier.localSize);
3436 }
3437 else if (qualifierType == "local_size_y")
3438 {
3439 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3440 &qualifier.localSize);
3441 }
3442 else if (qualifierType == "local_size_z")
3443 {
3444 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3445 &qualifier.localSize);
3446 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00003447 else if (qualifierType == "num_views" && mMultiviewAvailable &&
3448 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
3449 mShaderType == GL_VERTEX_SHADER)
3450 {
3451 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3452 }
Martin Radev802abe02016-08-04 17:48:32 +03003453 else
3454 {
3455 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003456 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003457
Jamie Madilla5efff92013-06-06 11:56:47 -04003458 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003459}
3460
Olli Etuaho613b9592016-09-05 12:05:53 +03003461TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3462{
3463 return new TTypeQualifierBuilder(
3464 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3465 mShaderVersion);
3466}
3467
Jamie Madillb98c3a82015-07-23 14:26:04 -04003468TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003469 TLayoutQualifier rightQualifier,
3470 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003471{
Martin Radevc28888b2016-07-22 15:27:42 +03003472 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003473 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003474}
3475
Olli Etuaho4de340a2016-12-16 09:32:03 +00003476TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3477 const TFieldList *newlyAddedFields,
3478 const TSourceLoc &location)
3479{
3480 for (TField *field : *newlyAddedFields)
3481 {
3482 for (TField *oldField : *processedFields)
3483 {
3484 if (oldField->name() == field->name())
3485 {
3486 error(location, "duplicate field name in structure", field->name().c_str());
3487 }
3488 }
3489 processedFields->push_back(field);
3490 }
3491 return processedFields;
3492}
3493
Martin Radev70866b82016-07-22 15:27:42 +03003494TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3495 const TTypeQualifierBuilder &typeQualifierBuilder,
3496 TPublicType *typeSpecifier,
3497 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003498{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003499 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003500
Martin Radev70866b82016-07-22 15:27:42 +03003501 typeSpecifier->qualifier = typeQualifier.qualifier;
3502 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003503 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003504 typeSpecifier->invariant = typeQualifier.invariant;
3505 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303506 {
Martin Radev70866b82016-07-22 15:27:42 +03003507 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003508 }
Martin Radev70866b82016-07-22 15:27:42 +03003509 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003510}
3511
Jamie Madillb98c3a82015-07-23 14:26:04 -04003512TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3513 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003514{
Martin Radev4a9cd802016-09-01 16:51:51 +03003515 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3516 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003517
Martin Radev4a9cd802016-09-01 16:51:51 +03003518 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003519
Martin Radev4a9cd802016-09-01 16:51:51 +03003520 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003521
Arun Patole7e7e68d2015-05-22 12:02:25 +05303522 for (unsigned int i = 0; i < fieldList->size(); ++i)
3523 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003524 //
3525 // Careful not to replace already known aspects of type, like array-ness
3526 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303527 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003528 type->setBasicType(typeSpecifier.getBasicType());
3529 type->setPrimarySize(typeSpecifier.getPrimarySize());
3530 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003531 type->setPrecision(typeSpecifier.precision);
3532 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003533 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003534 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003535 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003536
3537 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303538 if (type->isArray())
3539 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003540 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003541 }
3542 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003543 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003544 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303545 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003546 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003547 }
3548
Martin Radev4a9cd802016-09-01 16:51:51 +03003549 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003550 }
3551
Jamie Madill98493dd2013-07-08 14:39:03 -04003552 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003553}
3554
Martin Radev4a9cd802016-09-01 16:51:51 +03003555TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3556 const TSourceLoc &nameLine,
3557 const TString *structName,
3558 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003559{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303560 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003561 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003562
Jamie Madill9b820842015-02-12 10:40:10 -05003563 // Store a bool in the struct if we're at global scope, to allow us to
3564 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003565 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003566
Jamie Madill98493dd2013-07-08 14:39:03 -04003567 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003568 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003569 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303570 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3571 if (!symbolTable.declare(userTypeDef))
3572 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003573 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003574 }
3575 }
3576
3577 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003578 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003579 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003580 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003581 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003582 switch (qualifier)
3583 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003584 case EvqGlobal:
3585 case EvqTemporary:
3586 break;
3587 default:
3588 error(field.line(), "invalid qualifier on struct member",
3589 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003590 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003591 }
Martin Radev70866b82016-07-22 15:27:42 +03003592 if (field.type()->isInvariant())
3593 {
3594 error(field.line(), "invalid qualifier on struct member", "invariant");
3595 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003596 if (IsImage(field.type()->getBasicType()))
3597 {
3598 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3599 }
3600
3601 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003602
3603 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003604 }
3605
Martin Radev4a9cd802016-09-01 16:51:51 +03003606 TTypeSpecifierNonArray typeSpecifierNonArray;
3607 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3608 typeSpecifierNonArray.userDef = structureType;
3609 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003610 exitStructDeclaration();
3611
Martin Radev4a9cd802016-09-01 16:51:51 +03003612 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003613}
3614
Jamie Madillb98c3a82015-07-23 14:26:04 -04003615TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003616 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003617 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003618{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003619 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003620 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003621 init->isVector())
3622 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003623 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3624 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003625 return nullptr;
3626 }
3627
Olli Etuahoac5274d2015-02-20 10:19:08 +02003628 if (statementList)
3629 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00003630 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02003631 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003632 return nullptr;
3633 }
3634 }
3635
Olli Etuahoa3a36662015-02-17 13:46:51 +02003636 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3637 if (node == nullptr)
3638 {
3639 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003640 return nullptr;
3641 }
3642 return node;
3643}
3644
3645TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3646{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003647 if (mSwitchNestingLevel == 0)
3648 {
3649 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003650 return nullptr;
3651 }
3652 if (condition == nullptr)
3653 {
3654 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003655 return nullptr;
3656 }
3657 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003658 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003659 {
3660 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003661 }
3662 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003663 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3664 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3665 // fold in case labels.
3666 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003667 {
3668 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003669 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003670 TIntermCase *node = intermediate.addCase(condition, loc);
3671 if (node == nullptr)
3672 {
3673 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003674 return nullptr;
3675 }
3676 return node;
3677}
3678
3679TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3680{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003681 if (mSwitchNestingLevel == 0)
3682 {
3683 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003684 return nullptr;
3685 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003686 TIntermCase *node = intermediate.addCase(nullptr, loc);
3687 if (node == nullptr)
3688 {
3689 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003690 return nullptr;
3691 }
3692 return node;
3693}
3694
Jamie Madillb98c3a82015-07-23 14:26:04 -04003695TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3696 TIntermTyped *child,
3697 const TSourceLoc &loc,
3698 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003699{
3700 if (child == nullptr)
3701 {
3702 return nullptr;
3703 }
3704
3705 switch (op)
3706 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003707 case EOpLogicalNot:
3708 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3709 child->isVector())
3710 {
3711 return nullptr;
3712 }
3713 break;
3714 case EOpBitwiseNot:
3715 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3716 child->isMatrix() || child->isArray())
3717 {
3718 return nullptr;
3719 }
3720 break;
3721 case EOpPostIncrement:
3722 case EOpPreIncrement:
3723 case EOpPostDecrement:
3724 case EOpPreDecrement:
3725 case EOpNegative:
3726 case EOpPositive:
3727 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003728 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003729 {
3730 return nullptr;
3731 }
3732 // Operators for built-ins are already type checked against their prototype.
3733 default:
3734 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003735 }
3736
Olli Etuahof119a262016-08-19 15:54:22 +03003737 TIntermUnary *node = new TIntermUnary(op, child);
3738 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003739
Olli Etuaho77ba4082016-12-16 12:01:18 +00003740 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03003741 if (foldedNode)
3742 return foldedNode;
3743
3744 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003745}
3746
Olli Etuaho09b22472015-02-11 11:47:26 +02003747TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3748{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003749 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003750 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003751 {
3752 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003753 return child;
3754 }
3755 return node;
3756}
3757
Jamie Madillb98c3a82015-07-23 14:26:04 -04003758TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3759 TIntermTyped *child,
3760 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003761{
Olli Etuaho856c4972016-08-08 11:38:39 +03003762 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003763 return addUnaryMath(op, child, loc);
3764}
3765
Jamie Madillb98c3a82015-07-23 14:26:04 -04003766bool TParseContext::binaryOpCommonCheck(TOperator op,
3767 TIntermTyped *left,
3768 TIntermTyped *right,
3769 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003770{
Olli Etuaho244be012016-08-18 15:26:02 +03003771 if (left->getType().getStruct() || right->getType().getStruct())
3772 {
3773 switch (op)
3774 {
3775 case EOpIndexDirectStruct:
3776 ASSERT(left->getType().getStruct());
3777 break;
3778 case EOpEqual:
3779 case EOpNotEqual:
3780 case EOpAssign:
3781 case EOpInitialize:
3782 if (left->getType() != right->getType())
3783 {
3784 return false;
3785 }
3786 break;
3787 default:
3788 error(loc, "Invalid operation for structs", GetOperatorString(op));
3789 return false;
3790 }
3791 }
3792
Olli Etuahod6b14282015-03-17 14:31:35 +02003793 if (left->isArray() || right->isArray())
3794 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003795 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003796 {
3797 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3798 return false;
3799 }
3800
3801 if (left->isArray() != right->isArray())
3802 {
3803 error(loc, "array / non-array mismatch", GetOperatorString(op));
3804 return false;
3805 }
3806
3807 switch (op)
3808 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003809 case EOpEqual:
3810 case EOpNotEqual:
3811 case EOpAssign:
3812 case EOpInitialize:
3813 break;
3814 default:
3815 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3816 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003817 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003818 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003819 if (left->getArraySize() != right->getArraySize())
3820 {
3821 error(loc, "array size mismatch", GetOperatorString(op));
3822 return false;
3823 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003824 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003825
3826 // Check ops which require integer / ivec parameters
3827 bool isBitShift = false;
3828 switch (op)
3829 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003830 case EOpBitShiftLeft:
3831 case EOpBitShiftRight:
3832 case EOpBitShiftLeftAssign:
3833 case EOpBitShiftRightAssign:
3834 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3835 // check that the basic type is an integer type.
3836 isBitShift = true;
3837 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3838 {
3839 return false;
3840 }
3841 break;
3842 case EOpBitwiseAnd:
3843 case EOpBitwiseXor:
3844 case EOpBitwiseOr:
3845 case EOpBitwiseAndAssign:
3846 case EOpBitwiseXorAssign:
3847 case EOpBitwiseOrAssign:
3848 // It is enough to check the type of only one operand, since later it
3849 // is checked that the operand types match.
3850 if (!IsInteger(left->getBasicType()))
3851 {
3852 return false;
3853 }
3854 break;
3855 default:
3856 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003857 }
3858
3859 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3860 // So the basic type should usually match.
3861 if (!isBitShift && left->getBasicType() != right->getBasicType())
3862 {
3863 return false;
3864 }
3865
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003866 // Check that:
3867 // 1. Type sizes match exactly on ops that require that.
3868 // 2. Restrictions for structs that contain arrays or samplers are respected.
3869 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003870 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003871 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003872 case EOpAssign:
3873 case EOpInitialize:
3874 case EOpEqual:
3875 case EOpNotEqual:
3876 // ESSL 1.00 sections 5.7, 5.8, 5.9
3877 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3878 {
3879 error(loc, "undefined operation for structs containing arrays",
3880 GetOperatorString(op));
3881 return false;
3882 }
3883 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3884 // we interpret the spec so that this extends to structs containing samplers,
3885 // similarly to ESSL 1.00 spec.
3886 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3887 left->getType().isStructureContainingSamplers())
3888 {
3889 error(loc, "undefined operation for structs containing samplers",
3890 GetOperatorString(op));
3891 return false;
3892 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003893
3894 if ((op == EOpAssign || op == EOpInitialize) &&
3895 left->getType().isStructureContainingImages())
3896 {
3897 error(loc, "undefined operation for structs containing images",
3898 GetOperatorString(op));
3899 return false;
3900 }
Olli Etuahoe1805592017-01-02 16:41:20 +00003901 if ((left->getNominalSize() != right->getNominalSize()) ||
3902 (left->getSecondarySize() != right->getSecondarySize()))
3903 {
3904 error(loc, "dimension mismatch", GetOperatorString(op));
3905 return false;
3906 }
3907 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003908 case EOpLessThan:
3909 case EOpGreaterThan:
3910 case EOpLessThanEqual:
3911 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00003912 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003913 {
Olli Etuahoe1805592017-01-02 16:41:20 +00003914 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003915 return false;
3916 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003917 break;
3918 case EOpAdd:
3919 case EOpSub:
3920 case EOpDiv:
3921 case EOpIMod:
3922 case EOpBitShiftLeft:
3923 case EOpBitShiftRight:
3924 case EOpBitwiseAnd:
3925 case EOpBitwiseXor:
3926 case EOpBitwiseOr:
3927 case EOpAddAssign:
3928 case EOpSubAssign:
3929 case EOpDivAssign:
3930 case EOpIModAssign:
3931 case EOpBitShiftLeftAssign:
3932 case EOpBitShiftRightAssign:
3933 case EOpBitwiseAndAssign:
3934 case EOpBitwiseXorAssign:
3935 case EOpBitwiseOrAssign:
3936 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3937 {
3938 return false;
3939 }
3940
3941 // Are the sizes compatible?
3942 if (left->getNominalSize() != right->getNominalSize() ||
3943 left->getSecondarySize() != right->getSecondarySize())
3944 {
3945 // If the nominal sizes of operands do not match:
3946 // One of them must be a scalar.
3947 if (!left->isScalar() && !right->isScalar())
3948 return false;
3949
3950 // In the case of compound assignment other than multiply-assign,
3951 // the right side needs to be a scalar. Otherwise a vector/matrix
3952 // would be assigned to a scalar. A scalar can't be shifted by a
3953 // vector either.
3954 if (!right->isScalar() &&
3955 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3956 return false;
3957 }
3958 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003959 default:
3960 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003961 }
3962
Olli Etuahod6b14282015-03-17 14:31:35 +02003963 return true;
3964}
3965
Olli Etuaho1dded802016-08-18 18:13:13 +03003966bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3967 const TType &left,
3968 const TType &right)
3969{
3970 switch (op)
3971 {
3972 case EOpMul:
3973 case EOpMulAssign:
3974 return left.getNominalSize() == right.getNominalSize() &&
3975 left.getSecondarySize() == right.getSecondarySize();
3976 case EOpVectorTimesScalar:
3977 return true;
3978 case EOpVectorTimesScalarAssign:
3979 ASSERT(!left.isMatrix() && !right.isMatrix());
3980 return left.isVector() && !right.isVector();
3981 case EOpVectorTimesMatrix:
3982 return left.getNominalSize() == right.getRows();
3983 case EOpVectorTimesMatrixAssign:
3984 ASSERT(!left.isMatrix() && right.isMatrix());
3985 return left.isVector() && left.getNominalSize() == right.getRows() &&
3986 left.getNominalSize() == right.getCols();
3987 case EOpMatrixTimesVector:
3988 return left.getCols() == right.getNominalSize();
3989 case EOpMatrixTimesScalar:
3990 return true;
3991 case EOpMatrixTimesScalarAssign:
3992 ASSERT(left.isMatrix() && !right.isMatrix());
3993 return !right.isVector();
3994 case EOpMatrixTimesMatrix:
3995 return left.getCols() == right.getRows();
3996 case EOpMatrixTimesMatrixAssign:
3997 ASSERT(left.isMatrix() && right.isMatrix());
3998 // We need to check two things:
3999 // 1. The matrix multiplication step is valid.
4000 // 2. The result will have the same number of columns as the lvalue.
4001 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4002
4003 default:
4004 UNREACHABLE();
4005 return false;
4006 }
4007}
4008
Jamie Madillb98c3a82015-07-23 14:26:04 -04004009TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4010 TIntermTyped *left,
4011 TIntermTyped *right,
4012 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004013{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004014 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004015 return nullptr;
4016
Olli Etuahofc1806e2015-03-17 13:03:11 +02004017 switch (op)
4018 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004019 case EOpEqual:
4020 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004021 case EOpLessThan:
4022 case EOpGreaterThan:
4023 case EOpLessThanEqual:
4024 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004025 break;
4026 case EOpLogicalOr:
4027 case EOpLogicalXor:
4028 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004029 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4030 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004031 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004032 {
4033 return nullptr;
4034 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004035 // Basic types matching should have been already checked.
4036 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004037 break;
4038 case EOpAdd:
4039 case EOpSub:
4040 case EOpDiv:
4041 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004042 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4043 !right->getType().getStruct());
4044 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004045 {
4046 return nullptr;
4047 }
4048 break;
4049 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004050 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4051 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004052 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004053 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004054 {
4055 return nullptr;
4056 }
4057 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004058 default:
4059 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004060 }
4061
Olli Etuaho1dded802016-08-18 18:13:13 +03004062 if (op == EOpMul)
4063 {
4064 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4065 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4066 {
4067 return nullptr;
4068 }
4069 }
4070
Olli Etuaho3fdec912016-08-18 15:08:06 +03004071 TIntermBinary *node = new TIntermBinary(op, left, right);
4072 node->setLine(loc);
4073
Olli Etuaho3fdec912016-08-18 15:08:06 +03004074 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004075 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004076 if (foldedNode)
4077 return foldedNode;
4078
4079 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004080}
4081
Jamie Madillb98c3a82015-07-23 14:26:04 -04004082TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4083 TIntermTyped *left,
4084 TIntermTyped *right,
4085 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004086{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004087 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004088 if (node == 0)
4089 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004090 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4091 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004092 return left;
4093 }
4094 return node;
4095}
4096
Jamie Madillb98c3a82015-07-23 14:26:04 -04004097TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4098 TIntermTyped *left,
4099 TIntermTyped *right,
4100 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004101{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004102 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004103 if (node == 0)
4104 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004105 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4106 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004107 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004108 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004109 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4110 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004111 }
4112 return node;
4113}
4114
Olli Etuaho13389b62016-10-16 11:48:18 +01004115TIntermBinary *TParseContext::createAssign(TOperator op,
4116 TIntermTyped *left,
4117 TIntermTyped *right,
4118 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004119{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004120 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004121 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004122 if (op == EOpMulAssign)
4123 {
4124 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4125 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4126 {
4127 return nullptr;
4128 }
4129 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004130 TIntermBinary *node = new TIntermBinary(op, left, right);
4131 node->setLine(loc);
4132
Olli Etuaho3fdec912016-08-18 15:08:06 +03004133 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004134 }
4135 return nullptr;
4136}
4137
Jamie Madillb98c3a82015-07-23 14:26:04 -04004138TIntermTyped *TParseContext::addAssign(TOperator op,
4139 TIntermTyped *left,
4140 TIntermTyped *right,
4141 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004142{
4143 TIntermTyped *node = createAssign(op, left, right, loc);
4144 if (node == nullptr)
4145 {
4146 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004147 return left;
4148 }
4149 return node;
4150}
4151
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004152TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4153 TIntermTyped *right,
4154 const TSourceLoc &loc)
4155{
Corentin Wallez0d959252016-07-12 17:26:32 -04004156 // WebGL2 section 5.26, the following results in an error:
4157 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004158 if (mShaderSpec == SH_WEBGL2_SPEC &&
4159 (left->isArray() || left->getBasicType() == EbtVoid ||
4160 left->getType().isStructureContainingArrays() || right->isArray() ||
4161 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004162 {
4163 error(loc,
4164 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4165 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004166 }
4167
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004168 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004169}
4170
Olli Etuaho49300862015-02-20 14:54:49 +02004171TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4172{
4173 switch (op)
4174 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004175 case EOpContinue:
4176 if (mLoopNestingLevel <= 0)
4177 {
4178 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004179 }
4180 break;
4181 case EOpBreak:
4182 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4183 {
4184 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004185 }
4186 break;
4187 case EOpReturn:
4188 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4189 {
4190 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004191 }
4192 break;
4193 default:
4194 // No checks for discard
4195 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004196 }
4197 return intermediate.addBranch(op, loc);
4198}
4199
Jamie Madillb98c3a82015-07-23 14:26:04 -04004200TIntermBranch *TParseContext::addBranch(TOperator op,
4201 TIntermTyped *returnValue,
4202 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004203{
4204 ASSERT(op == EOpReturn);
4205 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004206 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004207 {
4208 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004209 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004210 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004211 {
4212 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004213 }
4214 return intermediate.addBranch(op, returnValue, loc);
4215}
4216
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004217void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4218{
4219 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004220 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004221 TIntermNode *offset = nullptr;
4222 TIntermSequence *arguments = functionCall->getSequence();
4223 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4224 name.compare(0, 16, "textureLodOffset") == 0 ||
4225 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4226 name.compare(0, 17, "textureGradOffset") == 0 ||
4227 name.compare(0, 21, "textureProjGradOffset") == 0)
4228 {
4229 offset = arguments->back();
4230 }
4231 else if (name.compare(0, 13, "textureOffset") == 0 ||
4232 name.compare(0, 17, "textureProjOffset") == 0)
4233 {
4234 // A bias parameter might follow the offset parameter.
4235 ASSERT(arguments->size() >= 3);
4236 offset = (*arguments)[2];
4237 }
4238 if (offset != nullptr)
4239 {
4240 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4241 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4242 {
4243 TString unmangledName = TFunction::unmangleName(name);
4244 error(functionCall->getLine(), "Texture offset must be a constant expression",
4245 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004246 }
4247 else
4248 {
4249 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4250 size_t size = offsetConstantUnion->getType().getObjectSize();
4251 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4252 for (size_t i = 0u; i < size; ++i)
4253 {
4254 int offsetValue = values[i].getIConst();
4255 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4256 {
4257 std::stringstream tokenStream;
4258 tokenStream << offsetValue;
4259 std::string token = tokenStream.str();
4260 error(offset->getLine(), "Texture offset value out of valid range",
4261 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004262 }
4263 }
4264 }
4265 }
4266}
4267
Martin Radev2cc85b32016-08-05 16:22:53 +03004268// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4269void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4270{
4271 ASSERT(!functionCall->isUserDefined());
4272 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4273
4274 if (name.compare(0, 5, "image") == 0)
4275 {
4276 TIntermSequence *arguments = functionCall->getSequence();
4277 TIntermNode *imageNode = (*arguments)[0];
4278 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4279
4280 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4281
4282 if (name.compare(5, 5, "Store") == 0)
4283 {
4284 if (memoryQualifier.readonly)
4285 {
4286 error(imageNode->getLine(),
4287 "'imageStore' cannot be used with images qualified as 'readonly'",
4288 imageSymbol->getSymbol().c_str());
4289 }
4290 }
4291 else if (name.compare(5, 4, "Load") == 0)
4292 {
4293 if (memoryQualifier.writeonly)
4294 {
4295 error(imageNode->getLine(),
4296 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4297 imageSymbol->getSymbol().c_str());
4298 }
4299 }
4300 }
4301}
4302
4303// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4304void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4305 const TFunction *functionDefinition,
4306 const TIntermAggregate *functionCall)
4307{
4308 ASSERT(functionCall->isUserDefined());
4309
4310 const TIntermSequence &arguments = *functionCall->getSequence();
4311
4312 ASSERT(functionDefinition->getParamCount() == arguments.size());
4313
4314 for (size_t i = 0; i < arguments.size(); ++i)
4315 {
4316 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4317 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4318 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4319
4320 if (IsImage(functionArgumentType.getBasicType()))
4321 {
4322 const TMemoryQualifier &functionArgumentMemoryQualifier =
4323 functionArgumentType.getMemoryQualifier();
4324 const TMemoryQualifier &functionParameterMemoryQualifier =
4325 functionParameterType.getMemoryQualifier();
4326 if (functionArgumentMemoryQualifier.readonly &&
4327 !functionParameterMemoryQualifier.readonly)
4328 {
4329 error(functionCall->getLine(),
4330 "Function call discards the 'readonly' qualifier from image",
4331 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4332 }
4333
4334 if (functionArgumentMemoryQualifier.writeonly &&
4335 !functionParameterMemoryQualifier.writeonly)
4336 {
4337 error(functionCall->getLine(),
4338 "Function call discards the 'writeonly' qualifier from image",
4339 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4340 }
Martin Radev049edfa2016-11-11 14:35:37 +02004341
4342 if (functionArgumentMemoryQualifier.coherent &&
4343 !functionParameterMemoryQualifier.coherent)
4344 {
4345 error(functionCall->getLine(),
4346 "Function call discards the 'coherent' qualifier from image",
4347 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4348 }
4349
4350 if (functionArgumentMemoryQualifier.volatileQualifier &&
4351 !functionParameterMemoryQualifier.volatileQualifier)
4352 {
4353 error(functionCall->getLine(),
4354 "Function call discards the 'volatile' qualifier from image",
4355 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4356 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004357 }
4358 }
4359}
4360
Jamie Madillb98c3a82015-07-23 14:26:04 -04004361TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4362 TIntermNode *paramNode,
4363 TIntermNode *thisNode,
4364 const TSourceLoc &loc,
4365 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004366{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004367 *fatalError = false;
4368 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004369 TIntermTyped *callNode = nullptr;
4370
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004371 if (thisNode != nullptr)
4372 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004373 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004374 int arraySize = 0;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004375 TIntermTyped *typedThis = thisNode->getAsTyped();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004376 if (fnCall->getName() != "length")
4377 {
4378 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004379 }
4380 else if (paramNode != nullptr)
4381 {
4382 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004383 }
4384 else if (typedThis == nullptr || !typedThis->isArray())
4385 {
4386 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004387 }
4388 else
4389 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004390 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004391 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004392 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004393 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004394 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004395 // (func()).length()
4396 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004397 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4398 // expression.
4399 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4400 // spec section 5.9 which allows "An array, vector or matrix expression with the
4401 // length method applied".
4402 error(loc, "length can only be called on array names, not on array expressions",
4403 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004404 }
4405 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004406 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004407 callNode =
4408 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004409 }
4410 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004411 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004412 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004413 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004414 }
4415 else
4416 {
4417 //
4418 // Not a constructor. Find it in the symbol table.
4419 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304420 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004421 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004422 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004423 if (fnCandidate)
4424 {
4425 //
4426 // A declared function.
4427 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004428 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004429 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004430 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004431 }
4432 op = fnCandidate->getBuiltInOp();
4433 if (builtIn && op != EOpNull)
4434 {
4435 //
4436 // A function call mapped to a built-in operation.
4437 //
4438 if (fnCandidate->getParamCount() == 1)
4439 {
4440 //
4441 // Treat it like a built-in unary operator.
4442 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004443 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4444 paramNode = paramAgg->getSequence()->front();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004445 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004446 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004447 if (callNode == nullptr)
4448 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004449 std::stringstream reasonStream;
4450 reasonStream << "wrong operand type for built in unary function: "
4451 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
4452 std::string reason = reasonStream.str();
4453 error(paramNode->getLine(), reason.c_str(), "Internal Error");
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004454 *fatalError = true;
4455 return nullptr;
4456 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004457 }
4458 else
4459 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004460 TIntermAggregate *aggregate =
4461 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004462 aggregate->setType(fnCandidate->getReturnType());
4463 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004464 if (aggregate->areChildrenConstQualified())
4465 {
4466 aggregate->getTypePointer()->setQualifier(EvqConst);
4467 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004468
4469 // Some built-in functions have out parameters too.
4470 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304471
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004472 // See if we can constant fold a built-in. Note that this may be possible even
4473 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004474 TIntermTyped *foldedNode =
Olli Etuaho77ba4082016-12-16 12:01:18 +00004475 intermediate.foldAggregateBuiltIn(aggregate, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304476 if (foldedNode)
4477 {
Arun Patole274f0702015-05-05 13:33:30 +05304478 callNode = foldedNode;
4479 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004480 else
4481 {
4482 callNode = aggregate;
4483 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004484 }
4485 }
4486 else
4487 {
4488 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004489 TIntermAggregate *aggregate =
4490 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004491 aggregate->setType(fnCandidate->getReturnType());
4492
Jamie Madillb98c3a82015-07-23 14:26:04 -04004493 // this is how we know whether the given function is a builtIn function or a user
4494 // defined function
4495 // if builtIn == false, it's a userDefined -> could be an overloaded
4496 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004497 // if builtIn == true, it's definitely a builtIn function with EOpNull
4498 if (!builtIn)
4499 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004500 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004501
Olli Etuahobd674552016-10-06 13:28:42 +01004502 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004503 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004504 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004505 aggregate->setBuiltInFunctionPrecision();
4506
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004507 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004508
4509 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4510 }
4511 else
4512 {
4513 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004514 }
4515
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004516 callNode = aggregate;
4517
4518 functionCallLValueErrorCheck(fnCandidate, aggregate);
4519 }
4520 }
4521 else
4522 {
4523 // error message was put out by findFunction()
4524 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004525 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004526 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004527 callNode = intermediate.addConstantUnion(unionArray,
4528 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004529 }
4530 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004531 return callNode;
4532}
4533
Jamie Madillb98c3a82015-07-23 14:26:04 -04004534TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004535 TIntermTyped *trueExpression,
4536 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004537 const TSourceLoc &loc)
4538{
Olli Etuaho856c4972016-08-08 11:38:39 +03004539 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004540
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004541 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004542 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004543 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4544 falseExpression->getCompleteString());
4545 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004546 }
Olli Etuahode318b22016-10-25 16:18:25 +01004547 if (IsOpaqueType(trueExpression->getBasicType()))
4548 {
4549 // ESSL 1.00 section 4.1.7
4550 // ESSL 3.00 section 4.1.7
4551 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4552 // Note that structs containing opaque types don't need to be checked as structs are
4553 // forbidden below.
4554 error(loc, "ternary operator is not allowed for opaque types", ":");
4555 return falseExpression;
4556 }
4557
Olli Etuahoa2d53032015-04-15 14:14:44 +03004558 // ESSL1 sections 5.2 and 5.7:
4559 // ESSL3 section 5.7:
4560 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004561 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004562 {
4563 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004564 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004565 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004566 // WebGL2 section 5.26, the following results in an error:
4567 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004568 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004569 {
4570 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004571 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004572 }
4573
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004574 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004575}
Olli Etuaho49300862015-02-20 14:54:49 +02004576
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004577//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004578// Parse an array of strings using yyparse.
4579//
4580// Returns 0 for success.
4581//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004582int PaParseStrings(size_t count,
4583 const char *const string[],
4584 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304585 TParseContext *context)
4586{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004587 if ((count == 0) || (string == NULL))
4588 return 1;
4589
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004590 if (glslang_initialize(context))
4591 return 1;
4592
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004593 int error = glslang_scan(count, string, length, context);
4594 if (!error)
4595 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004596
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004597 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004598
alokp@chromium.org6b495712012-06-29 00:06:58 +00004599 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004600}
Jamie Madill45bcc782016-11-07 13:58:48 -05004601
4602} // namespace sh