blob: 43fab6c3ada2ee6ce1717e0315a97bc69659e2c4 [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 Etuaho16c745a2017-01-16 17:02:27 +00002398TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2399 const TFunction &parsedFunction,
2400 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002401{
Olli Etuaho476197f2016-10-11 13:59:08 +01002402 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2403 // first declaration. Either way the instance in the symbol table is used to track whether the
2404 // function is declared multiple times.
2405 TFunction *function = static_cast<TFunction *>(
2406 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2407 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002408 {
2409 // ESSL 1.00.17 section 4.2.7.
2410 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2411 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002412 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002413 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002414
Olli Etuaho16c745a2017-01-16 17:02:27 +00002415 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(function->getReturnType());
Olli Etuaho476197f2016-10-11 13:59:08 +01002416 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2417 // point to the data that already exists in the symbol table.
Olli Etuahobd674552016-10-06 13:28:42 +01002418 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuaho16c745a2017-01-16 17:02:27 +00002419 prototype->setLine(location);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002420
Olli Etuaho476197f2016-10-11 13:59:08 +01002421 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002422 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002423 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002424 if (param.name != 0)
2425 {
2426 TVariable variable(param.name, *param.type);
2427
2428 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2429 variable.getUniqueId(), variable.getName(), variable.getType(), location);
Olli Etuaho16c745a2017-01-16 17:02:27 +00002430 prototype->appendParameter(paramSymbol);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002431 }
2432 else
2433 {
2434 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
Olli Etuaho16c745a2017-01-16 17:02:27 +00002435 prototype->appendParameter(paramSymbol);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002436 }
2437 }
2438
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002439 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002440
2441 if (!symbolTable.atGlobalLevel())
2442 {
2443 // ESSL 3.00.4 section 4.2.4.
2444 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002445 }
2446
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002447 return prototype;
2448}
2449
Olli Etuaho336b1472016-10-05 16:37:55 +01002450TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2451 const TFunction &function,
2452 TIntermAggregate *functionParameters,
2453 TIntermBlock *functionBody,
2454 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002455{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002456 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002457 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2458 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002459 error(location, "function does not return a value:", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002460 }
2461
Olli Etuahof51fdd22016-10-03 10:03:40 +01002462 if (functionBody == nullptr)
2463 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002464 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002465 functionBody->setLine(location);
2466 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002467 TIntermFunctionDefinition *functionNode =
2468 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2469 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002470
Olli Etuahobd674552016-10-06 13:28:42 +01002471 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002472
2473 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002474 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002475}
2476
Olli Etuaho476197f2016-10-11 13:59:08 +01002477void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2478 TFunction **function,
2479 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002480{
Olli Etuaho476197f2016-10-11 13:59:08 +01002481 ASSERT(function);
2482 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002483 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002484 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002485
2486 if (builtIn)
2487 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002488 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002489 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002490 else
Jamie Madill185fb402015-06-12 15:48:48 -04002491 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002492 TFunction *prevDec = static_cast<TFunction *>(
2493 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2494
2495 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2496 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2497 // occurance.
2498 if (*function != prevDec)
2499 {
2500 // Swap the parameters of the previous declaration to the parameters of the function
2501 // definition (parameter names may differ).
2502 prevDec->swapParameters(**function);
2503
2504 // The function definition will share the same symbol as any previous declaration.
2505 *function = prevDec;
2506 }
2507
2508 if ((*function)->isDefined())
2509 {
2510 error(location, "function already has a body", (*function)->getName().c_str());
2511 }
2512
2513 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002514 }
Jamie Madill185fb402015-06-12 15:48:48 -04002515
2516 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002517 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002518 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002519 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002520 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002521 error(location, "function cannot take any parameter(s)",
2522 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002523 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002524 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002525 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002526 error(location, "main function cannot return a value",
2527 (*function)->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002528 }
2529 }
2530
2531 //
2532 // Remember the return type for later checking for RETURN statements.
2533 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002534 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002535 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002536
2537 //
2538 // Insert parameters into the symbol table.
2539 // If the parameter has no name, it's not an error, just don't insert it
2540 // (could be used for unused args).
2541 //
2542 // Also, accumulate the list of parameters into the HIL, so lower level code
2543 // knows where to find parameters.
2544 //
2545 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002546 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002547 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002548 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002549 if (param.name != 0)
2550 {
2551 TVariable *variable = new TVariable(param.name, *param.type);
2552 //
2553 // Insert the parameters with name in the symbol table.
2554 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002555 if (!symbolTable.declare(variable))
2556 {
Jamie Madill185fb402015-06-12 15:48:48 -04002557 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002558 paramNodes = intermediate.growAggregate(
2559 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2560 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002561 }
2562
2563 //
2564 // Add the parameter to the HIL
2565 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002566 TIntermSymbol *symbol = intermediate.addSymbol(
2567 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002568
2569 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2570 }
2571 else
2572 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002573 paramNodes = intermediate.growAggregate(
2574 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002575 }
2576 }
2577 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2578 *aggregateOut = paramNodes;
2579 setLoopNestingLevel(0);
2580}
2581
Jamie Madillb98c3a82015-07-23 14:26:04 -04002582TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002583{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002584 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002585 // We don't know at this point whether this is a function definition or a prototype.
2586 // The definition production code will check for redefinitions.
2587 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002588 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002589 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2590 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002591 //
2592 TFunction *prevDec =
2593 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302594
Martin Radevda6254b2016-12-14 17:00:36 +02002595 if (getShaderVersion() >= 300 &&
2596 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2597 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302598 {
Martin Radevda6254b2016-12-14 17:00:36 +02002599 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302600 // Therefore overloading or redefining builtin functions is an error.
2601 error(location, "Name of a built-in function cannot be redeclared as function",
2602 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302603 }
2604 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002605 {
2606 if (prevDec->getReturnType() != function->getReturnType())
2607 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002608 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002609 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002610 }
2611 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2612 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002613 if (prevDec->getParam(i).type->getQualifier() !=
2614 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002615 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002616 error(location,
2617 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002618 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002619 }
2620 }
2621 }
2622
2623 //
2624 // Check for previously declared variables using the same name.
2625 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002626 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002627 if (prevSym)
2628 {
2629 if (!prevSym->isFunction())
2630 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002631 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002632 }
2633 }
2634 else
2635 {
2636 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002637 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002638 }
2639
2640 // We're at the inner scope level of the function's arguments and body statement.
2641 // Add the function prototype to the surrounding scope instead.
2642 symbolTable.getOuterLevel()->insert(function);
2643
2644 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002645 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2646 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002647 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2648 //
2649 return function;
2650}
2651
Olli Etuaho9de84a52016-06-14 17:36:01 +03002652TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2653 const TString *name,
2654 const TSourceLoc &location)
2655{
2656 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2657 {
2658 error(location, "no qualifiers allowed for function return",
2659 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002660 }
2661 if (!type.layoutQualifier.isEmpty())
2662 {
2663 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002664 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002665 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002666 checkIsNotSampler(location, type.typeSpecifierNonArray,
2667 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002668 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002669 if (mShaderVersion < 300)
2670 {
2671 // Array return values are forbidden, but there's also no valid syntax for declaring array
2672 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002673 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002674
2675 if (type.isStructureContainingArrays())
2676 {
2677 // ESSL 1.00.17 section 6.1 Function Definitions
2678 error(location, "structures containing arrays can't be function return values",
2679 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002680 }
2681 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002682
2683 // Add the function as a prototype after parsing it (we do not support recursion)
2684 return new TFunction(name, new TType(type));
2685}
2686
Jamie Madill06145232015-05-13 13:10:01 -04002687TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002688{
Jamie Madill06145232015-05-13 13:10:01 -04002689 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002690 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002691 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002692 error(publicType.getLine(), "constructor can't be a structure definition",
2693 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002694 }
2695
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002696 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002697 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002698 {
2699 op = EOpConstructStruct;
2700 }
2701 else
2702 {
Geoff Lang156d7192016-07-21 16:11:00 -04002703 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002704 if (op == EOpNull)
2705 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002706 error(publicType.getLine(), "cannot construct this type",
2707 getBasicString(publicType.getBasicType()));
2708 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002709 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002710 }
2711 }
2712
2713 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002714 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002715 return new TFunction(&tempString, type, op);
2716}
2717
Jamie Madillb98c3a82015-07-23 14:26:04 -04002718// This function is used to test for the correctness of the parameters passed to various constructor
2719// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720//
Olli Etuaho856c4972016-08-08 11:38:39 +03002721// 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 +00002722//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002723TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002724 TOperator op,
2725 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302726 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002727{
Olli Etuaho856c4972016-08-08 11:38:39 +03002728 TType type = fnCall->getReturnType();
2729 if (type.isUnsizedArray())
2730 {
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002731 if (fnCall->getParamCount() == 0)
2732 {
2733 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2734 type.setArraySize(1u);
2735 return TIntermTyped::CreateZero(type);
2736 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002737 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2738 }
2739 bool constType = true;
2740 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2741 {
2742 const TConstParameter &param = fnCall->getParam(i);
2743 if (param.type->getQualifier() != EvqConst)
2744 constType = false;
2745 }
2746 if (constType)
2747 type.setQualifier(EvqConst);
2748
Olli Etuaho8a176262016-08-16 14:23:01 +03002749 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002750 {
2751 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2752 dummyNode->setType(type);
2753 return dummyNode;
2754 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002755 TIntermAggregate *constructor = arguments->getAsAggregate();
2756 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002757
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002758 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002759 constructor->setOp(op);
2760 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002761 ASSERT(constructor->isConstructor());
2762
2763 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002764 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002765
Olli Etuaho21203702014-11-13 16:16:21 +02002766 // Structs should not be precision qualified, the individual members may be.
2767 // Built-in types on the other hand should be precision qualified.
2768 if (op != EOpConstructStruct)
2769 {
2770 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002771 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002772 }
2773
Olli Etuaho856c4972016-08-08 11:38:39 +03002774 constructor->setType(type);
2775
Olli Etuaho77ba4082016-12-16 12:01:18 +00002776 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002777 if (constConstructor)
2778 {
2779 return constConstructor;
2780 }
2781
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002782 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002783}
2784
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002785//
2786// Interface/uniform blocks
2787//
Olli Etuaho13389b62016-10-16 11:48:18 +01002788TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002789 const TTypeQualifierBuilder &typeQualifierBuilder,
2790 const TSourceLoc &nameLine,
2791 const TString &blockName,
2792 TFieldList *fieldList,
2793 const TString *instanceName,
2794 const TSourceLoc &instanceLine,
2795 TIntermTyped *arrayIndex,
2796 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002797{
Olli Etuaho856c4972016-08-08 11:38:39 +03002798 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002799
Olli Etuaho77ba4082016-12-16 12:01:18 +00002800 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002801
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002802 if (typeQualifier.qualifier != EvqUniform)
2803 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002804 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
2805 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002806 }
2807
Martin Radev70866b82016-07-22 15:27:42 +03002808 if (typeQualifier.invariant)
2809 {
2810 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2811 }
2812
Martin Radev2cc85b32016-08-05 16:22:53 +03002813 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2814
Jamie Madill099c0f32013-06-20 11:55:52 -04002815 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002816 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002817
Jamie Madill099c0f32013-06-20 11:55:52 -04002818 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2819 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002820 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002821 }
2822
Jamie Madill1566ef72013-06-20 11:55:54 -04002823 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2824 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002825 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002826 }
2827
Olli Etuaho856c4972016-08-08 11:38:39 +03002828 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002829
Martin Radev2cc85b32016-08-05 16:22:53 +03002830 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2831
Arun Patole7e7e68d2015-05-22 12:02:25 +05302832 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2833 if (!symbolTable.declare(blockNameSymbol))
2834 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002835 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002836 }
2837
Jamie Madill98493dd2013-07-08 14:39:03 -04002838 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302839 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2840 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002841 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302842 TType *fieldType = field->type();
2843 if (IsSampler(fieldType->getBasicType()))
2844 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002845 error(field->line(),
2846 "unsupported type - sampler types are not allowed in interface blocks",
2847 fieldType->getBasicString());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002848 }
2849
Martin Radev2cc85b32016-08-05 16:22:53 +03002850 if (IsImage(fieldType->getBasicType()))
2851 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002852 error(field->line(),
2853 "unsupported type - image types are not allowed in interface blocks",
2854 fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002855 }
2856
Jamie Madill98493dd2013-07-08 14:39:03 -04002857 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002858 switch (qualifier)
2859 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002860 case EvqGlobal:
2861 case EvqUniform:
2862 break;
2863 default:
2864 error(field->line(), "invalid qualifier on interface block member",
2865 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002866 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002867 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002868
Martin Radev70866b82016-07-22 15:27:42 +03002869 if (fieldType->isInvariant())
2870 {
2871 error(field->line(), "invalid qualifier on interface block member", "invariant");
2872 }
2873
Jamie Madilla5efff92013-06-06 11:56:47 -04002874 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002875 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002876 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002877
Jamie Madill98493dd2013-07-08 14:39:03 -04002878 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002879 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002880 error(field->line(), "invalid layout qualifier: cannot be used here",
2881 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04002882 }
2883
Jamie Madill98493dd2013-07-08 14:39:03 -04002884 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002885 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002886 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002887 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002888 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002889 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002890 warning(field->line(),
2891 "extraneous layout qualifier: only has an effect on matrix types",
2892 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04002893 }
2894
Jamie Madill98493dd2013-07-08 14:39:03 -04002895 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002896 }
2897
Jamie Madill98493dd2013-07-08 14:39:03 -04002898 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002899 unsigned int arraySize = 0;
2900 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002901 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002902 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002903 }
2904
Jamie Madillb98c3a82015-07-23 14:26:04 -04002905 TInterfaceBlock *interfaceBlock =
2906 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2907 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2908 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002909
2910 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002911 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002912
Jamie Madill98493dd2013-07-08 14:39:03 -04002913 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002914 {
2915 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002916 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2917 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002918 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302919 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002920
2921 // set parent pointer of the field variable
2922 fieldType->setInterfaceBlock(interfaceBlock);
2923
Arun Patole7e7e68d2015-05-22 12:02:25 +05302924 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002925 fieldVariable->setQualifier(typeQualifier.qualifier);
2926
Arun Patole7e7e68d2015-05-22 12:02:25 +05302927 if (!symbolTable.declare(fieldVariable))
2928 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002929 error(field->line(), "redefinition of an interface block member name",
2930 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002931 }
2932 }
2933 }
2934 else
2935 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002936 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002937
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002938 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302939 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002940 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002941
Arun Patole7e7e68d2015-05-22 12:02:25 +05302942 if (!symbolTable.declare(instanceTypeDef))
2943 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002944 error(instanceLine, "redefinition of an interface block instance name",
2945 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002946 }
2947
Jamie Madillb98c3a82015-07-23 14:26:04 -04002948 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002949 symbolName = instanceTypeDef->getName();
2950 }
2951
Olli Etuaho13389b62016-10-16 11:48:18 +01002952 TIntermSymbol *blockSymbol =
2953 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2954 TIntermDeclaration *declaration = new TIntermDeclaration();
2955 declaration->appendDeclarator(blockSymbol);
2956 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002957
2958 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002959 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002960}
2961
Olli Etuaho383b7912016-08-05 11:22:59 +03002962void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002963{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002964 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002965
2966 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00002967 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302968 if (mStructNestingLevel > 1)
2969 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002970 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002971 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002972}
2973
2974void TParseContext::exitStructDeclaration()
2975{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002976 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002977}
2978
Olli Etuaho8a176262016-08-16 14:23:01 +03002979void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002980{
Jamie Madillacb4b812016-11-07 13:50:29 -05002981 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302982 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002983 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002984 }
2985
Arun Patole7e7e68d2015-05-22 12:02:25 +05302986 if (field.type()->getBasicType() != EbtStruct)
2987 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002988 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002989 }
2990
2991 // We're already inside a structure definition at this point, so add
2992 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302993 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2994 {
Jamie Madill41a49272014-03-18 16:10:13 -04002995 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002996 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2997 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002998 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00002999 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003000 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003001 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003002}
3003
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003004//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003005// Parse an array index expression
3006//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003007TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3008 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303009 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003010{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003011 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3012 {
3013 if (baseExpression->getAsSymbolNode())
3014 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303015 error(location, " left of '[' is not of type array, matrix, or vector ",
3016 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003017 }
3018 else
3019 {
3020 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3021 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003022
3023 TConstantUnion *unionArray = new TConstantUnion[1];
3024 unionArray->setFConst(0.0f);
3025 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
3026 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003027 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003028
Jamie Madill21c1e452014-12-29 11:33:41 -05003029 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3030
Olli Etuaho36b05142015-11-12 13:10:42 +02003031 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3032 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3033 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3034 // index is a constant expression.
3035 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3036 {
3037 if (baseExpression->isInterfaceBlock())
3038 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003039 error(location,
3040 "array indexes for interface blocks arrays must be constant integral expressions",
3041 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003042 }
3043 else if (baseExpression->getQualifier() == EvqFragmentOut)
3044 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003045 error(location,
3046 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003047 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003048 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3049 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003050 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003051 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003052 }
3053
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003054 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003055 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003056 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3057 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3058 // constant fold expressions that are not constant expressions). The most compatible way to
3059 // handle this case is to report a warning instead of an error and force the index to be in
3060 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003061 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003062 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003063
3064 int safeIndex = -1;
3065
3066 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003067 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003068 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003069 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003070 if (mShaderSpec == SH_WEBGL2_SPEC)
3071 {
3072 // Error has been already generated if index is not const.
3073 if (indexExpression->getQualifier() == EvqConst)
3074 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003075 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003076 }
3077 safeIndex = 0;
3078 }
3079 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3080 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003081 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003082 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003083 "GL_EXT_draw_buffers is disabled",
3084 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003085 safeIndex = 0;
3086 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003087 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003088 // Only do generic out-of-range check if similar error hasn't already been reported.
3089 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003090 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003091 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3092 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003093 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003094 }
3095 }
3096 else if (baseExpression->isMatrix())
3097 {
3098 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003099 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003100 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003101 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003102 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003103 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003104 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3105 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003106 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003107 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003108
3109 ASSERT(safeIndex >= 0);
3110 // Data of constant unions can't be changed, because it may be shared with other
3111 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3112 // sanitized object.
3113 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003114 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003115 TConstantUnion *safeConstantUnion = new TConstantUnion();
3116 safeConstantUnion->setIConst(safeIndex);
3117 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003118 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003119
3120 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003121 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003122 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003123 else
3124 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003125 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003126 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003127 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003128}
3129
Olli Etuaho90892fb2016-07-14 14:44:51 +03003130int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3131 const TSourceLoc &location,
3132 int index,
3133 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003134 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003135{
3136 if (index >= arraySize || index < 0)
3137 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003138 std::stringstream reasonStream;
3139 reasonStream << reason << " '" << index << "'";
3140 std::string token = reasonStream.str();
3141 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003142 if (index < 0)
3143 {
3144 return 0;
3145 }
3146 else
3147 {
3148 return arraySize - 1;
3149 }
3150 }
3151 return index;
3152}
3153
Jamie Madillb98c3a82015-07-23 14:26:04 -04003154TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3155 const TSourceLoc &dotLocation,
3156 const TString &fieldString,
3157 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003158{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003159 if (baseExpression->isArray())
3160 {
3161 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003162 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003163 }
3164
3165 if (baseExpression->isVector())
3166 {
3167 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003168 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3169 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003170 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003171 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003172 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003173 }
3174
Olli Etuahob6fa0432016-09-28 16:28:05 +01003175 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003176 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003177 else if (baseExpression->getBasicType() == EbtStruct)
3178 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303179 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003180 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003181 {
3182 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003183 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003184 }
3185 else
3186 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003187 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003188 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003189 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003190 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003191 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003192 {
3193 fieldFound = true;
3194 break;
3195 }
3196 }
3197 if (fieldFound)
3198 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003199 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3200 index->setLine(fieldLocation);
3201 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003202 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003203 }
3204 else
3205 {
3206 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003207 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003208 }
3209 }
3210 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003211 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003212 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303213 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003214 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003215 {
3216 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003217 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003218 }
3219 else
3220 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003221 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003222 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003223 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003224 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003225 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003226 {
3227 fieldFound = true;
3228 break;
3229 }
3230 }
3231 if (fieldFound)
3232 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003233 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3234 index->setLine(fieldLocation);
3235 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003236 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003237 }
3238 else
3239 {
3240 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003241 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003242 }
3243 }
3244 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003245 else
3246 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003247 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003248 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003249 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303250 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003251 }
3252 else
3253 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303254 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003255 " field selection requires structure, vector, or interface block on left hand "
3256 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303257 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003258 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003259 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003260 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003261}
3262
Jamie Madillb98c3a82015-07-23 14:26:04 -04003263TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3264 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003265{
Martin Radev802abe02016-08-04 17:48:32 +03003266 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003267
3268 if (qualifierType == "shared")
3269 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003270 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003271 {
3272 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3273 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003274 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003275 }
3276 else if (qualifierType == "packed")
3277 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003278 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003279 {
3280 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3281 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003282 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003283 }
3284 else if (qualifierType == "std140")
3285 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003286 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003287 }
3288 else if (qualifierType == "row_major")
3289 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003290 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003291 }
3292 else if (qualifierType == "column_major")
3293 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003294 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003295 }
3296 else if (qualifierType == "location")
3297 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003298 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3299 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003300 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003301 else if (qualifierType == "rgba32f")
3302 {
3303 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3304 qualifier.imageInternalFormat = EiifRGBA32F;
3305 }
3306 else if (qualifierType == "rgba16f")
3307 {
3308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3309 qualifier.imageInternalFormat = EiifRGBA16F;
3310 }
3311 else if (qualifierType == "r32f")
3312 {
3313 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3314 qualifier.imageInternalFormat = EiifR32F;
3315 }
3316 else if (qualifierType == "rgba8")
3317 {
3318 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3319 qualifier.imageInternalFormat = EiifRGBA8;
3320 }
3321 else if (qualifierType == "rgba8_snorm")
3322 {
3323 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3324 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3325 }
3326 else if (qualifierType == "rgba32i")
3327 {
3328 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3329 qualifier.imageInternalFormat = EiifRGBA32I;
3330 }
3331 else if (qualifierType == "rgba16i")
3332 {
3333 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3334 qualifier.imageInternalFormat = EiifRGBA16I;
3335 }
3336 else if (qualifierType == "rgba8i")
3337 {
3338 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3339 qualifier.imageInternalFormat = EiifRGBA8I;
3340 }
3341 else if (qualifierType == "r32i")
3342 {
3343 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3344 qualifier.imageInternalFormat = EiifR32I;
3345 }
3346 else if (qualifierType == "rgba32ui")
3347 {
3348 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3349 qualifier.imageInternalFormat = EiifRGBA32UI;
3350 }
3351 else if (qualifierType == "rgba16ui")
3352 {
3353 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3354 qualifier.imageInternalFormat = EiifRGBA16UI;
3355 }
3356 else if (qualifierType == "rgba8ui")
3357 {
3358 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3359 qualifier.imageInternalFormat = EiifRGBA8UI;
3360 }
3361 else if (qualifierType == "r32ui")
3362 {
3363 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3364 qualifier.imageInternalFormat = EiifR32UI;
3365 }
3366
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003367 else
3368 {
3369 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003370 }
3371
Jamie Madilla5efff92013-06-06 11:56:47 -04003372 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003373}
3374
Martin Radev802abe02016-08-04 17:48:32 +03003375void TParseContext::parseLocalSize(const TString &qualifierType,
3376 const TSourceLoc &qualifierTypeLine,
3377 int intValue,
3378 const TSourceLoc &intValueLine,
3379 const std::string &intValueString,
3380 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003381 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003382{
Olli Etuaho856c4972016-08-08 11:38:39 +03003383 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003384 if (intValue < 1)
3385 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003386 std::stringstream reasonStream;
3387 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3388 std::string reason = reasonStream.str();
3389 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003390 }
3391 (*localSize)[index] = intValue;
3392}
3393
Olli Etuaho09b04a22016-12-15 13:30:26 +00003394void TParseContext::parseNumViews(int intValue,
3395 const TSourceLoc &intValueLine,
3396 const std::string &intValueString,
3397 int *numViews)
3398{
3399 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3400 // specification.
3401 if (intValue < 1)
3402 {
3403 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3404 }
3405 *numViews = intValue;
3406}
3407
Jamie Madillb98c3a82015-07-23 14:26:04 -04003408TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3409 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003410 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303411 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003412{
Martin Radev802abe02016-08-04 17:48:32 +03003413 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003414
Martin Radev802abe02016-08-04 17:48:32 +03003415 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003416
Martin Radev802abe02016-08-04 17:48:32 +03003417 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003418 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003419 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003420 if (intValue < 0)
3421 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003422 error(intValueLine, "out of range: location must be non-negative",
3423 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003424 }
3425 else
3426 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003427 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003428 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003429 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003430 }
Martin Radev802abe02016-08-04 17:48:32 +03003431 else if (qualifierType == "local_size_x")
3432 {
3433 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3434 &qualifier.localSize);
3435 }
3436 else if (qualifierType == "local_size_y")
3437 {
3438 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3439 &qualifier.localSize);
3440 }
3441 else if (qualifierType == "local_size_z")
3442 {
3443 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3444 &qualifier.localSize);
3445 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00003446 else if (qualifierType == "num_views" && mMultiviewAvailable &&
3447 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
3448 mShaderType == GL_VERTEX_SHADER)
3449 {
3450 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3451 }
Martin Radev802abe02016-08-04 17:48:32 +03003452 else
3453 {
3454 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003455 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003456
Jamie Madilla5efff92013-06-06 11:56:47 -04003457 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003458}
3459
Olli Etuaho613b9592016-09-05 12:05:53 +03003460TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3461{
3462 return new TTypeQualifierBuilder(
3463 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3464 mShaderVersion);
3465}
3466
Jamie Madillb98c3a82015-07-23 14:26:04 -04003467TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003468 TLayoutQualifier rightQualifier,
3469 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003470{
Martin Radevc28888b2016-07-22 15:27:42 +03003471 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003472 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003473}
3474
Olli Etuaho4de340a2016-12-16 09:32:03 +00003475TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3476 const TFieldList *newlyAddedFields,
3477 const TSourceLoc &location)
3478{
3479 for (TField *field : *newlyAddedFields)
3480 {
3481 for (TField *oldField : *processedFields)
3482 {
3483 if (oldField->name() == field->name())
3484 {
3485 error(location, "duplicate field name in structure", field->name().c_str());
3486 }
3487 }
3488 processedFields->push_back(field);
3489 }
3490 return processedFields;
3491}
3492
Martin Radev70866b82016-07-22 15:27:42 +03003493TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3494 const TTypeQualifierBuilder &typeQualifierBuilder,
3495 TPublicType *typeSpecifier,
3496 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003497{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003498 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003499
Martin Radev70866b82016-07-22 15:27:42 +03003500 typeSpecifier->qualifier = typeQualifier.qualifier;
3501 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003502 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003503 typeSpecifier->invariant = typeQualifier.invariant;
3504 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303505 {
Martin Radev70866b82016-07-22 15:27:42 +03003506 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003507 }
Martin Radev70866b82016-07-22 15:27:42 +03003508 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003509}
3510
Jamie Madillb98c3a82015-07-23 14:26:04 -04003511TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3512 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003513{
Martin Radev4a9cd802016-09-01 16:51:51 +03003514 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3515 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003516
Martin Radev4a9cd802016-09-01 16:51:51 +03003517 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003518
Martin Radev4a9cd802016-09-01 16:51:51 +03003519 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003520
Arun Patole7e7e68d2015-05-22 12:02:25 +05303521 for (unsigned int i = 0; i < fieldList->size(); ++i)
3522 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003523 //
3524 // Careful not to replace already known aspects of type, like array-ness
3525 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303526 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003527 type->setBasicType(typeSpecifier.getBasicType());
3528 type->setPrimarySize(typeSpecifier.getPrimarySize());
3529 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003530 type->setPrecision(typeSpecifier.precision);
3531 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003532 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003533 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003534 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003535
3536 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303537 if (type->isArray())
3538 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003539 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003540 }
3541 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003542 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003543 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303544 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003545 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003546 }
3547
Martin Radev4a9cd802016-09-01 16:51:51 +03003548 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003549 }
3550
Jamie Madill98493dd2013-07-08 14:39:03 -04003551 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003552}
3553
Martin Radev4a9cd802016-09-01 16:51:51 +03003554TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3555 const TSourceLoc &nameLine,
3556 const TString *structName,
3557 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003558{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303559 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003560 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003561
Jamie Madill9b820842015-02-12 10:40:10 -05003562 // Store a bool in the struct if we're at global scope, to allow us to
3563 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003564 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003565
Jamie Madill98493dd2013-07-08 14:39:03 -04003566 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003567 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003568 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303569 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3570 if (!symbolTable.declare(userTypeDef))
3571 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003572 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003573 }
3574 }
3575
3576 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003577 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003578 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003579 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003580 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003581 switch (qualifier)
3582 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003583 case EvqGlobal:
3584 case EvqTemporary:
3585 break;
3586 default:
3587 error(field.line(), "invalid qualifier on struct member",
3588 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003589 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003590 }
Martin Radev70866b82016-07-22 15:27:42 +03003591 if (field.type()->isInvariant())
3592 {
3593 error(field.line(), "invalid qualifier on struct member", "invariant");
3594 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003595 if (IsImage(field.type()->getBasicType()))
3596 {
3597 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3598 }
3599
3600 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003601
3602 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003603 }
3604
Martin Radev4a9cd802016-09-01 16:51:51 +03003605 TTypeSpecifierNonArray typeSpecifierNonArray;
3606 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3607 typeSpecifierNonArray.userDef = structureType;
3608 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003609 exitStructDeclaration();
3610
Martin Radev4a9cd802016-09-01 16:51:51 +03003611 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003612}
3613
Jamie Madillb98c3a82015-07-23 14:26:04 -04003614TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003615 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003616 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003617{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003618 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003619 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003620 init->isVector())
3621 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003622 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3623 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003624 return nullptr;
3625 }
3626
Olli Etuahoac5274d2015-02-20 10:19:08 +02003627 if (statementList)
3628 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00003629 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02003630 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003631 return nullptr;
3632 }
3633 }
3634
Olli Etuahoa3a36662015-02-17 13:46:51 +02003635 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3636 if (node == nullptr)
3637 {
3638 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003639 return nullptr;
3640 }
3641 return node;
3642}
3643
3644TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3645{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003646 if (mSwitchNestingLevel == 0)
3647 {
3648 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003649 return nullptr;
3650 }
3651 if (condition == nullptr)
3652 {
3653 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003654 return nullptr;
3655 }
3656 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003657 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003658 {
3659 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003660 }
3661 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003662 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3663 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3664 // fold in case labels.
3665 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003666 {
3667 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003668 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003669 TIntermCase *node = intermediate.addCase(condition, loc);
3670 if (node == nullptr)
3671 {
3672 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003673 return nullptr;
3674 }
3675 return node;
3676}
3677
3678TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3679{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003680 if (mSwitchNestingLevel == 0)
3681 {
3682 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003683 return nullptr;
3684 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003685 TIntermCase *node = intermediate.addCase(nullptr, loc);
3686 if (node == nullptr)
3687 {
3688 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003689 return nullptr;
3690 }
3691 return node;
3692}
3693
Jamie Madillb98c3a82015-07-23 14:26:04 -04003694TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3695 TIntermTyped *child,
3696 const TSourceLoc &loc,
3697 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003698{
3699 if (child == nullptr)
3700 {
3701 return nullptr;
3702 }
3703
3704 switch (op)
3705 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003706 case EOpLogicalNot:
3707 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3708 child->isVector())
3709 {
3710 return nullptr;
3711 }
3712 break;
3713 case EOpBitwiseNot:
3714 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3715 child->isMatrix() || child->isArray())
3716 {
3717 return nullptr;
3718 }
3719 break;
3720 case EOpPostIncrement:
3721 case EOpPreIncrement:
3722 case EOpPostDecrement:
3723 case EOpPreDecrement:
3724 case EOpNegative:
3725 case EOpPositive:
3726 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003727 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003728 {
3729 return nullptr;
3730 }
3731 // Operators for built-ins are already type checked against their prototype.
3732 default:
3733 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003734 }
3735
Olli Etuahof119a262016-08-19 15:54:22 +03003736 TIntermUnary *node = new TIntermUnary(op, child);
3737 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003738
Olli Etuaho77ba4082016-12-16 12:01:18 +00003739 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03003740 if (foldedNode)
3741 return foldedNode;
3742
3743 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003744}
3745
Olli Etuaho09b22472015-02-11 11:47:26 +02003746TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3747{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003748 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003749 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003750 {
3751 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003752 return child;
3753 }
3754 return node;
3755}
3756
Jamie Madillb98c3a82015-07-23 14:26:04 -04003757TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3758 TIntermTyped *child,
3759 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003760{
Olli Etuaho856c4972016-08-08 11:38:39 +03003761 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003762 return addUnaryMath(op, child, loc);
3763}
3764
Jamie Madillb98c3a82015-07-23 14:26:04 -04003765bool TParseContext::binaryOpCommonCheck(TOperator op,
3766 TIntermTyped *left,
3767 TIntermTyped *right,
3768 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003769{
Olli Etuaho244be012016-08-18 15:26:02 +03003770 if (left->getType().getStruct() || right->getType().getStruct())
3771 {
3772 switch (op)
3773 {
3774 case EOpIndexDirectStruct:
3775 ASSERT(left->getType().getStruct());
3776 break;
3777 case EOpEqual:
3778 case EOpNotEqual:
3779 case EOpAssign:
3780 case EOpInitialize:
3781 if (left->getType() != right->getType())
3782 {
3783 return false;
3784 }
3785 break;
3786 default:
3787 error(loc, "Invalid operation for structs", GetOperatorString(op));
3788 return false;
3789 }
3790 }
3791
Olli Etuahod6b14282015-03-17 14:31:35 +02003792 if (left->isArray() || right->isArray())
3793 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003794 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003795 {
3796 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3797 return false;
3798 }
3799
3800 if (left->isArray() != right->isArray())
3801 {
3802 error(loc, "array / non-array mismatch", GetOperatorString(op));
3803 return false;
3804 }
3805
3806 switch (op)
3807 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003808 case EOpEqual:
3809 case EOpNotEqual:
3810 case EOpAssign:
3811 case EOpInitialize:
3812 break;
3813 default:
3814 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3815 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003816 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003817 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003818 if (left->getArraySize() != right->getArraySize())
3819 {
3820 error(loc, "array size mismatch", GetOperatorString(op));
3821 return false;
3822 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003823 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003824
3825 // Check ops which require integer / ivec parameters
3826 bool isBitShift = false;
3827 switch (op)
3828 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003829 case EOpBitShiftLeft:
3830 case EOpBitShiftRight:
3831 case EOpBitShiftLeftAssign:
3832 case EOpBitShiftRightAssign:
3833 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3834 // check that the basic type is an integer type.
3835 isBitShift = true;
3836 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3837 {
3838 return false;
3839 }
3840 break;
3841 case EOpBitwiseAnd:
3842 case EOpBitwiseXor:
3843 case EOpBitwiseOr:
3844 case EOpBitwiseAndAssign:
3845 case EOpBitwiseXorAssign:
3846 case EOpBitwiseOrAssign:
3847 // It is enough to check the type of only one operand, since later it
3848 // is checked that the operand types match.
3849 if (!IsInteger(left->getBasicType()))
3850 {
3851 return false;
3852 }
3853 break;
3854 default:
3855 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003856 }
3857
3858 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3859 // So the basic type should usually match.
3860 if (!isBitShift && left->getBasicType() != right->getBasicType())
3861 {
3862 return false;
3863 }
3864
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003865 // Check that:
3866 // 1. Type sizes match exactly on ops that require that.
3867 // 2. Restrictions for structs that contain arrays or samplers are respected.
3868 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003869 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003870 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003871 case EOpAssign:
3872 case EOpInitialize:
3873 case EOpEqual:
3874 case EOpNotEqual:
3875 // ESSL 1.00 sections 5.7, 5.8, 5.9
3876 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3877 {
3878 error(loc, "undefined operation for structs containing arrays",
3879 GetOperatorString(op));
3880 return false;
3881 }
3882 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3883 // we interpret the spec so that this extends to structs containing samplers,
3884 // similarly to ESSL 1.00 spec.
3885 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3886 left->getType().isStructureContainingSamplers())
3887 {
3888 error(loc, "undefined operation for structs containing samplers",
3889 GetOperatorString(op));
3890 return false;
3891 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003892
3893 if ((op == EOpAssign || op == EOpInitialize) &&
3894 left->getType().isStructureContainingImages())
3895 {
3896 error(loc, "undefined operation for structs containing images",
3897 GetOperatorString(op));
3898 return false;
3899 }
Olli Etuahoe1805592017-01-02 16:41:20 +00003900 if ((left->getNominalSize() != right->getNominalSize()) ||
3901 (left->getSecondarySize() != right->getSecondarySize()))
3902 {
3903 error(loc, "dimension mismatch", GetOperatorString(op));
3904 return false;
3905 }
3906 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003907 case EOpLessThan:
3908 case EOpGreaterThan:
3909 case EOpLessThanEqual:
3910 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00003911 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003912 {
Olli Etuahoe1805592017-01-02 16:41:20 +00003913 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003914 return false;
3915 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003916 break;
3917 case EOpAdd:
3918 case EOpSub:
3919 case EOpDiv:
3920 case EOpIMod:
3921 case EOpBitShiftLeft:
3922 case EOpBitShiftRight:
3923 case EOpBitwiseAnd:
3924 case EOpBitwiseXor:
3925 case EOpBitwiseOr:
3926 case EOpAddAssign:
3927 case EOpSubAssign:
3928 case EOpDivAssign:
3929 case EOpIModAssign:
3930 case EOpBitShiftLeftAssign:
3931 case EOpBitShiftRightAssign:
3932 case EOpBitwiseAndAssign:
3933 case EOpBitwiseXorAssign:
3934 case EOpBitwiseOrAssign:
3935 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3936 {
3937 return false;
3938 }
3939
3940 // Are the sizes compatible?
3941 if (left->getNominalSize() != right->getNominalSize() ||
3942 left->getSecondarySize() != right->getSecondarySize())
3943 {
3944 // If the nominal sizes of operands do not match:
3945 // One of them must be a scalar.
3946 if (!left->isScalar() && !right->isScalar())
3947 return false;
3948
3949 // In the case of compound assignment other than multiply-assign,
3950 // the right side needs to be a scalar. Otherwise a vector/matrix
3951 // would be assigned to a scalar. A scalar can't be shifted by a
3952 // vector either.
3953 if (!right->isScalar() &&
3954 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3955 return false;
3956 }
3957 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003958 default:
3959 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003960 }
3961
Olli Etuahod6b14282015-03-17 14:31:35 +02003962 return true;
3963}
3964
Olli Etuaho1dded802016-08-18 18:13:13 +03003965bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3966 const TType &left,
3967 const TType &right)
3968{
3969 switch (op)
3970 {
3971 case EOpMul:
3972 case EOpMulAssign:
3973 return left.getNominalSize() == right.getNominalSize() &&
3974 left.getSecondarySize() == right.getSecondarySize();
3975 case EOpVectorTimesScalar:
3976 return true;
3977 case EOpVectorTimesScalarAssign:
3978 ASSERT(!left.isMatrix() && !right.isMatrix());
3979 return left.isVector() && !right.isVector();
3980 case EOpVectorTimesMatrix:
3981 return left.getNominalSize() == right.getRows();
3982 case EOpVectorTimesMatrixAssign:
3983 ASSERT(!left.isMatrix() && right.isMatrix());
3984 return left.isVector() && left.getNominalSize() == right.getRows() &&
3985 left.getNominalSize() == right.getCols();
3986 case EOpMatrixTimesVector:
3987 return left.getCols() == right.getNominalSize();
3988 case EOpMatrixTimesScalar:
3989 return true;
3990 case EOpMatrixTimesScalarAssign:
3991 ASSERT(left.isMatrix() && !right.isMatrix());
3992 return !right.isVector();
3993 case EOpMatrixTimesMatrix:
3994 return left.getCols() == right.getRows();
3995 case EOpMatrixTimesMatrixAssign:
3996 ASSERT(left.isMatrix() && right.isMatrix());
3997 // We need to check two things:
3998 // 1. The matrix multiplication step is valid.
3999 // 2. The result will have the same number of columns as the lvalue.
4000 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4001
4002 default:
4003 UNREACHABLE();
4004 return false;
4005 }
4006}
4007
Jamie Madillb98c3a82015-07-23 14:26:04 -04004008TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4009 TIntermTyped *left,
4010 TIntermTyped *right,
4011 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004012{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004013 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004014 return nullptr;
4015
Olli Etuahofc1806e2015-03-17 13:03:11 +02004016 switch (op)
4017 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004018 case EOpEqual:
4019 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004020 case EOpLessThan:
4021 case EOpGreaterThan:
4022 case EOpLessThanEqual:
4023 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004024 break;
4025 case EOpLogicalOr:
4026 case EOpLogicalXor:
4027 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004028 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4029 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004030 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004031 {
4032 return nullptr;
4033 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004034 // Basic types matching should have been already checked.
4035 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004036 break;
4037 case EOpAdd:
4038 case EOpSub:
4039 case EOpDiv:
4040 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004041 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4042 !right->getType().getStruct());
4043 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004044 {
4045 return nullptr;
4046 }
4047 break;
4048 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004049 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4050 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004051 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004052 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004053 {
4054 return nullptr;
4055 }
4056 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004057 default:
4058 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004059 }
4060
Olli Etuaho1dded802016-08-18 18:13:13 +03004061 if (op == EOpMul)
4062 {
4063 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4064 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4065 {
4066 return nullptr;
4067 }
4068 }
4069
Olli Etuaho3fdec912016-08-18 15:08:06 +03004070 TIntermBinary *node = new TIntermBinary(op, left, right);
4071 node->setLine(loc);
4072
Olli Etuaho3fdec912016-08-18 15:08:06 +03004073 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004074 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004075 if (foldedNode)
4076 return foldedNode;
4077
4078 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004079}
4080
Jamie Madillb98c3a82015-07-23 14:26:04 -04004081TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4082 TIntermTyped *left,
4083 TIntermTyped *right,
4084 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004085{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004086 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004087 if (node == 0)
4088 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004089 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4090 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004091 return left;
4092 }
4093 return node;
4094}
4095
Jamie Madillb98c3a82015-07-23 14:26:04 -04004096TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4097 TIntermTyped *left,
4098 TIntermTyped *right,
4099 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004100{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004101 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004102 if (node == 0)
4103 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004104 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4105 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004106 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004107 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004108 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4109 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004110 }
4111 return node;
4112}
4113
Olli Etuaho13389b62016-10-16 11:48:18 +01004114TIntermBinary *TParseContext::createAssign(TOperator op,
4115 TIntermTyped *left,
4116 TIntermTyped *right,
4117 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004118{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004119 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004120 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004121 if (op == EOpMulAssign)
4122 {
4123 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4124 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4125 {
4126 return nullptr;
4127 }
4128 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004129 TIntermBinary *node = new TIntermBinary(op, left, right);
4130 node->setLine(loc);
4131
Olli Etuaho3fdec912016-08-18 15:08:06 +03004132 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004133 }
4134 return nullptr;
4135}
4136
Jamie Madillb98c3a82015-07-23 14:26:04 -04004137TIntermTyped *TParseContext::addAssign(TOperator op,
4138 TIntermTyped *left,
4139 TIntermTyped *right,
4140 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004141{
4142 TIntermTyped *node = createAssign(op, left, right, loc);
4143 if (node == nullptr)
4144 {
4145 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004146 return left;
4147 }
4148 return node;
4149}
4150
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004151TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4152 TIntermTyped *right,
4153 const TSourceLoc &loc)
4154{
Corentin Wallez0d959252016-07-12 17:26:32 -04004155 // WebGL2 section 5.26, the following results in an error:
4156 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004157 if (mShaderSpec == SH_WEBGL2_SPEC &&
4158 (left->isArray() || left->getBasicType() == EbtVoid ||
4159 left->getType().isStructureContainingArrays() || right->isArray() ||
4160 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004161 {
4162 error(loc,
4163 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4164 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004165 }
4166
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004167 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004168}
4169
Olli Etuaho49300862015-02-20 14:54:49 +02004170TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4171{
4172 switch (op)
4173 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004174 case EOpContinue:
4175 if (mLoopNestingLevel <= 0)
4176 {
4177 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004178 }
4179 break;
4180 case EOpBreak:
4181 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4182 {
4183 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004184 }
4185 break;
4186 case EOpReturn:
4187 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4188 {
4189 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004190 }
4191 break;
4192 default:
4193 // No checks for discard
4194 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004195 }
4196 return intermediate.addBranch(op, loc);
4197}
4198
Jamie Madillb98c3a82015-07-23 14:26:04 -04004199TIntermBranch *TParseContext::addBranch(TOperator op,
4200 TIntermTyped *returnValue,
4201 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004202{
4203 ASSERT(op == EOpReturn);
4204 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004205 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004206 {
4207 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004208 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004209 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004210 {
4211 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004212 }
4213 return intermediate.addBranch(op, returnValue, loc);
4214}
4215
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004216void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4217{
4218 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004219 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004220 TIntermNode *offset = nullptr;
4221 TIntermSequence *arguments = functionCall->getSequence();
4222 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4223 name.compare(0, 16, "textureLodOffset") == 0 ||
4224 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4225 name.compare(0, 17, "textureGradOffset") == 0 ||
4226 name.compare(0, 21, "textureProjGradOffset") == 0)
4227 {
4228 offset = arguments->back();
4229 }
4230 else if (name.compare(0, 13, "textureOffset") == 0 ||
4231 name.compare(0, 17, "textureProjOffset") == 0)
4232 {
4233 // A bias parameter might follow the offset parameter.
4234 ASSERT(arguments->size() >= 3);
4235 offset = (*arguments)[2];
4236 }
4237 if (offset != nullptr)
4238 {
4239 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4240 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4241 {
4242 TString unmangledName = TFunction::unmangleName(name);
4243 error(functionCall->getLine(), "Texture offset must be a constant expression",
4244 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004245 }
4246 else
4247 {
4248 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4249 size_t size = offsetConstantUnion->getType().getObjectSize();
4250 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4251 for (size_t i = 0u; i < size; ++i)
4252 {
4253 int offsetValue = values[i].getIConst();
4254 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4255 {
4256 std::stringstream tokenStream;
4257 tokenStream << offsetValue;
4258 std::string token = tokenStream.str();
4259 error(offset->getLine(), "Texture offset value out of valid range",
4260 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004261 }
4262 }
4263 }
4264 }
4265}
4266
Martin Radev2cc85b32016-08-05 16:22:53 +03004267// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4268void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4269{
4270 ASSERT(!functionCall->isUserDefined());
4271 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4272
4273 if (name.compare(0, 5, "image") == 0)
4274 {
4275 TIntermSequence *arguments = functionCall->getSequence();
4276 TIntermNode *imageNode = (*arguments)[0];
4277 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4278
4279 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4280
4281 if (name.compare(5, 5, "Store") == 0)
4282 {
4283 if (memoryQualifier.readonly)
4284 {
4285 error(imageNode->getLine(),
4286 "'imageStore' cannot be used with images qualified as 'readonly'",
4287 imageSymbol->getSymbol().c_str());
4288 }
4289 }
4290 else if (name.compare(5, 4, "Load") == 0)
4291 {
4292 if (memoryQualifier.writeonly)
4293 {
4294 error(imageNode->getLine(),
4295 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4296 imageSymbol->getSymbol().c_str());
4297 }
4298 }
4299 }
4300}
4301
4302// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4303void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4304 const TFunction *functionDefinition,
4305 const TIntermAggregate *functionCall)
4306{
4307 ASSERT(functionCall->isUserDefined());
4308
4309 const TIntermSequence &arguments = *functionCall->getSequence();
4310
4311 ASSERT(functionDefinition->getParamCount() == arguments.size());
4312
4313 for (size_t i = 0; i < arguments.size(); ++i)
4314 {
4315 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4316 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4317 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4318
4319 if (IsImage(functionArgumentType.getBasicType()))
4320 {
4321 const TMemoryQualifier &functionArgumentMemoryQualifier =
4322 functionArgumentType.getMemoryQualifier();
4323 const TMemoryQualifier &functionParameterMemoryQualifier =
4324 functionParameterType.getMemoryQualifier();
4325 if (functionArgumentMemoryQualifier.readonly &&
4326 !functionParameterMemoryQualifier.readonly)
4327 {
4328 error(functionCall->getLine(),
4329 "Function call discards the 'readonly' qualifier from image",
4330 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4331 }
4332
4333 if (functionArgumentMemoryQualifier.writeonly &&
4334 !functionParameterMemoryQualifier.writeonly)
4335 {
4336 error(functionCall->getLine(),
4337 "Function call discards the 'writeonly' qualifier from image",
4338 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4339 }
Martin Radev049edfa2016-11-11 14:35:37 +02004340
4341 if (functionArgumentMemoryQualifier.coherent &&
4342 !functionParameterMemoryQualifier.coherent)
4343 {
4344 error(functionCall->getLine(),
4345 "Function call discards the 'coherent' qualifier from image",
4346 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4347 }
4348
4349 if (functionArgumentMemoryQualifier.volatileQualifier &&
4350 !functionParameterMemoryQualifier.volatileQualifier)
4351 {
4352 error(functionCall->getLine(),
4353 "Function call discards the 'volatile' qualifier from image",
4354 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4355 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004356 }
4357 }
4358}
4359
Jamie Madillb98c3a82015-07-23 14:26:04 -04004360TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4361 TIntermNode *paramNode,
4362 TIntermNode *thisNode,
4363 const TSourceLoc &loc,
4364 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004365{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004366 *fatalError = false;
4367 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004368 TIntermTyped *callNode = nullptr;
4369
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004370 if (thisNode != nullptr)
4371 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004372 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004373 int arraySize = 0;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004374 TIntermTyped *typedThis = thisNode->getAsTyped();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004375 if (fnCall->getName() != "length")
4376 {
4377 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004378 }
4379 else if (paramNode != nullptr)
4380 {
4381 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004382 }
4383 else if (typedThis == nullptr || !typedThis->isArray())
4384 {
4385 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004386 }
4387 else
4388 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004389 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004390 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004391 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004392 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004393 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004394 // (func()).length()
4395 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004396 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4397 // expression.
4398 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4399 // spec section 5.9 which allows "An array, vector or matrix expression with the
4400 // length method applied".
4401 error(loc, "length can only be called on array names, not on array expressions",
4402 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004403 }
4404 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004405 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004406 callNode =
4407 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004408 }
4409 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004410 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004411 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004412 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004413 }
4414 else
4415 {
4416 //
4417 // Not a constructor. Find it in the symbol table.
4418 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304419 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004420 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004421 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004422 if (fnCandidate)
4423 {
4424 //
4425 // A declared function.
4426 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004427 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004428 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004429 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004430 }
4431 op = fnCandidate->getBuiltInOp();
4432 if (builtIn && op != EOpNull)
4433 {
4434 //
4435 // A function call mapped to a built-in operation.
4436 //
4437 if (fnCandidate->getParamCount() == 1)
4438 {
4439 //
4440 // Treat it like a built-in unary operator.
4441 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004442 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4443 paramNode = paramAgg->getSequence()->front();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004444 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004445 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004446 if (callNode == nullptr)
4447 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004448 std::stringstream reasonStream;
4449 reasonStream << "wrong operand type for built in unary function: "
4450 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
4451 std::string reason = reasonStream.str();
4452 error(paramNode->getLine(), reason.c_str(), "Internal Error");
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004453 *fatalError = true;
4454 return nullptr;
4455 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004456 }
4457 else
4458 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004459 TIntermAggregate *aggregate =
4460 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004461 aggregate->setType(fnCandidate->getReturnType());
4462 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004463 if (aggregate->areChildrenConstQualified())
4464 {
4465 aggregate->getTypePointer()->setQualifier(EvqConst);
4466 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004467
4468 // Some built-in functions have out parameters too.
4469 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304470
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004471 // See if we can constant fold a built-in. Note that this may be possible even
4472 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004473 TIntermTyped *foldedNode =
Olli Etuaho77ba4082016-12-16 12:01:18 +00004474 intermediate.foldAggregateBuiltIn(aggregate, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304475 if (foldedNode)
4476 {
Arun Patole274f0702015-05-05 13:33:30 +05304477 callNode = foldedNode;
4478 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004479 else
4480 {
4481 callNode = aggregate;
4482 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004483 }
4484 }
4485 else
4486 {
4487 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004488 TIntermAggregate *aggregate =
4489 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004490 aggregate->setType(fnCandidate->getReturnType());
4491
Jamie Madillb98c3a82015-07-23 14:26:04 -04004492 // this is how we know whether the given function is a builtIn function or a user
4493 // defined function
4494 // if builtIn == false, it's a userDefined -> could be an overloaded
4495 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004496 // if builtIn == true, it's definitely a builtIn function with EOpNull
4497 if (!builtIn)
4498 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004499 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004500
Olli Etuahobd674552016-10-06 13:28:42 +01004501 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004502 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004503 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004504 aggregate->setBuiltInFunctionPrecision();
4505
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004506 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004507
4508 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4509 }
4510 else
4511 {
4512 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004513 }
4514
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004515 callNode = aggregate;
4516
4517 functionCallLValueErrorCheck(fnCandidate, aggregate);
4518 }
4519 }
4520 else
4521 {
4522 // error message was put out by findFunction()
4523 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004524 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004525 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004526 callNode = intermediate.addConstantUnion(unionArray,
4527 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004528 }
4529 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004530 return callNode;
4531}
4532
Jamie Madillb98c3a82015-07-23 14:26:04 -04004533TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004534 TIntermTyped *trueExpression,
4535 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004536 const TSourceLoc &loc)
4537{
Olli Etuaho856c4972016-08-08 11:38:39 +03004538 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004539
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004540 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004541 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004542 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4543 falseExpression->getCompleteString());
4544 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004545 }
Olli Etuahode318b22016-10-25 16:18:25 +01004546 if (IsOpaqueType(trueExpression->getBasicType()))
4547 {
4548 // ESSL 1.00 section 4.1.7
4549 // ESSL 3.00 section 4.1.7
4550 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4551 // Note that structs containing opaque types don't need to be checked as structs are
4552 // forbidden below.
4553 error(loc, "ternary operator is not allowed for opaque types", ":");
4554 return falseExpression;
4555 }
4556
Olli Etuahoa2d53032015-04-15 14:14:44 +03004557 // ESSL1 sections 5.2 and 5.7:
4558 // ESSL3 section 5.7:
4559 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004560 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004561 {
4562 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004563 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004564 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004565 // WebGL2 section 5.26, the following results in an error:
4566 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004567 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004568 {
4569 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004570 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004571 }
4572
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004573 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004574}
Olli Etuaho49300862015-02-20 14:54:49 +02004575
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004576//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004577// Parse an array of strings using yyparse.
4578//
4579// Returns 0 for success.
4580//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004581int PaParseStrings(size_t count,
4582 const char *const string[],
4583 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304584 TParseContext *context)
4585{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004586 if ((count == 0) || (string == NULL))
4587 return 1;
4588
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004589 if (glslang_initialize(context))
4590 return 1;
4591
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004592 int error = glslang_scan(count, string, length, context);
4593 if (!error)
4594 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004595
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004596 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004597
alokp@chromium.org6b495712012-06-29 00:06:58 +00004598 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004599}
Jamie Madill45bcc782016-11-07 13:58:48 -05004600
4601} // namespace sh