blob: 52e766998250c33c69b7da6880a63cb20a47a2da [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,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800555 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300556 TOperator op,
557 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000558{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400560 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530561 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400562 case EOpConstructMat2:
563 case EOpConstructMat2x3:
564 case EOpConstructMat2x4:
565 case EOpConstructMat3x2:
566 case EOpConstructMat3:
567 case EOpConstructMat3x4:
568 case EOpConstructMat4x2:
569 case EOpConstructMat4x3:
570 case EOpConstructMat4:
571 constructingMatrix = true;
572 break;
573 default:
574 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000575 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 //
578 // Note: It's okay to have too many components available, but not okay to have unused
579 // arguments. 'full' will go to true when enough args have been seen. If we loop
580 // again, there is an extra argument, so 'overfull' will become true.
581 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000582
Jamie Madillb98c3a82015-07-23 14:26:04 -0400583 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400584 bool full = false;
585 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000586 bool matrixInMatrix = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500587 bool arrayArg = false;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800588 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530589 {
Olli Etuaho72d10202017-01-19 15:58:30 +0000590 const TIntermTyped *argTyped = arg->getAsTyped();
591 size += argTyped->getType().getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530592
Olli Etuaho72d10202017-01-19 15:58:30 +0000593 if (constructingMatrix && argTyped->getType().isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000594 matrixInMatrix = true;
595 if (full)
596 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300597 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 full = true;
Olli Etuaho72d10202017-01-19 15:58:30 +0000599 if (argTyped->getType().isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000600 arrayArg = true;
601 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530602
Olli Etuaho856c4972016-08-08 11:38:39 +0300603 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300604 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300605 // The size of an unsized constructor should already have been determined.
606 ASSERT(!type.isUnsizedArray());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800607 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300608 {
609 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300610 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300611 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000612 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000613
Arun Patole7e7e68d2015-05-22 12:02:25 +0530614 if (arrayArg && op != EOpConstructStruct)
615 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000616 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300617 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000618 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619
Olli Etuaho856c4972016-08-08 11:38:39 +0300620 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530621 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800622 if (arguments->size() != 1)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530623 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400624 error(line, "constructing matrix from matrix can only take one argument",
625 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300626 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000627 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000628 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629
Arun Patole7e7e68d2015-05-22 12:02:25 +0530630 if (overFull)
631 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000632 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300633 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000634 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530635
Olli Etuaho856c4972016-08-08 11:38:39 +0300636 if (op == EOpConstructStruct && !type.isArray() &&
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800637 type.getStruct()->fields().size() != arguments->size())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530638 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400639 error(line,
640 "Number of constructor parameters does not match the number of structure fields",
641 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300642 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644
Olli Etuaho856c4972016-08-08 11:38:39 +0300645 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530646 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300647 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
648 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530649 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000650 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300651 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000652 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000653 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800655 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200657 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300658 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000659 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200660
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800661 for (TIntermNode *const &argNode : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530662 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200663 TIntermTyped *argTyped = argNode->getAsTyped();
664 ASSERT(argTyped != nullptr);
665 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
666 {
667 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300668 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200669 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300670 if (op != EOpConstructStruct && IsImage(argTyped->getBasicType()))
671 {
672 error(line, "cannot convert an image", "constructor");
673 return false;
674 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200675 if (argTyped->getBasicType() == EbtVoid)
676 {
677 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300678 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200679 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681
Olli Etuaho856c4972016-08-08 11:38:39 +0300682 if (type.isArray())
683 {
684 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
685 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800686 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300687 {
688 const TType &argType = argNode->getAsTyped()->getType();
689 // It has already been checked that the argument is not an array.
690 ASSERT(!argType.isArray());
691 if (!argType.sameElementType(type))
692 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000693 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300694 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300695 }
696 }
697 }
698 else if (op == EOpConstructStruct)
699 {
700 const TFieldList &fields = type.getStruct()->fields();
Olli Etuaho856c4972016-08-08 11:38:39 +0300701
702 for (size_t i = 0; i < fields.size(); i++)
703 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800704 if (i >= arguments->size() ||
705 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300706 {
707 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000708 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300709 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300710 }
711 }
712 }
713
Olli Etuaho8a176262016-08-16 14:23:01 +0300714 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000715}
716
Jamie Madillb98c3a82015-07-23 14:26:04 -0400717// This function checks to see if a void variable has been declared and raise an error message for
718// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000719//
720// returns true in case of an error
721//
Olli Etuaho856c4972016-08-08 11:38:39 +0300722bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400723 const TString &identifier,
724 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300726 if (type == EbtVoid)
727 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000728 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300729 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300730 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000731
Olli Etuaho8a176262016-08-16 14:23:01 +0300732 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000733}
734
Jamie Madillb98c3a82015-07-23 14:26:04 -0400735// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300736// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300737void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530739 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
740 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000741 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530742 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743}
744
Jamie Madillb98c3a82015-07-23 14:26:04 -0400745// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300746// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300747void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000748{
Martin Radev4a9cd802016-09-01 16:51:51 +0300749 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530750 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000751 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530752 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753}
754
Olli Etuaho856c4972016-08-08 11:38:39 +0300755bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300756 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400757 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530759 if (pType.type == EbtStruct)
760 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300761 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530762 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000763 std::stringstream reasonStream;
764 reasonStream << reason << " (structure contains a sampler)";
765 std::string reasonStr = reasonStream.str();
766 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300767 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000768 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530769
Olli Etuaho8a176262016-08-16 14:23:01 +0300770 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530771 }
772 else if (IsSampler(pType.type))
773 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000774 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300775 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000776 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000777
Olli Etuaho8a176262016-08-16 14:23:01 +0300778 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779}
780
Martin Radev2cc85b32016-08-05 16:22:53 +0300781bool TParseContext::checkIsNotImage(const TSourceLoc &line,
782 const TTypeSpecifierNonArray &pType,
783 const char *reason)
784{
785 if (pType.type == EbtStruct)
786 {
787 if (ContainsImage(*pType.userDef))
788 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000789 std::stringstream reasonStream;
790 reasonStream << reason << " (structure contains an image)";
791 std::string reasonStr = reasonStream.str();
792 error(line, reasonStr.c_str(), getBasicString(pType.type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300793
794 return false;
795 }
796
797 return true;
798 }
799 else if (IsImage(pType.type))
800 {
801 error(line, reason, getBasicString(pType.type));
802
803 return false;
804 }
805
806 return true;
807}
808
Olli Etuaho856c4972016-08-08 11:38:39 +0300809void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
810 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400811{
812 if (pType.layoutQualifier.location != -1)
813 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400814 error(line, "location must only be specified for a single input or output variable",
815 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400816 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400817}
818
Olli Etuaho856c4972016-08-08 11:38:39 +0300819void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
820 const TLayoutQualifier &layoutQualifier)
821{
822 if (layoutQualifier.location != -1)
823 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000824 error(location, "invalid layout qualifier: only valid on program inputs and outputs",
825 "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300826 }
827}
828
Martin Radev2cc85b32016-08-05 16:22:53 +0300829void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
830 TQualifier qualifier,
831 const TType &type)
832{
833 checkOutParameterIsNotSampler(line, qualifier, type);
834 checkOutParameterIsNotImage(line, qualifier, type);
835}
836
Olli Etuaho856c4972016-08-08 11:38:39 +0300837void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
838 TQualifier qualifier,
839 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840{
Martin Radev2cc85b32016-08-05 16:22:53 +0300841 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
842 if (IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530843 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000844 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846}
847
Martin Radev2cc85b32016-08-05 16:22:53 +0300848void TParseContext::checkOutParameterIsNotImage(const TSourceLoc &line,
849 TQualifier qualifier,
850 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851{
Martin Radev2cc85b32016-08-05 16:22:53 +0300852 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
853 if (IsImage(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530854 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300855 error(line, "images cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000856 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300860unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530862 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000863
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200864 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
865 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
866 // fold as array size.
867 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000868 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000869 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300870 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000871 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872
Olli Etuaho856c4972016-08-08 11:38:39 +0300873 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400874
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000875 if (constant->getBasicType() == EbtUInt)
876 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300877 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000878 }
879 else
880 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300881 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000882
Olli Etuaho856c4972016-08-08 11:38:39 +0300883 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000884 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400885 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300886 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000887 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400888
Olli Etuaho856c4972016-08-08 11:38:39 +0300889 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400890 }
891
Olli Etuaho856c4972016-08-08 11:38:39 +0300892 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400893 {
894 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300895 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400896 }
897
898 // The size of arrays is restricted here to prevent issues further down the
899 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
900 // 4096 registers so this should be reasonable even for aggressively optimizable code.
901 const unsigned int sizeLimit = 65536;
902
Olli Etuaho856c4972016-08-08 11:38:39 +0300903 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400904 {
905 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300906 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000907 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300908
909 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910}
911
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300913bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
914 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915{
Olli Etuaho8a176262016-08-16 14:23:01 +0300916 if ((elementQualifier.qualifier == EvqAttribute) ||
917 (elementQualifier.qualifier == EvqVertexIn) ||
918 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300919 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400920 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300921 TType(elementQualifier).getQualifierString());
922 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000923 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924
Olli Etuaho8a176262016-08-16 14:23:01 +0300925 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926}
927
Olli Etuaho8a176262016-08-16 14:23:01 +0300928// See if this element type can be formed into an array.
929bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 //
932 // Can the type be an array?
933 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300934 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400935 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300936 error(line, "cannot declare arrays of arrays",
937 TType(elementType).getCompleteString().c_str());
938 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000939 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300940 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
941 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
942 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300943 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300944 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300945 {
946 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300947 TType(elementType).getCompleteString().c_str());
948 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300949 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950
Olli Etuaho8a176262016-08-16 14:23:01 +0300951 return true;
952}
953
954// Check if this qualified element type can be formed into an array.
955bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
956 const TPublicType &elementType)
957{
958 if (checkIsValidTypeForArray(indexLocation, elementType))
959 {
960 return checkIsValidQualifierForArray(indexLocation, elementType);
961 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000962 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963}
964
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300966void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
967 const TString &identifier,
968 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969{
Olli Etuaho3739d232015-04-08 12:23:44 +0300970 ASSERT(type != nullptr);
971 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000972 {
973 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300974 type->qualifier = EvqTemporary;
975
976 // Generate informative error messages for ESSL1.
977 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400978 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000979 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530980 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400981 "structures containing arrays may not be declared constant since they cannot be "
982 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530983 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000984 }
985 else
986 {
987 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
988 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300989 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000990 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300991 if (type->isUnsizedArray())
992 {
993 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300994 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995}
996
Olli Etuaho2935c582015-04-08 14:32:06 +0300997// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998// and update the symbol table.
999//
Olli Etuaho2935c582015-04-08 14:32:06 +03001000// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001002bool TParseContext::declareVariable(const TSourceLoc &line,
1003 const TString &identifier,
1004 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001005 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006{
Olli Etuaho2935c582015-04-08 14:32:06 +03001007 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008
Olli Etuaho856c4972016-08-08 11:38:39 +03001009 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010
Olli Etuaho2935c582015-04-08 14:32:06 +03001011 // gl_LastFragData may be redeclared with a new precision qualifier
1012 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1013 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001014 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1015 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001016 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001017 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001018 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001019 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001020 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001021 }
1022 }
1023 else
1024 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001025 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1026 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001027 return false;
1028 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001029 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030
Olli Etuaho8a176262016-08-16 14:23:01 +03001031 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001032 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033
Olli Etuaho2935c582015-04-08 14:32:06 +03001034 (*variable) = new TVariable(&identifier, type);
1035 if (!symbolTable.declare(*variable))
1036 {
1037 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001038 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001039 return false;
1040 }
1041
Olli Etuaho8a176262016-08-16 14:23:01 +03001042 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001043 return false;
1044
1045 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046}
1047
Martin Radev70866b82016-07-22 15:27:42 +03001048void TParseContext::checkIsParameterQualifierValid(
1049 const TSourceLoc &line,
1050 const TTypeQualifierBuilder &typeQualifierBuilder,
1051 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301052{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001053 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001054
1055 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301056 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001057 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1058 }
1059
1060 if (!IsImage(type->getBasicType()))
1061 {
1062 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, line);
1063 }
1064 else
1065 {
1066 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001067 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068
Martin Radev70866b82016-07-22 15:27:42 +03001069 type->setQualifier(typeQualifier.qualifier);
1070
1071 if (typeQualifier.precision != EbpUndefined)
1072 {
1073 type->setPrecision(typeQualifier.precision);
1074 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075}
1076
Olli Etuaho856c4972016-08-08 11:38:39 +03001077bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001078{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001079 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001080 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301081 if (iter == extBehavior.end())
1082 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001083 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001084 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001085 }
zmo@google.comf5450912011-09-09 01:37:19 +00001086 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301087 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1088 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001089 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1090 // associated with more than one extension.
1091 if (extension == "GL_OVR_multiview")
1092 {
1093 return checkCanUseExtension(line, "GL_OVR_multiview2");
1094 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001095 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001096 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001097 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301098 if (iter->second == EBhWarn)
1099 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001100 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001101 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
Olli Etuaho8a176262016-08-16 14:23:01 +03001104 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105}
1106
Martin Radevb8b01222016-11-20 23:25:53 +02001107void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1108 const TSourceLoc &location)
1109{
1110 if (publicType.isUnsizedArray())
1111 {
1112 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1113 // error. It is assumed that this applies to empty declarations as well.
1114 error(location, "empty array declaration needs to specify a size", "");
1115 }
1116 if (publicType.qualifier == EvqShared && !publicType.layoutQualifier.isEmpty())
1117 {
1118 error(location, "Shared memory declarations cannot have layout specified", "layout");
1119 }
1120}
1121
Jamie Madillb98c3a82015-07-23 14:26:04 -04001122// These checks are common for all declarations starting a declarator list, and declarators that
1123// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +03001124void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001125 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001126{
Olli Etuahofa33d582015-04-09 14:33:12 +03001127 switch (publicType.qualifier)
1128 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001129 case EvqVaryingIn:
1130 case EvqVaryingOut:
1131 case EvqAttribute:
1132 case EvqVertexIn:
1133 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001134 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001135 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001136 {
1137 error(identifierLocation, "cannot be used with a structure",
1138 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001139 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001140 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001141
Jamie Madillb98c3a82015-07-23 14:26:04 -04001142 default:
1143 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001144 }
1145
Jamie Madillb98c3a82015-07-23 14:26:04 -04001146 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001147 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1148 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001149 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001150 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001151 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001152 if (publicType.qualifier != EvqUniform &&
1153 !checkIsNotImage(identifierLocation, publicType.typeSpecifierNonArray,
1154 "images must be uniform"))
1155 {
1156 return;
1157 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001158
1159 // check for layout qualifier issues
1160 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1161
1162 if (layoutQualifier.matrixPacking != EmpUnspecified)
1163 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001164 error(identifierLocation, "layout qualifier only valid for interface blocks",
1165 getMatrixPackingString(layoutQualifier.matrixPacking));
Olli Etuaho383b7912016-08-05 11:22:59 +03001166 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001167 }
1168
1169 if (layoutQualifier.blockStorage != EbsUnspecified)
1170 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001171 error(identifierLocation, "layout qualifier only valid for interface blocks",
1172 getBlockStorageString(layoutQualifier.blockStorage));
Olli Etuaho383b7912016-08-05 11:22:59 +03001173 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001174 }
1175
Olli Etuaho383b7912016-08-05 11:22:59 +03001176 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001177 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001178 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001179 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001180
1181 if (IsImage(publicType.getBasicType()))
1182 {
1183
1184 switch (layoutQualifier.imageInternalFormat)
1185 {
1186 case EiifRGBA32F:
1187 case EiifRGBA16F:
1188 case EiifR32F:
1189 case EiifRGBA8:
1190 case EiifRGBA8_SNORM:
1191 if (!IsFloatImage(publicType.getBasicType()))
1192 {
1193 error(identifierLocation,
1194 "internal image format requires a floating image type",
1195 getBasicString(publicType.getBasicType()));
1196 return;
1197 }
1198 break;
1199 case EiifRGBA32I:
1200 case EiifRGBA16I:
1201 case EiifRGBA8I:
1202 case EiifR32I:
1203 if (!IsIntegerImage(publicType.getBasicType()))
1204 {
1205 error(identifierLocation,
1206 "internal image format requires an integer image type",
1207 getBasicString(publicType.getBasicType()));
1208 return;
1209 }
1210 break;
1211 case EiifRGBA32UI:
1212 case EiifRGBA16UI:
1213 case EiifRGBA8UI:
1214 case EiifR32UI:
1215 if (!IsUnsignedImage(publicType.getBasicType()))
1216 {
1217 error(identifierLocation,
1218 "internal image format requires an unsigned image type",
1219 getBasicString(publicType.getBasicType()));
1220 return;
1221 }
1222 break;
1223 case EiifUnspecified:
1224 error(identifierLocation, "layout qualifier", "No image internal format specified");
1225 return;
1226 default:
1227 error(identifierLocation, "layout qualifier", "unrecognized token");
1228 return;
1229 }
1230
1231 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1232 switch (layoutQualifier.imageInternalFormat)
1233 {
1234 case EiifR32F:
1235 case EiifR32I:
1236 case EiifR32UI:
1237 break;
1238 default:
1239 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1240 {
1241 error(identifierLocation, "layout qualifier",
1242 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1243 "image variables must be qualified readonly and/or writeonly");
1244 return;
1245 }
1246 break;
1247 }
1248 }
1249 else
1250 {
1251
1252 if (!checkInternalFormatIsNotSpecified(identifierLocation,
1253 layoutQualifier.imageInternalFormat))
1254 {
1255 return;
1256 }
1257
1258 if (!checkIsMemoryQualifierNotSpecified(publicType.memoryQualifier, identifierLocation))
1259 {
1260 return;
1261 }
1262 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001263}
1264
Olli Etuaho856c4972016-08-08 11:38:39 +03001265void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1266 const TString &layoutQualifierName,
1267 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001268{
1269
1270 if (mShaderVersion < versionRequired)
1271 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001272 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001273 }
1274}
1275
Olli Etuaho856c4972016-08-08 11:38:39 +03001276bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1277 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001278{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001279 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001280 for (size_t i = 0u; i < localSize.size(); ++i)
1281 {
1282 if (localSize[i] != -1)
1283 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001284 error(location,
1285 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1286 "global layout declaration",
1287 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001288 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001289 }
1290 }
1291
Olli Etuaho8a176262016-08-16 14:23:01 +03001292 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001293}
1294
Martin Radev2cc85b32016-08-05 16:22:53 +03001295bool TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
1296 TLayoutImageInternalFormat internalFormat)
1297{
1298 if (internalFormat != EiifUnspecified)
1299 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001300 error(location, "invalid layout qualifier: only valid when used with images",
1301 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001302 return false;
1303 }
1304 return true;
1305}
1306
Olli Etuaho383b7912016-08-05 11:22:59 +03001307void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001308 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001309{
1310 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1311 {
1312 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1313 if (qual == EvqOut || qual == EvqInOut)
1314 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001315 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001316 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001317 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001318 TString unmangledName =
1319 TFunction::unmangleName(fnCall->getFunctionSymbolInfo()->getName());
Olli Etuaho856c4972016-08-08 11:38:39 +03001320 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001321 "Constant value cannot be passed for 'out' or 'inout' parameters.",
1322 unmangledName.c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001323 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001324 }
1325 }
1326 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001327}
1328
Martin Radev70866b82016-07-22 15:27:42 +03001329void TParseContext::checkInvariantVariableQualifier(bool invariant,
1330 const TQualifier qualifier,
1331 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001332{
Martin Radev70866b82016-07-22 15:27:42 +03001333 if (!invariant)
1334 return;
1335
1336 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001337 {
Martin Radev70866b82016-07-22 15:27:42 +03001338 // input variables in the fragment shader can be also qualified as invariant
1339 if (!sh::CanBeInvariantESSL1(qualifier))
1340 {
1341 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1342 }
1343 }
1344 else
1345 {
1346 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1347 {
1348 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1349 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001350 }
1351}
1352
Arun Patole7e7e68d2015-05-22 12:02:25 +05301353bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001354{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001355 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001356 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1357 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001358}
1359
Arun Patole7e7e68d2015-05-22 12:02:25 +05301360bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001361{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001362 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001363}
1364
Jamie Madillb98c3a82015-07-23 14:26:04 -04001365void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1366 const char *extName,
1367 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001368{
1369 pp::SourceLocation srcLoc;
1370 srcLoc.file = loc.first_file;
1371 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001372 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001373}
1374
Jamie Madillb98c3a82015-07-23 14:26:04 -04001375void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1376 const char *name,
1377 const char *value,
1378 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001379{
1380 pp::SourceLocation srcLoc;
1381 srcLoc.file = loc.first_file;
1382 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001383 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001384}
1385
Martin Radev4c4c8e72016-08-04 12:25:34 +03001386sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001387{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001388 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001389 for (size_t i = 0u; i < result.size(); ++i)
1390 {
1391 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1392 {
1393 result[i] = 1;
1394 }
1395 else
1396 {
1397 result[i] = mComputeShaderLocalSize[i];
1398 }
1399 }
1400 return result;
1401}
1402
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001403/////////////////////////////////////////////////////////////////////////////////
1404//
1405// Non-Errors.
1406//
1407/////////////////////////////////////////////////////////////////////////////////
1408
Jamie Madill5c097022014-08-20 16:38:32 -04001409const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1410 const TString *name,
1411 const TSymbol *symbol)
1412{
1413 const TVariable *variable = NULL;
1414
1415 if (!symbol)
1416 {
1417 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001418 }
1419 else if (!symbol->isVariable())
1420 {
1421 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001422 }
1423 else
1424 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001425 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001426
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001427 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001428 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001429 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001430 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001431 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001432
1433 // Reject shaders using both gl_FragData and gl_FragColor
1434 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001435 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001436 {
1437 mUsesFragData = true;
1438 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001439 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001440 {
1441 mUsesFragColor = true;
1442 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001443 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1444 {
1445 mUsesSecondaryOutputs = true;
1446 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001447
1448 // This validation is not quite correct - it's only an error to write to
1449 // both FragData and FragColor. For simplicity, and because users shouldn't
1450 // be rewarded for reading from undefined varaibles, return an error
1451 // if they are both referenced, rather than assigned.
1452 if (mUsesFragData && mUsesFragColor)
1453 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001454 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1455 if (mUsesSecondaryOutputs)
1456 {
1457 errorMessage =
1458 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1459 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1460 }
1461 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001462 }
Martin Radevb0883602016-08-04 17:48:58 +03001463
1464 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1465 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1466 qualifier == EvqWorkGroupSize)
1467 {
1468 error(location,
1469 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1470 "gl_WorkGroupSize");
1471 }
Jamie Madill5c097022014-08-20 16:38:32 -04001472 }
1473
1474 if (!variable)
1475 {
1476 TType type(EbtFloat, EbpUndefined);
1477 TVariable *fakeVariable = new TVariable(name, type);
1478 symbolTable.declare(fakeVariable);
1479 variable = fakeVariable;
1480 }
1481
1482 return variable;
1483}
1484
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001485TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1486 const TString *name,
1487 const TSymbol *symbol)
1488{
1489 const TVariable *variable = getNamedVariable(location, name, symbol);
1490
Olli Etuaho09b04a22016-12-15 13:30:26 +00001491 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1492 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1493 {
1494 // WEBGL_multiview spec
1495 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1496 "gl_ViewID_OVR");
1497 }
1498
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001499 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001500 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001501 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001502 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001503 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001504 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1505 mComputeShaderLocalSizeDeclared)
1506 {
1507 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1508 // needs to be added to the AST as a constant and not as a symbol.
1509 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1510 TConstantUnion *constArray = new TConstantUnion[3];
1511 for (size_t i = 0; i < 3; ++i)
1512 {
1513 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1514 }
1515
1516 ASSERT(variable->getType().getBasicType() == EbtUInt);
1517 ASSERT(variable->getType().getObjectSize() == 3);
1518
1519 TType type(variable->getType());
1520 type.setQualifier(EvqConst);
1521 return intermediate.addConstantUnion(constArray, type, location);
1522 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001523 else
1524 {
1525 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1526 variable->getType(), location);
1527 }
1528}
1529
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001531// Initializers show up in several places in the grammar. Have one set of
1532// code to handle them here.
1533//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001534// Returns true on error, false if no error
1535//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001536bool TParseContext::executeInitializer(const TSourceLoc &line,
1537 const TString &identifier,
1538 const TPublicType &pType,
1539 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001540 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001541{
Olli Etuaho13389b62016-10-16 11:48:18 +01001542 ASSERT(initNode != nullptr);
1543 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001544 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001545
Olli Etuaho2935c582015-04-08 14:32:06 +03001546 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001547 if (type.isUnsizedArray())
1548 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001549 // We have not checked yet whether the initializer actually is an array or not.
1550 if (initializer->isArray())
1551 {
1552 type.setArraySize(initializer->getArraySize());
1553 }
1554 else
1555 {
1556 // Having a non-array initializer for an unsized array will result in an error later,
1557 // so we don't generate an error message here.
1558 type.setArraySize(1u);
1559 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001560 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001561 if (!declareVariable(line, identifier, type, &variable))
1562 {
1563 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001565
Olli Etuahob0c645e2015-05-12 14:25:36 +03001566 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001567 if (symbolTable.atGlobalLevel() &&
1568 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001569 {
1570 // Error message does not completely match behavior with ESSL 1.00, but
1571 // we want to steer developers towards only using constant expressions.
1572 error(line, "global variable initializers must be constant expressions", "=");
1573 return true;
1574 }
1575 if (globalInitWarning)
1576 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001577 warning(
1578 line,
1579 "global variable initializers should be constant expressions "
1580 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1581 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001582 }
1583
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001584 //
1585 // identifier must be of type constant, a global, or a temporary
1586 //
1587 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301588 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1589 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001590 error(line, " cannot initialize this type of qualifier ",
1591 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001592 return true;
1593 }
1594 //
1595 // test for and propagate constant
1596 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597
Arun Patole7e7e68d2015-05-22 12:02:25 +05301598 if (qualifier == EvqConst)
1599 {
1600 if (qualifier != initializer->getType().getQualifier())
1601 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001602 std::stringstream reasonStream;
1603 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1604 << "'";
1605 std::string reason = reasonStream.str();
1606 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001607 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001608 return true;
1609 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301610 if (type != initializer->getType())
1611 {
1612 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001613 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001614 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001615 return true;
1616 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001617
1618 // Save the constant folded value to the variable if possible. For example array
1619 // initializers are not folded, since that way copying the array literal to multiple places
1620 // in the shader is avoided.
1621 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1622 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301623 if (initializer->getAsConstantUnion())
1624 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001625 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001626 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001627 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301628 }
1629 else if (initializer->getAsSymbolNode())
1630 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001631 const TSymbol *symbol =
1632 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1633 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001635 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001636 if (constArray)
1637 {
1638 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001639 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001640 return false;
1641 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001642 }
1643 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001644
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001645 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1646 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001647 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1648 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001649 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001650 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1651 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001652 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001653
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001654 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001655}
1656
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001657void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1658{
1659 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1660 typeSpecifier->getBasicType());
1661
1662 if (mShaderVersion < 300 && typeSpecifier->array)
1663 {
1664 error(typeSpecifier->getLine(), "not supported", "first-class array");
1665 typeSpecifier->clearArrayness();
1666 }
1667}
1668
Martin Radev70866b82016-07-22 15:27:42 +03001669TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301670 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001671{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001672 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001673
Martin Radev70866b82016-07-22 15:27:42 +03001674 TPublicType returnType = typeSpecifier;
1675 returnType.qualifier = typeQualifier.qualifier;
1676 returnType.invariant = typeQualifier.invariant;
1677 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001678 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001679 returnType.precision = typeSpecifier.precision;
1680
1681 if (typeQualifier.precision != EbpUndefined)
1682 {
1683 returnType.precision = typeQualifier.precision;
1684 }
1685
Martin Radev4a9cd802016-09-01 16:51:51 +03001686 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1687 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001688
Martin Radev4a9cd802016-09-01 16:51:51 +03001689 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1690 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001691
Martin Radev4a9cd802016-09-01 16:51:51 +03001692 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001693
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001694 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001695 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001696 if (typeSpecifier.array)
1697 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001698 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001699 returnType.clearArrayness();
1700 }
1701
Martin Radev70866b82016-07-22 15:27:42 +03001702 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001703 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001704 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001705 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001706 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001707 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001708
Martin Radev70866b82016-07-22 15:27:42 +03001709 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001710 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001711 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001712 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001713 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001714 }
1715 }
1716 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001717 {
Martin Radev70866b82016-07-22 15:27:42 +03001718 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001719 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001720 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001721 }
Martin Radev70866b82016-07-22 15:27:42 +03001722 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1723 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001724 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001725 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1726 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001727 }
Martin Radev70866b82016-07-22 15:27:42 +03001728 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001729 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001730 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001731 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001732 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001733 }
1734
1735 return returnType;
1736}
1737
Olli Etuaho856c4972016-08-08 11:38:39 +03001738void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1739 const TPublicType &type,
1740 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001741{
1742 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001743 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001744 {
1745 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001746 }
1747
1748 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1749 switch (qualifier)
1750 {
1751 case EvqVertexIn:
1752 // ESSL 3.00 section 4.3.4
1753 if (type.array)
1754 {
1755 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001756 }
1757 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1758 return;
1759 case EvqFragmentOut:
1760 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001761 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001762 {
1763 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001764 }
1765 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1766 return;
1767 default:
1768 break;
1769 }
1770
1771 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1772 // restrictions.
1773 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001774 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1775 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001776 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1777 {
1778 error(qualifierLocation, "must use 'flat' interpolation here",
1779 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001780 }
1781
Martin Radev4a9cd802016-09-01 16:51:51 +03001782 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001783 {
1784 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1785 // These restrictions are only implied by the ESSL 3.00 spec, but
1786 // the ESSL 3.10 spec lists these restrictions explicitly.
1787 if (type.array)
1788 {
1789 error(qualifierLocation, "cannot be an array of structures",
1790 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001791 }
1792 if (type.isStructureContainingArrays())
1793 {
1794 error(qualifierLocation, "cannot be a structure containing an array",
1795 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001796 }
1797 if (type.isStructureContainingType(EbtStruct))
1798 {
1799 error(qualifierLocation, "cannot be a structure containing a structure",
1800 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001801 }
1802 if (type.isStructureContainingType(EbtBool))
1803 {
1804 error(qualifierLocation, "cannot be a structure containing a bool",
1805 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001806 }
1807 }
1808}
1809
Martin Radev2cc85b32016-08-05 16:22:53 +03001810void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1811{
1812 if (qualifier.getType() == QtStorage)
1813 {
1814 const TStorageQualifierWrapper &storageQualifier =
1815 static_cast<const TStorageQualifierWrapper &>(qualifier);
1816 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1817 !symbolTable.atGlobalLevel())
1818 {
1819 error(storageQualifier.getLine(),
1820 "Local variables can only use the const storage qualifier.",
1821 storageQualifier.getQualifierString().c_str());
1822 }
1823 }
1824}
1825
1826bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1827 const TSourceLoc &location)
1828{
1829 if (memoryQualifier.readonly)
1830 {
1831 error(location, "Only allowed with images.", "readonly");
1832 return false;
1833 }
1834 if (memoryQualifier.writeonly)
1835 {
1836 error(location, "Only allowed with images.", "writeonly");
1837 return false;
1838 }
Martin Radev049edfa2016-11-11 14:35:37 +02001839 if (memoryQualifier.coherent)
1840 {
1841 error(location, "Only allowed with images.", "coherent");
1842 return false;
1843 }
1844 if (memoryQualifier.restrictQualifier)
1845 {
1846 error(location, "Only allowed with images.", "restrict");
1847 return false;
1848 }
1849 if (memoryQualifier.volatileQualifier)
1850 {
1851 error(location, "Only allowed with images.", "volatile");
1852 return false;
1853 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001854 return true;
1855}
1856
Olli Etuaho13389b62016-10-16 11:48:18 +01001857TIntermDeclaration *TParseContext::parseSingleDeclaration(
1858 TPublicType &publicType,
1859 const TSourceLoc &identifierOrTypeLocation,
1860 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001861{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001862 TType type(publicType);
1863 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1864 mDirectiveHandler.pragma().stdgl.invariantAll)
1865 {
1866 TQualifier qualifier = type.getQualifier();
1867
1868 // The directive handler has already taken care of rejecting invalid uses of this pragma
1869 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1870 // affected variable declarations:
1871 //
1872 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1873 // elsewhere, in TranslatorGLSL.)
1874 //
1875 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1876 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1877 // the way this is currently implemented we have to enable this compiler option before
1878 // parsing the shader and determining the shading language version it uses. If this were
1879 // implemented as a post-pass, the workaround could be more targeted.
1880 //
1881 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1882 // the specification, but there are desktop OpenGL drivers that expect that this is the
1883 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1884 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1885 {
1886 type.setInvariant(true);
1887 }
1888 }
1889
1890 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001891
Olli Etuahobab4c082015-04-24 16:38:49 +03001892 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001893
Olli Etuahobab4c082015-04-24 16:38:49 +03001894 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1895
Olli Etuaho13389b62016-10-16 11:48:18 +01001896 TIntermDeclaration *declaration = new TIntermDeclaration();
1897 declaration->setLine(identifierOrTypeLocation);
1898
Olli Etuahobab4c082015-04-24 16:38:49 +03001899 if (emptyDeclaration)
1900 {
Martin Radevb8b01222016-11-20 23:25:53 +02001901 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobab4c082015-04-24 16:38:49 +03001902 }
1903 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001904 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001905 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001906
Olli Etuaho856c4972016-08-08 11:38:39 +03001907 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001908
Olli Etuaho2935c582015-04-08 14:32:06 +03001909 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001910 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001911
1912 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001913 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001914 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001915 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001916 }
1917
Olli Etuaho13389b62016-10-16 11:48:18 +01001918 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1919 // that may just declare a type.
1920 declaration->appendDeclarator(symbol);
1921
1922 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001923}
1924
Olli Etuaho13389b62016-10-16 11:48:18 +01001925TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1926 const TSourceLoc &identifierLocation,
1927 const TString &identifier,
1928 const TSourceLoc &indexLocation,
1929 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001930{
Olli Etuahofa33d582015-04-09 14:33:12 +03001931 mDeferredSingleDeclarationErrorCheck = false;
1932
Olli Etuaho383b7912016-08-05 11:22:59 +03001933 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001934
Olli Etuaho856c4972016-08-08 11:38:39 +03001935 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001936
Olli Etuaho8a176262016-08-16 14:23:01 +03001937 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001938
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001939 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001940
Olli Etuaho856c4972016-08-08 11:38:39 +03001941 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001942 // Make the type an array even if size check failed.
1943 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1944 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001945
Olli Etuaho2935c582015-04-08 14:32:06 +03001946 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001947 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001948
Olli Etuaho13389b62016-10-16 11:48:18 +01001949 TIntermDeclaration *declaration = new TIntermDeclaration();
1950 declaration->setLine(identifierLocation);
1951
Olli Etuahoe7847b02015-03-16 11:56:12 +02001952 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001953 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001954 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001955 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001956 declaration->appendDeclarator(symbol);
1957 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001958
Olli Etuaho13389b62016-10-16 11:48:18 +01001959 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001960}
1961
Olli Etuaho13389b62016-10-16 11:48:18 +01001962TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1963 const TSourceLoc &identifierLocation,
1964 const TString &identifier,
1965 const TSourceLoc &initLocation,
1966 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001967{
Olli Etuahofa33d582015-04-09 14:33:12 +03001968 mDeferredSingleDeclarationErrorCheck = false;
1969
Olli Etuaho383b7912016-08-05 11:22:59 +03001970 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001971
Olli Etuaho13389b62016-10-16 11:48:18 +01001972 TIntermDeclaration *declaration = new TIntermDeclaration();
1973 declaration->setLine(identifierLocation);
1974
1975 TIntermBinary *initNode = nullptr;
1976 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001977 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001978 if (initNode)
1979 {
1980 declaration->appendDeclarator(initNode);
1981 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001982 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001983 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001984}
1985
Olli Etuaho13389b62016-10-16 11:48:18 +01001986TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04001987 TPublicType &publicType,
1988 const TSourceLoc &identifierLocation,
1989 const TString &identifier,
1990 const TSourceLoc &indexLocation,
1991 TIntermTyped *indexExpression,
1992 const TSourceLoc &initLocation,
1993 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001994{
1995 mDeferredSingleDeclarationErrorCheck = false;
1996
Olli Etuaho383b7912016-08-05 11:22:59 +03001997 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001998
Olli Etuaho8a176262016-08-16 14:23:01 +03001999 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002000
2001 TPublicType arrayType(publicType);
2002
Olli Etuaho856c4972016-08-08 11:38:39 +03002003 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002004 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2005 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002006 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002007 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002008 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002009 }
2010 // Make the type an array even if size check failed.
2011 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2012 arrayType.setArraySize(size);
2013
Olli Etuaho13389b62016-10-16 11:48:18 +01002014 TIntermDeclaration *declaration = new TIntermDeclaration();
2015 declaration->setLine(identifierLocation);
2016
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002017 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002018 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002019 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2020 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002021 if (initNode)
2022 {
2023 declaration->appendDeclarator(initNode);
2024 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002025 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002026
2027 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002028}
2029
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002030TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002031 const TTypeQualifierBuilder &typeQualifierBuilder,
2032 const TSourceLoc &identifierLoc,
2033 const TString *identifier,
2034 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002035{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002036 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002037
Martin Radev70866b82016-07-22 15:27:42 +03002038 if (!typeQualifier.invariant)
2039 {
2040 error(identifierLoc, "Expected invariant", identifier->c_str());
2041 return nullptr;
2042 }
2043 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2044 {
2045 return nullptr;
2046 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002047 if (!symbol)
2048 {
2049 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002050 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002051 }
Martin Radev70866b82016-07-22 15:27:42 +03002052 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002053 {
Martin Radev70866b82016-07-22 15:27:42 +03002054 error(identifierLoc, "invariant declaration specifies qualifier",
2055 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002056 }
Martin Radev70866b82016-07-22 15:27:42 +03002057 if (typeQualifier.precision != EbpUndefined)
2058 {
2059 error(identifierLoc, "invariant declaration specifies precision",
2060 getPrecisionString(typeQualifier.precision));
2061 }
2062 if (!typeQualifier.layoutQualifier.isEmpty())
2063 {
2064 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2065 }
2066
2067 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2068 ASSERT(variable);
2069 const TType &type = variable->getType();
2070
2071 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2072 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002073 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002074
2075 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2076
2077 TIntermSymbol *intermSymbol =
2078 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2079
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002080 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002081}
2082
Olli Etuaho13389b62016-10-16 11:48:18 +01002083void TParseContext::parseDeclarator(TPublicType &publicType,
2084 const TSourceLoc &identifierLocation,
2085 const TString &identifier,
2086 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002087{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002088 // If the declaration starting this declarator list was empty (example: int,), some checks were
2089 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002090 if (mDeferredSingleDeclarationErrorCheck)
2091 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002092 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002093 mDeferredSingleDeclarationErrorCheck = false;
2094 }
2095
Olli Etuaho856c4972016-08-08 11:38:39 +03002096 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002097
Olli Etuaho856c4972016-08-08 11:38:39 +03002098 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002099
Olli Etuaho2935c582015-04-08 14:32:06 +03002100 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002101 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002102
Jamie Madillb98c3a82015-07-23 14:26:04 -04002103 TIntermSymbol *symbol =
2104 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002105 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002106 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002107 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002108 declarationOut->appendDeclarator(symbol);
2109 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002110}
2111
Olli Etuaho13389b62016-10-16 11:48:18 +01002112void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2113 const TSourceLoc &identifierLocation,
2114 const TString &identifier,
2115 const TSourceLoc &arrayLocation,
2116 TIntermTyped *indexExpression,
2117 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002118{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002119 // If the declaration starting this declarator list was empty (example: int,), some checks were
2120 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002121 if (mDeferredSingleDeclarationErrorCheck)
2122 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002123 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002124 mDeferredSingleDeclarationErrorCheck = false;
2125 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002126
Olli Etuaho856c4972016-08-08 11:38:39 +03002127 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002128
Olli Etuaho856c4972016-08-08 11:38:39 +03002129 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002130
Olli Etuaho8a176262016-08-16 14:23:01 +03002131 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002132 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002133 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002134 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002135 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002136
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002137 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002138 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002139
Jamie Madillb98c3a82015-07-23 14:26:04 -04002140 TIntermSymbol *symbol =
2141 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002142 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002143 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002144
Olli Etuaho13389b62016-10-16 11:48:18 +01002145 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002146 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002147}
2148
Olli Etuaho13389b62016-10-16 11:48:18 +01002149void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2150 const TSourceLoc &identifierLocation,
2151 const TString &identifier,
2152 const TSourceLoc &initLocation,
2153 TIntermTyped *initializer,
2154 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002155{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002156 // If the declaration starting this declarator list was empty (example: int,), some checks were
2157 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002158 if (mDeferredSingleDeclarationErrorCheck)
2159 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002160 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002161 mDeferredSingleDeclarationErrorCheck = false;
2162 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002163
Olli Etuaho856c4972016-08-08 11:38:39 +03002164 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002165
Olli Etuaho13389b62016-10-16 11:48:18 +01002166 TIntermBinary *initNode = nullptr;
2167 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002168 {
2169 //
2170 // build the intermediate representation
2171 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002172 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002173 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002174 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002175 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002176 }
2177}
2178
Olli Etuaho13389b62016-10-16 11:48:18 +01002179void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2180 const TSourceLoc &identifierLocation,
2181 const TString &identifier,
2182 const TSourceLoc &indexLocation,
2183 TIntermTyped *indexExpression,
2184 const TSourceLoc &initLocation,
2185 TIntermTyped *initializer,
2186 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002187{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002188 // If the declaration starting this declarator list was empty (example: int,), some checks were
2189 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002190 if (mDeferredSingleDeclarationErrorCheck)
2191 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002192 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002193 mDeferredSingleDeclarationErrorCheck = false;
2194 }
2195
Olli Etuaho856c4972016-08-08 11:38:39 +03002196 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002197
Olli Etuaho8a176262016-08-16 14:23:01 +03002198 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002199
2200 TPublicType arrayType(publicType);
2201
Olli Etuaho856c4972016-08-08 11:38:39 +03002202 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002203 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2204 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002205 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002206 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002207 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002208 }
2209 // Make the type an array even if size check failed.
2210 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2211 arrayType.setArraySize(size);
2212
2213 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002214 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002215 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2216 {
2217 if (initNode)
2218 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002219 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002220 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002221 }
2222}
2223
Martin Radev70866b82016-07-22 15:27:42 +03002224void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002225{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002226 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002227 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002228
Martin Radev70866b82016-07-22 15:27:42 +03002229 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2230 typeQualifier.line);
2231
Jamie Madillc2128ff2016-07-04 10:26:17 -04002232 // It should never be the case, but some strange parser errors can send us here.
2233 if (layoutQualifier.isEmpty())
2234 {
2235 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002236 return;
2237 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002238
Martin Radev802abe02016-08-04 17:48:32 +03002239 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002240 {
Martin Radev802abe02016-08-04 17:48:32 +03002241 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002242 return;
2243 }
2244
Martin Radev2cc85b32016-08-05 16:22:53 +03002245 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2246
2247 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2248
Martin Radev802abe02016-08-04 17:48:32 +03002249 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002250 {
Martin Radev802abe02016-08-04 17:48:32 +03002251 if (mComputeShaderLocalSizeDeclared &&
2252 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2253 {
2254 error(typeQualifier.line, "Work group size does not match the previous declaration",
2255 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002256 return;
2257 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002258
Martin Radev802abe02016-08-04 17:48:32 +03002259 if (mShaderVersion < 310)
2260 {
2261 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002262 return;
2263 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002264
Martin Radev4c4c8e72016-08-04 12:25:34 +03002265 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002266 {
2267 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002268 return;
2269 }
2270
2271 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2272 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2273
2274 const TConstantUnion *maxComputeWorkGroupSizeData =
2275 maxComputeWorkGroupSize->getConstPointer();
2276
2277 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2278 {
2279 if (layoutQualifier.localSize[i] != -1)
2280 {
2281 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2282 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2283 if (mComputeShaderLocalSize[i] < 1 ||
2284 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2285 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002286 std::stringstream reasonStream;
2287 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2288 << maxComputeWorkGroupSizeValue;
2289 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002290
Olli Etuaho4de340a2016-12-16 09:32:03 +00002291 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002292 return;
2293 }
2294 }
2295 }
2296
2297 mComputeShaderLocalSizeDeclared = true;
2298 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00002299 else if (mMultiviewAvailable &&
2300 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
2301 typeQualifier.qualifier == EvqVertexIn)
2302 {
2303 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2304 // specification.
2305 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2306 {
2307 error(typeQualifier.line, "Number of views does not match the previous declaration",
2308 "layout");
2309 return;
2310 }
2311
2312 if (layoutQualifier.numViews == -1)
2313 {
2314 error(typeQualifier.line, "No num_views specified", "layout");
2315 return;
2316 }
2317
2318 if (layoutQualifier.numViews > mMaxNumViews)
2319 {
2320 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2321 "layout");
2322 return;
2323 }
2324
2325 mNumViews = layoutQualifier.numViews;
2326 }
Martin Radev802abe02016-08-04 17:48:32 +03002327 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002328 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002329 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002330 {
Martin Radev802abe02016-08-04 17:48:32 +03002331 return;
2332 }
2333
2334 if (typeQualifier.qualifier != EvqUniform)
2335 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002336 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2337 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002338 return;
2339 }
2340
2341 if (mShaderVersion < 300)
2342 {
2343 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2344 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002345 return;
2346 }
2347
Olli Etuaho09b04a22016-12-15 13:30:26 +00002348 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002349
2350 if (layoutQualifier.matrixPacking != EmpUnspecified)
2351 {
2352 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2353 }
2354
2355 if (layoutQualifier.blockStorage != EbsUnspecified)
2356 {
2357 mDefaultBlockStorage = layoutQualifier.blockStorage;
2358 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002359 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002360}
2361
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002362TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2363 const TFunction &function,
2364 const TSourceLoc &location,
2365 bool insertParametersToSymbolTable)
2366{
2367 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(function.getReturnType());
2368 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2369 // point to the data that already exists in the symbol table.
2370 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2371 prototype->setLine(location);
2372
2373 for (size_t i = 0; i < function.getParamCount(); i++)
2374 {
2375 const TConstParameter &param = function.getParam(i);
2376
2377 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2378 // be used for unused args).
2379 if (param.name != nullptr)
2380 {
2381 TVariable *variable = new TVariable(param.name, *param.type);
2382
2383 // Insert the parameter in the symbol table.
2384 if (insertParametersToSymbolTable && !symbolTable.declare(variable))
2385 {
2386 error(location, "redefinition", variable->getName().c_str());
2387 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2388 continue;
2389 }
2390 TIntermSymbol *symbol = intermediate.addSymbol(
2391 variable->getUniqueId(), variable->getName(), variable->getType(), location);
2392 prototype->appendParameter(symbol);
2393 }
2394 else
2395 {
2396 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2397 }
2398 }
2399 return prototype;
2400}
2401
Olli Etuaho16c745a2017-01-16 17:02:27 +00002402TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2403 const TFunction &parsedFunction,
2404 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002405{
Olli Etuaho476197f2016-10-11 13:59:08 +01002406 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2407 // first declaration. Either way the instance in the symbol table is used to track whether the
2408 // function is declared multiple times.
2409 TFunction *function = static_cast<TFunction *>(
2410 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2411 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002412 {
2413 // ESSL 1.00.17 section 4.2.7.
2414 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2415 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002416 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002417 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002418
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002419 TIntermFunctionPrototype *prototype =
2420 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002421
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002422 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002423
2424 if (!symbolTable.atGlobalLevel())
2425 {
2426 // ESSL 3.00.4 section 4.2.4.
2427 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002428 }
2429
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002430 return prototype;
2431}
2432
Olli Etuaho336b1472016-10-05 16:37:55 +01002433TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002434 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002435 TIntermBlock *functionBody,
2436 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002437{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002438 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002439 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2440 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002441 error(location, "function does not return a value:",
2442 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002443 }
2444
Olli Etuahof51fdd22016-10-03 10:03:40 +01002445 if (functionBody == nullptr)
2446 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002447 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002448 functionBody->setLine(location);
2449 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002450 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002451 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002452 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002453
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002454 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002455 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002456}
2457
Olli Etuaho476197f2016-10-11 13:59:08 +01002458void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2459 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002460 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002461{
Olli Etuaho476197f2016-10-11 13:59:08 +01002462 ASSERT(function);
2463 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002464 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002465 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002466
2467 if (builtIn)
2468 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002469 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002470 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002471 else
Jamie Madill185fb402015-06-12 15:48:48 -04002472 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002473 TFunction *prevDec = static_cast<TFunction *>(
2474 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2475
2476 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2477 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2478 // occurance.
2479 if (*function != prevDec)
2480 {
2481 // Swap the parameters of the previous declaration to the parameters of the function
2482 // definition (parameter names may differ).
2483 prevDec->swapParameters(**function);
2484
2485 // The function definition will share the same symbol as any previous declaration.
2486 *function = prevDec;
2487 }
2488
2489 if ((*function)->isDefined())
2490 {
2491 error(location, "function already has a body", (*function)->getName().c_str());
2492 }
2493
2494 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002495 }
Jamie Madill185fb402015-06-12 15:48:48 -04002496
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002497 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002498 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002499 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002500
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002501 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002502 setLoopNestingLevel(0);
2503}
2504
Jamie Madillb98c3a82015-07-23 14:26:04 -04002505TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002506{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002507 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002508 // We don't know at this point whether this is a function definition or a prototype.
2509 // The definition production code will check for redefinitions.
2510 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002511 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002512 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2513 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002514 //
2515 TFunction *prevDec =
2516 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302517
Martin Radevda6254b2016-12-14 17:00:36 +02002518 if (getShaderVersion() >= 300 &&
2519 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2520 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302521 {
Martin Radevda6254b2016-12-14 17:00:36 +02002522 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302523 // Therefore overloading or redefining builtin functions is an error.
2524 error(location, "Name of a built-in function cannot be redeclared as function",
2525 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302526 }
2527 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002528 {
2529 if (prevDec->getReturnType() != function->getReturnType())
2530 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002531 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002532 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002533 }
2534 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2535 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002536 if (prevDec->getParam(i).type->getQualifier() !=
2537 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002538 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002539 error(location,
2540 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002541 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002542 }
2543 }
2544 }
2545
2546 //
2547 // Check for previously declared variables using the same name.
2548 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002549 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002550 if (prevSym)
2551 {
2552 if (!prevSym->isFunction())
2553 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002554 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002555 }
2556 }
2557 else
2558 {
2559 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002560 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002561 }
2562
2563 // We're at the inner scope level of the function's arguments and body statement.
2564 // Add the function prototype to the surrounding scope instead.
2565 symbolTable.getOuterLevel()->insert(function);
2566
Olli Etuaho78d13742017-01-18 13:06:10 +00002567 // Raise error message if main function takes any parameters or return anything other than void
2568 if (function->getName() == "main")
2569 {
2570 if (function->getParamCount() > 0)
2571 {
2572 error(location, "function cannot take any parameter(s)", "main");
2573 }
2574 if (function->getReturnType().getBasicType() != EbtVoid)
2575 {
2576 error(location, "main function cannot return a value",
2577 function->getReturnType().getBasicString());
2578 }
2579 }
2580
Jamie Madill185fb402015-06-12 15:48:48 -04002581 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002582 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2583 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002584 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2585 //
2586 return function;
2587}
2588
Olli Etuaho9de84a52016-06-14 17:36:01 +03002589TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2590 const TString *name,
2591 const TSourceLoc &location)
2592{
2593 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2594 {
2595 error(location, "no qualifiers allowed for function return",
2596 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002597 }
2598 if (!type.layoutQualifier.isEmpty())
2599 {
2600 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002601 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002602 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002603 checkIsNotSampler(location, type.typeSpecifierNonArray,
2604 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002605 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002606 if (mShaderVersion < 300)
2607 {
2608 // Array return values are forbidden, but there's also no valid syntax for declaring array
2609 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002610 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002611
2612 if (type.isStructureContainingArrays())
2613 {
2614 // ESSL 1.00.17 section 6.1 Function Definitions
2615 error(location, "structures containing arrays can't be function return values",
2616 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002617 }
2618 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002619
2620 // Add the function as a prototype after parsing it (we do not support recursion)
2621 return new TFunction(name, new TType(type));
2622}
2623
Jamie Madill06145232015-05-13 13:10:01 -04002624TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002625{
Jamie Madill06145232015-05-13 13:10:01 -04002626 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002627 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002628 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002629 error(publicType.getLine(), "constructor can't be a structure definition",
2630 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002631 }
2632
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002633 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002634 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002635 {
2636 op = EOpConstructStruct;
2637 }
2638 else
2639 {
Geoff Lang156d7192016-07-21 16:11:00 -04002640 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002641 if (op == EOpNull)
2642 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002643 error(publicType.getLine(), "cannot construct this type",
2644 getBasicString(publicType.getBasicType()));
2645 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002646 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002647 }
2648 }
2649
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002650 const TType *type = new TType(publicType);
Olli Etuaho72d10202017-01-19 15:58:30 +00002651 return new TFunction(nullptr, type, op);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002652}
2653
Jamie Madillb98c3a82015-07-23 14:26:04 -04002654// This function is used to test for the correctness of the parameters passed to various constructor
2655// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002656//
Olli Etuaho856c4972016-08-08 11:38:39 +03002657// 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 +00002658//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002659TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002660 TOperator op,
Olli Etuaho72d10202017-01-19 15:58:30 +00002661 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302662 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002663{
Olli Etuaho856c4972016-08-08 11:38:39 +03002664 if (type.isUnsizedArray())
2665 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002666 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002667 {
2668 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2669 type.setArraySize(1u);
2670 return TIntermTyped::CreateZero(type);
2671 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002672 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03002673 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002674
Olli Etuaho72d10202017-01-19 15:58:30 +00002675 if (!checkConstructorArguments(line, arguments, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002676 {
Olli Etuaho72d10202017-01-19 15:58:30 +00002677 return TIntermTyped::CreateZero(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03002678 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002679
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002680 TIntermAggregate *constructorNode = new TIntermAggregate(type, op, arguments);
2681 constructorNode->setLine(line);
2682 ASSERT(constructorNode->isConstructor());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002683
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002684 TIntermTyped *constConstructor =
2685 intermediate.foldAggregateBuiltIn(constructorNode, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002686 if (constConstructor)
2687 {
2688 return constConstructor;
2689 }
2690
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002691 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692}
2693
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002694//
2695// Interface/uniform blocks
2696//
Olli Etuaho13389b62016-10-16 11:48:18 +01002697TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002698 const TTypeQualifierBuilder &typeQualifierBuilder,
2699 const TSourceLoc &nameLine,
2700 const TString &blockName,
2701 TFieldList *fieldList,
2702 const TString *instanceName,
2703 const TSourceLoc &instanceLine,
2704 TIntermTyped *arrayIndex,
2705 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002706{
Olli Etuaho856c4972016-08-08 11:38:39 +03002707 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002708
Olli Etuaho77ba4082016-12-16 12:01:18 +00002709 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002710
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002711 if (typeQualifier.qualifier != EvqUniform)
2712 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002713 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
2714 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002715 }
2716
Martin Radev70866b82016-07-22 15:27:42 +03002717 if (typeQualifier.invariant)
2718 {
2719 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2720 }
2721
Martin Radev2cc85b32016-08-05 16:22:53 +03002722 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2723
Jamie Madill099c0f32013-06-20 11:55:52 -04002724 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002725 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002726
Jamie Madill099c0f32013-06-20 11:55:52 -04002727 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2728 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002729 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002730 }
2731
Jamie Madill1566ef72013-06-20 11:55:54 -04002732 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2733 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002734 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002735 }
2736
Olli Etuaho856c4972016-08-08 11:38:39 +03002737 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002738
Martin Radev2cc85b32016-08-05 16:22:53 +03002739 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2740
Arun Patole7e7e68d2015-05-22 12:02:25 +05302741 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2742 if (!symbolTable.declare(blockNameSymbol))
2743 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002744 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002745 }
2746
Jamie Madill98493dd2013-07-08 14:39:03 -04002747 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302748 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2749 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002750 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302751 TType *fieldType = field->type();
2752 if (IsSampler(fieldType->getBasicType()))
2753 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002754 error(field->line(),
2755 "unsupported type - sampler types are not allowed in interface blocks",
2756 fieldType->getBasicString());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002757 }
2758
Martin Radev2cc85b32016-08-05 16:22:53 +03002759 if (IsImage(fieldType->getBasicType()))
2760 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002761 error(field->line(),
2762 "unsupported type - image types are not allowed in interface blocks",
2763 fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002764 }
2765
Jamie Madill98493dd2013-07-08 14:39:03 -04002766 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002767 switch (qualifier)
2768 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002769 case EvqGlobal:
2770 case EvqUniform:
2771 break;
2772 default:
2773 error(field->line(), "invalid qualifier on interface block member",
2774 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002775 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002776 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002777
Martin Radev70866b82016-07-22 15:27:42 +03002778 if (fieldType->isInvariant())
2779 {
2780 error(field->line(), "invalid qualifier on interface block member", "invariant");
2781 }
2782
Jamie Madilla5efff92013-06-06 11:56:47 -04002783 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002784 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002785 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002786
Jamie Madill98493dd2013-07-08 14:39:03 -04002787 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002788 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002789 error(field->line(), "invalid layout qualifier: cannot be used here",
2790 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04002791 }
2792
Jamie Madill98493dd2013-07-08 14:39:03 -04002793 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002794 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002795 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002796 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002797 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002798 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002799 warning(field->line(),
2800 "extraneous layout qualifier: only has an effect on matrix types",
2801 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04002802 }
2803
Jamie Madill98493dd2013-07-08 14:39:03 -04002804 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002805 }
2806
Jamie Madill98493dd2013-07-08 14:39:03 -04002807 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002808 unsigned int arraySize = 0;
2809 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002810 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002811 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002812 }
2813
Jamie Madillb98c3a82015-07-23 14:26:04 -04002814 TInterfaceBlock *interfaceBlock =
2815 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2816 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2817 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002818
2819 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002820 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002821
Jamie Madill98493dd2013-07-08 14:39:03 -04002822 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002823 {
2824 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002825 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2826 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002827 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302828 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002829
2830 // set parent pointer of the field variable
2831 fieldType->setInterfaceBlock(interfaceBlock);
2832
Arun Patole7e7e68d2015-05-22 12:02:25 +05302833 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002834 fieldVariable->setQualifier(typeQualifier.qualifier);
2835
Arun Patole7e7e68d2015-05-22 12:02:25 +05302836 if (!symbolTable.declare(fieldVariable))
2837 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002838 error(field->line(), "redefinition of an interface block member name",
2839 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002840 }
2841 }
2842 }
2843 else
2844 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002845 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002846
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002847 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302848 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002849 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002850
Arun Patole7e7e68d2015-05-22 12:02:25 +05302851 if (!symbolTable.declare(instanceTypeDef))
2852 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002853 error(instanceLine, "redefinition of an interface block instance name",
2854 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002855 }
2856
Jamie Madillb98c3a82015-07-23 14:26:04 -04002857 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002858 symbolName = instanceTypeDef->getName();
2859 }
2860
Olli Etuaho13389b62016-10-16 11:48:18 +01002861 TIntermSymbol *blockSymbol =
2862 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2863 TIntermDeclaration *declaration = new TIntermDeclaration();
2864 declaration->appendDeclarator(blockSymbol);
2865 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002866
2867 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002868 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002869}
2870
Olli Etuaho383b7912016-08-05 11:22:59 +03002871void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002872{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002873 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002874
2875 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00002876 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302877 if (mStructNestingLevel > 1)
2878 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002879 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002880 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002881}
2882
2883void TParseContext::exitStructDeclaration()
2884{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002885 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002886}
2887
Olli Etuaho8a176262016-08-16 14:23:01 +03002888void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002889{
Jamie Madillacb4b812016-11-07 13:50:29 -05002890 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302891 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002892 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002893 }
2894
Arun Patole7e7e68d2015-05-22 12:02:25 +05302895 if (field.type()->getBasicType() != EbtStruct)
2896 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002897 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002898 }
2899
2900 // We're already inside a structure definition at this point, so add
2901 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302902 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2903 {
Jamie Madill41a49272014-03-18 16:10:13 -04002904 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002905 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2906 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002907 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00002908 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03002909 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002910 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002911}
2912
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002913//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002914// Parse an array index expression
2915//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002916TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2917 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302918 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002919{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002920 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2921 {
2922 if (baseExpression->getAsSymbolNode())
2923 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302924 error(location, " left of '[' is not of type array, matrix, or vector ",
2925 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002926 }
2927 else
2928 {
2929 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2930 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002931
2932 TConstantUnion *unionArray = new TConstantUnion[1];
2933 unionArray->setFConst(0.0f);
2934 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2935 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002936 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002937
Jamie Madill21c1e452014-12-29 11:33:41 -05002938 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2939
Olli Etuaho36b05142015-11-12 13:10:42 +02002940 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2941 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2942 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2943 // index is a constant expression.
2944 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2945 {
2946 if (baseExpression->isInterfaceBlock())
2947 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002948 error(location,
2949 "array indexes for interface blocks arrays must be constant integral expressions",
2950 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02002951 }
2952 else if (baseExpression->getQualifier() == EvqFragmentOut)
2953 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002954 error(location,
2955 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02002956 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002957 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2958 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002959 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02002960 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002961 }
2962
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002963 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002964 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002965 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2966 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2967 // constant fold expressions that are not constant expressions). The most compatible way to
2968 // handle this case is to report a warning instead of an error and force the index to be in
2969 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002970 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002971 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002972
2973 int safeIndex = -1;
2974
2975 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002976 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002977 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002978 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002979 if (mShaderSpec == SH_WEBGL2_SPEC)
2980 {
2981 // Error has been already generated if index is not const.
2982 if (indexExpression->getQualifier() == EvqConst)
2983 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002984 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002985 }
2986 safeIndex = 0;
2987 }
2988 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2989 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002990 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002991 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00002992 "GL_EXT_draw_buffers is disabled",
2993 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002994 safeIndex = 0;
2995 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002996 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002997 // Only do generic out-of-range check if similar error hasn't already been reported.
2998 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002999 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003000 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3001 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003002 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003003 }
3004 }
3005 else if (baseExpression->isMatrix())
3006 {
3007 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003008 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003009 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003010 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003011 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003012 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003013 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3014 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003015 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003016 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003017
3018 ASSERT(safeIndex >= 0);
3019 // Data of constant unions can't be changed, because it may be shared with other
3020 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3021 // sanitized object.
3022 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003023 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003024 TConstantUnion *safeConstantUnion = new TConstantUnion();
3025 safeConstantUnion->setIConst(safeIndex);
3026 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003027 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003028
3029 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003030 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003031 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003032 else
3033 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003034 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003035 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003036 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003037}
3038
Olli Etuaho90892fb2016-07-14 14:44:51 +03003039int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3040 const TSourceLoc &location,
3041 int index,
3042 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003043 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003044{
3045 if (index >= arraySize || index < 0)
3046 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003047 std::stringstream reasonStream;
3048 reasonStream << reason << " '" << index << "'";
3049 std::string token = reasonStream.str();
3050 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003051 if (index < 0)
3052 {
3053 return 0;
3054 }
3055 else
3056 {
3057 return arraySize - 1;
3058 }
3059 }
3060 return index;
3061}
3062
Jamie Madillb98c3a82015-07-23 14:26:04 -04003063TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3064 const TSourceLoc &dotLocation,
3065 const TString &fieldString,
3066 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003067{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003068 if (baseExpression->isArray())
3069 {
3070 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003071 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003072 }
3073
3074 if (baseExpression->isVector())
3075 {
3076 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003077 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3078 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003079 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003080 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003081 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003082 }
3083
Olli Etuahob6fa0432016-09-28 16:28:05 +01003084 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003085 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003086 else if (baseExpression->getBasicType() == EbtStruct)
3087 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303088 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003089 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003090 {
3091 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003092 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003093 }
3094 else
3095 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003096 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003097 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003098 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003099 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003100 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003101 {
3102 fieldFound = true;
3103 break;
3104 }
3105 }
3106 if (fieldFound)
3107 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003108 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3109 index->setLine(fieldLocation);
3110 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003111 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003112 }
3113 else
3114 {
3115 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003116 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003117 }
3118 }
3119 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003120 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003121 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303122 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003123 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003124 {
3125 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003126 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003127 }
3128 else
3129 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003130 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003131 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003132 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003133 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003134 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003135 {
3136 fieldFound = true;
3137 break;
3138 }
3139 }
3140 if (fieldFound)
3141 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003142 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3143 index->setLine(fieldLocation);
3144 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003145 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003146 }
3147 else
3148 {
3149 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003150 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003151 }
3152 }
3153 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003154 else
3155 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003156 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003157 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003158 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303159 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003160 }
3161 else
3162 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303163 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003164 " field selection requires structure, vector, or interface block on left hand "
3165 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303166 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003167 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003168 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003169 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003170}
3171
Jamie Madillb98c3a82015-07-23 14:26:04 -04003172TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3173 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003174{
Martin Radev802abe02016-08-04 17:48:32 +03003175 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003176
3177 if (qualifierType == "shared")
3178 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003179 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003180 {
3181 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3182 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003183 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003184 }
3185 else if (qualifierType == "packed")
3186 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003187 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003188 {
3189 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3190 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003191 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003192 }
3193 else if (qualifierType == "std140")
3194 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003195 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003196 }
3197 else if (qualifierType == "row_major")
3198 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003199 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003200 }
3201 else if (qualifierType == "column_major")
3202 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003203 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003204 }
3205 else if (qualifierType == "location")
3206 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003207 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3208 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003209 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003210 else if (qualifierType == "rgba32f")
3211 {
3212 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3213 qualifier.imageInternalFormat = EiifRGBA32F;
3214 }
3215 else if (qualifierType == "rgba16f")
3216 {
3217 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3218 qualifier.imageInternalFormat = EiifRGBA16F;
3219 }
3220 else if (qualifierType == "r32f")
3221 {
3222 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3223 qualifier.imageInternalFormat = EiifR32F;
3224 }
3225 else if (qualifierType == "rgba8")
3226 {
3227 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3228 qualifier.imageInternalFormat = EiifRGBA8;
3229 }
3230 else if (qualifierType == "rgba8_snorm")
3231 {
3232 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3233 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3234 }
3235 else if (qualifierType == "rgba32i")
3236 {
3237 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3238 qualifier.imageInternalFormat = EiifRGBA32I;
3239 }
3240 else if (qualifierType == "rgba16i")
3241 {
3242 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3243 qualifier.imageInternalFormat = EiifRGBA16I;
3244 }
3245 else if (qualifierType == "rgba8i")
3246 {
3247 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3248 qualifier.imageInternalFormat = EiifRGBA8I;
3249 }
3250 else if (qualifierType == "r32i")
3251 {
3252 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3253 qualifier.imageInternalFormat = EiifR32I;
3254 }
3255 else if (qualifierType == "rgba32ui")
3256 {
3257 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3258 qualifier.imageInternalFormat = EiifRGBA32UI;
3259 }
3260 else if (qualifierType == "rgba16ui")
3261 {
3262 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3263 qualifier.imageInternalFormat = EiifRGBA16UI;
3264 }
3265 else if (qualifierType == "rgba8ui")
3266 {
3267 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3268 qualifier.imageInternalFormat = EiifRGBA8UI;
3269 }
3270 else if (qualifierType == "r32ui")
3271 {
3272 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3273 qualifier.imageInternalFormat = EiifR32UI;
3274 }
3275
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003276 else
3277 {
3278 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003279 }
3280
Jamie Madilla5efff92013-06-06 11:56:47 -04003281 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003282}
3283
Martin Radev802abe02016-08-04 17:48:32 +03003284void TParseContext::parseLocalSize(const TString &qualifierType,
3285 const TSourceLoc &qualifierTypeLine,
3286 int intValue,
3287 const TSourceLoc &intValueLine,
3288 const std::string &intValueString,
3289 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003290 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003291{
Olli Etuaho856c4972016-08-08 11:38:39 +03003292 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003293 if (intValue < 1)
3294 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003295 std::stringstream reasonStream;
3296 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3297 std::string reason = reasonStream.str();
3298 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003299 }
3300 (*localSize)[index] = intValue;
3301}
3302
Olli Etuaho09b04a22016-12-15 13:30:26 +00003303void TParseContext::parseNumViews(int intValue,
3304 const TSourceLoc &intValueLine,
3305 const std::string &intValueString,
3306 int *numViews)
3307{
3308 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3309 // specification.
3310 if (intValue < 1)
3311 {
3312 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3313 }
3314 *numViews = intValue;
3315}
3316
Jamie Madillb98c3a82015-07-23 14:26:04 -04003317TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3318 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003319 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303320 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003321{
Martin Radev802abe02016-08-04 17:48:32 +03003322 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003323
Martin Radev802abe02016-08-04 17:48:32 +03003324 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003325
Martin Radev802abe02016-08-04 17:48:32 +03003326 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003327 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003328 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003329 if (intValue < 0)
3330 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003331 error(intValueLine, "out of range: location must be non-negative",
3332 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003333 }
3334 else
3335 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003336 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003337 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003338 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003339 }
Martin Radev802abe02016-08-04 17:48:32 +03003340 else if (qualifierType == "local_size_x")
3341 {
3342 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3343 &qualifier.localSize);
3344 }
3345 else if (qualifierType == "local_size_y")
3346 {
3347 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3348 &qualifier.localSize);
3349 }
3350 else if (qualifierType == "local_size_z")
3351 {
3352 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3353 &qualifier.localSize);
3354 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00003355 else if (qualifierType == "num_views" && mMultiviewAvailable &&
3356 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
3357 mShaderType == GL_VERTEX_SHADER)
3358 {
3359 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3360 }
Martin Radev802abe02016-08-04 17:48:32 +03003361 else
3362 {
3363 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003364 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003365
Jamie Madilla5efff92013-06-06 11:56:47 -04003366 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003367}
3368
Olli Etuaho613b9592016-09-05 12:05:53 +03003369TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3370{
3371 return new TTypeQualifierBuilder(
3372 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3373 mShaderVersion);
3374}
3375
Jamie Madillb98c3a82015-07-23 14:26:04 -04003376TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003377 TLayoutQualifier rightQualifier,
3378 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003379{
Martin Radevc28888b2016-07-22 15:27:42 +03003380 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003381 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003382}
3383
Olli Etuaho4de340a2016-12-16 09:32:03 +00003384TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3385 const TFieldList *newlyAddedFields,
3386 const TSourceLoc &location)
3387{
3388 for (TField *field : *newlyAddedFields)
3389 {
3390 for (TField *oldField : *processedFields)
3391 {
3392 if (oldField->name() == field->name())
3393 {
3394 error(location, "duplicate field name in structure", field->name().c_str());
3395 }
3396 }
3397 processedFields->push_back(field);
3398 }
3399 return processedFields;
3400}
3401
Martin Radev70866b82016-07-22 15:27:42 +03003402TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3403 const TTypeQualifierBuilder &typeQualifierBuilder,
3404 TPublicType *typeSpecifier,
3405 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003406{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003407 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003408
Martin Radev70866b82016-07-22 15:27:42 +03003409 typeSpecifier->qualifier = typeQualifier.qualifier;
3410 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003411 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003412 typeSpecifier->invariant = typeQualifier.invariant;
3413 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303414 {
Martin Radev70866b82016-07-22 15:27:42 +03003415 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003416 }
Martin Radev70866b82016-07-22 15:27:42 +03003417 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003418}
3419
Jamie Madillb98c3a82015-07-23 14:26:04 -04003420TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3421 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003422{
Martin Radev4a9cd802016-09-01 16:51:51 +03003423 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3424 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003425
Martin Radev4a9cd802016-09-01 16:51:51 +03003426 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003427
Martin Radev4a9cd802016-09-01 16:51:51 +03003428 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003429
Arun Patole7e7e68d2015-05-22 12:02:25 +05303430 for (unsigned int i = 0; i < fieldList->size(); ++i)
3431 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003432 //
3433 // Careful not to replace already known aspects of type, like array-ness
3434 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303435 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003436 type->setBasicType(typeSpecifier.getBasicType());
3437 type->setPrimarySize(typeSpecifier.getPrimarySize());
3438 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003439 type->setPrecision(typeSpecifier.precision);
3440 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003441 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003442 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003443 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003444
3445 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303446 if (type->isArray())
3447 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003448 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003449 }
3450 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003451 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003452 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303453 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003454 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003455 }
3456
Martin Radev4a9cd802016-09-01 16:51:51 +03003457 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003458 }
3459
Jamie Madill98493dd2013-07-08 14:39:03 -04003460 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003461}
3462
Martin Radev4a9cd802016-09-01 16:51:51 +03003463TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3464 const TSourceLoc &nameLine,
3465 const TString *structName,
3466 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003467{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303468 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003469 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003470
Jamie Madill9b820842015-02-12 10:40:10 -05003471 // Store a bool in the struct if we're at global scope, to allow us to
3472 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003473 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003474
Jamie Madill98493dd2013-07-08 14:39:03 -04003475 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003476 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003477 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303478 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3479 if (!symbolTable.declare(userTypeDef))
3480 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003481 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003482 }
3483 }
3484
3485 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003486 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003487 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003488 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003489 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003490 switch (qualifier)
3491 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003492 case EvqGlobal:
3493 case EvqTemporary:
3494 break;
3495 default:
3496 error(field.line(), "invalid qualifier on struct member",
3497 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003498 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003499 }
Martin Radev70866b82016-07-22 15:27:42 +03003500 if (field.type()->isInvariant())
3501 {
3502 error(field.line(), "invalid qualifier on struct member", "invariant");
3503 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003504 if (IsImage(field.type()->getBasicType()))
3505 {
3506 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3507 }
3508
3509 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003510
3511 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003512 }
3513
Martin Radev4a9cd802016-09-01 16:51:51 +03003514 TTypeSpecifierNonArray typeSpecifierNonArray;
3515 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3516 typeSpecifierNonArray.userDef = structureType;
3517 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003518 exitStructDeclaration();
3519
Martin Radev4a9cd802016-09-01 16:51:51 +03003520 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003521}
3522
Jamie Madillb98c3a82015-07-23 14:26:04 -04003523TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003524 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003525 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003526{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003527 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003528 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003529 init->isVector())
3530 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003531 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3532 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003533 return nullptr;
3534 }
3535
Olli Etuahoac5274d2015-02-20 10:19:08 +02003536 if (statementList)
3537 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00003538 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02003539 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003540 return nullptr;
3541 }
3542 }
3543
Olli Etuahoa3a36662015-02-17 13:46:51 +02003544 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3545 if (node == nullptr)
3546 {
3547 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003548 return nullptr;
3549 }
3550 return node;
3551}
3552
3553TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3554{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003555 if (mSwitchNestingLevel == 0)
3556 {
3557 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003558 return nullptr;
3559 }
3560 if (condition == nullptr)
3561 {
3562 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003563 return nullptr;
3564 }
3565 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003566 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003567 {
3568 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003569 }
3570 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003571 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3572 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3573 // fold in case labels.
3574 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003575 {
3576 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003577 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003578 TIntermCase *node = intermediate.addCase(condition, loc);
3579 if (node == nullptr)
3580 {
3581 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003582 return nullptr;
3583 }
3584 return node;
3585}
3586
3587TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3588{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003589 if (mSwitchNestingLevel == 0)
3590 {
3591 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003592 return nullptr;
3593 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003594 TIntermCase *node = intermediate.addCase(nullptr, loc);
3595 if (node == nullptr)
3596 {
3597 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003598 return nullptr;
3599 }
3600 return node;
3601}
3602
Jamie Madillb98c3a82015-07-23 14:26:04 -04003603TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3604 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003605 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003606{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003607 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003608
3609 switch (op)
3610 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003611 case EOpLogicalNot:
3612 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3613 child->isVector())
3614 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003615 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003616 return nullptr;
3617 }
3618 break;
3619 case EOpBitwiseNot:
3620 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3621 child->isMatrix() || child->isArray())
3622 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003623 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003624 return nullptr;
3625 }
3626 break;
3627 case EOpPostIncrement:
3628 case EOpPreIncrement:
3629 case EOpPostDecrement:
3630 case EOpPreDecrement:
3631 case EOpNegative:
3632 case EOpPositive:
3633 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003634 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003635 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003636 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003637 return nullptr;
3638 }
3639 // Operators for built-ins are already type checked against their prototype.
3640 default:
3641 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003642 }
3643
Olli Etuahof119a262016-08-19 15:54:22 +03003644 TIntermUnary *node = new TIntermUnary(op, child);
3645 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003646
Olli Etuaho77ba4082016-12-16 12:01:18 +00003647 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03003648 if (foldedNode)
3649 return foldedNode;
3650
3651 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003652}
3653
Olli Etuaho09b22472015-02-11 11:47:26 +02003654TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3655{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003656 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003657 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003658 {
Olli Etuaho09b22472015-02-11 11:47:26 +02003659 return child;
3660 }
3661 return node;
3662}
3663
Jamie Madillb98c3a82015-07-23 14:26:04 -04003664TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3665 TIntermTyped *child,
3666 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003667{
Olli Etuaho856c4972016-08-08 11:38:39 +03003668 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003669 return addUnaryMath(op, child, loc);
3670}
3671
Jamie Madillb98c3a82015-07-23 14:26:04 -04003672bool TParseContext::binaryOpCommonCheck(TOperator op,
3673 TIntermTyped *left,
3674 TIntermTyped *right,
3675 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003676{
Olli Etuaho244be012016-08-18 15:26:02 +03003677 if (left->getType().getStruct() || right->getType().getStruct())
3678 {
3679 switch (op)
3680 {
3681 case EOpIndexDirectStruct:
3682 ASSERT(left->getType().getStruct());
3683 break;
3684 case EOpEqual:
3685 case EOpNotEqual:
3686 case EOpAssign:
3687 case EOpInitialize:
3688 if (left->getType() != right->getType())
3689 {
3690 return false;
3691 }
3692 break;
3693 default:
3694 error(loc, "Invalid operation for structs", GetOperatorString(op));
3695 return false;
3696 }
3697 }
3698
Olli Etuahod6b14282015-03-17 14:31:35 +02003699 if (left->isArray() || right->isArray())
3700 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003701 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003702 {
3703 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3704 return false;
3705 }
3706
3707 if (left->isArray() != right->isArray())
3708 {
3709 error(loc, "array / non-array mismatch", GetOperatorString(op));
3710 return false;
3711 }
3712
3713 switch (op)
3714 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003715 case EOpEqual:
3716 case EOpNotEqual:
3717 case EOpAssign:
3718 case EOpInitialize:
3719 break;
3720 default:
3721 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3722 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003723 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003724 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003725 if (left->getArraySize() != right->getArraySize())
3726 {
3727 error(loc, "array size mismatch", GetOperatorString(op));
3728 return false;
3729 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003730 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003731
3732 // Check ops which require integer / ivec parameters
3733 bool isBitShift = false;
3734 switch (op)
3735 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003736 case EOpBitShiftLeft:
3737 case EOpBitShiftRight:
3738 case EOpBitShiftLeftAssign:
3739 case EOpBitShiftRightAssign:
3740 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3741 // check that the basic type is an integer type.
3742 isBitShift = true;
3743 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3744 {
3745 return false;
3746 }
3747 break;
3748 case EOpBitwiseAnd:
3749 case EOpBitwiseXor:
3750 case EOpBitwiseOr:
3751 case EOpBitwiseAndAssign:
3752 case EOpBitwiseXorAssign:
3753 case EOpBitwiseOrAssign:
3754 // It is enough to check the type of only one operand, since later it
3755 // is checked that the operand types match.
3756 if (!IsInteger(left->getBasicType()))
3757 {
3758 return false;
3759 }
3760 break;
3761 default:
3762 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003763 }
3764
3765 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3766 // So the basic type should usually match.
3767 if (!isBitShift && left->getBasicType() != right->getBasicType())
3768 {
3769 return false;
3770 }
3771
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003772 // Check that:
3773 // 1. Type sizes match exactly on ops that require that.
3774 // 2. Restrictions for structs that contain arrays or samplers are respected.
3775 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003776 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003777 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003778 case EOpAssign:
3779 case EOpInitialize:
3780 case EOpEqual:
3781 case EOpNotEqual:
3782 // ESSL 1.00 sections 5.7, 5.8, 5.9
3783 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3784 {
3785 error(loc, "undefined operation for structs containing arrays",
3786 GetOperatorString(op));
3787 return false;
3788 }
3789 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3790 // we interpret the spec so that this extends to structs containing samplers,
3791 // similarly to ESSL 1.00 spec.
3792 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3793 left->getType().isStructureContainingSamplers())
3794 {
3795 error(loc, "undefined operation for structs containing samplers",
3796 GetOperatorString(op));
3797 return false;
3798 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003799
3800 if ((op == EOpAssign || op == EOpInitialize) &&
3801 left->getType().isStructureContainingImages())
3802 {
3803 error(loc, "undefined operation for structs containing images",
3804 GetOperatorString(op));
3805 return false;
3806 }
Olli Etuahoe1805592017-01-02 16:41:20 +00003807 if ((left->getNominalSize() != right->getNominalSize()) ||
3808 (left->getSecondarySize() != right->getSecondarySize()))
3809 {
3810 error(loc, "dimension mismatch", GetOperatorString(op));
3811 return false;
3812 }
3813 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003814 case EOpLessThan:
3815 case EOpGreaterThan:
3816 case EOpLessThanEqual:
3817 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00003818 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003819 {
Olli Etuahoe1805592017-01-02 16:41:20 +00003820 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003821 return false;
3822 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003823 break;
3824 case EOpAdd:
3825 case EOpSub:
3826 case EOpDiv:
3827 case EOpIMod:
3828 case EOpBitShiftLeft:
3829 case EOpBitShiftRight:
3830 case EOpBitwiseAnd:
3831 case EOpBitwiseXor:
3832 case EOpBitwiseOr:
3833 case EOpAddAssign:
3834 case EOpSubAssign:
3835 case EOpDivAssign:
3836 case EOpIModAssign:
3837 case EOpBitShiftLeftAssign:
3838 case EOpBitShiftRightAssign:
3839 case EOpBitwiseAndAssign:
3840 case EOpBitwiseXorAssign:
3841 case EOpBitwiseOrAssign:
3842 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3843 {
3844 return false;
3845 }
3846
3847 // Are the sizes compatible?
3848 if (left->getNominalSize() != right->getNominalSize() ||
3849 left->getSecondarySize() != right->getSecondarySize())
3850 {
3851 // If the nominal sizes of operands do not match:
3852 // One of them must be a scalar.
3853 if (!left->isScalar() && !right->isScalar())
3854 return false;
3855
3856 // In the case of compound assignment other than multiply-assign,
3857 // the right side needs to be a scalar. Otherwise a vector/matrix
3858 // would be assigned to a scalar. A scalar can't be shifted by a
3859 // vector either.
3860 if (!right->isScalar() &&
3861 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3862 return false;
3863 }
3864 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003865 default:
3866 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003867 }
3868
Olli Etuahod6b14282015-03-17 14:31:35 +02003869 return true;
3870}
3871
Olli Etuaho1dded802016-08-18 18:13:13 +03003872bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3873 const TType &left,
3874 const TType &right)
3875{
3876 switch (op)
3877 {
3878 case EOpMul:
3879 case EOpMulAssign:
3880 return left.getNominalSize() == right.getNominalSize() &&
3881 left.getSecondarySize() == right.getSecondarySize();
3882 case EOpVectorTimesScalar:
3883 return true;
3884 case EOpVectorTimesScalarAssign:
3885 ASSERT(!left.isMatrix() && !right.isMatrix());
3886 return left.isVector() && !right.isVector();
3887 case EOpVectorTimesMatrix:
3888 return left.getNominalSize() == right.getRows();
3889 case EOpVectorTimesMatrixAssign:
3890 ASSERT(!left.isMatrix() && right.isMatrix());
3891 return left.isVector() && left.getNominalSize() == right.getRows() &&
3892 left.getNominalSize() == right.getCols();
3893 case EOpMatrixTimesVector:
3894 return left.getCols() == right.getNominalSize();
3895 case EOpMatrixTimesScalar:
3896 return true;
3897 case EOpMatrixTimesScalarAssign:
3898 ASSERT(left.isMatrix() && !right.isMatrix());
3899 return !right.isVector();
3900 case EOpMatrixTimesMatrix:
3901 return left.getCols() == right.getRows();
3902 case EOpMatrixTimesMatrixAssign:
3903 ASSERT(left.isMatrix() && right.isMatrix());
3904 // We need to check two things:
3905 // 1. The matrix multiplication step is valid.
3906 // 2. The result will have the same number of columns as the lvalue.
3907 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3908
3909 default:
3910 UNREACHABLE();
3911 return false;
3912 }
3913}
3914
Jamie Madillb98c3a82015-07-23 14:26:04 -04003915TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3916 TIntermTyped *left,
3917 TIntermTyped *right,
3918 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003919{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003920 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003921 return nullptr;
3922
Olli Etuahofc1806e2015-03-17 13:03:11 +02003923 switch (op)
3924 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003925 case EOpEqual:
3926 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04003927 case EOpLessThan:
3928 case EOpGreaterThan:
3929 case EOpLessThanEqual:
3930 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04003931 break;
3932 case EOpLogicalOr:
3933 case EOpLogicalXor:
3934 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003935 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3936 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003937 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003938 {
3939 return nullptr;
3940 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003941 // Basic types matching should have been already checked.
3942 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003943 break;
3944 case EOpAdd:
3945 case EOpSub:
3946 case EOpDiv:
3947 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003948 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3949 !right->getType().getStruct());
3950 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003951 {
3952 return nullptr;
3953 }
3954 break;
3955 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003956 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3957 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003958 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003959 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003960 {
3961 return nullptr;
3962 }
3963 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003964 default:
3965 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003966 }
3967
Olli Etuaho1dded802016-08-18 18:13:13 +03003968 if (op == EOpMul)
3969 {
3970 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3971 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3972 {
3973 return nullptr;
3974 }
3975 }
3976
Olli Etuaho3fdec912016-08-18 15:08:06 +03003977 TIntermBinary *node = new TIntermBinary(op, left, right);
3978 node->setLine(loc);
3979
Olli Etuaho3fdec912016-08-18 15:08:06 +03003980 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003981 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03003982 if (foldedNode)
3983 return foldedNode;
3984
3985 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003986}
3987
Jamie Madillb98c3a82015-07-23 14:26:04 -04003988TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3989 TIntermTyped *left,
3990 TIntermTyped *right,
3991 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003992{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003993 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003994 if (node == 0)
3995 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003996 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3997 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003998 return left;
3999 }
4000 return node;
4001}
4002
Jamie Madillb98c3a82015-07-23 14:26:04 -04004003TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4004 TIntermTyped *left,
4005 TIntermTyped *right,
4006 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004007{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004008 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004009 if (node == 0)
4010 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004011 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4012 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004013 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004014 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004015 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4016 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004017 }
4018 return node;
4019}
4020
Olli Etuaho13389b62016-10-16 11:48:18 +01004021TIntermBinary *TParseContext::createAssign(TOperator op,
4022 TIntermTyped *left,
4023 TIntermTyped *right,
4024 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004025{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004026 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004027 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004028 if (op == EOpMulAssign)
4029 {
4030 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4031 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4032 {
4033 return nullptr;
4034 }
4035 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004036 TIntermBinary *node = new TIntermBinary(op, left, right);
4037 node->setLine(loc);
4038
Olli Etuaho3fdec912016-08-18 15:08:06 +03004039 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004040 }
4041 return nullptr;
4042}
4043
Jamie Madillb98c3a82015-07-23 14:26:04 -04004044TIntermTyped *TParseContext::addAssign(TOperator op,
4045 TIntermTyped *left,
4046 TIntermTyped *right,
4047 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004048{
4049 TIntermTyped *node = createAssign(op, left, right, loc);
4050 if (node == nullptr)
4051 {
4052 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004053 return left;
4054 }
4055 return node;
4056}
4057
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004058TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4059 TIntermTyped *right,
4060 const TSourceLoc &loc)
4061{
Corentin Wallez0d959252016-07-12 17:26:32 -04004062 // WebGL2 section 5.26, the following results in an error:
4063 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004064 if (mShaderSpec == SH_WEBGL2_SPEC &&
4065 (left->isArray() || left->getBasicType() == EbtVoid ||
4066 left->getType().isStructureContainingArrays() || right->isArray() ||
4067 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004068 {
4069 error(loc,
4070 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4071 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004072 }
4073
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004074 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004075}
4076
Olli Etuaho49300862015-02-20 14:54:49 +02004077TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4078{
4079 switch (op)
4080 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004081 case EOpContinue:
4082 if (mLoopNestingLevel <= 0)
4083 {
4084 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004085 }
4086 break;
4087 case EOpBreak:
4088 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4089 {
4090 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004091 }
4092 break;
4093 case EOpReturn:
4094 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4095 {
4096 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004097 }
4098 break;
4099 default:
4100 // No checks for discard
4101 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004102 }
4103 return intermediate.addBranch(op, loc);
4104}
4105
Jamie Madillb98c3a82015-07-23 14:26:04 -04004106TIntermBranch *TParseContext::addBranch(TOperator op,
4107 TIntermTyped *returnValue,
4108 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004109{
4110 ASSERT(op == EOpReturn);
4111 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004112 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004113 {
4114 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004115 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004116 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004117 {
4118 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004119 }
4120 return intermediate.addBranch(op, returnValue, loc);
4121}
4122
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004123void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4124{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004125 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004126 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004127 TIntermNode *offset = nullptr;
4128 TIntermSequence *arguments = functionCall->getSequence();
4129 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4130 name.compare(0, 16, "textureLodOffset") == 0 ||
4131 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4132 name.compare(0, 17, "textureGradOffset") == 0 ||
4133 name.compare(0, 21, "textureProjGradOffset") == 0)
4134 {
4135 offset = arguments->back();
4136 }
4137 else if (name.compare(0, 13, "textureOffset") == 0 ||
4138 name.compare(0, 17, "textureProjOffset") == 0)
4139 {
4140 // A bias parameter might follow the offset parameter.
4141 ASSERT(arguments->size() >= 3);
4142 offset = (*arguments)[2];
4143 }
4144 if (offset != nullptr)
4145 {
4146 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4147 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4148 {
4149 TString unmangledName = TFunction::unmangleName(name);
4150 error(functionCall->getLine(), "Texture offset must be a constant expression",
4151 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004152 }
4153 else
4154 {
4155 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4156 size_t size = offsetConstantUnion->getType().getObjectSize();
4157 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4158 for (size_t i = 0u; i < size; ++i)
4159 {
4160 int offsetValue = values[i].getIConst();
4161 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4162 {
4163 std::stringstream tokenStream;
4164 tokenStream << offsetValue;
4165 std::string token = tokenStream.str();
4166 error(offset->getLine(), "Texture offset value out of valid range",
4167 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004168 }
4169 }
4170 }
4171 }
4172}
4173
Martin Radev2cc85b32016-08-05 16:22:53 +03004174// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4175void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4176{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004177 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004178 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4179
4180 if (name.compare(0, 5, "image") == 0)
4181 {
4182 TIntermSequence *arguments = functionCall->getSequence();
4183 TIntermNode *imageNode = (*arguments)[0];
4184 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4185
4186 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4187
4188 if (name.compare(5, 5, "Store") == 0)
4189 {
4190 if (memoryQualifier.readonly)
4191 {
4192 error(imageNode->getLine(),
4193 "'imageStore' cannot be used with images qualified as 'readonly'",
4194 imageSymbol->getSymbol().c_str());
4195 }
4196 }
4197 else if (name.compare(5, 4, "Load") == 0)
4198 {
4199 if (memoryQualifier.writeonly)
4200 {
4201 error(imageNode->getLine(),
4202 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4203 imageSymbol->getSymbol().c_str());
4204 }
4205 }
4206 }
4207}
4208
4209// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4210void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4211 const TFunction *functionDefinition,
4212 const TIntermAggregate *functionCall)
4213{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004214 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004215
4216 const TIntermSequence &arguments = *functionCall->getSequence();
4217
4218 ASSERT(functionDefinition->getParamCount() == arguments.size());
4219
4220 for (size_t i = 0; i < arguments.size(); ++i)
4221 {
4222 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4223 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4224 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4225
4226 if (IsImage(functionArgumentType.getBasicType()))
4227 {
4228 const TMemoryQualifier &functionArgumentMemoryQualifier =
4229 functionArgumentType.getMemoryQualifier();
4230 const TMemoryQualifier &functionParameterMemoryQualifier =
4231 functionParameterType.getMemoryQualifier();
4232 if (functionArgumentMemoryQualifier.readonly &&
4233 !functionParameterMemoryQualifier.readonly)
4234 {
4235 error(functionCall->getLine(),
4236 "Function call discards the 'readonly' qualifier from image",
4237 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4238 }
4239
4240 if (functionArgumentMemoryQualifier.writeonly &&
4241 !functionParameterMemoryQualifier.writeonly)
4242 {
4243 error(functionCall->getLine(),
4244 "Function call discards the 'writeonly' qualifier from image",
4245 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4246 }
Martin Radev049edfa2016-11-11 14:35:37 +02004247
4248 if (functionArgumentMemoryQualifier.coherent &&
4249 !functionParameterMemoryQualifier.coherent)
4250 {
4251 error(functionCall->getLine(),
4252 "Function call discards the 'coherent' qualifier from image",
4253 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4254 }
4255
4256 if (functionArgumentMemoryQualifier.volatileQualifier &&
4257 !functionParameterMemoryQualifier.volatileQualifier)
4258 {
4259 error(functionCall->getLine(),
4260 "Function call discards the 'volatile' qualifier from image",
4261 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4262 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004263 }
4264 }
4265}
4266
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004267TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004268{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004269 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004270}
4271
4272TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004273 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004274 TIntermNode *thisNode,
4275 const TSourceLoc &loc)
4276{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004277 if (thisNode != nullptr)
4278 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004279 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004280 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004281
4282 TOperator op = fnCall->getBuiltInOp();
4283 if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004284 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004285 return addConstructor(arguments, op, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004286 }
4287 else
4288 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004289 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4290 }
4291}
4292
4293TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4294 TIntermSequence *arguments,
4295 TIntermNode *thisNode,
4296 const TSourceLoc &loc)
4297{
4298 TConstantUnion *unionArray = new TConstantUnion[1];
4299 int arraySize = 0;
4300 TIntermTyped *typedThis = thisNode->getAsTyped();
4301 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4302 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4303 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4304 // So accessing fnCall->getName() below is safe.
4305 if (fnCall->getName() != "length")
4306 {
4307 error(loc, "invalid method", fnCall->getName().c_str());
4308 }
4309 else if (!arguments->empty())
4310 {
4311 error(loc, "method takes no parameters", "length");
4312 }
4313 else if (typedThis == nullptr || !typedThis->isArray())
4314 {
4315 error(loc, "length can only be called on arrays", "length");
4316 }
4317 else
4318 {
4319 arraySize = typedThis->getArraySize();
4320 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004321 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004322 // This code path can be hit with expressions like these:
4323 // (a = b).length()
4324 // (func()).length()
4325 // (int[3](0, 1, 2)).length()
4326 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4327 // expression.
4328 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4329 // spec section 5.9 which allows "An array, vector or matrix expression with the
4330 // length method applied".
4331 error(loc, "length can only be called on array names, not on array expressions",
4332 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004333 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004334 }
4335 unionArray->setIConst(arraySize);
4336 return intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
4337}
4338
4339TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4340 TIntermSequence *arguments,
4341 const TSourceLoc &loc)
4342{
4343 // First find by unmangled name to check whether the function name has been
4344 // hidden by a variable name or struct typename.
4345 // If a function is found, check for one with a matching argument list.
4346 bool builtIn;
4347 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4348 if (symbol != nullptr && !symbol->isFunction())
4349 {
4350 error(loc, "function name expected", fnCall->getName().c_str());
4351 }
4352 else
4353 {
4354 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4355 mShaderVersion, &builtIn);
4356 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004357 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004358 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4359 }
4360 else
4361 {
4362 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004363 //
4364 // A declared function.
4365 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004366 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004367 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004368 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004369 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004370 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004371 if (builtIn && op != EOpNull)
4372 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004373 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004374 if (fnCandidate->getParamCount() == 1)
4375 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004376 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004377 TIntermNode *unaryParamNode = arguments->front();
4378 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004379 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004380 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004381 }
4382 else
4383 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004384 TIntermAggregate *callNode =
4385 new TIntermAggregate(fnCandidate->getReturnType(), op, arguments);
4386 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004387
4388 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004389 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304390
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004391 // See if we can constant fold a built-in. Note that this may be possible even
4392 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004393 TIntermTyped *foldedNode =
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004394 intermediate.foldAggregateBuiltIn(callNode, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304395 if (foldedNode)
4396 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004397 return foldedNode;
Arun Patole274f0702015-05-05 13:33:30 +05304398 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004399 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004400 }
4401 }
4402 else
4403 {
4404 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004405 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004406
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004407 // If builtIn == false, the function is user defined - could be an overloaded
4408 // built-in as well.
4409 // if builtIn == true, it's a builtIn function with no op associated with it.
4410 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004411 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004412 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004413 callNode = new TIntermAggregate(fnCandidate->getReturnType(),
4414 EOpCallBuiltInFunction, arguments);
4415 // Note that name needs to be set before texture function type is determined.
4416 callNode->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
4417 callNode->setBuiltInFunctionPrecision();
4418 checkTextureOffsetConst(callNode);
4419 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004420 }
4421 else
4422 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004423 callNode = new TIntermAggregate(fnCandidate->getReturnType(),
4424 EOpCallFunctionInAST, arguments);
4425 callNode->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
4426 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004427 }
4428
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004429 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004430
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004431 callNode->setLine(loc);
4432
4433 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004434 }
4435 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004436 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004437
4438 // Error message was already written. Put on a dummy node for error recovery.
4439 return TIntermTyped::CreateZero(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004440}
4441
Jamie Madillb98c3a82015-07-23 14:26:04 -04004442TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004443 TIntermTyped *trueExpression,
4444 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004445 const TSourceLoc &loc)
4446{
Olli Etuaho856c4972016-08-08 11:38:39 +03004447 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004448
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004449 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004450 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004451 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4452 falseExpression->getCompleteString());
4453 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004454 }
Olli Etuahode318b22016-10-25 16:18:25 +01004455 if (IsOpaqueType(trueExpression->getBasicType()))
4456 {
4457 // ESSL 1.00 section 4.1.7
4458 // ESSL 3.00 section 4.1.7
4459 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4460 // Note that structs containing opaque types don't need to be checked as structs are
4461 // forbidden below.
4462 error(loc, "ternary operator is not allowed for opaque types", ":");
4463 return falseExpression;
4464 }
4465
Olli Etuahoa2d53032015-04-15 14:14:44 +03004466 // ESSL1 sections 5.2 and 5.7:
4467 // ESSL3 section 5.7:
4468 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004469 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004470 {
4471 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004472 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004473 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004474 // WebGL2 section 5.26, the following results in an error:
4475 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004476 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004477 {
4478 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004479 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004480 }
4481
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004482 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004483}
Olli Etuaho49300862015-02-20 14:54:49 +02004484
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004485//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004486// Parse an array of strings using yyparse.
4487//
4488// Returns 0 for success.
4489//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004490int PaParseStrings(size_t count,
4491 const char *const string[],
4492 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304493 TParseContext *context)
4494{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004495 if ((count == 0) || (string == NULL))
4496 return 1;
4497
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004498 if (glslang_initialize(context))
4499 return 1;
4500
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004501 int error = glslang_scan(count, string, length, context);
4502 if (!error)
4503 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004504
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004505 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004506
alokp@chromium.org6b495712012-06-29 00:06:58 +00004507 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004508}
Jamie Madill45bcc782016-11-07 13:58:48 -05004509
4510} // namespace sh