blob: 8904c5cfd19896465e945749e1989f8aa755617e [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 Etuaho72d10202017-01-19 15:58:30 +0000555 const TIntermAggregate *argumentsNode,
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 Etuaho72d10202017-01-19 15:58:30 +0000588 for (TIntermNode *arg : *argumentsNode->getSequence())
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 Etuaho72d10202017-01-19 15:58:30 +0000607 if (static_cast<size_t>(type.getArraySize()) != argumentsNode->getSequence()->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 Etuaho72d10202017-01-19 15:58:30 +0000622 if (argumentsNode->getSequence()->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 Etuaho72d10202017-01-19 15:58:30 +0000637 type.getStruct()->fields().size() != argumentsNode->getSequence()->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 Etuaho72d10202017-01-19 15:58:30 +0000655 if (argumentsNode->getSequence()->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 Etuaho72d10202017-01-19 15:58:30 +0000661 for (TIntermNode *const &argNode : *argumentsNode->getSequence())
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 Etuaho72d10202017-01-19 15:58:30 +0000686 for (TIntermNode *const &argNode : *argumentsNode->getSequence())
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 Etuaho72d10202017-01-19 15:58:30 +0000701 const TIntermSequence *args = argumentsNode->getSequence();
Olli Etuaho856c4972016-08-08 11:38:39 +0300702
703 for (size_t i = 0; i < fields.size(); i++)
704 {
705 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
706 {
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//
1531// Look up a function name in the symbol table, and make sure it is a function.
1532//
1533// Return the function symbol if found, otherwise 0.
1534//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001535const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1536 TFunction *call,
1537 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301538 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001539{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001540 // First find by unmangled name to check whether the function name has been
1541 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001542 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301543 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1544 if (symbol == 0 || symbol->isFunction())
1545 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001546 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001547 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001548
Arun Patole7e7e68d2015-05-22 12:02:25 +05301549 if (symbol == 0)
1550 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001551 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001552 return 0;
1553 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001554
Arun Patole7e7e68d2015-05-22 12:02:25 +05301555 if (!symbol->isFunction())
1556 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001557 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001558 return 0;
1559 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001560
Jamie Madillb98c3a82015-07-23 14:26:04 -04001561 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001562}
1563
1564//
1565// Initializers show up in several places in the grammar. Have one set of
1566// code to handle them here.
1567//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001568// Returns true on error, false if no error
1569//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001570bool TParseContext::executeInitializer(const TSourceLoc &line,
1571 const TString &identifier,
1572 const TPublicType &pType,
1573 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001574 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001575{
Olli Etuaho13389b62016-10-16 11:48:18 +01001576 ASSERT(initNode != nullptr);
1577 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001578 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001579
Olli Etuaho2935c582015-04-08 14:32:06 +03001580 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001581 if (type.isUnsizedArray())
1582 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001583 // We have not checked yet whether the initializer actually is an array or not.
1584 if (initializer->isArray())
1585 {
1586 type.setArraySize(initializer->getArraySize());
1587 }
1588 else
1589 {
1590 // Having a non-array initializer for an unsized array will result in an error later,
1591 // so we don't generate an error message here.
1592 type.setArraySize(1u);
1593 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001594 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001595 if (!declareVariable(line, identifier, type, &variable))
1596 {
1597 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001598 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001599
Olli Etuahob0c645e2015-05-12 14:25:36 +03001600 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001601 if (symbolTable.atGlobalLevel() &&
1602 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001603 {
1604 // Error message does not completely match behavior with ESSL 1.00, but
1605 // we want to steer developers towards only using constant expressions.
1606 error(line, "global variable initializers must be constant expressions", "=");
1607 return true;
1608 }
1609 if (globalInitWarning)
1610 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001611 warning(
1612 line,
1613 "global variable initializers should be constant expressions "
1614 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1615 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001616 }
1617
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001618 //
1619 // identifier must be of type constant, a global, or a temporary
1620 //
1621 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301622 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1623 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001624 error(line, " cannot initialize this type of qualifier ",
1625 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001626 return true;
1627 }
1628 //
1629 // test for and propagate constant
1630 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001631
Arun Patole7e7e68d2015-05-22 12:02:25 +05301632 if (qualifier == EvqConst)
1633 {
1634 if (qualifier != initializer->getType().getQualifier())
1635 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001636 std::stringstream reasonStream;
1637 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1638 << "'";
1639 std::string reason = reasonStream.str();
1640 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001641 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001642 return true;
1643 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301644 if (type != initializer->getType())
1645 {
1646 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001647 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001648 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001649 return true;
1650 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001651
1652 // Save the constant folded value to the variable if possible. For example array
1653 // initializers are not folded, since that way copying the array literal to multiple places
1654 // in the shader is avoided.
1655 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1656 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301657 if (initializer->getAsConstantUnion())
1658 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001659 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001660 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001661 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301662 }
1663 else if (initializer->getAsSymbolNode())
1664 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001665 const TSymbol *symbol =
1666 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1667 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001669 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001670 if (constArray)
1671 {
1672 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001673 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001674 return false;
1675 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001676 }
1677 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001678
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001679 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1680 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001681 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1682 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001683 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001684 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1685 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001686 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001688 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689}
1690
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001691void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1692{
1693 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1694 typeSpecifier->getBasicType());
1695
1696 if (mShaderVersion < 300 && typeSpecifier->array)
1697 {
1698 error(typeSpecifier->getLine(), "not supported", "first-class array");
1699 typeSpecifier->clearArrayness();
1700 }
1701}
1702
Martin Radev70866b82016-07-22 15:27:42 +03001703TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301704 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001705{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001706 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001707
Martin Radev70866b82016-07-22 15:27:42 +03001708 TPublicType returnType = typeSpecifier;
1709 returnType.qualifier = typeQualifier.qualifier;
1710 returnType.invariant = typeQualifier.invariant;
1711 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001712 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001713 returnType.precision = typeSpecifier.precision;
1714
1715 if (typeQualifier.precision != EbpUndefined)
1716 {
1717 returnType.precision = typeQualifier.precision;
1718 }
1719
Martin Radev4a9cd802016-09-01 16:51:51 +03001720 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1721 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001722
Martin Radev4a9cd802016-09-01 16:51:51 +03001723 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1724 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001725
Martin Radev4a9cd802016-09-01 16:51:51 +03001726 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001727
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001728 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001729 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001730 if (typeSpecifier.array)
1731 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001732 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001733 returnType.clearArrayness();
1734 }
1735
Martin Radev70866b82016-07-22 15:27:42 +03001736 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001737 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001738 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001739 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001740 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001741 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001742
Martin Radev70866b82016-07-22 15:27:42 +03001743 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001744 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001745 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001746 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001747 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001748 }
1749 }
1750 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001751 {
Martin Radev70866b82016-07-22 15:27:42 +03001752 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001753 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001754 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001755 }
Martin Radev70866b82016-07-22 15:27:42 +03001756 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1757 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001758 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001759 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1760 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001761 }
Martin Radev70866b82016-07-22 15:27:42 +03001762 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001763 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001764 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001765 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001766 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001767 }
1768
1769 return returnType;
1770}
1771
Olli Etuaho856c4972016-08-08 11:38:39 +03001772void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1773 const TPublicType &type,
1774 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001775{
1776 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001777 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001778 {
1779 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001780 }
1781
1782 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1783 switch (qualifier)
1784 {
1785 case EvqVertexIn:
1786 // ESSL 3.00 section 4.3.4
1787 if (type.array)
1788 {
1789 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001790 }
1791 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1792 return;
1793 case EvqFragmentOut:
1794 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001795 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001796 {
1797 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001798 }
1799 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1800 return;
1801 default:
1802 break;
1803 }
1804
1805 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1806 // restrictions.
1807 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001808 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1809 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001810 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1811 {
1812 error(qualifierLocation, "must use 'flat' interpolation here",
1813 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001814 }
1815
Martin Radev4a9cd802016-09-01 16:51:51 +03001816 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001817 {
1818 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1819 // These restrictions are only implied by the ESSL 3.00 spec, but
1820 // the ESSL 3.10 spec lists these restrictions explicitly.
1821 if (type.array)
1822 {
1823 error(qualifierLocation, "cannot be an array of structures",
1824 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001825 }
1826 if (type.isStructureContainingArrays())
1827 {
1828 error(qualifierLocation, "cannot be a structure containing an array",
1829 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001830 }
1831 if (type.isStructureContainingType(EbtStruct))
1832 {
1833 error(qualifierLocation, "cannot be a structure containing a structure",
1834 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001835 }
1836 if (type.isStructureContainingType(EbtBool))
1837 {
1838 error(qualifierLocation, "cannot be a structure containing a bool",
1839 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001840 }
1841 }
1842}
1843
Martin Radev2cc85b32016-08-05 16:22:53 +03001844void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1845{
1846 if (qualifier.getType() == QtStorage)
1847 {
1848 const TStorageQualifierWrapper &storageQualifier =
1849 static_cast<const TStorageQualifierWrapper &>(qualifier);
1850 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1851 !symbolTable.atGlobalLevel())
1852 {
1853 error(storageQualifier.getLine(),
1854 "Local variables can only use the const storage qualifier.",
1855 storageQualifier.getQualifierString().c_str());
1856 }
1857 }
1858}
1859
1860bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1861 const TSourceLoc &location)
1862{
1863 if (memoryQualifier.readonly)
1864 {
1865 error(location, "Only allowed with images.", "readonly");
1866 return false;
1867 }
1868 if (memoryQualifier.writeonly)
1869 {
1870 error(location, "Only allowed with images.", "writeonly");
1871 return false;
1872 }
Martin Radev049edfa2016-11-11 14:35:37 +02001873 if (memoryQualifier.coherent)
1874 {
1875 error(location, "Only allowed with images.", "coherent");
1876 return false;
1877 }
1878 if (memoryQualifier.restrictQualifier)
1879 {
1880 error(location, "Only allowed with images.", "restrict");
1881 return false;
1882 }
1883 if (memoryQualifier.volatileQualifier)
1884 {
1885 error(location, "Only allowed with images.", "volatile");
1886 return false;
1887 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001888 return true;
1889}
1890
Olli Etuaho13389b62016-10-16 11:48:18 +01001891TIntermDeclaration *TParseContext::parseSingleDeclaration(
1892 TPublicType &publicType,
1893 const TSourceLoc &identifierOrTypeLocation,
1894 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001895{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001896 TType type(publicType);
1897 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1898 mDirectiveHandler.pragma().stdgl.invariantAll)
1899 {
1900 TQualifier qualifier = type.getQualifier();
1901
1902 // The directive handler has already taken care of rejecting invalid uses of this pragma
1903 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1904 // affected variable declarations:
1905 //
1906 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1907 // elsewhere, in TranslatorGLSL.)
1908 //
1909 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1910 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1911 // the way this is currently implemented we have to enable this compiler option before
1912 // parsing the shader and determining the shading language version it uses. If this were
1913 // implemented as a post-pass, the workaround could be more targeted.
1914 //
1915 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1916 // the specification, but there are desktop OpenGL drivers that expect that this is the
1917 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1918 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1919 {
1920 type.setInvariant(true);
1921 }
1922 }
1923
1924 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001925
Olli Etuahobab4c082015-04-24 16:38:49 +03001926 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001927
Olli Etuahobab4c082015-04-24 16:38:49 +03001928 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1929
Olli Etuaho13389b62016-10-16 11:48:18 +01001930 TIntermDeclaration *declaration = new TIntermDeclaration();
1931 declaration->setLine(identifierOrTypeLocation);
1932
Olli Etuahobab4c082015-04-24 16:38:49 +03001933 if (emptyDeclaration)
1934 {
Martin Radevb8b01222016-11-20 23:25:53 +02001935 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobab4c082015-04-24 16:38:49 +03001936 }
1937 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001938 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001939 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001940
Olli Etuaho856c4972016-08-08 11:38:39 +03001941 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001942
Olli Etuaho2935c582015-04-08 14:32:06 +03001943 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001944 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001945
1946 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001947 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001948 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001949 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001950 }
1951
Olli Etuaho13389b62016-10-16 11:48:18 +01001952 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1953 // that may just declare a type.
1954 declaration->appendDeclarator(symbol);
1955
1956 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001957}
1958
Olli Etuaho13389b62016-10-16 11:48:18 +01001959TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1960 const TSourceLoc &identifierLocation,
1961 const TString &identifier,
1962 const TSourceLoc &indexLocation,
1963 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001964{
Olli Etuahofa33d582015-04-09 14:33:12 +03001965 mDeferredSingleDeclarationErrorCheck = false;
1966
Olli Etuaho383b7912016-08-05 11:22:59 +03001967 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001968
Olli Etuaho856c4972016-08-08 11:38:39 +03001969 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001970
Olli Etuaho8a176262016-08-16 14:23:01 +03001971 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001972
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001973 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001974
Olli Etuaho856c4972016-08-08 11:38:39 +03001975 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001976 // Make the type an array even if size check failed.
1977 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1978 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001979
Olli Etuaho2935c582015-04-08 14:32:06 +03001980 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001981 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001982
Olli Etuaho13389b62016-10-16 11:48:18 +01001983 TIntermDeclaration *declaration = new TIntermDeclaration();
1984 declaration->setLine(identifierLocation);
1985
Olli Etuahoe7847b02015-03-16 11:56:12 +02001986 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001987 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001988 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001989 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001990 declaration->appendDeclarator(symbol);
1991 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001992
Olli Etuaho13389b62016-10-16 11:48:18 +01001993 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001994}
1995
Olli Etuaho13389b62016-10-16 11:48:18 +01001996TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1997 const TSourceLoc &identifierLocation,
1998 const TString &identifier,
1999 const TSourceLoc &initLocation,
2000 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002001{
Olli Etuahofa33d582015-04-09 14:33:12 +03002002 mDeferredSingleDeclarationErrorCheck = false;
2003
Olli Etuaho383b7912016-08-05 11:22:59 +03002004 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002005
Olli Etuaho13389b62016-10-16 11:48:18 +01002006 TIntermDeclaration *declaration = new TIntermDeclaration();
2007 declaration->setLine(identifierLocation);
2008
2009 TIntermBinary *initNode = nullptr;
2010 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002011 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002012 if (initNode)
2013 {
2014 declaration->appendDeclarator(initNode);
2015 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002016 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002017 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002018}
2019
Olli Etuaho13389b62016-10-16 11:48:18 +01002020TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002021 TPublicType &publicType,
2022 const TSourceLoc &identifierLocation,
2023 const TString &identifier,
2024 const TSourceLoc &indexLocation,
2025 TIntermTyped *indexExpression,
2026 const TSourceLoc &initLocation,
2027 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002028{
2029 mDeferredSingleDeclarationErrorCheck = false;
2030
Olli Etuaho383b7912016-08-05 11:22:59 +03002031 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002032
Olli Etuaho8a176262016-08-16 14:23:01 +03002033 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002034
2035 TPublicType arrayType(publicType);
2036
Olli Etuaho856c4972016-08-08 11:38:39 +03002037 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002038 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2039 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002040 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002041 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002042 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002043 }
2044 // Make the type an array even if size check failed.
2045 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2046 arrayType.setArraySize(size);
2047
Olli Etuaho13389b62016-10-16 11:48:18 +01002048 TIntermDeclaration *declaration = new TIntermDeclaration();
2049 declaration->setLine(identifierLocation);
2050
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002051 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002052 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002053 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2054 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002055 if (initNode)
2056 {
2057 declaration->appendDeclarator(initNode);
2058 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002059 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002060
2061 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002062}
2063
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002064TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002065 const TTypeQualifierBuilder &typeQualifierBuilder,
2066 const TSourceLoc &identifierLoc,
2067 const TString *identifier,
2068 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002069{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002070 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002071
Martin Radev70866b82016-07-22 15:27:42 +03002072 if (!typeQualifier.invariant)
2073 {
2074 error(identifierLoc, "Expected invariant", identifier->c_str());
2075 return nullptr;
2076 }
2077 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2078 {
2079 return nullptr;
2080 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002081 if (!symbol)
2082 {
2083 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002084 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002085 }
Martin Radev70866b82016-07-22 15:27:42 +03002086 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002087 {
Martin Radev70866b82016-07-22 15:27:42 +03002088 error(identifierLoc, "invariant declaration specifies qualifier",
2089 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002090 }
Martin Radev70866b82016-07-22 15:27:42 +03002091 if (typeQualifier.precision != EbpUndefined)
2092 {
2093 error(identifierLoc, "invariant declaration specifies precision",
2094 getPrecisionString(typeQualifier.precision));
2095 }
2096 if (!typeQualifier.layoutQualifier.isEmpty())
2097 {
2098 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2099 }
2100
2101 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2102 ASSERT(variable);
2103 const TType &type = variable->getType();
2104
2105 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2106 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002107 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002108
2109 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2110
2111 TIntermSymbol *intermSymbol =
2112 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2113
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002114 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002115}
2116
Olli Etuaho13389b62016-10-16 11:48:18 +01002117void TParseContext::parseDeclarator(TPublicType &publicType,
2118 const TSourceLoc &identifierLocation,
2119 const TString &identifier,
2120 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002121{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002122 // If the declaration starting this declarator list was empty (example: int,), some checks were
2123 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002124 if (mDeferredSingleDeclarationErrorCheck)
2125 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002126 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002127 mDeferredSingleDeclarationErrorCheck = false;
2128 }
2129
Olli Etuaho856c4972016-08-08 11:38:39 +03002130 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002131
Olli Etuaho856c4972016-08-08 11:38:39 +03002132 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002133
Olli Etuaho2935c582015-04-08 14:32:06 +03002134 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002135 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002136
Jamie Madillb98c3a82015-07-23 14:26:04 -04002137 TIntermSymbol *symbol =
2138 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002139 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002140 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002141 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002142 declarationOut->appendDeclarator(symbol);
2143 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002144}
2145
Olli Etuaho13389b62016-10-16 11:48:18 +01002146void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2147 const TSourceLoc &identifierLocation,
2148 const TString &identifier,
2149 const TSourceLoc &arrayLocation,
2150 TIntermTyped *indexExpression,
2151 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002152{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002153 // If the declaration starting this declarator list was empty (example: int,), some checks were
2154 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002155 if (mDeferredSingleDeclarationErrorCheck)
2156 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002157 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002158 mDeferredSingleDeclarationErrorCheck = false;
2159 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002160
Olli Etuaho856c4972016-08-08 11:38:39 +03002161 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002162
Olli Etuaho856c4972016-08-08 11:38:39 +03002163 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002164
Olli Etuaho8a176262016-08-16 14:23:01 +03002165 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002166 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002167 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002168 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002169 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002170
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002171 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002172 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002173
Jamie Madillb98c3a82015-07-23 14:26:04 -04002174 TIntermSymbol *symbol =
2175 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002176 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002177 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002178
Olli Etuaho13389b62016-10-16 11:48:18 +01002179 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002180 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002181}
2182
Olli Etuaho13389b62016-10-16 11:48:18 +01002183void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2184 const TSourceLoc &identifierLocation,
2185 const TString &identifier,
2186 const TSourceLoc &initLocation,
2187 TIntermTyped *initializer,
2188 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002189{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002190 // If the declaration starting this declarator list was empty (example: int,), some checks were
2191 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002192 if (mDeferredSingleDeclarationErrorCheck)
2193 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002194 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002195 mDeferredSingleDeclarationErrorCheck = false;
2196 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002197
Olli Etuaho856c4972016-08-08 11:38:39 +03002198 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002199
Olli Etuaho13389b62016-10-16 11:48:18 +01002200 TIntermBinary *initNode = nullptr;
2201 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002202 {
2203 //
2204 // build the intermediate representation
2205 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002206 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002207 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002208 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002209 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002210 }
2211}
2212
Olli Etuaho13389b62016-10-16 11:48:18 +01002213void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2214 const TSourceLoc &identifierLocation,
2215 const TString &identifier,
2216 const TSourceLoc &indexLocation,
2217 TIntermTyped *indexExpression,
2218 const TSourceLoc &initLocation,
2219 TIntermTyped *initializer,
2220 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002221{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002222 // If the declaration starting this declarator list was empty (example: int,), some checks were
2223 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002224 if (mDeferredSingleDeclarationErrorCheck)
2225 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002226 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002227 mDeferredSingleDeclarationErrorCheck = false;
2228 }
2229
Olli Etuaho856c4972016-08-08 11:38:39 +03002230 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002231
Olli Etuaho8a176262016-08-16 14:23:01 +03002232 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002233
2234 TPublicType arrayType(publicType);
2235
Olli Etuaho856c4972016-08-08 11:38:39 +03002236 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002237 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2238 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002239 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002240 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002241 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002242 }
2243 // Make the type an array even if size check failed.
2244 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2245 arrayType.setArraySize(size);
2246
2247 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002248 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002249 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2250 {
2251 if (initNode)
2252 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002253 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002254 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002255 }
2256}
2257
Martin Radev70866b82016-07-22 15:27:42 +03002258void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002259{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002260 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002261 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002262
Martin Radev70866b82016-07-22 15:27:42 +03002263 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2264 typeQualifier.line);
2265
Jamie Madillc2128ff2016-07-04 10:26:17 -04002266 // It should never be the case, but some strange parser errors can send us here.
2267 if (layoutQualifier.isEmpty())
2268 {
2269 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002270 return;
2271 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002272
Martin Radev802abe02016-08-04 17:48:32 +03002273 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002274 {
Martin Radev802abe02016-08-04 17:48:32 +03002275 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002276 return;
2277 }
2278
Martin Radev2cc85b32016-08-05 16:22:53 +03002279 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2280
2281 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2282
Martin Radev802abe02016-08-04 17:48:32 +03002283 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002284 {
Martin Radev802abe02016-08-04 17:48:32 +03002285 if (mComputeShaderLocalSizeDeclared &&
2286 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2287 {
2288 error(typeQualifier.line, "Work group size does not match the previous declaration",
2289 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002290 return;
2291 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002292
Martin Radev802abe02016-08-04 17:48:32 +03002293 if (mShaderVersion < 310)
2294 {
2295 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002296 return;
2297 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002298
Martin Radev4c4c8e72016-08-04 12:25:34 +03002299 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002300 {
2301 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002302 return;
2303 }
2304
2305 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2306 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2307
2308 const TConstantUnion *maxComputeWorkGroupSizeData =
2309 maxComputeWorkGroupSize->getConstPointer();
2310
2311 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2312 {
2313 if (layoutQualifier.localSize[i] != -1)
2314 {
2315 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2316 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2317 if (mComputeShaderLocalSize[i] < 1 ||
2318 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2319 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002320 std::stringstream reasonStream;
2321 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2322 << maxComputeWorkGroupSizeValue;
2323 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002324
Olli Etuaho4de340a2016-12-16 09:32:03 +00002325 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002326 return;
2327 }
2328 }
2329 }
2330
2331 mComputeShaderLocalSizeDeclared = true;
2332 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00002333 else if (mMultiviewAvailable &&
2334 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
2335 typeQualifier.qualifier == EvqVertexIn)
2336 {
2337 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2338 // specification.
2339 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2340 {
2341 error(typeQualifier.line, "Number of views does not match the previous declaration",
2342 "layout");
2343 return;
2344 }
2345
2346 if (layoutQualifier.numViews == -1)
2347 {
2348 error(typeQualifier.line, "No num_views specified", "layout");
2349 return;
2350 }
2351
2352 if (layoutQualifier.numViews > mMaxNumViews)
2353 {
2354 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2355 "layout");
2356 return;
2357 }
2358
2359 mNumViews = layoutQualifier.numViews;
2360 }
Martin Radev802abe02016-08-04 17:48:32 +03002361 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002362 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002363 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002364 {
Martin Radev802abe02016-08-04 17:48:32 +03002365 return;
2366 }
2367
2368 if (typeQualifier.qualifier != EvqUniform)
2369 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002370 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2371 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002372 return;
2373 }
2374
2375 if (mShaderVersion < 300)
2376 {
2377 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2378 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002379 return;
2380 }
2381
Olli Etuaho09b04a22016-12-15 13:30:26 +00002382 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002383
2384 if (layoutQualifier.matrixPacking != EmpUnspecified)
2385 {
2386 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2387 }
2388
2389 if (layoutQualifier.blockStorage != EbsUnspecified)
2390 {
2391 mDefaultBlockStorage = layoutQualifier.blockStorage;
2392 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002393 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002394}
2395
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002396TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2397 const TFunction &function,
2398 const TSourceLoc &location,
2399 bool insertParametersToSymbolTable)
2400{
2401 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(function.getReturnType());
2402 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2403 // point to the data that already exists in the symbol table.
2404 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2405 prototype->setLine(location);
2406
2407 for (size_t i = 0; i < function.getParamCount(); i++)
2408 {
2409 const TConstParameter &param = function.getParam(i);
2410
2411 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2412 // be used for unused args).
2413 if (param.name != nullptr)
2414 {
2415 TVariable *variable = new TVariable(param.name, *param.type);
2416
2417 // Insert the parameter in the symbol table.
2418 if (insertParametersToSymbolTable && !symbolTable.declare(variable))
2419 {
2420 error(location, "redefinition", variable->getName().c_str());
2421 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2422 continue;
2423 }
2424 TIntermSymbol *symbol = intermediate.addSymbol(
2425 variable->getUniqueId(), variable->getName(), variable->getType(), location);
2426 prototype->appendParameter(symbol);
2427 }
2428 else
2429 {
2430 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2431 }
2432 }
2433 return prototype;
2434}
2435
Olli Etuaho16c745a2017-01-16 17:02:27 +00002436TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2437 const TFunction &parsedFunction,
2438 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002439{
Olli Etuaho476197f2016-10-11 13:59:08 +01002440 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2441 // first declaration. Either way the instance in the symbol table is used to track whether the
2442 // function is declared multiple times.
2443 TFunction *function = static_cast<TFunction *>(
2444 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2445 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002446 {
2447 // ESSL 1.00.17 section 4.2.7.
2448 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2449 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002450 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002451 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002452
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002453 TIntermFunctionPrototype *prototype =
2454 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002455
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002456 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002457
2458 if (!symbolTable.atGlobalLevel())
2459 {
2460 // ESSL 3.00.4 section 4.2.4.
2461 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002462 }
2463
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002464 return prototype;
2465}
2466
Olli Etuaho336b1472016-10-05 16:37:55 +01002467TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002468 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002469 TIntermBlock *functionBody,
2470 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002471{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002472 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002473 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2474 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002475 error(location, "function does not return a value:",
2476 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002477 }
2478
Olli Etuahof51fdd22016-10-03 10:03:40 +01002479 if (functionBody == nullptr)
2480 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002481 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002482 functionBody->setLine(location);
2483 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002484 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002485 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002486 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002487
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002488 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002489 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002490}
2491
Olli Etuaho476197f2016-10-11 13:59:08 +01002492void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2493 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002494 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002495{
Olli Etuaho476197f2016-10-11 13:59:08 +01002496 ASSERT(function);
2497 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002498 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002499 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002500
2501 if (builtIn)
2502 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002503 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002504 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002505 else
Jamie Madill185fb402015-06-12 15:48:48 -04002506 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002507 TFunction *prevDec = static_cast<TFunction *>(
2508 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2509
2510 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2511 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2512 // occurance.
2513 if (*function != prevDec)
2514 {
2515 // Swap the parameters of the previous declaration to the parameters of the function
2516 // definition (parameter names may differ).
2517 prevDec->swapParameters(**function);
2518
2519 // The function definition will share the same symbol as any previous declaration.
2520 *function = prevDec;
2521 }
2522
2523 if ((*function)->isDefined())
2524 {
2525 error(location, "function already has a body", (*function)->getName().c_str());
2526 }
2527
2528 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002529 }
Jamie Madill185fb402015-06-12 15:48:48 -04002530
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002531 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002532 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002533 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002534
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002535 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002536 setLoopNestingLevel(0);
2537}
2538
Jamie Madillb98c3a82015-07-23 14:26:04 -04002539TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002540{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002541 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002542 // We don't know at this point whether this is a function definition or a prototype.
2543 // The definition production code will check for redefinitions.
2544 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002545 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002546 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2547 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002548 //
2549 TFunction *prevDec =
2550 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302551
Martin Radevda6254b2016-12-14 17:00:36 +02002552 if (getShaderVersion() >= 300 &&
2553 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2554 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302555 {
Martin Radevda6254b2016-12-14 17:00:36 +02002556 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302557 // Therefore overloading or redefining builtin functions is an error.
2558 error(location, "Name of a built-in function cannot be redeclared as function",
2559 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302560 }
2561 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002562 {
2563 if (prevDec->getReturnType() != function->getReturnType())
2564 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002565 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002566 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002567 }
2568 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2569 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002570 if (prevDec->getParam(i).type->getQualifier() !=
2571 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002572 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002573 error(location,
2574 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002575 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002576 }
2577 }
2578 }
2579
2580 //
2581 // Check for previously declared variables using the same name.
2582 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002583 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002584 if (prevSym)
2585 {
2586 if (!prevSym->isFunction())
2587 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002588 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002589 }
2590 }
2591 else
2592 {
2593 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002594 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002595 }
2596
2597 // We're at the inner scope level of the function's arguments and body statement.
2598 // Add the function prototype to the surrounding scope instead.
2599 symbolTable.getOuterLevel()->insert(function);
2600
Olli Etuaho78d13742017-01-18 13:06:10 +00002601 // Raise error message if main function takes any parameters or return anything other than void
2602 if (function->getName() == "main")
2603 {
2604 if (function->getParamCount() > 0)
2605 {
2606 error(location, "function cannot take any parameter(s)", "main");
2607 }
2608 if (function->getReturnType().getBasicType() != EbtVoid)
2609 {
2610 error(location, "main function cannot return a value",
2611 function->getReturnType().getBasicString());
2612 }
2613 }
2614
Jamie Madill185fb402015-06-12 15:48:48 -04002615 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002616 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2617 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002618 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2619 //
2620 return function;
2621}
2622
Olli Etuaho9de84a52016-06-14 17:36:01 +03002623TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2624 const TString *name,
2625 const TSourceLoc &location)
2626{
2627 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2628 {
2629 error(location, "no qualifiers allowed for function return",
2630 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002631 }
2632 if (!type.layoutQualifier.isEmpty())
2633 {
2634 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002635 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002636 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002637 checkIsNotSampler(location, type.typeSpecifierNonArray,
2638 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002639 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002640 if (mShaderVersion < 300)
2641 {
2642 // Array return values are forbidden, but there's also no valid syntax for declaring array
2643 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002644 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002645
2646 if (type.isStructureContainingArrays())
2647 {
2648 // ESSL 1.00.17 section 6.1 Function Definitions
2649 error(location, "structures containing arrays can't be function return values",
2650 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002651 }
2652 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002653
2654 // Add the function as a prototype after parsing it (we do not support recursion)
2655 return new TFunction(name, new TType(type));
2656}
2657
Jamie Madill06145232015-05-13 13:10:01 -04002658TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002659{
Jamie Madill06145232015-05-13 13:10:01 -04002660 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002661 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002662 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002663 error(publicType.getLine(), "constructor can't be a structure definition",
2664 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002665 }
2666
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002667 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002668 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002669 {
2670 op = EOpConstructStruct;
2671 }
2672 else
2673 {
Geoff Lang156d7192016-07-21 16:11:00 -04002674 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002675 if (op == EOpNull)
2676 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002677 error(publicType.getLine(), "cannot construct this type",
2678 getBasicString(publicType.getBasicType()));
2679 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002680 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002681 }
2682 }
2683
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002684 const TType *type = new TType(publicType);
Olli Etuaho72d10202017-01-19 15:58:30 +00002685 return new TFunction(nullptr, type, op);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002686}
2687
Jamie Madillb98c3a82015-07-23 14:26:04 -04002688// This function is used to test for the correctness of the parameters passed to various constructor
2689// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690//
Olli Etuaho856c4972016-08-08 11:38:39 +03002691// 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 +00002692//
Olli Etuaho72d10202017-01-19 15:58:30 +00002693TIntermTyped *TParseContext::addConstructor(TIntermAggregate *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002694 TOperator op,
Olli Etuaho72d10202017-01-19 15:58:30 +00002695 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302696 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002697{
Olli Etuaho856c4972016-08-08 11:38:39 +03002698 if (type.isUnsizedArray())
2699 {
Olli Etuaho72d10202017-01-19 15:58:30 +00002700 if (arguments->getSequence()->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002701 {
2702 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2703 type.setArraySize(1u);
2704 return TIntermTyped::CreateZero(type);
2705 }
Olli Etuaho72d10202017-01-19 15:58:30 +00002706 type.setArraySize(static_cast<unsigned int>(arguments->getSequence()->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03002707 }
2708 bool constType = true;
Olli Etuaho72d10202017-01-19 15:58:30 +00002709 for (TIntermNode *arg : *arguments->getSequence())
Olli Etuaho856c4972016-08-08 11:38:39 +03002710 {
Olli Etuaho72d10202017-01-19 15:58:30 +00002711 TIntermTyped *argTyped = arg->getAsTyped();
2712 ASSERT(argTyped);
2713 if (argTyped->getQualifier() != EvqConst)
Olli Etuaho856c4972016-08-08 11:38:39 +03002714 constType = false;
2715 }
2716 if (constType)
2717 type.setQualifier(EvqConst);
2718
Olli Etuaho72d10202017-01-19 15:58:30 +00002719 if (!checkConstructorArguments(line, arguments, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002720 {
Olli Etuaho72d10202017-01-19 15:58:30 +00002721 return TIntermTyped::CreateZero(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03002722 }
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002723 // Turn the argument list itself into a constructor
Olli Etuaho72d10202017-01-19 15:58:30 +00002724 arguments->setOp(op);
2725 arguments->setLine(line);
2726 ASSERT(arguments->isConstructor());
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002727
2728 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho72d10202017-01-19 15:58:30 +00002729 arguments->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730
Olli Etuaho21203702014-11-13 16:16:21 +02002731 // Structs should not be precision qualified, the individual members may be.
2732 // Built-in types on the other hand should be precision qualified.
2733 if (op != EOpConstructStruct)
2734 {
Olli Etuaho72d10202017-01-19 15:58:30 +00002735 arguments->setPrecisionFromChildren();
2736 type.setPrecision(arguments->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002737 }
2738
Olli Etuaho72d10202017-01-19 15:58:30 +00002739 arguments->setType(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03002740
Olli Etuaho72d10202017-01-19 15:58:30 +00002741 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(arguments, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002742 if (constConstructor)
2743 {
2744 return constConstructor;
2745 }
2746
Olli Etuaho72d10202017-01-19 15:58:30 +00002747 return arguments;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748}
2749
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002750//
2751// Interface/uniform blocks
2752//
Olli Etuaho13389b62016-10-16 11:48:18 +01002753TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002754 const TTypeQualifierBuilder &typeQualifierBuilder,
2755 const TSourceLoc &nameLine,
2756 const TString &blockName,
2757 TFieldList *fieldList,
2758 const TString *instanceName,
2759 const TSourceLoc &instanceLine,
2760 TIntermTyped *arrayIndex,
2761 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002762{
Olli Etuaho856c4972016-08-08 11:38:39 +03002763 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002764
Olli Etuaho77ba4082016-12-16 12:01:18 +00002765 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002766
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002767 if (typeQualifier.qualifier != EvqUniform)
2768 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002769 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
2770 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002771 }
2772
Martin Radev70866b82016-07-22 15:27:42 +03002773 if (typeQualifier.invariant)
2774 {
2775 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2776 }
2777
Martin Radev2cc85b32016-08-05 16:22:53 +03002778 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2779
Jamie Madill099c0f32013-06-20 11:55:52 -04002780 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002781 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002782
Jamie Madill099c0f32013-06-20 11:55:52 -04002783 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2784 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002785 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002786 }
2787
Jamie Madill1566ef72013-06-20 11:55:54 -04002788 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2789 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002790 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002791 }
2792
Olli Etuaho856c4972016-08-08 11:38:39 +03002793 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002794
Martin Radev2cc85b32016-08-05 16:22:53 +03002795 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2796
Arun Patole7e7e68d2015-05-22 12:02:25 +05302797 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2798 if (!symbolTable.declare(blockNameSymbol))
2799 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002800 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002801 }
2802
Jamie Madill98493dd2013-07-08 14:39:03 -04002803 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302804 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2805 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002806 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302807 TType *fieldType = field->type();
2808 if (IsSampler(fieldType->getBasicType()))
2809 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002810 error(field->line(),
2811 "unsupported type - sampler types are not allowed in interface blocks",
2812 fieldType->getBasicString());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002813 }
2814
Martin Radev2cc85b32016-08-05 16:22:53 +03002815 if (IsImage(fieldType->getBasicType()))
2816 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002817 error(field->line(),
2818 "unsupported type - image types are not allowed in interface blocks",
2819 fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002820 }
2821
Jamie Madill98493dd2013-07-08 14:39:03 -04002822 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002823 switch (qualifier)
2824 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002825 case EvqGlobal:
2826 case EvqUniform:
2827 break;
2828 default:
2829 error(field->line(), "invalid qualifier on interface block member",
2830 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002831 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002832 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002833
Martin Radev70866b82016-07-22 15:27:42 +03002834 if (fieldType->isInvariant())
2835 {
2836 error(field->line(), "invalid qualifier on interface block member", "invariant");
2837 }
2838
Jamie Madilla5efff92013-06-06 11:56:47 -04002839 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002840 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002841 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002842
Jamie Madill98493dd2013-07-08 14:39:03 -04002843 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002844 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002845 error(field->line(), "invalid layout qualifier: cannot be used here",
2846 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04002847 }
2848
Jamie Madill98493dd2013-07-08 14:39:03 -04002849 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002850 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002851 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002852 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002853 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002854 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002855 warning(field->line(),
2856 "extraneous layout qualifier: only has an effect on matrix types",
2857 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04002858 }
2859
Jamie Madill98493dd2013-07-08 14:39:03 -04002860 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002861 }
2862
Jamie Madill98493dd2013-07-08 14:39:03 -04002863 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002864 unsigned int arraySize = 0;
2865 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002866 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002867 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002868 }
2869
Jamie Madillb98c3a82015-07-23 14:26:04 -04002870 TInterfaceBlock *interfaceBlock =
2871 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2872 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2873 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002874
2875 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002876 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002877
Jamie Madill98493dd2013-07-08 14:39:03 -04002878 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002879 {
2880 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002881 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2882 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002883 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302884 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002885
2886 // set parent pointer of the field variable
2887 fieldType->setInterfaceBlock(interfaceBlock);
2888
Arun Patole7e7e68d2015-05-22 12:02:25 +05302889 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002890 fieldVariable->setQualifier(typeQualifier.qualifier);
2891
Arun Patole7e7e68d2015-05-22 12:02:25 +05302892 if (!symbolTable.declare(fieldVariable))
2893 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002894 error(field->line(), "redefinition of an interface block member name",
2895 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002896 }
2897 }
2898 }
2899 else
2900 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002901 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002902
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002903 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302904 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002905 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002906
Arun Patole7e7e68d2015-05-22 12:02:25 +05302907 if (!symbolTable.declare(instanceTypeDef))
2908 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002909 error(instanceLine, "redefinition of an interface block instance name",
2910 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002911 }
2912
Jamie Madillb98c3a82015-07-23 14:26:04 -04002913 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002914 symbolName = instanceTypeDef->getName();
2915 }
2916
Olli Etuaho13389b62016-10-16 11:48:18 +01002917 TIntermSymbol *blockSymbol =
2918 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2919 TIntermDeclaration *declaration = new TIntermDeclaration();
2920 declaration->appendDeclarator(blockSymbol);
2921 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002922
2923 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002924 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002925}
2926
Olli Etuaho383b7912016-08-05 11:22:59 +03002927void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002928{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002929 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002930
2931 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00002932 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302933 if (mStructNestingLevel > 1)
2934 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002935 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002936 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002937}
2938
2939void TParseContext::exitStructDeclaration()
2940{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002941 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002942}
2943
Olli Etuaho8a176262016-08-16 14:23:01 +03002944void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002945{
Jamie Madillacb4b812016-11-07 13:50:29 -05002946 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302947 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002948 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002949 }
2950
Arun Patole7e7e68d2015-05-22 12:02:25 +05302951 if (field.type()->getBasicType() != EbtStruct)
2952 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002953 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002954 }
2955
2956 // We're already inside a structure definition at this point, so add
2957 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302958 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2959 {
Jamie Madill41a49272014-03-18 16:10:13 -04002960 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002961 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2962 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002963 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00002964 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03002965 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002966 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002967}
2968
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002969//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002970// Parse an array index expression
2971//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002972TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2973 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302974 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002975{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002976 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2977 {
2978 if (baseExpression->getAsSymbolNode())
2979 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302980 error(location, " left of '[' is not of type array, matrix, or vector ",
2981 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002982 }
2983 else
2984 {
2985 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2986 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002987
2988 TConstantUnion *unionArray = new TConstantUnion[1];
2989 unionArray->setFConst(0.0f);
2990 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2991 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002992 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002993
Jamie Madill21c1e452014-12-29 11:33:41 -05002994 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2995
Olli Etuaho36b05142015-11-12 13:10:42 +02002996 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2997 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2998 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2999 // index is a constant expression.
3000 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3001 {
3002 if (baseExpression->isInterfaceBlock())
3003 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003004 error(location,
3005 "array indexes for interface blocks arrays must be constant integral expressions",
3006 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003007 }
3008 else if (baseExpression->getQualifier() == EvqFragmentOut)
3009 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003010 error(location,
3011 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003012 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003013 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3014 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003015 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003016 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003017 }
3018
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003019 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003020 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003021 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3022 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3023 // constant fold expressions that are not constant expressions). The most compatible way to
3024 // handle this case is to report a warning instead of an error and force the index to be in
3025 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003026 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003027 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003028
3029 int safeIndex = -1;
3030
3031 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003032 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003033 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003034 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003035 if (mShaderSpec == SH_WEBGL2_SPEC)
3036 {
3037 // Error has been already generated if index is not const.
3038 if (indexExpression->getQualifier() == EvqConst)
3039 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003040 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003041 }
3042 safeIndex = 0;
3043 }
3044 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3045 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003046 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003047 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003048 "GL_EXT_draw_buffers is disabled",
3049 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003050 safeIndex = 0;
3051 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003052 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003053 // Only do generic out-of-range check if similar error hasn't already been reported.
3054 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003055 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003056 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3057 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003058 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003059 }
3060 }
3061 else if (baseExpression->isMatrix())
3062 {
3063 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003064 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003065 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003066 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003067 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003068 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003069 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3070 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003071 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003072 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003073
3074 ASSERT(safeIndex >= 0);
3075 // Data of constant unions can't be changed, because it may be shared with other
3076 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3077 // sanitized object.
3078 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003079 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003080 TConstantUnion *safeConstantUnion = new TConstantUnion();
3081 safeConstantUnion->setIConst(safeIndex);
3082 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003083 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003084
3085 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003086 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003087 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003088 else
3089 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003090 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003091 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003092 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003093}
3094
Olli Etuaho90892fb2016-07-14 14:44:51 +03003095int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3096 const TSourceLoc &location,
3097 int index,
3098 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003099 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003100{
3101 if (index >= arraySize || index < 0)
3102 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003103 std::stringstream reasonStream;
3104 reasonStream << reason << " '" << index << "'";
3105 std::string token = reasonStream.str();
3106 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003107 if (index < 0)
3108 {
3109 return 0;
3110 }
3111 else
3112 {
3113 return arraySize - 1;
3114 }
3115 }
3116 return index;
3117}
3118
Jamie Madillb98c3a82015-07-23 14:26:04 -04003119TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3120 const TSourceLoc &dotLocation,
3121 const TString &fieldString,
3122 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003123{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003124 if (baseExpression->isArray())
3125 {
3126 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003127 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003128 }
3129
3130 if (baseExpression->isVector())
3131 {
3132 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003133 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3134 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003135 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003136 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003137 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003138 }
3139
Olli Etuahob6fa0432016-09-28 16:28:05 +01003140 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003141 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003142 else if (baseExpression->getBasicType() == EbtStruct)
3143 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303144 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003145 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003146 {
3147 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003148 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003149 }
3150 else
3151 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003152 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003153 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003154 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003155 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003156 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003157 {
3158 fieldFound = true;
3159 break;
3160 }
3161 }
3162 if (fieldFound)
3163 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003164 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3165 index->setLine(fieldLocation);
3166 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003167 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003168 }
3169 else
3170 {
3171 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003172 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003173 }
3174 }
3175 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003176 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003177 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303178 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003179 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003180 {
3181 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003182 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003183 }
3184 else
3185 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003186 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003187 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003188 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003189 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003190 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003191 {
3192 fieldFound = true;
3193 break;
3194 }
3195 }
3196 if (fieldFound)
3197 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003198 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3199 index->setLine(fieldLocation);
3200 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003201 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003202 }
3203 else
3204 {
3205 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003206 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003207 }
3208 }
3209 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003210 else
3211 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003212 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003213 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003214 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303215 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003216 }
3217 else
3218 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303219 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003220 " field selection requires structure, vector, or interface block on left hand "
3221 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303222 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003223 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003224 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003225 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003226}
3227
Jamie Madillb98c3a82015-07-23 14:26:04 -04003228TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3229 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003230{
Martin Radev802abe02016-08-04 17:48:32 +03003231 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003232
3233 if (qualifierType == "shared")
3234 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003235 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003236 {
3237 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3238 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003239 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003240 }
3241 else if (qualifierType == "packed")
3242 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003243 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003244 {
3245 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3246 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003247 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003248 }
3249 else if (qualifierType == "std140")
3250 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003251 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003252 }
3253 else if (qualifierType == "row_major")
3254 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003255 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003256 }
3257 else if (qualifierType == "column_major")
3258 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003259 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003260 }
3261 else if (qualifierType == "location")
3262 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003263 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3264 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003265 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003266 else if (qualifierType == "rgba32f")
3267 {
3268 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3269 qualifier.imageInternalFormat = EiifRGBA32F;
3270 }
3271 else if (qualifierType == "rgba16f")
3272 {
3273 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3274 qualifier.imageInternalFormat = EiifRGBA16F;
3275 }
3276 else if (qualifierType == "r32f")
3277 {
3278 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3279 qualifier.imageInternalFormat = EiifR32F;
3280 }
3281 else if (qualifierType == "rgba8")
3282 {
3283 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3284 qualifier.imageInternalFormat = EiifRGBA8;
3285 }
3286 else if (qualifierType == "rgba8_snorm")
3287 {
3288 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3289 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3290 }
3291 else if (qualifierType == "rgba32i")
3292 {
3293 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3294 qualifier.imageInternalFormat = EiifRGBA32I;
3295 }
3296 else if (qualifierType == "rgba16i")
3297 {
3298 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3299 qualifier.imageInternalFormat = EiifRGBA16I;
3300 }
3301 else if (qualifierType == "rgba8i")
3302 {
3303 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3304 qualifier.imageInternalFormat = EiifRGBA8I;
3305 }
3306 else if (qualifierType == "r32i")
3307 {
3308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3309 qualifier.imageInternalFormat = EiifR32I;
3310 }
3311 else if (qualifierType == "rgba32ui")
3312 {
3313 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3314 qualifier.imageInternalFormat = EiifRGBA32UI;
3315 }
3316 else if (qualifierType == "rgba16ui")
3317 {
3318 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3319 qualifier.imageInternalFormat = EiifRGBA16UI;
3320 }
3321 else if (qualifierType == "rgba8ui")
3322 {
3323 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3324 qualifier.imageInternalFormat = EiifRGBA8UI;
3325 }
3326 else if (qualifierType == "r32ui")
3327 {
3328 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3329 qualifier.imageInternalFormat = EiifR32UI;
3330 }
3331
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003332 else
3333 {
3334 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003335 }
3336
Jamie Madilla5efff92013-06-06 11:56:47 -04003337 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003338}
3339
Martin Radev802abe02016-08-04 17:48:32 +03003340void TParseContext::parseLocalSize(const TString &qualifierType,
3341 const TSourceLoc &qualifierTypeLine,
3342 int intValue,
3343 const TSourceLoc &intValueLine,
3344 const std::string &intValueString,
3345 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003346 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003347{
Olli Etuaho856c4972016-08-08 11:38:39 +03003348 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003349 if (intValue < 1)
3350 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003351 std::stringstream reasonStream;
3352 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3353 std::string reason = reasonStream.str();
3354 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003355 }
3356 (*localSize)[index] = intValue;
3357}
3358
Olli Etuaho09b04a22016-12-15 13:30:26 +00003359void TParseContext::parseNumViews(int intValue,
3360 const TSourceLoc &intValueLine,
3361 const std::string &intValueString,
3362 int *numViews)
3363{
3364 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3365 // specification.
3366 if (intValue < 1)
3367 {
3368 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3369 }
3370 *numViews = intValue;
3371}
3372
Jamie Madillb98c3a82015-07-23 14:26:04 -04003373TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3374 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003375 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303376 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003377{
Martin Radev802abe02016-08-04 17:48:32 +03003378 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003379
Martin Radev802abe02016-08-04 17:48:32 +03003380 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003381
Martin Radev802abe02016-08-04 17:48:32 +03003382 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003383 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003384 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003385 if (intValue < 0)
3386 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003387 error(intValueLine, "out of range: location must be non-negative",
3388 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003389 }
3390 else
3391 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003392 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003393 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003394 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003395 }
Martin Radev802abe02016-08-04 17:48:32 +03003396 else if (qualifierType == "local_size_x")
3397 {
3398 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3399 &qualifier.localSize);
3400 }
3401 else if (qualifierType == "local_size_y")
3402 {
3403 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3404 &qualifier.localSize);
3405 }
3406 else if (qualifierType == "local_size_z")
3407 {
3408 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3409 &qualifier.localSize);
3410 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00003411 else if (qualifierType == "num_views" && mMultiviewAvailable &&
3412 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
3413 mShaderType == GL_VERTEX_SHADER)
3414 {
3415 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3416 }
Martin Radev802abe02016-08-04 17:48:32 +03003417 else
3418 {
3419 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003420 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003421
Jamie Madilla5efff92013-06-06 11:56:47 -04003422 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003423}
3424
Olli Etuaho613b9592016-09-05 12:05:53 +03003425TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3426{
3427 return new TTypeQualifierBuilder(
3428 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3429 mShaderVersion);
3430}
3431
Jamie Madillb98c3a82015-07-23 14:26:04 -04003432TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003433 TLayoutQualifier rightQualifier,
3434 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003435{
Martin Radevc28888b2016-07-22 15:27:42 +03003436 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003437 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003438}
3439
Olli Etuaho4de340a2016-12-16 09:32:03 +00003440TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3441 const TFieldList *newlyAddedFields,
3442 const TSourceLoc &location)
3443{
3444 for (TField *field : *newlyAddedFields)
3445 {
3446 for (TField *oldField : *processedFields)
3447 {
3448 if (oldField->name() == field->name())
3449 {
3450 error(location, "duplicate field name in structure", field->name().c_str());
3451 }
3452 }
3453 processedFields->push_back(field);
3454 }
3455 return processedFields;
3456}
3457
Martin Radev70866b82016-07-22 15:27:42 +03003458TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3459 const TTypeQualifierBuilder &typeQualifierBuilder,
3460 TPublicType *typeSpecifier,
3461 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003462{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003463 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003464
Martin Radev70866b82016-07-22 15:27:42 +03003465 typeSpecifier->qualifier = typeQualifier.qualifier;
3466 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003467 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003468 typeSpecifier->invariant = typeQualifier.invariant;
3469 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303470 {
Martin Radev70866b82016-07-22 15:27:42 +03003471 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003472 }
Martin Radev70866b82016-07-22 15:27:42 +03003473 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003474}
3475
Jamie Madillb98c3a82015-07-23 14:26:04 -04003476TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3477 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003478{
Martin Radev4a9cd802016-09-01 16:51:51 +03003479 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3480 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003481
Martin Radev4a9cd802016-09-01 16:51:51 +03003482 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003483
Martin Radev4a9cd802016-09-01 16:51:51 +03003484 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003485
Arun Patole7e7e68d2015-05-22 12:02:25 +05303486 for (unsigned int i = 0; i < fieldList->size(); ++i)
3487 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003488 //
3489 // Careful not to replace already known aspects of type, like array-ness
3490 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303491 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003492 type->setBasicType(typeSpecifier.getBasicType());
3493 type->setPrimarySize(typeSpecifier.getPrimarySize());
3494 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003495 type->setPrecision(typeSpecifier.precision);
3496 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003497 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003498 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003499 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003500
3501 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303502 if (type->isArray())
3503 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003504 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003505 }
3506 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003507 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003508 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303509 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003510 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003511 }
3512
Martin Radev4a9cd802016-09-01 16:51:51 +03003513 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003514 }
3515
Jamie Madill98493dd2013-07-08 14:39:03 -04003516 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003517}
3518
Martin Radev4a9cd802016-09-01 16:51:51 +03003519TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3520 const TSourceLoc &nameLine,
3521 const TString *structName,
3522 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003523{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303524 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003525 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003526
Jamie Madill9b820842015-02-12 10:40:10 -05003527 // Store a bool in the struct if we're at global scope, to allow us to
3528 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003529 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003530
Jamie Madill98493dd2013-07-08 14:39:03 -04003531 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003532 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003533 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303534 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3535 if (!symbolTable.declare(userTypeDef))
3536 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003537 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003538 }
3539 }
3540
3541 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003542 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003543 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003544 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003545 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003546 switch (qualifier)
3547 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003548 case EvqGlobal:
3549 case EvqTemporary:
3550 break;
3551 default:
3552 error(field.line(), "invalid qualifier on struct member",
3553 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003554 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003555 }
Martin Radev70866b82016-07-22 15:27:42 +03003556 if (field.type()->isInvariant())
3557 {
3558 error(field.line(), "invalid qualifier on struct member", "invariant");
3559 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003560 if (IsImage(field.type()->getBasicType()))
3561 {
3562 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3563 }
3564
3565 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003566
3567 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003568 }
3569
Martin Radev4a9cd802016-09-01 16:51:51 +03003570 TTypeSpecifierNonArray typeSpecifierNonArray;
3571 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3572 typeSpecifierNonArray.userDef = structureType;
3573 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003574 exitStructDeclaration();
3575
Martin Radev4a9cd802016-09-01 16:51:51 +03003576 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003577}
3578
Jamie Madillb98c3a82015-07-23 14:26:04 -04003579TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003580 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003581 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003582{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003583 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003584 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003585 init->isVector())
3586 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003587 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3588 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003589 return nullptr;
3590 }
3591
Olli Etuahoac5274d2015-02-20 10:19:08 +02003592 if (statementList)
3593 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00003594 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02003595 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003596 return nullptr;
3597 }
3598 }
3599
Olli Etuahoa3a36662015-02-17 13:46:51 +02003600 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3601 if (node == nullptr)
3602 {
3603 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003604 return nullptr;
3605 }
3606 return node;
3607}
3608
3609TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3610{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003611 if (mSwitchNestingLevel == 0)
3612 {
3613 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003614 return nullptr;
3615 }
3616 if (condition == nullptr)
3617 {
3618 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003619 return nullptr;
3620 }
3621 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003622 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003623 {
3624 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003625 }
3626 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003627 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3628 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3629 // fold in case labels.
3630 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003631 {
3632 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003633 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003634 TIntermCase *node = intermediate.addCase(condition, loc);
3635 if (node == nullptr)
3636 {
3637 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003638 return nullptr;
3639 }
3640 return node;
3641}
3642
3643TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3644{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003645 if (mSwitchNestingLevel == 0)
3646 {
3647 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003648 return nullptr;
3649 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003650 TIntermCase *node = intermediate.addCase(nullptr, loc);
3651 if (node == nullptr)
3652 {
3653 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003654 return nullptr;
3655 }
3656 return node;
3657}
3658
Jamie Madillb98c3a82015-07-23 14:26:04 -04003659TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3660 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003661 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003662{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003663 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003664
3665 switch (op)
3666 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003667 case EOpLogicalNot:
3668 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3669 child->isVector())
3670 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003671 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003672 return nullptr;
3673 }
3674 break;
3675 case EOpBitwiseNot:
3676 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3677 child->isMatrix() || child->isArray())
3678 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003679 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003680 return nullptr;
3681 }
3682 break;
3683 case EOpPostIncrement:
3684 case EOpPreIncrement:
3685 case EOpPostDecrement:
3686 case EOpPreDecrement:
3687 case EOpNegative:
3688 case EOpPositive:
3689 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003690 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003691 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003692 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003693 return nullptr;
3694 }
3695 // Operators for built-ins are already type checked against their prototype.
3696 default:
3697 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003698 }
3699
Olli Etuahof119a262016-08-19 15:54:22 +03003700 TIntermUnary *node = new TIntermUnary(op, child);
3701 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003702
Olli Etuaho77ba4082016-12-16 12:01:18 +00003703 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03003704 if (foldedNode)
3705 return foldedNode;
3706
3707 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003708}
3709
Olli Etuaho09b22472015-02-11 11:47:26 +02003710TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3711{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003712 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003713 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003714 {
Olli Etuaho09b22472015-02-11 11:47:26 +02003715 return child;
3716 }
3717 return node;
3718}
3719
Jamie Madillb98c3a82015-07-23 14:26:04 -04003720TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3721 TIntermTyped *child,
3722 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003723{
Olli Etuaho856c4972016-08-08 11:38:39 +03003724 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003725 return addUnaryMath(op, child, loc);
3726}
3727
Jamie Madillb98c3a82015-07-23 14:26:04 -04003728bool TParseContext::binaryOpCommonCheck(TOperator op,
3729 TIntermTyped *left,
3730 TIntermTyped *right,
3731 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003732{
Olli Etuaho244be012016-08-18 15:26:02 +03003733 if (left->getType().getStruct() || right->getType().getStruct())
3734 {
3735 switch (op)
3736 {
3737 case EOpIndexDirectStruct:
3738 ASSERT(left->getType().getStruct());
3739 break;
3740 case EOpEqual:
3741 case EOpNotEqual:
3742 case EOpAssign:
3743 case EOpInitialize:
3744 if (left->getType() != right->getType())
3745 {
3746 return false;
3747 }
3748 break;
3749 default:
3750 error(loc, "Invalid operation for structs", GetOperatorString(op));
3751 return false;
3752 }
3753 }
3754
Olli Etuahod6b14282015-03-17 14:31:35 +02003755 if (left->isArray() || right->isArray())
3756 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003757 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003758 {
3759 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3760 return false;
3761 }
3762
3763 if (left->isArray() != right->isArray())
3764 {
3765 error(loc, "array / non-array mismatch", GetOperatorString(op));
3766 return false;
3767 }
3768
3769 switch (op)
3770 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003771 case EOpEqual:
3772 case EOpNotEqual:
3773 case EOpAssign:
3774 case EOpInitialize:
3775 break;
3776 default:
3777 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3778 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003779 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003780 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003781 if (left->getArraySize() != right->getArraySize())
3782 {
3783 error(loc, "array size mismatch", GetOperatorString(op));
3784 return false;
3785 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003786 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003787
3788 // Check ops which require integer / ivec parameters
3789 bool isBitShift = false;
3790 switch (op)
3791 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003792 case EOpBitShiftLeft:
3793 case EOpBitShiftRight:
3794 case EOpBitShiftLeftAssign:
3795 case EOpBitShiftRightAssign:
3796 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3797 // check that the basic type is an integer type.
3798 isBitShift = true;
3799 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3800 {
3801 return false;
3802 }
3803 break;
3804 case EOpBitwiseAnd:
3805 case EOpBitwiseXor:
3806 case EOpBitwiseOr:
3807 case EOpBitwiseAndAssign:
3808 case EOpBitwiseXorAssign:
3809 case EOpBitwiseOrAssign:
3810 // It is enough to check the type of only one operand, since later it
3811 // is checked that the operand types match.
3812 if (!IsInteger(left->getBasicType()))
3813 {
3814 return false;
3815 }
3816 break;
3817 default:
3818 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003819 }
3820
3821 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3822 // So the basic type should usually match.
3823 if (!isBitShift && left->getBasicType() != right->getBasicType())
3824 {
3825 return false;
3826 }
3827
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003828 // Check that:
3829 // 1. Type sizes match exactly on ops that require that.
3830 // 2. Restrictions for structs that contain arrays or samplers are respected.
3831 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003832 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003833 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003834 case EOpAssign:
3835 case EOpInitialize:
3836 case EOpEqual:
3837 case EOpNotEqual:
3838 // ESSL 1.00 sections 5.7, 5.8, 5.9
3839 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3840 {
3841 error(loc, "undefined operation for structs containing arrays",
3842 GetOperatorString(op));
3843 return false;
3844 }
3845 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3846 // we interpret the spec so that this extends to structs containing samplers,
3847 // similarly to ESSL 1.00 spec.
3848 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3849 left->getType().isStructureContainingSamplers())
3850 {
3851 error(loc, "undefined operation for structs containing samplers",
3852 GetOperatorString(op));
3853 return false;
3854 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003855
3856 if ((op == EOpAssign || op == EOpInitialize) &&
3857 left->getType().isStructureContainingImages())
3858 {
3859 error(loc, "undefined operation for structs containing images",
3860 GetOperatorString(op));
3861 return false;
3862 }
Olli Etuahoe1805592017-01-02 16:41:20 +00003863 if ((left->getNominalSize() != right->getNominalSize()) ||
3864 (left->getSecondarySize() != right->getSecondarySize()))
3865 {
3866 error(loc, "dimension mismatch", GetOperatorString(op));
3867 return false;
3868 }
3869 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003870 case EOpLessThan:
3871 case EOpGreaterThan:
3872 case EOpLessThanEqual:
3873 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00003874 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003875 {
Olli Etuahoe1805592017-01-02 16:41:20 +00003876 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003877 return false;
3878 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003879 break;
3880 case EOpAdd:
3881 case EOpSub:
3882 case EOpDiv:
3883 case EOpIMod:
3884 case EOpBitShiftLeft:
3885 case EOpBitShiftRight:
3886 case EOpBitwiseAnd:
3887 case EOpBitwiseXor:
3888 case EOpBitwiseOr:
3889 case EOpAddAssign:
3890 case EOpSubAssign:
3891 case EOpDivAssign:
3892 case EOpIModAssign:
3893 case EOpBitShiftLeftAssign:
3894 case EOpBitShiftRightAssign:
3895 case EOpBitwiseAndAssign:
3896 case EOpBitwiseXorAssign:
3897 case EOpBitwiseOrAssign:
3898 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3899 {
3900 return false;
3901 }
3902
3903 // Are the sizes compatible?
3904 if (left->getNominalSize() != right->getNominalSize() ||
3905 left->getSecondarySize() != right->getSecondarySize())
3906 {
3907 // If the nominal sizes of operands do not match:
3908 // One of them must be a scalar.
3909 if (!left->isScalar() && !right->isScalar())
3910 return false;
3911
3912 // In the case of compound assignment other than multiply-assign,
3913 // the right side needs to be a scalar. Otherwise a vector/matrix
3914 // would be assigned to a scalar. A scalar can't be shifted by a
3915 // vector either.
3916 if (!right->isScalar() &&
3917 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3918 return false;
3919 }
3920 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003921 default:
3922 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003923 }
3924
Olli Etuahod6b14282015-03-17 14:31:35 +02003925 return true;
3926}
3927
Olli Etuaho1dded802016-08-18 18:13:13 +03003928bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3929 const TType &left,
3930 const TType &right)
3931{
3932 switch (op)
3933 {
3934 case EOpMul:
3935 case EOpMulAssign:
3936 return left.getNominalSize() == right.getNominalSize() &&
3937 left.getSecondarySize() == right.getSecondarySize();
3938 case EOpVectorTimesScalar:
3939 return true;
3940 case EOpVectorTimesScalarAssign:
3941 ASSERT(!left.isMatrix() && !right.isMatrix());
3942 return left.isVector() && !right.isVector();
3943 case EOpVectorTimesMatrix:
3944 return left.getNominalSize() == right.getRows();
3945 case EOpVectorTimesMatrixAssign:
3946 ASSERT(!left.isMatrix() && right.isMatrix());
3947 return left.isVector() && left.getNominalSize() == right.getRows() &&
3948 left.getNominalSize() == right.getCols();
3949 case EOpMatrixTimesVector:
3950 return left.getCols() == right.getNominalSize();
3951 case EOpMatrixTimesScalar:
3952 return true;
3953 case EOpMatrixTimesScalarAssign:
3954 ASSERT(left.isMatrix() && !right.isMatrix());
3955 return !right.isVector();
3956 case EOpMatrixTimesMatrix:
3957 return left.getCols() == right.getRows();
3958 case EOpMatrixTimesMatrixAssign:
3959 ASSERT(left.isMatrix() && right.isMatrix());
3960 // We need to check two things:
3961 // 1. The matrix multiplication step is valid.
3962 // 2. The result will have the same number of columns as the lvalue.
3963 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3964
3965 default:
3966 UNREACHABLE();
3967 return false;
3968 }
3969}
3970
Jamie Madillb98c3a82015-07-23 14:26:04 -04003971TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3972 TIntermTyped *left,
3973 TIntermTyped *right,
3974 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003975{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003976 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003977 return nullptr;
3978
Olli Etuahofc1806e2015-03-17 13:03:11 +02003979 switch (op)
3980 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003981 case EOpEqual:
3982 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04003983 case EOpLessThan:
3984 case EOpGreaterThan:
3985 case EOpLessThanEqual:
3986 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04003987 break;
3988 case EOpLogicalOr:
3989 case EOpLogicalXor:
3990 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003991 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3992 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003993 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003994 {
3995 return nullptr;
3996 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003997 // Basic types matching should have been already checked.
3998 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003999 break;
4000 case EOpAdd:
4001 case EOpSub:
4002 case EOpDiv:
4003 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004004 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4005 !right->getType().getStruct());
4006 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004007 {
4008 return nullptr;
4009 }
4010 break;
4011 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004012 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4013 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004014 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004015 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004016 {
4017 return nullptr;
4018 }
4019 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004020 default:
4021 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004022 }
4023
Olli Etuaho1dded802016-08-18 18:13:13 +03004024 if (op == EOpMul)
4025 {
4026 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4027 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4028 {
4029 return nullptr;
4030 }
4031 }
4032
Olli Etuaho3fdec912016-08-18 15:08:06 +03004033 TIntermBinary *node = new TIntermBinary(op, left, right);
4034 node->setLine(loc);
4035
Olli Etuaho3fdec912016-08-18 15:08:06 +03004036 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004037 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004038 if (foldedNode)
4039 return foldedNode;
4040
4041 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004042}
4043
Jamie Madillb98c3a82015-07-23 14:26:04 -04004044TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4045 TIntermTyped *left,
4046 TIntermTyped *right,
4047 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004048{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004049 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004050 if (node == 0)
4051 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004052 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4053 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004054 return left;
4055 }
4056 return node;
4057}
4058
Jamie Madillb98c3a82015-07-23 14:26:04 -04004059TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4060 TIntermTyped *left,
4061 TIntermTyped *right,
4062 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004063{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004064 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004065 if (node == 0)
4066 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004067 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4068 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004069 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004070 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004071 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4072 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004073 }
4074 return node;
4075}
4076
Olli Etuaho13389b62016-10-16 11:48:18 +01004077TIntermBinary *TParseContext::createAssign(TOperator op,
4078 TIntermTyped *left,
4079 TIntermTyped *right,
4080 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004081{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004082 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004083 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004084 if (op == EOpMulAssign)
4085 {
4086 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4087 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4088 {
4089 return nullptr;
4090 }
4091 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004092 TIntermBinary *node = new TIntermBinary(op, left, right);
4093 node->setLine(loc);
4094
Olli Etuaho3fdec912016-08-18 15:08:06 +03004095 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004096 }
4097 return nullptr;
4098}
4099
Jamie Madillb98c3a82015-07-23 14:26:04 -04004100TIntermTyped *TParseContext::addAssign(TOperator op,
4101 TIntermTyped *left,
4102 TIntermTyped *right,
4103 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004104{
4105 TIntermTyped *node = createAssign(op, left, right, loc);
4106 if (node == nullptr)
4107 {
4108 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004109 return left;
4110 }
4111 return node;
4112}
4113
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004114TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4115 TIntermTyped *right,
4116 const TSourceLoc &loc)
4117{
Corentin Wallez0d959252016-07-12 17:26:32 -04004118 // WebGL2 section 5.26, the following results in an error:
4119 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004120 if (mShaderSpec == SH_WEBGL2_SPEC &&
4121 (left->isArray() || left->getBasicType() == EbtVoid ||
4122 left->getType().isStructureContainingArrays() || right->isArray() ||
4123 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004124 {
4125 error(loc,
4126 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4127 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004128 }
4129
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004130 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004131}
4132
Olli Etuaho49300862015-02-20 14:54:49 +02004133TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4134{
4135 switch (op)
4136 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004137 case EOpContinue:
4138 if (mLoopNestingLevel <= 0)
4139 {
4140 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004141 }
4142 break;
4143 case EOpBreak:
4144 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4145 {
4146 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004147 }
4148 break;
4149 case EOpReturn:
4150 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4151 {
4152 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004153 }
4154 break;
4155 default:
4156 // No checks for discard
4157 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004158 }
4159 return intermediate.addBranch(op, loc);
4160}
4161
Jamie Madillb98c3a82015-07-23 14:26:04 -04004162TIntermBranch *TParseContext::addBranch(TOperator op,
4163 TIntermTyped *returnValue,
4164 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004165{
4166 ASSERT(op == EOpReturn);
4167 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004168 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004169 {
4170 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004171 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004172 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004173 {
4174 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004175 }
4176 return intermediate.addBranch(op, returnValue, loc);
4177}
4178
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004179void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4180{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004181 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004182 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004183 TIntermNode *offset = nullptr;
4184 TIntermSequence *arguments = functionCall->getSequence();
4185 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4186 name.compare(0, 16, "textureLodOffset") == 0 ||
4187 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4188 name.compare(0, 17, "textureGradOffset") == 0 ||
4189 name.compare(0, 21, "textureProjGradOffset") == 0)
4190 {
4191 offset = arguments->back();
4192 }
4193 else if (name.compare(0, 13, "textureOffset") == 0 ||
4194 name.compare(0, 17, "textureProjOffset") == 0)
4195 {
4196 // A bias parameter might follow the offset parameter.
4197 ASSERT(arguments->size() >= 3);
4198 offset = (*arguments)[2];
4199 }
4200 if (offset != nullptr)
4201 {
4202 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4203 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4204 {
4205 TString unmangledName = TFunction::unmangleName(name);
4206 error(functionCall->getLine(), "Texture offset must be a constant expression",
4207 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004208 }
4209 else
4210 {
4211 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4212 size_t size = offsetConstantUnion->getType().getObjectSize();
4213 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4214 for (size_t i = 0u; i < size; ++i)
4215 {
4216 int offsetValue = values[i].getIConst();
4217 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4218 {
4219 std::stringstream tokenStream;
4220 tokenStream << offsetValue;
4221 std::string token = tokenStream.str();
4222 error(offset->getLine(), "Texture offset value out of valid range",
4223 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004224 }
4225 }
4226 }
4227 }
4228}
4229
Martin Radev2cc85b32016-08-05 16:22:53 +03004230// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4231void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4232{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004233 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004234 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4235
4236 if (name.compare(0, 5, "image") == 0)
4237 {
4238 TIntermSequence *arguments = functionCall->getSequence();
4239 TIntermNode *imageNode = (*arguments)[0];
4240 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4241
4242 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4243
4244 if (name.compare(5, 5, "Store") == 0)
4245 {
4246 if (memoryQualifier.readonly)
4247 {
4248 error(imageNode->getLine(),
4249 "'imageStore' cannot be used with images qualified as 'readonly'",
4250 imageSymbol->getSymbol().c_str());
4251 }
4252 }
4253 else if (name.compare(5, 4, "Load") == 0)
4254 {
4255 if (memoryQualifier.writeonly)
4256 {
4257 error(imageNode->getLine(),
4258 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4259 imageSymbol->getSymbol().c_str());
4260 }
4261 }
4262 }
4263}
4264
4265// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4266void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4267 const TFunction *functionDefinition,
4268 const TIntermAggregate *functionCall)
4269{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004270 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004271
4272 const TIntermSequence &arguments = *functionCall->getSequence();
4273
4274 ASSERT(functionDefinition->getParamCount() == arguments.size());
4275
4276 for (size_t i = 0; i < arguments.size(); ++i)
4277 {
4278 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4279 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4280 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4281
4282 if (IsImage(functionArgumentType.getBasicType()))
4283 {
4284 const TMemoryQualifier &functionArgumentMemoryQualifier =
4285 functionArgumentType.getMemoryQualifier();
4286 const TMemoryQualifier &functionParameterMemoryQualifier =
4287 functionParameterType.getMemoryQualifier();
4288 if (functionArgumentMemoryQualifier.readonly &&
4289 !functionParameterMemoryQualifier.readonly)
4290 {
4291 error(functionCall->getLine(),
4292 "Function call discards the 'readonly' qualifier from image",
4293 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4294 }
4295
4296 if (functionArgumentMemoryQualifier.writeonly &&
4297 !functionParameterMemoryQualifier.writeonly)
4298 {
4299 error(functionCall->getLine(),
4300 "Function call discards the 'writeonly' qualifier from image",
4301 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4302 }
Martin Radev049edfa2016-11-11 14:35:37 +02004303
4304 if (functionArgumentMemoryQualifier.coherent &&
4305 !functionParameterMemoryQualifier.coherent)
4306 {
4307 error(functionCall->getLine(),
4308 "Function call discards the 'coherent' qualifier from image",
4309 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4310 }
4311
4312 if (functionArgumentMemoryQualifier.volatileQualifier &&
4313 !functionParameterMemoryQualifier.volatileQualifier)
4314 {
4315 error(functionCall->getLine(),
4316 "Function call discards the 'volatile' qualifier from image",
4317 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4318 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004319 }
4320 }
4321}
4322
Olli Etuaho72d10202017-01-19 15:58:30 +00004323TIntermAggregate *TParseContext::createEmptyArgumentsNode(const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004324{
Olli Etuaho72d10202017-01-19 15:58:30 +00004325 TIntermAggregate *argumentsNode = new TIntermAggregate(EOpNull);
4326 argumentsNode->setLine(loc);
4327 return argumentsNode;
4328}
4329
4330TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4331 TIntermAggregate *argumentsNode,
4332 TIntermNode *thisNode,
4333 const TSourceLoc &loc)
4334{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004335 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004336 TIntermTyped *callNode = nullptr;
4337
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004338 if (thisNode != nullptr)
4339 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004340 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004341 int arraySize = 0;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004342 TIntermTyped *typedThis = thisNode->getAsTyped();
Olli Etuaho72d10202017-01-19 15:58:30 +00004343 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4344 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4345 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4346 // So accessing fnCall->getName() below is safe.
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004347 if (fnCall->getName() != "length")
4348 {
4349 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004350 }
Olli Etuaho72d10202017-01-19 15:58:30 +00004351 else if (!argumentsNode->getSequence()->empty())
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004352 {
4353 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004354 }
4355 else if (typedThis == nullptr || !typedThis->isArray())
4356 {
4357 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004358 }
4359 else
4360 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004361 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004362 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004363 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004364 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004365 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004366 // (func()).length()
4367 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004368 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4369 // expression.
4370 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4371 // spec section 5.9 which allows "An array, vector or matrix expression with the
4372 // length method applied".
4373 error(loc, "length can only be called on array names, not on array expressions",
4374 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004375 }
4376 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004377 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004378 callNode =
4379 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004380 }
4381 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004382 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004383 // Then this should be a constructor.
Olli Etuaho72d10202017-01-19 15:58:30 +00004384 callNode = addConstructor(argumentsNode, op, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004385 }
4386 else
4387 {
4388 //
4389 // Not a constructor. Find it in the symbol table.
4390 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304391 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004392 bool builtIn;
Olli Etuaho72d10202017-01-19 15:58:30 +00004393 for (TIntermNode *arg : *argumentsNode->getSequence())
4394 {
4395 fnCall->addParameter(TConstParameter(&arg->getAsTyped()->getType()));
4396 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004397 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004398 if (fnCandidate)
4399 {
4400 //
4401 // A declared function.
4402 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004403 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004404 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004405 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004406 }
4407 op = fnCandidate->getBuiltInOp();
4408 if (builtIn && op != EOpNull)
4409 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004410 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004411 if (fnCandidate->getParamCount() == 1)
4412 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004413 // Treat it like a built-in unary operator.
Olli Etuaho72d10202017-01-19 15:58:30 +00004414 TIntermNode *unaryParamNode = argumentsNode->getSequence()->front();
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004415 callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
4416 ASSERT(callNode != nullptr);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004417 }
4418 else
4419 {
Olli Etuaho72d10202017-01-19 15:58:30 +00004420 ASSERT(argumentsNode->getOp() == EOpNull);
4421 argumentsNode->setOp(op);
4422 argumentsNode->setType(fnCandidate->getReturnType());
4423 argumentsNode->setPrecisionForBuiltInOp();
4424 if (argumentsNode->areChildrenConstQualified())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004425 {
Olli Etuaho72d10202017-01-19 15:58:30 +00004426 argumentsNode->getTypePointer()->setQualifier(EvqConst);
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004427 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004428
4429 // Some built-in functions have out parameters too.
Olli Etuaho72d10202017-01-19 15:58:30 +00004430 functionCallLValueErrorCheck(fnCandidate, argumentsNode);
Arun Patole274f0702015-05-05 13:33:30 +05304431
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004432 // See if we can constant fold a built-in. Note that this may be possible even
4433 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004434 TIntermTyped *foldedNode =
Olli Etuaho72d10202017-01-19 15:58:30 +00004435 intermediate.foldAggregateBuiltIn(argumentsNode, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304436 if (foldedNode)
4437 {
Arun Patole274f0702015-05-05 13:33:30 +05304438 callNode = foldedNode;
4439 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004440 else
4441 {
Olli Etuaho72d10202017-01-19 15:58:30 +00004442 callNode = argumentsNode;
Olli Etuahob43846e2015-06-02 18:18:57 +03004443 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004444 }
4445 }
4446 else
4447 {
4448 // This is a real function call
Olli Etuaho72d10202017-01-19 15:58:30 +00004449 ASSERT(argumentsNode->getOp() == EOpNull);
Olli Etuaho72d10202017-01-19 15:58:30 +00004450 argumentsNode->setType(fnCandidate->getReturnType());
Olli Etuaho72d10202017-01-19 15:58:30 +00004451 argumentsNode->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004452
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004453 // If builtIn == false, the function is user defined - could be an overloaded
4454 // built-in as well.
4455 // if builtIn == true, it's a builtIn function with no op associated with it.
4456 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004457 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004458 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004459 argumentsNode->setOp(EOpCallBuiltInFunction);
Olli Etuaho72d10202017-01-19 15:58:30 +00004460 argumentsNode->setBuiltInFunctionPrecision();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004461
Olli Etuaho72d10202017-01-19 15:58:30 +00004462 checkTextureOffsetConst(argumentsNode);
Olli Etuaho72d10202017-01-19 15:58:30 +00004463 checkImageMemoryAccessForBuiltinFunctions(argumentsNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004464 }
4465 else
4466 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004467 argumentsNode->setOp(EOpCallFunctionInAST);
Olli Etuaho72d10202017-01-19 15:58:30 +00004468 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, argumentsNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004469 }
4470
Olli Etuaho72d10202017-01-19 15:58:30 +00004471 callNode = argumentsNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004472
Olli Etuaho72d10202017-01-19 15:58:30 +00004473 functionCallLValueErrorCheck(fnCandidate, argumentsNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004474 }
4475 }
4476 else
4477 {
4478 // error message was put out by findFunction()
4479 // Put on a dummy node for error recovery
Olli Etuaho72d10202017-01-19 15:58:30 +00004480 callNode = TIntermTyped::CreateZero(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004481 }
4482 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004483 return callNode;
4484}
4485
Jamie Madillb98c3a82015-07-23 14:26:04 -04004486TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004487 TIntermTyped *trueExpression,
4488 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004489 const TSourceLoc &loc)
4490{
Olli Etuaho856c4972016-08-08 11:38:39 +03004491 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004492
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004493 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004494 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004495 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4496 falseExpression->getCompleteString());
4497 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004498 }
Olli Etuahode318b22016-10-25 16:18:25 +01004499 if (IsOpaqueType(trueExpression->getBasicType()))
4500 {
4501 // ESSL 1.00 section 4.1.7
4502 // ESSL 3.00 section 4.1.7
4503 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4504 // Note that structs containing opaque types don't need to be checked as structs are
4505 // forbidden below.
4506 error(loc, "ternary operator is not allowed for opaque types", ":");
4507 return falseExpression;
4508 }
4509
Olli Etuahoa2d53032015-04-15 14:14:44 +03004510 // ESSL1 sections 5.2 and 5.7:
4511 // ESSL3 section 5.7:
4512 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004513 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004514 {
4515 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004516 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004517 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004518 // WebGL2 section 5.26, the following results in an error:
4519 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004520 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004521 {
4522 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004523 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004524 }
4525
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004526 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004527}
Olli Etuaho49300862015-02-20 14:54:49 +02004528
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004529//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004530// Parse an array of strings using yyparse.
4531//
4532// Returns 0 for success.
4533//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004534int PaParseStrings(size_t count,
4535 const char *const string[],
4536 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304537 TParseContext *context)
4538{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004539 if ((count == 0) || (string == NULL))
4540 return 1;
4541
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004542 if (glslang_initialize(context))
4543 return 1;
4544
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004545 int error = glslang_scan(count, string, length, context);
4546 if (!error)
4547 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004548
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004549 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004550
alokp@chromium.org6b495712012-06-29 00:06:58 +00004551 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004552}
Jamie Madill45bcc782016-11-07 13:58:48 -05004553
4554} // namespace sh