blob: 7fa3b0e8e15091ccee15d71e8a9f050994d41c0d [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
jchen104cdac9e2017-05-08 11:01:20 +080012#include "common/mathutil.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080017#include "compiler/translator/ValidateSwitch.h"
18#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030019#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000020
Jamie Madill45bcc782016-11-07 13:58:48 -050021namespace sh
22{
23
alokp@chromium.org8b851c62012-06-15 16:25:11 +000024///////////////////////////////////////////////////////////////////////
25//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026// Sub- vector and matrix fields
27//
28////////////////////////////////////////////////////////////////////////
29
Martin Radev2cc85b32016-08-05 16:22:53 +030030namespace
31{
32
33const int kWebGLMaxStructNesting = 4;
34
Olli Etuaho0f684632017-07-13 12:42:15 +030035bool ContainsSampler(const TStructure *structType);
36
Martin Radev2cc85b32016-08-05 16:22:53 +030037bool ContainsSampler(const TType &type)
38{
39 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030040 {
Martin Radev2cc85b32016-08-05 16:22:53 +030041 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030042 }
jchen10cc2a10e2017-05-03 14:05:12 +080043 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030044 {
Olli Etuaho0f684632017-07-13 12:42:15 +030045 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030046 }
47
48 return false;
49}
50
Olli Etuaho0f684632017-07-13 12:42:15 +030051bool ContainsSampler(const TStructure *structType)
52{
53 for (const auto &field : structType->fields())
54 {
55 if (ContainsSampler(*field->type()))
56 return true;
57 }
58 return false;
59}
60
Olli Etuaho485eefd2017-02-14 17:40:06 +000061// Get a token from an image argument to use as an error message token.
62const char *GetImageArgumentToken(TIntermTyped *imageNode)
63{
64 ASSERT(IsImage(imageNode->getBasicType()));
65 while (imageNode->getAsBinaryNode() &&
66 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
67 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
68 {
69 imageNode = imageNode->getAsBinaryNode()->getLeft();
70 }
71 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
72 if (imageSymbol)
73 {
74 return imageSymbol->getSymbol().c_str();
75 }
76 return "image";
77}
78
Olli Etuahocce89652017-06-19 16:04:09 +030079bool CanSetDefaultPrecisionOnType(const TPublicType &type)
80{
81 if (!SupportsPrecision(type.getBasicType()))
82 {
83 return false;
84 }
85 if (type.getBasicType() == EbtUInt)
86 {
87 // ESSL 3.00.4 section 4.5.4
88 return false;
89 }
90 if (type.isAggregate())
91 {
92 // Not allowed to set for aggregate types
93 return false;
94 }
95 return true;
96}
97
Martin Radev2cc85b32016-08-05 16:22:53 +030098} // namespace
99
jchen104cdac9e2017-05-08 11:01:20 +0800100// This tracks each binding point's current default offset for inheritance of subsequent
101// variables using the same binding, and keeps offsets unique and non overlapping.
102// See GLSL ES 3.1, section 4.4.6.
103class TParseContext::AtomicCounterBindingState
104{
105 public:
106 AtomicCounterBindingState() : mDefaultOffset(0) {}
107 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
108 // newly inserted span.
109 int insertSpan(int start, size_t length)
110 {
111 gl::RangeI newSpan(start, start + static_cast<int>(length));
112 for (const auto &span : mSpans)
113 {
114 if (newSpan.intersects(span))
115 {
116 return -1;
117 }
118 }
119 mSpans.push_back(newSpan);
120 mDefaultOffset = newSpan.high();
121 return start;
122 }
123 // Inserts a new span starting from the default offset.
124 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
125 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
126
127 private:
128 int mDefaultOffset;
129 std::vector<gl::RangeI> mSpans;
130};
131
Jamie Madillacb4b812016-11-07 13:50:29 -0500132TParseContext::TParseContext(TSymbolTable &symt,
133 TExtensionBehavior &ext,
134 sh::GLenum type,
135 ShShaderSpec spec,
136 ShCompileOptions options,
137 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000138 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500139 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300140 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300141 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500142 mShaderType(type),
143 mShaderSpec(spec),
144 mCompileOptions(options),
145 mShaderVersion(100),
146 mTreeRoot(nullptr),
147 mLoopNestingLevel(0),
148 mStructNestingLevel(0),
149 mSwitchNestingLevel(0),
150 mCurrentFunctionType(nullptr),
151 mFunctionReturnsValue(false),
152 mChecksPrecisionErrors(checksPrecErrors),
153 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800154 mDefaultUniformMatrixPacking(EmpColumnMajor),
155 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
156 mDefaultBufferMatrixPacking(EmpColumnMajor),
157 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000158 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500159 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000160 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500161 mShaderVersion,
162 mShaderType,
163 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000164 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500165 mScanner(nullptr),
166 mUsesFragData(false),
167 mUsesFragColor(false),
168 mUsesSecondaryOutputs(false),
169 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
170 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000171 mMultiviewAvailable(resources.OVR_multiview == 1),
Jamie Madillacb4b812016-11-07 13:50:29 -0500172 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000173 mNumViews(-1),
174 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000175 mMaxImageUnits(resources.MaxImageUnits),
176 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000177 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800178 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800179 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800180 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800181 mDeclaringFunction(false),
182 mGeometryShaderInputPrimitiveType(EptUndefined),
183 mGeometryShaderOutputPrimitiveType(EptUndefined),
184 mGeometryShaderInvocations(0),
185 mGeometryShaderMaxVertices(-1),
186 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
187 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices)
Jamie Madillacb4b812016-11-07 13:50:29 -0500188{
189 mComputeShaderLocalSize.fill(-1);
190}
191
jchen104cdac9e2017-05-08 11:01:20 +0800192TParseContext::~TParseContext()
193{
194}
195
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300196bool TParseContext::parseVectorFields(const TSourceLoc &line,
197 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400198 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300199 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300201 ASSERT(fieldOffsets);
202 size_t fieldCount = compString.size();
203 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530204 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000205 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000206 return false;
207 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300208 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209
Jamie Madillb98c3a82015-07-23 14:26:04 -0400210 enum
211 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000212 exyzw,
213 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000214 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000215 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300217 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530218 {
219 switch (compString[i])
220 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400221 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300222 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400223 fieldSet[i] = exyzw;
224 break;
225 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300226 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400227 fieldSet[i] = ergba;
228 break;
229 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300230 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400231 fieldSet[i] = estpq;
232 break;
233 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300234 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400235 fieldSet[i] = exyzw;
236 break;
237 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300238 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400239 fieldSet[i] = ergba;
240 break;
241 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300242 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400243 fieldSet[i] = estpq;
244 break;
245 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300246 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400247 fieldSet[i] = exyzw;
248 break;
249 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300250 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400251 fieldSet[i] = ergba;
252 break;
253 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300254 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400255 fieldSet[i] = estpq;
256 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530257
Jamie Madillb98c3a82015-07-23 14:26:04 -0400258 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300259 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400260 fieldSet[i] = exyzw;
261 break;
262 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300263 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400264 fieldSet[i] = ergba;
265 break;
266 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300267 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268 fieldSet[i] = estpq;
269 break;
270 default:
271 error(line, "illegal vector field selection", compString.c_str());
272 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000273 }
274 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300276 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530277 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300278 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530279 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400280 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000281 return false;
282 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283
Arun Patole7e7e68d2015-05-22 12:02:25 +0530284 if (i > 0)
285 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400286 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530287 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400288 error(line, "illegal - vector component fields not from the same set",
289 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000290 return false;
291 }
292 }
293 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000295 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296}
297
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298///////////////////////////////////////////////////////////////////////
299//
300// Errors
301//
302////////////////////////////////////////////////////////////////////////
303
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304//
305// Used by flex/bison to output all syntax and parsing errors.
306//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000307void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000309 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310}
311
Olli Etuaho4de340a2016-12-16 09:32:03 +0000312void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530313{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000314 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000315}
316
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200317void TParseContext::outOfRangeError(bool isError,
318 const TSourceLoc &loc,
319 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000320 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200321{
322 if (isError)
323 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000324 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200325 }
326 else
327 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000328 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200329 }
330}
331
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332//
333// Same error message for all places assignments don't work.
334//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530335void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000337 std::stringstream reasonStream;
338 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
339 std::string reason = reasonStream.str();
340 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341}
342
343//
344// Same error message for all places unary operations don't work.
345//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530346void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000348 std::stringstream reasonStream;
349 reasonStream << "wrong operand type - no operation '" << op
350 << "' exists that takes an operand of type " << operand
351 << " (or there is no acceptable conversion)";
352 std::string reason = reasonStream.str();
353 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354}
355
356//
357// Same error message for all binary operations don't work.
358//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400359void TParseContext::binaryOpError(const TSourceLoc &line,
360 const char *op,
361 TString left,
362 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000364 std::stringstream reasonStream;
365 reasonStream << "wrong operand types - no operation '" << op
366 << "' exists that takes a left-hand operand of type '" << left
367 << "' and a right operand of type '" << right
368 << "' (or there is no acceptable conversion)";
369 std::string reason = reasonStream.str();
370 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371}
372
Olli Etuaho856c4972016-08-08 11:38:39 +0300373void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
374 TPrecision precision,
375 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530376{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400377 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300378 return;
Martin Radev70866b82016-07-22 15:27:42 +0300379
380 if (precision != EbpUndefined && !SupportsPrecision(type))
381 {
382 error(line, "illegal type for precision qualifier", getBasicString(type));
383 }
384
Olli Etuaho183d7e22015-11-20 15:59:09 +0200385 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530386 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200387 switch (type)
388 {
389 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400390 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300391 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200392 case EbtInt:
393 case EbtUInt:
394 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400395 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300396 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200397 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800398 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200399 {
jchen10cc2a10e2017-05-03 14:05:12 +0800400 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300401 return;
402 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200403 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000404 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000405}
406
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407// Both test and if necessary, spit out an error, to see if the node is really
408// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300409bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500411 TIntermSymbol *symNode = node->getAsSymbolNode();
412 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100413 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
414
415 if (swizzleNode)
416 {
417 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
418 if (ok && swizzleNode->hasDuplicateOffsets())
419 {
420 error(line, " l-value of swizzle cannot have duplicate components", op);
421 return false;
422 }
423 return ok;
424 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000425
Arun Patole7e7e68d2015-05-22 12:02:25 +0530426 if (binaryNode)
427 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400428 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530429 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400430 case EOpIndexDirect:
431 case EOpIndexIndirect:
432 case EOpIndexDirectStruct:
433 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300434 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400435 default:
436 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000437 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000438 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300439 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000440 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000441
jchen10cc2a10e2017-05-03 14:05:12 +0800442 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530443 switch (node->getQualifier())
444 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400445 case EvqConst:
446 message = "can't modify a const";
447 break;
448 case EvqConstReadOnly:
449 message = "can't modify a const";
450 break;
451 case EvqAttribute:
452 message = "can't modify an attribute";
453 break;
454 case EvqFragmentIn:
455 message = "can't modify an input";
456 break;
457 case EvqVertexIn:
458 message = "can't modify an input";
459 break;
460 case EvqUniform:
461 message = "can't modify a uniform";
462 break;
463 case EvqVaryingIn:
464 message = "can't modify a varying";
465 break;
466 case EvqFragCoord:
467 message = "can't modify gl_FragCoord";
468 break;
469 case EvqFrontFacing:
470 message = "can't modify gl_FrontFacing";
471 break;
472 case EvqPointCoord:
473 message = "can't modify gl_PointCoord";
474 break;
Martin Radevb0883602016-08-04 17:48:58 +0300475 case EvqNumWorkGroups:
476 message = "can't modify gl_NumWorkGroups";
477 break;
478 case EvqWorkGroupSize:
479 message = "can't modify gl_WorkGroupSize";
480 break;
481 case EvqWorkGroupID:
482 message = "can't modify gl_WorkGroupID";
483 break;
484 case EvqLocalInvocationID:
485 message = "can't modify gl_LocalInvocationID";
486 break;
487 case EvqGlobalInvocationID:
488 message = "can't modify gl_GlobalInvocationID";
489 break;
490 case EvqLocalInvocationIndex:
491 message = "can't modify gl_LocalInvocationIndex";
492 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300493 case EvqViewIDOVR:
494 message = "can't modify gl_ViewID_OVR";
495 break;
Martin Radev802abe02016-08-04 17:48:32 +0300496 case EvqComputeIn:
497 message = "can't modify work group size variable";
498 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400499 default:
500 //
501 // Type that can't be written to?
502 //
503 if (node->getBasicType() == EbtVoid)
504 {
505 message = "can't modify void";
506 }
jchen10cc2a10e2017-05-03 14:05:12 +0800507 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400508 {
jchen10cc2a10e2017-05-03 14:05:12 +0800509 message = "can't modify a variable with type ";
510 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300511 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800512 else if (node->getMemoryQualifier().readonly)
513 {
514 message = "can't modify a readonly variable";
515 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000516 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517
jchen10cc2a10e2017-05-03 14:05:12 +0800518 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530519 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000520 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521
Olli Etuaho8a176262016-08-16 14:23:01 +0300522 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000523 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000524
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000525 //
526 // Everything else is okay, no error.
527 //
jchen10cc2a10e2017-05-03 14:05:12 +0800528 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300529 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000530
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 //
532 // If we get here, we have an error and a message.
533 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530534 if (symNode)
535 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000536 const char *symbol = symNode->getSymbol().c_str();
537 std::stringstream reasonStream;
538 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
539 std::string reason = reasonStream.str();
540 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000541 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530542 else
543 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000544 std::stringstream reasonStream;
545 reasonStream << "l-value required (" << message << ")";
546 std::string reason = reasonStream.str();
547 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000548 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549
Olli Etuaho8a176262016-08-16 14:23:01 +0300550 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000551}
552
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000553// Both test, and if necessary spit out an error, to see if the node is really
554// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300555void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000556{
Olli Etuaho383b7912016-08-05 11:22:59 +0300557 if (node->getQualifier() != EvqConst)
558 {
559 error(node->getLine(), "constant expression required", "");
560 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000561}
562
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563// Both test, and if necessary spit out an error, to see if the node is really
564// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300565void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566{
Olli Etuaho383b7912016-08-05 11:22:59 +0300567 if (!node->isScalarInt())
568 {
569 error(node->getLine(), "integer expression required", token);
570 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571}
572
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000573// Both test, and if necessary spit out an error, to see if we are currently
574// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800575bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576{
Olli Etuaho856c4972016-08-08 11:38:39 +0300577 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300578 {
579 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800580 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300581 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800582 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583}
584
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300585// ESSL 3.00.5 sections 3.8 and 3.9.
586// If it starts "gl_" or contains two consecutive underscores, it's reserved.
587// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300588bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000589{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530590 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300591 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530592 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300593 error(line, reservedErrMsg, "gl_");
594 return false;
595 }
596 if (sh::IsWebGLBasedSpec(mShaderSpec))
597 {
598 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530599 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300600 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300601 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000602 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300603 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530604 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300605 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300606 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 }
608 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300609 if (identifier.find("__") != TString::npos)
610 {
611 error(line,
612 "identifiers containing two consecutive underscores (__) are reserved as "
613 "possible future keywords",
614 identifier.c_str());
615 return false;
616 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300617 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000618}
619
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300620// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300621bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800622 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300623 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800625 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530626 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200627 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300628 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000629 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200630
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300631 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530632 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300633 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200634 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300635 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200636 {
jchen10cc2a10e2017-05-03 14:05:12 +0800637 std::string reason("cannot convert a variable with type ");
638 reason += getBasicString(argTyped->getBasicType());
639 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300640 return false;
641 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800642 else if (argTyped->getMemoryQualifier().writeonly)
643 {
644 error(line, "cannot convert a variable with writeonly", "constructor");
645 return false;
646 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200647 if (argTyped->getBasicType() == EbtVoid)
648 {
649 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300650 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200651 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000652 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653
Olli Etuaho856c4972016-08-08 11:38:39 +0300654 if (type.isArray())
655 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300656 // The size of an unsized constructor should already have been determined.
657 ASSERT(!type.isUnsizedArray());
658 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
659 {
660 error(line, "array constructor needs one argument per array element", "constructor");
661 return false;
662 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300663 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
664 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800665 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300666 {
667 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500668 if (argType.isArray())
669 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300670 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500671 return false;
672 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300673 if (!argType.sameElementType(type))
674 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000675 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300676 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300677 }
678 }
679 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300680 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300681 {
682 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300683 if (fields.size() != arguments->size())
684 {
685 error(line,
686 "Number of constructor parameters does not match the number of structure fields",
687 "constructor");
688 return false;
689 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300690
691 for (size_t i = 0; i < fields.size(); i++)
692 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800693 if (i >= arguments->size() ||
694 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300695 {
696 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000697 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300698 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300699 }
700 }
701 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300702 else
703 {
704 // We're constructing a scalar, vector, or matrix.
705
706 // Note: It's okay to have too many components available, but not okay to have unused
707 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
708 // there is an extra argument, so 'overFull' will become true.
709
710 size_t size = 0;
711 bool full = false;
712 bool overFull = false;
713 bool matrixArg = false;
714 for (TIntermNode *arg : *arguments)
715 {
716 const TIntermTyped *argTyped = arg->getAsTyped();
717 ASSERT(argTyped != nullptr);
718
Olli Etuaho487b63a2017-05-23 15:55:09 +0300719 if (argTyped->getBasicType() == EbtStruct)
720 {
721 error(line, "a struct cannot be used as a constructor argument for this type",
722 "constructor");
723 return false;
724 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300725 if (argTyped->getType().isArray())
726 {
727 error(line, "constructing from a non-dereferenced array", "constructor");
728 return false;
729 }
730 if (argTyped->getType().isMatrix())
731 {
732 matrixArg = true;
733 }
734
735 size += argTyped->getType().getObjectSize();
736 if (full)
737 {
738 overFull = true;
739 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300740 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300741 {
742 full = true;
743 }
744 }
745
746 if (type.isMatrix() && matrixArg)
747 {
748 if (arguments->size() != 1)
749 {
750 error(line, "constructing matrix from matrix can only take one argument",
751 "constructor");
752 return false;
753 }
754 }
755 else
756 {
757 if (size != 1 && size < type.getObjectSize())
758 {
759 error(line, "not enough data provided for construction", "constructor");
760 return false;
761 }
762 if (overFull)
763 {
764 error(line, "too many arguments", "constructor");
765 return false;
766 }
767 }
768 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300769
Olli Etuaho8a176262016-08-16 14:23:01 +0300770 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771}
772
Jamie Madillb98c3a82015-07-23 14:26:04 -0400773// This function checks to see if a void variable has been declared and raise an error message for
774// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775//
776// returns true in case of an error
777//
Olli Etuaho856c4972016-08-08 11:38:39 +0300778bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400779 const TString &identifier,
780 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300782 if (type == EbtVoid)
783 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000784 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300785 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300786 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787
Olli Etuaho8a176262016-08-16 14:23:01 +0300788 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789}
790
Jamie Madillb98c3a82015-07-23 14:26:04 -0400791// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300792// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300793bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300795 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530796 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000797 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300798 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530799 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300800 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801}
802
Jamie Madillb98c3a82015-07-23 14:26:04 -0400803// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300804// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300805void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806{
Martin Radev4a9cd802016-09-01 16:51:51 +0300807 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530808 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000809 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530810 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811}
812
jchen10cc2a10e2017-05-03 14:05:12 +0800813bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
814 const TTypeSpecifierNonArray &pType,
815 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530817 if (pType.type == EbtStruct)
818 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300819 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530820 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000821 std::stringstream reasonStream;
822 reasonStream << reason << " (structure contains a sampler)";
823 std::string reasonStr = reasonStream.str();
824 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300825 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 }
jchen10cc2a10e2017-05-03 14:05:12 +0800827 // only samplers need to be checked from structs, since other opaque types can't be struct
828 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300829 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530830 }
jchen10cc2a10e2017-05-03 14:05:12 +0800831 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530832 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000833 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300834 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000835 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836
Olli Etuaho8a176262016-08-16 14:23:01 +0300837 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838}
839
Olli Etuaho856c4972016-08-08 11:38:39 +0300840void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
841 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400842{
843 if (pType.layoutQualifier.location != -1)
844 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400845 error(line, "location must only be specified for a single input or output variable",
846 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400847 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400848}
849
Olli Etuaho856c4972016-08-08 11:38:39 +0300850void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
851 const TLayoutQualifier &layoutQualifier)
852{
853 if (layoutQualifier.location != -1)
854 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000855 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
856 if (mShaderVersion >= 310)
857 {
858 errorMsg =
859 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
860 }
861 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300862 }
863}
864
Martin Radev2cc85b32016-08-05 16:22:53 +0300865void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
866 TQualifier qualifier,
867 const TType &type)
868{
Martin Radev2cc85b32016-08-05 16:22:53 +0300869 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800870 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530871 {
jchen10cc2a10e2017-05-03 14:05:12 +0800872 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000873 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874}
875
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300877unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530879 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000880
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200881 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
882 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
883 // fold as array size.
884 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000885 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000886 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300887 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000888 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889
Olli Etuaho856c4972016-08-08 11:38:39 +0300890 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400891
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000892 if (constant->getBasicType() == EbtUInt)
893 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300894 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000895 }
896 else
897 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300898 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000899
Olli Etuaho856c4972016-08-08 11:38:39 +0300900 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000901 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400902 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300903 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000904 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400905
Olli Etuaho856c4972016-08-08 11:38:39 +0300906 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400907 }
908
Olli Etuaho856c4972016-08-08 11:38:39 +0300909 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400910 {
911 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300912 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400913 }
914
915 // The size of arrays is restricted here to prevent issues further down the
916 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
917 // 4096 registers so this should be reasonable even for aggressively optimizable code.
918 const unsigned int sizeLimit = 65536;
919
Olli Etuaho856c4972016-08-08 11:38:39 +0300920 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400921 {
922 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300923 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000924 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300925
926 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927}
928
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300930bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
931 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932{
Olli Etuaho8a176262016-08-16 14:23:01 +0300933 if ((elementQualifier.qualifier == EvqAttribute) ||
934 (elementQualifier.qualifier == EvqVertexIn) ||
935 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300936 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400937 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300938 TType(elementQualifier).getQualifierString());
939 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000940 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941
Olli Etuaho8a176262016-08-16 14:23:01 +0300942 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943}
944
Olli Etuaho8a176262016-08-16 14:23:01 +0300945// See if this element type can be formed into an array.
946bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000948 //
949 // Can the type be an array?
950 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300951 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400952 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300953 error(line, "cannot declare arrays of arrays",
954 TType(elementType).getCompleteString().c_str());
955 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000956 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300957 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
958 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
959 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300960 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300961 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300962 {
963 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300964 TType(elementType).getCompleteString().c_str());
965 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300966 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967
Olli Etuaho8a176262016-08-16 14:23:01 +0300968 return true;
969}
970
971// Check if this qualified element type can be formed into an array.
972bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
973 const TPublicType &elementType)
974{
975 if (checkIsValidTypeForArray(indexLocation, elementType))
976 {
977 return checkIsValidQualifierForArray(indexLocation, elementType);
978 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000979 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980}
981
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300983void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
984 const TString &identifier,
985 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986{
Olli Etuaho3739d232015-04-08 12:23:44 +0300987 ASSERT(type != nullptr);
988 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000989 {
990 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300991 type->qualifier = EvqTemporary;
992
993 // Generate informative error messages for ESSL1.
994 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400995 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000996 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530997 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400998 "structures containing arrays may not be declared constant since they cannot be "
999 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301000 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001001 }
1002 else
1003 {
1004 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1005 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001006 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001007 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001008 if (type->isUnsizedArray())
1009 {
1010 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001011 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012}
1013
Olli Etuaho2935c582015-04-08 14:32:06 +03001014// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015// and update the symbol table.
1016//
Olli Etuaho2935c582015-04-08 14:32:06 +03001017// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001019bool TParseContext::declareVariable(const TSourceLoc &line,
1020 const TString &identifier,
1021 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001022 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023{
Olli Etuaho2935c582015-04-08 14:32:06 +03001024 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025
Olli Etuaho43364892017-02-13 16:00:12 +00001026 checkBindingIsValid(line, type);
1027
Olli Etuaho856c4972016-08-08 11:38:39 +03001028 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029
Olli Etuaho2935c582015-04-08 14:32:06 +03001030 // gl_LastFragData may be redeclared with a new precision qualifier
1031 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1032 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001033 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1034 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001035 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001036 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001037 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001038 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001039 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001040 }
1041 }
1042 else
1043 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001044 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1045 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001046 return false;
1047 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001048 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049
Olli Etuaho8a176262016-08-16 14:23:01 +03001050 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001051 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001052
Olli Etuaho0f684632017-07-13 12:42:15 +03001053 (*variable) = symbolTable.declareVariable(&identifier, type);
1054 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001055 {
1056 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001057 return false;
1058 }
1059
Olli Etuaho8a176262016-08-16 14:23:01 +03001060 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001061 return false;
1062
1063 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064}
1065
Martin Radev70866b82016-07-22 15:27:42 +03001066void TParseContext::checkIsParameterQualifierValid(
1067 const TSourceLoc &line,
1068 const TTypeQualifierBuilder &typeQualifierBuilder,
1069 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301070{
Olli Etuahocce89652017-06-19 16:04:09 +03001071 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001072 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001073
1074 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301075 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001076 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1077 }
1078
1079 if (!IsImage(type->getBasicType()))
1080 {
Olli Etuaho43364892017-02-13 16:00:12 +00001081 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001082 }
1083 else
1084 {
1085 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001086 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087
Martin Radev70866b82016-07-22 15:27:42 +03001088 type->setQualifier(typeQualifier.qualifier);
1089
1090 if (typeQualifier.precision != EbpUndefined)
1091 {
1092 type->setPrecision(typeQualifier.precision);
1093 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094}
1095
Olli Etuaho856c4972016-08-08 11:38:39 +03001096bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001097{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001098 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001099 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301100 if (iter == extBehavior.end())
1101 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001102 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001103 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001104 }
zmo@google.comf5450912011-09-09 01:37:19 +00001105 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301106 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1107 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001108 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1109 // associated with more than one extension.
1110 if (extension == "GL_OVR_multiview")
1111 {
1112 return checkCanUseExtension(line, "GL_OVR_multiview2");
1113 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001114 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001115 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001116 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301117 if (iter->second == EBhWarn)
1118 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001119 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001120 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001121 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122
Olli Etuaho8a176262016-08-16 14:23:01 +03001123 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124}
1125
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001126// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1127// compile-time or link-time errors are the same whether or not the declaration is empty".
1128// This function implements all the checks that are done on qualifiers regardless of if the
1129// declaration is empty.
1130void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1131 const sh::TLayoutQualifier &layoutQualifier,
1132 const TSourceLoc &location)
1133{
1134 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1135 {
1136 error(location, "Shared memory declarations cannot have layout specified", "layout");
1137 }
1138
1139 if (layoutQualifier.matrixPacking != EmpUnspecified)
1140 {
1141 error(location, "layout qualifier only valid for interface blocks",
1142 getMatrixPackingString(layoutQualifier.matrixPacking));
1143 return;
1144 }
1145
1146 if (layoutQualifier.blockStorage != EbsUnspecified)
1147 {
1148 error(location, "layout qualifier only valid for interface blocks",
1149 getBlockStorageString(layoutQualifier.blockStorage));
1150 return;
1151 }
1152
1153 if (qualifier == EvqFragmentOut)
1154 {
1155 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1156 {
1157 error(location, "invalid layout qualifier combination", "yuv");
1158 return;
1159 }
1160 }
1161 else
1162 {
1163 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1164 }
1165
Olli Etuaho95468d12017-05-04 11:14:34 +03001166 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1167 // parsing steps. So it needs to be checked here.
1168 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1169 {
1170 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1171 }
1172
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001173 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1174 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1175 {
1176 canHaveLocation = true;
1177 // We're not checking whether the uniform location is in range here since that depends on
1178 // the type of the variable.
1179 // The type can only be fully determined for non-empty declarations.
1180 }
1181 if (!canHaveLocation)
1182 {
1183 checkLocationIsNotSpecified(location, layoutQualifier);
1184 }
1185}
1186
jchen104cdac9e2017-05-08 11:01:20 +08001187void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1188 const TSourceLoc &location)
1189{
1190 if (publicType.precision != EbpHigh)
1191 {
1192 error(location, "Can only be highp", "atomic counter");
1193 }
1194 // dEQP enforces compile error if location is specified. See uniform_location.test.
1195 if (publicType.layoutQualifier.location != -1)
1196 {
1197 error(location, "location must not be set for atomic_uint", "layout");
1198 }
1199 if (publicType.layoutQualifier.binding == -1)
1200 {
1201 error(location, "no binding specified", "atomic counter");
1202 }
1203}
1204
Martin Radevb8b01222016-11-20 23:25:53 +02001205void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1206 const TSourceLoc &location)
1207{
1208 if (publicType.isUnsizedArray())
1209 {
1210 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1211 // error. It is assumed that this applies to empty declarations as well.
1212 error(location, "empty array declaration needs to specify a size", "");
1213 }
Martin Radevb8b01222016-11-20 23:25:53 +02001214}
1215
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001216// These checks are done for all declarations that are non-empty. They're done for non-empty
1217// declarations starting a declarator list, and declarators that follow an empty declaration.
1218void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1219 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001220{
Olli Etuahofa33d582015-04-09 14:33:12 +03001221 switch (publicType.qualifier)
1222 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001223 case EvqVaryingIn:
1224 case EvqVaryingOut:
1225 case EvqAttribute:
1226 case EvqVertexIn:
1227 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001228 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001229 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001230 {
1231 error(identifierLocation, "cannot be used with a structure",
1232 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001233 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001234 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001235 break;
1236 case EvqBuffer:
1237 if (publicType.getBasicType() != EbtInterfaceBlock)
1238 {
1239 error(identifierLocation,
1240 "cannot declare buffer variables at global scope(outside a block)",
1241 getQualifierString(publicType.qualifier));
1242 return;
1243 }
1244 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001245 default:
1246 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001247 }
jchen10cc2a10e2017-05-03 14:05:12 +08001248 std::string reason(getBasicString(publicType.getBasicType()));
1249 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001250 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001251 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001252 {
1253 return;
1254 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001255
Andrei Volykhina5527072017-03-22 16:46:30 +03001256 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1257 publicType.qualifier != EvqConst) &&
1258 publicType.getBasicType() == EbtYuvCscStandardEXT)
1259 {
1260 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1261 getQualifierString(publicType.qualifier));
1262 return;
1263 }
1264
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001265 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1266 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001267 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1268 // But invalid shaders may still reach here with an unsized array declaration.
1269 if (!publicType.isUnsizedArray())
1270 {
1271 TType type(publicType);
1272 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1273 publicType.layoutQualifier);
1274 }
1275 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001276
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001277 // check for layout qualifier issues
1278 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001279
Martin Radev2cc85b32016-08-05 16:22:53 +03001280 if (IsImage(publicType.getBasicType()))
1281 {
1282
1283 switch (layoutQualifier.imageInternalFormat)
1284 {
1285 case EiifRGBA32F:
1286 case EiifRGBA16F:
1287 case EiifR32F:
1288 case EiifRGBA8:
1289 case EiifRGBA8_SNORM:
1290 if (!IsFloatImage(publicType.getBasicType()))
1291 {
1292 error(identifierLocation,
1293 "internal image format requires a floating image type",
1294 getBasicString(publicType.getBasicType()));
1295 return;
1296 }
1297 break;
1298 case EiifRGBA32I:
1299 case EiifRGBA16I:
1300 case EiifRGBA8I:
1301 case EiifR32I:
1302 if (!IsIntegerImage(publicType.getBasicType()))
1303 {
1304 error(identifierLocation,
1305 "internal image format requires an integer image type",
1306 getBasicString(publicType.getBasicType()));
1307 return;
1308 }
1309 break;
1310 case EiifRGBA32UI:
1311 case EiifRGBA16UI:
1312 case EiifRGBA8UI:
1313 case EiifR32UI:
1314 if (!IsUnsignedImage(publicType.getBasicType()))
1315 {
1316 error(identifierLocation,
1317 "internal image format requires an unsigned image type",
1318 getBasicString(publicType.getBasicType()));
1319 return;
1320 }
1321 break;
1322 case EiifUnspecified:
1323 error(identifierLocation, "layout qualifier", "No image internal format specified");
1324 return;
1325 default:
1326 error(identifierLocation, "layout qualifier", "unrecognized token");
1327 return;
1328 }
1329
1330 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1331 switch (layoutQualifier.imageInternalFormat)
1332 {
1333 case EiifR32F:
1334 case EiifR32I:
1335 case EiifR32UI:
1336 break;
1337 default:
1338 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1339 {
1340 error(identifierLocation, "layout qualifier",
1341 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1342 "image variables must be qualified readonly and/or writeonly");
1343 return;
1344 }
1345 break;
1346 }
1347 }
1348 else
1349 {
Olli Etuaho43364892017-02-13 16:00:12 +00001350 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001351 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1352 }
jchen104cdac9e2017-05-08 11:01:20 +08001353
1354 if (IsAtomicCounter(publicType.getBasicType()))
1355 {
1356 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1357 }
1358 else
1359 {
1360 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1361 }
Olli Etuaho43364892017-02-13 16:00:12 +00001362}
Martin Radev2cc85b32016-08-05 16:22:53 +03001363
Olli Etuaho43364892017-02-13 16:00:12 +00001364void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1365{
1366 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1367 int arraySize = type.isArray() ? type.getArraySize() : 1;
1368 if (IsImage(type.getBasicType()))
1369 {
1370 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1371 }
1372 else if (IsSampler(type.getBasicType()))
1373 {
1374 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1375 }
jchen104cdac9e2017-05-08 11:01:20 +08001376 else if (IsAtomicCounter(type.getBasicType()))
1377 {
1378 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1379 }
Olli Etuaho43364892017-02-13 16:00:12 +00001380 else
1381 {
1382 ASSERT(!IsOpaqueType(type.getBasicType()));
1383 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001384 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001385}
1386
Olli Etuaho856c4972016-08-08 11:38:39 +03001387void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1388 const TString &layoutQualifierName,
1389 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001390{
1391
1392 if (mShaderVersion < versionRequired)
1393 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001394 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001395 }
1396}
1397
Olli Etuaho856c4972016-08-08 11:38:39 +03001398bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1399 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001400{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001401 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001402 for (size_t i = 0u; i < localSize.size(); ++i)
1403 {
1404 if (localSize[i] != -1)
1405 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001406 error(location,
1407 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1408 "global layout declaration",
1409 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001410 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001411 }
1412 }
1413
Olli Etuaho8a176262016-08-16 14:23:01 +03001414 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001415}
1416
Olli Etuaho43364892017-02-13 16:00:12 +00001417void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001418 TLayoutImageInternalFormat internalFormat)
1419{
1420 if (internalFormat != EiifUnspecified)
1421 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001422 error(location, "invalid layout qualifier: only valid when used with images",
1423 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001424 }
Olli Etuaho43364892017-02-13 16:00:12 +00001425}
1426
1427void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1428{
1429 if (binding != -1)
1430 {
1431 error(location,
1432 "invalid layout qualifier: only valid when used with opaque types or blocks",
1433 "binding");
1434 }
1435}
1436
jchen104cdac9e2017-05-08 11:01:20 +08001437void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1438{
1439 if (offset != -1)
1440 {
1441 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1442 "offset");
1443 }
1444}
1445
Olli Etuaho43364892017-02-13 16:00:12 +00001446void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1447{
1448 // Expects arraySize to be 1 when setting binding for only a single variable.
1449 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1450 {
1451 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1452 }
1453}
1454
1455void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1456 int binding,
1457 int arraySize)
1458{
1459 // Expects arraySize to be 1 when setting binding for only a single variable.
1460 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1461 {
1462 error(location, "sampler binding greater than maximum texture units", "binding");
1463 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001464}
1465
Jiajia Qinbc585152017-06-23 15:42:17 +08001466void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1467 const TQualifier &qualifier,
1468 int binding,
1469 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001470{
1471 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001472 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001473 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001474 if (binding + size > mMaxUniformBufferBindings)
1475 {
1476 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1477 "binding");
1478 }
1479 }
1480 else if (qualifier == EvqBuffer)
1481 {
1482 if (binding + size > mMaxShaderStorageBufferBindings)
1483 {
1484 error(location,
1485 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1486 "binding");
1487 }
jchen10af713a22017-04-19 09:10:56 +08001488 }
1489}
jchen104cdac9e2017-05-08 11:01:20 +08001490void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1491{
1492 if (binding >= mMaxAtomicCounterBindings)
1493 {
1494 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1495 "binding");
1496 }
1497}
jchen10af713a22017-04-19 09:10:56 +08001498
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001499void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1500 int objectLocationCount,
1501 const TLayoutQualifier &layoutQualifier)
1502{
1503 int loc = layoutQualifier.location;
1504 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1505 {
1506 error(location, "Uniform location out of range", "location");
1507 }
1508}
1509
Andrei Volykhina5527072017-03-22 16:46:30 +03001510void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1511{
1512 if (yuv != false)
1513 {
1514 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1515 }
1516}
1517
Jiajia Qinbc585152017-06-23 15:42:17 +08001518void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1519 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001520{
1521 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1522 {
1523 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001524 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1525 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1526 qual == EvqInOut || qual == EvqConstReadOnly))
1527 {
1528 if (argument->getMemoryQualifier().writeonly)
1529 {
1530 error(argument->getLine(),
1531 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1532 fnCall->getFunctionSymbolInfo()->getName().c_str());
1533 return;
1534 }
1535 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001536 if (qual == EvqOut || qual == EvqInOut)
1537 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001538 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001539 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001540 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001541 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001542 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001543 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001544 }
1545 }
1546 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001547}
1548
Martin Radev70866b82016-07-22 15:27:42 +03001549void TParseContext::checkInvariantVariableQualifier(bool invariant,
1550 const TQualifier qualifier,
1551 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001552{
Martin Radev70866b82016-07-22 15:27:42 +03001553 if (!invariant)
1554 return;
1555
1556 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001557 {
Martin Radev70866b82016-07-22 15:27:42 +03001558 // input variables in the fragment shader can be also qualified as invariant
1559 if (!sh::CanBeInvariantESSL1(qualifier))
1560 {
1561 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1562 }
1563 }
1564 else
1565 {
1566 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1567 {
1568 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1569 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001570 }
1571}
1572
Arun Patole7e7e68d2015-05-22 12:02:25 +05301573bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001574{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001575 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001576 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1577 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001578}
1579
Arun Patole7e7e68d2015-05-22 12:02:25 +05301580bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001581{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001582 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001583}
1584
Jamie Madillb98c3a82015-07-23 14:26:04 -04001585void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1586 const char *extName,
1587 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001588{
1589 pp::SourceLocation srcLoc;
1590 srcLoc.file = loc.first_file;
1591 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001592 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001593}
1594
Jamie Madillb98c3a82015-07-23 14:26:04 -04001595void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1596 const char *name,
1597 const char *value,
1598 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001599{
1600 pp::SourceLocation srcLoc;
1601 srcLoc.file = loc.first_file;
1602 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001603 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001604}
1605
Martin Radev4c4c8e72016-08-04 12:25:34 +03001606sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001607{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001608 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001609 for (size_t i = 0u; i < result.size(); ++i)
1610 {
1611 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1612 {
1613 result[i] = 1;
1614 }
1615 else
1616 {
1617 result[i] = mComputeShaderLocalSize[i];
1618 }
1619 }
1620 return result;
1621}
1622
Olli Etuaho56229f12017-07-10 14:16:33 +03001623TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1624 const TSourceLoc &line)
1625{
1626 TIntermConstantUnion *node = new TIntermConstantUnion(
1627 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1628 node->setLine(line);
1629 return node;
1630}
1631
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632/////////////////////////////////////////////////////////////////////////////////
1633//
1634// Non-Errors.
1635//
1636/////////////////////////////////////////////////////////////////////////////////
1637
Jamie Madill5c097022014-08-20 16:38:32 -04001638const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1639 const TString *name,
1640 const TSymbol *symbol)
1641{
Jamie Madill5c097022014-08-20 16:38:32 -04001642 if (!symbol)
1643 {
1644 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001645 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001646 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001647
1648 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001649 {
1650 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001651 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001652 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001653
1654 const TVariable *variable = static_cast<const TVariable *>(symbol);
1655
1656 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
1657 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001658 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001659 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001660 }
1661
Olli Etuaho0f684632017-07-13 12:42:15 +03001662 // Reject shaders using both gl_FragData and gl_FragColor
1663 TQualifier qualifier = variable->getType().getQualifier();
1664 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001665 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001666 mUsesFragData = true;
1667 }
1668 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1669 {
1670 mUsesFragColor = true;
1671 }
1672 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1673 {
1674 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001675 }
1676
Olli Etuaho0f684632017-07-13 12:42:15 +03001677 // This validation is not quite correct - it's only an error to write to
1678 // both FragData and FragColor. For simplicity, and because users shouldn't
1679 // be rewarded for reading from undefined varaibles, return an error
1680 // if they are both referenced, rather than assigned.
1681 if (mUsesFragData && mUsesFragColor)
1682 {
1683 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1684 if (mUsesSecondaryOutputs)
1685 {
1686 errorMessage =
1687 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1688 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1689 }
1690 error(location, errorMessage, name->c_str());
1691 }
1692
1693 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1694 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1695 qualifier == EvqWorkGroupSize)
1696 {
1697 error(location,
1698 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1699 "gl_WorkGroupSize");
1700 }
Jamie Madill5c097022014-08-20 16:38:32 -04001701 return variable;
1702}
1703
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001704TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1705 const TString *name,
1706 const TSymbol *symbol)
1707{
1708 const TVariable *variable = getNamedVariable(location, name, symbol);
1709
Olli Etuaho0f684632017-07-13 12:42:15 +03001710 if (!variable)
1711 {
1712 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1713 node->setLine(location);
1714 return node;
1715 }
1716
Olli Etuaho09b04a22016-12-15 13:30:26 +00001717 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1718 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1719 {
1720 // WEBGL_multiview spec
1721 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1722 "gl_ViewID_OVR");
1723 }
1724
Olli Etuaho56229f12017-07-10 14:16:33 +03001725 TIntermTyped *node = nullptr;
1726
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001727 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001728 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001729 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001730 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001731 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001732 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1733 mComputeShaderLocalSizeDeclared)
1734 {
1735 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1736 // needs to be added to the AST as a constant and not as a symbol.
1737 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1738 TConstantUnion *constArray = new TConstantUnion[3];
1739 for (size_t i = 0; i < 3; ++i)
1740 {
1741 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1742 }
1743
1744 ASSERT(variable->getType().getBasicType() == EbtUInt);
1745 ASSERT(variable->getType().getObjectSize() == 3);
1746
1747 TType type(variable->getType());
1748 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001749 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001750 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001751 else
1752 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001753 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001754 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001755 ASSERT(node != nullptr);
1756 node->setLine(location);
1757 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001758}
1759
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760// Initializers show up in several places in the grammar. Have one set of
1761// code to handle them here.
1762//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001763// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001764bool TParseContext::executeInitializer(const TSourceLoc &line,
1765 const TString &identifier,
1766 const TPublicType &pType,
1767 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001768 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769{
Olli Etuaho13389b62016-10-16 11:48:18 +01001770 ASSERT(initNode != nullptr);
1771 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001772 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773
Olli Etuaho2935c582015-04-08 14:32:06 +03001774 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001775 if (type.isUnsizedArray())
1776 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001777 // We have not checked yet whether the initializer actually is an array or not.
1778 if (initializer->isArray())
1779 {
1780 type.setArraySize(initializer->getArraySize());
1781 }
1782 else
1783 {
1784 // Having a non-array initializer for an unsized array will result in an error later,
1785 // so we don't generate an error message here.
1786 type.setArraySize(1u);
1787 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001788 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001789 if (!declareVariable(line, identifier, type, &variable))
1790 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001791 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001792 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001793
Olli Etuahob0c645e2015-05-12 14:25:36 +03001794 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001795 if (symbolTable.atGlobalLevel() &&
1796 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001797 {
1798 // Error message does not completely match behavior with ESSL 1.00, but
1799 // we want to steer developers towards only using constant expressions.
1800 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001801 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001802 }
1803 if (globalInitWarning)
1804 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001805 warning(
1806 line,
1807 "global variable initializers should be constant expressions "
1808 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1809 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001810 }
1811
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001812 //
1813 // identifier must be of type constant, a global, or a temporary
1814 //
1815 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301816 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1817 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001818 error(line, " cannot initialize this type of qualifier ",
1819 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001820 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001821 }
1822 //
1823 // test for and propagate constant
1824 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825
Arun Patole7e7e68d2015-05-22 12:02:25 +05301826 if (qualifier == EvqConst)
1827 {
1828 if (qualifier != initializer->getType().getQualifier())
1829 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001830 std::stringstream reasonStream;
1831 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1832 << "'";
1833 std::string reason = reasonStream.str();
1834 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001835 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001836 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001837 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301838 if (type != initializer->getType())
1839 {
1840 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001841 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001842 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001843 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001844 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001845
1846 // Save the constant folded value to the variable if possible. For example array
1847 // initializers are not folded, since that way copying the array literal to multiple places
1848 // in the shader is avoided.
1849 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1850 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301851 if (initializer->getAsConstantUnion())
1852 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001853 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001854 ASSERT(*initNode == nullptr);
1855 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301856 }
1857 else if (initializer->getAsSymbolNode())
1858 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001859 const TSymbol *symbol =
1860 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1861 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001863 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001864 if (constArray)
1865 {
1866 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001867 ASSERT(*initNode == nullptr);
1868 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001869 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001870 }
1871 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001872
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001873 TIntermSymbol *intermSymbol =
1874 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1875 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001876 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1877 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001878 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001879 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001880 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001881 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001882
Olli Etuaho914b79a2017-06-19 16:03:19 +03001883 return true;
1884}
1885
1886TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1887 const TString &identifier,
1888 TIntermTyped *initializer,
1889 const TSourceLoc &loc)
1890{
1891 checkIsScalarBool(loc, pType);
1892 TIntermBinary *initNode = nullptr;
1893 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1894 {
1895 // The initializer is valid. The init condition needs to have a node - either the
1896 // initializer node, or a constant node in case the initialized variable is const and won't
1897 // be recorded in the AST.
1898 if (initNode == nullptr)
1899 {
1900 return initializer;
1901 }
1902 else
1903 {
1904 TIntermDeclaration *declaration = new TIntermDeclaration();
1905 declaration->appendDeclarator(initNode);
1906 return declaration;
1907 }
1908 }
1909 return nullptr;
1910}
1911
1912TIntermNode *TParseContext::addLoop(TLoopType type,
1913 TIntermNode *init,
1914 TIntermNode *cond,
1915 TIntermTyped *expr,
1916 TIntermNode *body,
1917 const TSourceLoc &line)
1918{
1919 TIntermNode *node = nullptr;
1920 TIntermTyped *typedCond = nullptr;
1921 if (cond)
1922 {
1923 typedCond = cond->getAsTyped();
1924 }
1925 if (cond == nullptr || typedCond)
1926 {
Olli Etuahocce89652017-06-19 16:04:09 +03001927 if (type == ELoopDoWhile)
1928 {
1929 checkIsScalarBool(line, typedCond);
1930 }
1931 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1932 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1933 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1934 !typedCond->isVector()));
1935
Olli Etuaho3ec75682017-07-05 17:02:55 +03001936 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001937 node->setLine(line);
1938 return node;
1939 }
1940
Olli Etuahocce89652017-06-19 16:04:09 +03001941 ASSERT(type != ELoopDoWhile);
1942
Olli Etuaho914b79a2017-06-19 16:03:19 +03001943 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1944 ASSERT(declaration);
1945 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1946 ASSERT(declarator->getLeft()->getAsSymbolNode());
1947
1948 // The condition is a declaration. In the AST representation we don't support declarations as
1949 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1950 // the loop.
1951 TIntermBlock *block = new TIntermBlock();
1952
1953 TIntermDeclaration *declareCondition = new TIntermDeclaration();
1954 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
1955 block->appendStatement(declareCondition);
1956
1957 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
1958 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03001959 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001960 block->appendStatement(loop);
1961 loop->setLine(line);
1962 block->setLine(line);
1963 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001964}
1965
Olli Etuahocce89652017-06-19 16:04:09 +03001966TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
1967 TIntermNodePair code,
1968 const TSourceLoc &loc)
1969{
Olli Etuaho56229f12017-07-10 14:16:33 +03001970 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03001971
1972 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03001973 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03001974 {
1975 if (cond->getAsConstantUnion()->getBConst(0) == true)
1976 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03001977 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03001978 }
1979 else
1980 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03001981 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03001982 }
1983 }
1984
Olli Etuaho3ec75682017-07-05 17:02:55 +03001985 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03001986 node->setLine(loc);
1987
1988 return node;
1989}
1990
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001991void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1992{
1993 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1994 typeSpecifier->getBasicType());
1995
1996 if (mShaderVersion < 300 && typeSpecifier->array)
1997 {
1998 error(typeSpecifier->getLine(), "not supported", "first-class array");
1999 typeSpecifier->clearArrayness();
2000 }
2001}
2002
Martin Radev70866b82016-07-22 15:27:42 +03002003TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302004 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002005{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002006 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002007
Martin Radev70866b82016-07-22 15:27:42 +03002008 TPublicType returnType = typeSpecifier;
2009 returnType.qualifier = typeQualifier.qualifier;
2010 returnType.invariant = typeQualifier.invariant;
2011 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002012 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002013 returnType.precision = typeSpecifier.precision;
2014
2015 if (typeQualifier.precision != EbpUndefined)
2016 {
2017 returnType.precision = typeQualifier.precision;
2018 }
2019
Martin Radev4a9cd802016-09-01 16:51:51 +03002020 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2021 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002022
Martin Radev4a9cd802016-09-01 16:51:51 +03002023 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2024 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002025
Martin Radev4a9cd802016-09-01 16:51:51 +03002026 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002027
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002028 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002029 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002030 if (typeSpecifier.array)
2031 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002032 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002033 returnType.clearArrayness();
2034 }
2035
Martin Radev70866b82016-07-22 15:27:42 +03002036 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002037 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002038 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002039 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002040 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002041 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002042
Martin Radev70866b82016-07-22 15:27:42 +03002043 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002044 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002045 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002046 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002047 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002048 }
2049 }
2050 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002051 {
Martin Radev70866b82016-07-22 15:27:42 +03002052 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002053 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002054 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002055 }
Martin Radev70866b82016-07-22 15:27:42 +03002056 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2057 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002058 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002059 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2060 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002061 }
Martin Radev70866b82016-07-22 15:27:42 +03002062 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002063 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002064 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002065 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002066 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002067 }
2068
2069 return returnType;
2070}
2071
Olli Etuaho856c4972016-08-08 11:38:39 +03002072void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2073 const TPublicType &type,
2074 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002075{
2076 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002077 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002078 {
2079 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002080 }
2081
2082 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2083 switch (qualifier)
2084 {
2085 case EvqVertexIn:
2086 // ESSL 3.00 section 4.3.4
2087 if (type.array)
2088 {
2089 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002090 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002091 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002092 return;
2093 case EvqFragmentOut:
2094 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002095 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002096 {
2097 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002098 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002099 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002100 return;
2101 default:
2102 break;
2103 }
2104
2105 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2106 // restrictions.
2107 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002108 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2109 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002110 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2111 {
2112 error(qualifierLocation, "must use 'flat' interpolation here",
2113 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002114 }
2115
Martin Radev4a9cd802016-09-01 16:51:51 +03002116 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002117 {
2118 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2119 // These restrictions are only implied by the ESSL 3.00 spec, but
2120 // the ESSL 3.10 spec lists these restrictions explicitly.
2121 if (type.array)
2122 {
2123 error(qualifierLocation, "cannot be an array of structures",
2124 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002125 }
2126 if (type.isStructureContainingArrays())
2127 {
2128 error(qualifierLocation, "cannot be a structure containing an array",
2129 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002130 }
2131 if (type.isStructureContainingType(EbtStruct))
2132 {
2133 error(qualifierLocation, "cannot be a structure containing a structure",
2134 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002135 }
2136 if (type.isStructureContainingType(EbtBool))
2137 {
2138 error(qualifierLocation, "cannot be a structure containing a bool",
2139 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002140 }
2141 }
2142}
2143
Martin Radev2cc85b32016-08-05 16:22:53 +03002144void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2145{
2146 if (qualifier.getType() == QtStorage)
2147 {
2148 const TStorageQualifierWrapper &storageQualifier =
2149 static_cast<const TStorageQualifierWrapper &>(qualifier);
2150 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2151 !symbolTable.atGlobalLevel())
2152 {
2153 error(storageQualifier.getLine(),
2154 "Local variables can only use the const storage qualifier.",
2155 storageQualifier.getQualifierString().c_str());
2156 }
2157 }
2158}
2159
Olli Etuaho43364892017-02-13 16:00:12 +00002160void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002161 const TSourceLoc &location)
2162{
Jiajia Qinbc585152017-06-23 15:42:17 +08002163 const std::string reason(
2164 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2165 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002166 if (memoryQualifier.readonly)
2167 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002168 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002169 }
2170 if (memoryQualifier.writeonly)
2171 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002172 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002173 }
Martin Radev049edfa2016-11-11 14:35:37 +02002174 if (memoryQualifier.coherent)
2175 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002176 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002177 }
2178 if (memoryQualifier.restrictQualifier)
2179 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002180 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002181 }
2182 if (memoryQualifier.volatileQualifier)
2183 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002184 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002185 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002186}
2187
jchen104cdac9e2017-05-08 11:01:20 +08002188// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2189// intermediate tree.
2190void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2191 size_t size,
2192 bool forceAppend,
2193 const TSourceLoc &loc,
2194 TType &type)
2195{
2196 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2197 int offset;
2198 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2199 {
2200 offset = bindingState.appendSpan(size);
2201 }
2202 else
2203 {
2204 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2205 }
2206 if (offset == -1)
2207 {
2208 error(loc, "Offset overlapping", "atomic counter");
2209 return;
2210 }
2211 TLayoutQualifier qualifier = type.getLayoutQualifier();
2212 qualifier.offset = offset;
2213 type.setLayoutQualifier(qualifier);
2214}
2215
Olli Etuaho13389b62016-10-16 11:48:18 +01002216TIntermDeclaration *TParseContext::parseSingleDeclaration(
2217 TPublicType &publicType,
2218 const TSourceLoc &identifierOrTypeLocation,
2219 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002220{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002221 TType type(publicType);
2222 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2223 mDirectiveHandler.pragma().stdgl.invariantAll)
2224 {
2225 TQualifier qualifier = type.getQualifier();
2226
2227 // The directive handler has already taken care of rejecting invalid uses of this pragma
2228 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2229 // affected variable declarations:
2230 //
2231 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2232 // elsewhere, in TranslatorGLSL.)
2233 //
2234 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2235 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2236 // the way this is currently implemented we have to enable this compiler option before
2237 // parsing the shader and determining the shading language version it uses. If this were
2238 // implemented as a post-pass, the workaround could be more targeted.
2239 //
2240 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2241 // the specification, but there are desktop OpenGL drivers that expect that this is the
2242 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2243 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2244 {
2245 type.setInvariant(true);
2246 }
2247 }
2248
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002249 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2250 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002251
Olli Etuahobab4c082015-04-24 16:38:49 +03002252 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002253 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002254
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002255 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002256 if (emptyDeclaration)
2257 {
Martin Radevb8b01222016-11-20 23:25:53 +02002258 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002259 // In most cases we don't need to create a symbol node for an empty declaration.
2260 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2261 if (type.getBasicType() == EbtStruct)
2262 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002263 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002264 }
jchen104cdac9e2017-05-08 11:01:20 +08002265 else if (IsAtomicCounter(publicType.getBasicType()))
2266 {
2267 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2268 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002269 }
2270 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002271 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002272 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002273
Olli Etuaho856c4972016-08-08 11:38:39 +03002274 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002275
jchen104cdac9e2017-05-08 11:01:20 +08002276 if (IsAtomicCounter(publicType.getBasicType()))
2277 {
2278
2279 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2280 identifierOrTypeLocation, type);
2281 }
2282
Olli Etuaho2935c582015-04-08 14:32:06 +03002283 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002284 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002285
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002286 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002287 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002288 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002289 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002290 }
2291
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002292 TIntermDeclaration *declaration = new TIntermDeclaration();
2293 declaration->setLine(identifierOrTypeLocation);
2294 if (symbol)
2295 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002296 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002297 declaration->appendDeclarator(symbol);
2298 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002299 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002300}
2301
Olli Etuaho13389b62016-10-16 11:48:18 +01002302TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2303 const TSourceLoc &identifierLocation,
2304 const TString &identifier,
2305 const TSourceLoc &indexLocation,
2306 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002307{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002308 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002309
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002310 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2311 identifierLocation);
2312
2313 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002314
Olli Etuaho856c4972016-08-08 11:38:39 +03002315 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002316
Olli Etuaho8a176262016-08-16 14:23:01 +03002317 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002318
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002319 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002320
Olli Etuaho856c4972016-08-08 11:38:39 +03002321 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002322 // Make the type an array even if size check failed.
2323 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2324 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002325
jchen104cdac9e2017-05-08 11:01:20 +08002326 if (IsAtomicCounter(publicType.getBasicType()))
2327 {
2328 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2329 identifierLocation, arrayType);
2330 }
2331
Olli Etuaho2935c582015-04-08 14:32:06 +03002332 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002333 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002334
Olli Etuaho13389b62016-10-16 11:48:18 +01002335 TIntermDeclaration *declaration = new TIntermDeclaration();
2336 declaration->setLine(identifierLocation);
2337
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002338 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002339 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002340 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2341 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002342 declaration->appendDeclarator(symbol);
2343 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002344
Olli Etuaho13389b62016-10-16 11:48:18 +01002345 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002346}
2347
Olli Etuaho13389b62016-10-16 11:48:18 +01002348TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2349 const TSourceLoc &identifierLocation,
2350 const TString &identifier,
2351 const TSourceLoc &initLocation,
2352 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002353{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002354 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002355
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002356 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2357 identifierLocation);
2358
2359 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002360
Olli Etuaho13389b62016-10-16 11:48:18 +01002361 TIntermDeclaration *declaration = new TIntermDeclaration();
2362 declaration->setLine(identifierLocation);
2363
2364 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002365 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002366 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002367 if (initNode)
2368 {
2369 declaration->appendDeclarator(initNode);
2370 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002371 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002372 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002373}
2374
Olli Etuaho13389b62016-10-16 11:48:18 +01002375TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002376 TPublicType &publicType,
2377 const TSourceLoc &identifierLocation,
2378 const TString &identifier,
2379 const TSourceLoc &indexLocation,
2380 TIntermTyped *indexExpression,
2381 const TSourceLoc &initLocation,
2382 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002383{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002384 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002385
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002386 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2387 identifierLocation);
2388
2389 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002390
Olli Etuaho8a176262016-08-16 14:23:01 +03002391 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002392
2393 TPublicType arrayType(publicType);
2394
Olli Etuaho856c4972016-08-08 11:38:39 +03002395 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002396 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2397 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002398 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002399 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002400 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002401 }
2402 // Make the type an array even if size check failed.
2403 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2404 arrayType.setArraySize(size);
2405
Olli Etuaho13389b62016-10-16 11:48:18 +01002406 TIntermDeclaration *declaration = new TIntermDeclaration();
2407 declaration->setLine(identifierLocation);
2408
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002409 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002410 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002411 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002412 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002413 if (initNode)
2414 {
2415 declaration->appendDeclarator(initNode);
2416 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002417 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002418
2419 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002420}
2421
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002422TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002423 const TTypeQualifierBuilder &typeQualifierBuilder,
2424 const TSourceLoc &identifierLoc,
2425 const TString *identifier,
2426 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002427{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002428 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002429
Martin Radev70866b82016-07-22 15:27:42 +03002430 if (!typeQualifier.invariant)
2431 {
2432 error(identifierLoc, "Expected invariant", identifier->c_str());
2433 return nullptr;
2434 }
2435 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2436 {
2437 return nullptr;
2438 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002439 if (!symbol)
2440 {
2441 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002442 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002443 }
Martin Radev70866b82016-07-22 15:27:42 +03002444 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002445 {
Martin Radev70866b82016-07-22 15:27:42 +03002446 error(identifierLoc, "invariant declaration specifies qualifier",
2447 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002448 }
Martin Radev70866b82016-07-22 15:27:42 +03002449 if (typeQualifier.precision != EbpUndefined)
2450 {
2451 error(identifierLoc, "invariant declaration specifies precision",
2452 getPrecisionString(typeQualifier.precision));
2453 }
2454 if (!typeQualifier.layoutQualifier.isEmpty())
2455 {
2456 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2457 }
2458
2459 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002460 if (!variable)
2461 {
2462 return nullptr;
2463 }
Martin Radev70866b82016-07-22 15:27:42 +03002464 const TType &type = variable->getType();
2465
2466 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2467 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002468 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002469
2470 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2471
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002472 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2473 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002474
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002475 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002476}
2477
Olli Etuaho13389b62016-10-16 11:48:18 +01002478void TParseContext::parseDeclarator(TPublicType &publicType,
2479 const TSourceLoc &identifierLocation,
2480 const TString &identifier,
2481 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002482{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002483 // If the declaration starting this declarator list was empty (example: int,), some checks were
2484 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002485 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002486 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002487 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2488 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002489 }
2490
Olli Etuaho856c4972016-08-08 11:38:39 +03002491 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002492
Olli Etuaho856c4972016-08-08 11:38:39 +03002493 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002494
Olli Etuaho2935c582015-04-08 14:32:06 +03002495 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002496 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002497 if (IsAtomicCounter(publicType.getBasicType()))
2498 {
2499 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2500 identifierLocation, type);
2501 }
Olli Etuaho43364892017-02-13 16:00:12 +00002502 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002503
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002504 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002505 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002506 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2507 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002508 declarationOut->appendDeclarator(symbol);
2509 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002510}
2511
Olli Etuaho13389b62016-10-16 11:48:18 +01002512void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2513 const TSourceLoc &identifierLocation,
2514 const TString &identifier,
2515 const TSourceLoc &arrayLocation,
2516 TIntermTyped *indexExpression,
2517 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002518{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002519 // If the declaration starting this declarator list was empty (example: int,), some checks were
2520 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002521 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002522 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002523 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2524 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002525 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002526
Olli Etuaho856c4972016-08-08 11:38:39 +03002527 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002528
Olli Etuaho856c4972016-08-08 11:38:39 +03002529 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002530
Olli Etuaho8a176262016-08-16 14:23:01 +03002531 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002532 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002533 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002534 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002535 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002536
jchen104cdac9e2017-05-08 11:01:20 +08002537 if (IsAtomicCounter(publicType.getBasicType()))
2538 {
2539 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2540 true, identifierLocation, arrayType);
2541 }
2542
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002543 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002544 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002545
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002546 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002547 {
2548 TIntermSymbol *symbol =
2549 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2550 symbol->setLine(identifierLocation);
2551 declarationOut->appendDeclarator(symbol);
2552 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002553 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002554}
2555
Olli Etuaho13389b62016-10-16 11:48:18 +01002556void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2557 const TSourceLoc &identifierLocation,
2558 const TString &identifier,
2559 const TSourceLoc &initLocation,
2560 TIntermTyped *initializer,
2561 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002562{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002563 // If the declaration starting this declarator list was empty (example: int,), some checks were
2564 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002565 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002566 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002567 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2568 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002569 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002570
Olli Etuaho856c4972016-08-08 11:38:39 +03002571 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002572
Olli Etuaho13389b62016-10-16 11:48:18 +01002573 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002574 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002575 {
2576 //
2577 // build the intermediate representation
2578 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002579 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002580 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002581 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002582 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002583 }
2584}
2585
Olli Etuaho13389b62016-10-16 11:48:18 +01002586void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2587 const TSourceLoc &identifierLocation,
2588 const TString &identifier,
2589 const TSourceLoc &indexLocation,
2590 TIntermTyped *indexExpression,
2591 const TSourceLoc &initLocation,
2592 TIntermTyped *initializer,
2593 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002594{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002595 // If the declaration starting this declarator list was empty (example: int,), some checks were
2596 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002597 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002598 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002599 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2600 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002601 }
2602
Olli Etuaho856c4972016-08-08 11:38:39 +03002603 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002604
Olli Etuaho8a176262016-08-16 14:23:01 +03002605 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002606
2607 TPublicType arrayType(publicType);
2608
Olli Etuaho856c4972016-08-08 11:38:39 +03002609 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002610 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2611 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002612 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002613 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002614 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002615 }
2616 // Make the type an array even if size check failed.
2617 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2618 arrayType.setArraySize(size);
2619
2620 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002621 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002622 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002623 {
2624 if (initNode)
2625 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002626 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002627 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002628 }
2629}
2630
jchen104cdac9e2017-05-08 11:01:20 +08002631void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2632 const TSourceLoc &location)
2633{
2634 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2635 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2636 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2637 {
2638 error(location, "Requires both binding and offset", "layout");
2639 return;
2640 }
2641 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2642}
2643
Olli Etuahocce89652017-06-19 16:04:09 +03002644void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2645 const TPublicType &type,
2646 const TSourceLoc &loc)
2647{
2648 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2649 !getFragmentPrecisionHigh())
2650 {
2651 error(loc, "precision is not supported in fragment shader", "highp");
2652 }
2653
2654 if (!CanSetDefaultPrecisionOnType(type))
2655 {
2656 error(loc, "illegal type argument for default precision qualifier",
2657 getBasicString(type.getBasicType()));
2658 return;
2659 }
2660 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2661}
2662
Shaob5cc1192017-07-06 10:47:20 +08002663bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2664{
2665 switch (typeQualifier.layoutQualifier.primitiveType)
2666 {
2667 case EptLines:
2668 case EptLinesAdjacency:
2669 case EptTriangles:
2670 case EptTrianglesAdjacency:
2671 return typeQualifier.qualifier == EvqGeometryIn;
2672
2673 case EptLineStrip:
2674 case EptTriangleStrip:
2675 return typeQualifier.qualifier == EvqGeometryOut;
2676
2677 case EptPoints:
2678 return true;
2679
2680 default:
2681 UNREACHABLE();
2682 return false;
2683 }
2684}
2685
2686bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2687{
2688 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2689
2690 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2691
2692 if (layoutQualifier.maxVertices != -1)
2693 {
2694 error(typeQualifier.line,
2695 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2696 return false;
2697 }
2698
2699 // Set mGeometryInputPrimitiveType if exists
2700 if (layoutQualifier.primitiveType != EptUndefined)
2701 {
2702 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2703 {
2704 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2705 return false;
2706 }
2707
2708 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2709 {
2710 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
2711 }
2712 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2713 {
2714 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2715 "layout");
2716 return false;
2717 }
2718 }
2719
2720 // Set mGeometryInvocations if exists
2721 if (layoutQualifier.invocations > 0)
2722 {
2723 if (mGeometryShaderInvocations == 0)
2724 {
2725 mGeometryShaderInvocations = layoutQualifier.invocations;
2726 }
2727 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2728 {
2729 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2730 "layout");
2731 return false;
2732 }
2733 }
2734
2735 return true;
2736}
2737
2738bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2739{
2740 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2741
2742 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2743
2744 if (layoutQualifier.invocations > 0)
2745 {
2746 error(typeQualifier.line,
2747 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2748 return false;
2749 }
2750
2751 // Set mGeometryOutputPrimitiveType if exists
2752 if (layoutQualifier.primitiveType != EptUndefined)
2753 {
2754 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2755 {
2756 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2757 return false;
2758 }
2759
2760 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2761 {
2762 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2763 }
2764 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2765 {
2766 error(typeQualifier.line,
2767 "primitive doesn't match earlier output primitive declaration", "layout");
2768 return false;
2769 }
2770 }
2771
2772 // Set mGeometryMaxVertices if exists
2773 if (layoutQualifier.maxVertices > -1)
2774 {
2775 if (mGeometryShaderMaxVertices == -1)
2776 {
2777 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2778 }
2779 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2780 {
2781 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2782 "layout");
2783 return false;
2784 }
2785 }
2786
2787 return true;
2788}
2789
Martin Radev70866b82016-07-22 15:27:42 +03002790void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002791{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002792 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002793 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002794
Martin Radev70866b82016-07-22 15:27:42 +03002795 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2796 typeQualifier.line);
2797
Jamie Madillc2128ff2016-07-04 10:26:17 -04002798 // It should never be the case, but some strange parser errors can send us here.
2799 if (layoutQualifier.isEmpty())
2800 {
2801 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002802 return;
2803 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002804
Martin Radev802abe02016-08-04 17:48:32 +03002805 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002806 {
Olli Etuaho43364892017-02-13 16:00:12 +00002807 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002808 return;
2809 }
2810
Olli Etuaho43364892017-02-13 16:00:12 +00002811 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2812
2813 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002814
2815 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2816
Andrei Volykhina5527072017-03-22 16:46:30 +03002817 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2818
jchen104cdac9e2017-05-08 11:01:20 +08002819 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2820
Martin Radev802abe02016-08-04 17:48:32 +03002821 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002822 {
Martin Radev802abe02016-08-04 17:48:32 +03002823 if (mComputeShaderLocalSizeDeclared &&
2824 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2825 {
2826 error(typeQualifier.line, "Work group size does not match the previous declaration",
2827 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002828 return;
2829 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002830
Martin Radev802abe02016-08-04 17:48:32 +03002831 if (mShaderVersion < 310)
2832 {
2833 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002834 return;
2835 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002836
Martin Radev4c4c8e72016-08-04 12:25:34 +03002837 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002838 {
2839 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002840 return;
2841 }
2842
2843 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2844 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2845
2846 const TConstantUnion *maxComputeWorkGroupSizeData =
2847 maxComputeWorkGroupSize->getConstPointer();
2848
2849 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2850 {
2851 if (layoutQualifier.localSize[i] != -1)
2852 {
2853 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2854 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2855 if (mComputeShaderLocalSize[i] < 1 ||
2856 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2857 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002858 std::stringstream reasonStream;
2859 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2860 << maxComputeWorkGroupSizeValue;
2861 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002862
Olli Etuaho4de340a2016-12-16 09:32:03 +00002863 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002864 return;
2865 }
2866 }
2867 }
2868
2869 mComputeShaderLocalSizeDeclared = true;
2870 }
Shaob5cc1192017-07-06 10:47:20 +08002871 else if (typeQualifier.qualifier == EvqGeometryIn)
2872 {
2873 if (mShaderVersion < 310)
2874 {
2875 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2876 return;
2877 }
2878
2879 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2880 {
2881 return;
2882 }
2883 }
2884 else if (typeQualifier.qualifier == EvqGeometryOut)
2885 {
2886 if (mShaderVersion < 310)
2887 {
2888 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2889 "layout");
2890 return;
2891 }
2892
2893 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2894 {
2895 return;
2896 }
2897 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002898 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002899 {
2900 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2901 // specification.
2902 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2903 {
2904 error(typeQualifier.line, "Number of views does not match the previous declaration",
2905 "layout");
2906 return;
2907 }
2908
2909 if (layoutQualifier.numViews == -1)
2910 {
2911 error(typeQualifier.line, "No num_views specified", "layout");
2912 return;
2913 }
2914
2915 if (layoutQualifier.numViews > mMaxNumViews)
2916 {
2917 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2918 "layout");
2919 return;
2920 }
2921
2922 mNumViews = layoutQualifier.numViews;
2923 }
Martin Radev802abe02016-08-04 17:48:32 +03002924 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002925 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002926 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002927 {
Martin Radev802abe02016-08-04 17:48:32 +03002928 return;
2929 }
2930
Jiajia Qinbc585152017-06-23 15:42:17 +08002931 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03002932 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002933 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00002934 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002935 return;
2936 }
2937
2938 if (mShaderVersion < 300)
2939 {
2940 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2941 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002942 return;
2943 }
2944
Olli Etuaho09b04a22016-12-15 13:30:26 +00002945 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002946
2947 if (layoutQualifier.matrixPacking != EmpUnspecified)
2948 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002949 if (typeQualifier.qualifier == EvqUniform)
2950 {
2951 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
2952 }
2953 else if (typeQualifier.qualifier == EvqBuffer)
2954 {
2955 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
2956 }
Martin Radev802abe02016-08-04 17:48:32 +03002957 }
2958
2959 if (layoutQualifier.blockStorage != EbsUnspecified)
2960 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002961 if (typeQualifier.qualifier == EvqUniform)
2962 {
2963 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
2964 }
2965 else if (typeQualifier.qualifier == EvqBuffer)
2966 {
2967 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
2968 }
Martin Radev802abe02016-08-04 17:48:32 +03002969 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002970 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002971}
2972
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002973TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2974 const TFunction &function,
2975 const TSourceLoc &location,
2976 bool insertParametersToSymbolTable)
2977{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03002978 checkIsNotReserved(location, function.getName());
2979
Olli Etuahofe486322017-03-21 09:30:54 +00002980 TIntermFunctionPrototype *prototype =
2981 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002982 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2983 // point to the data that already exists in the symbol table.
2984 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2985 prototype->setLine(location);
2986
2987 for (size_t i = 0; i < function.getParamCount(); i++)
2988 {
2989 const TConstParameter &param = function.getParam(i);
2990
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002991 TIntermSymbol *symbol = nullptr;
2992
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002993 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2994 // be used for unused args).
2995 if (param.name != nullptr)
2996 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002997 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002998 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002999 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003000 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3001 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003002 {
3003 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3004 variable->getType());
3005 }
3006 else
3007 {
3008 error(location, "redefinition", variable->getName().c_str());
3009 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003010 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003011 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003012 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003013 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003014 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3015 // symbol.
3016 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003017 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003018 symbol->setLine(location);
3019 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003020 }
3021 return prototype;
3022}
3023
Olli Etuaho16c745a2017-01-16 17:02:27 +00003024TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3025 const TFunction &parsedFunction,
3026 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003027{
Olli Etuaho476197f2016-10-11 13:59:08 +01003028 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3029 // first declaration. Either way the instance in the symbol table is used to track whether the
3030 // function is declared multiple times.
3031 TFunction *function = static_cast<TFunction *>(
3032 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3033 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003034 {
3035 // ESSL 1.00.17 section 4.2.7.
3036 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3037 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003038 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003039 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003040
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003041 TIntermFunctionPrototype *prototype =
3042 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003043
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003044 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003045
3046 if (!symbolTable.atGlobalLevel())
3047 {
3048 // ESSL 3.00.4 section 4.2.4.
3049 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003050 }
3051
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003052 return prototype;
3053}
3054
Olli Etuaho336b1472016-10-05 16:37:55 +01003055TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003056 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003057 TIntermBlock *functionBody,
3058 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003059{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003060 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003061 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3062 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003063 error(location, "function does not return a value:",
3064 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003065 }
3066
Olli Etuahof51fdd22016-10-03 10:03:40 +01003067 if (functionBody == nullptr)
3068 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003069 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003070 functionBody->setLine(location);
3071 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003072 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003073 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003074 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003075
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003076 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003077 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003078}
3079
Olli Etuaho476197f2016-10-11 13:59:08 +01003080void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3081 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003082 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003083{
Olli Etuaho476197f2016-10-11 13:59:08 +01003084 ASSERT(function);
3085 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003086 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003087 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003088
3089 if (builtIn)
3090 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003091 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003092 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003093 else
Jamie Madill185fb402015-06-12 15:48:48 -04003094 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003095 TFunction *prevDec = static_cast<TFunction *>(
3096 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3097
3098 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3099 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3100 // occurance.
3101 if (*function != prevDec)
3102 {
3103 // Swap the parameters of the previous declaration to the parameters of the function
3104 // definition (parameter names may differ).
3105 prevDec->swapParameters(**function);
3106
3107 // The function definition will share the same symbol as any previous declaration.
3108 *function = prevDec;
3109 }
3110
3111 if ((*function)->isDefined())
3112 {
3113 error(location, "function already has a body", (*function)->getName().c_str());
3114 }
3115
3116 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003117 }
Jamie Madill185fb402015-06-12 15:48:48 -04003118
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003119 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003120 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003121 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003122
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003123 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003124 setLoopNestingLevel(0);
3125}
3126
Jamie Madillb98c3a82015-07-23 14:26:04 -04003127TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003128{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003129 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003130 // We don't know at this point whether this is a function definition or a prototype.
3131 // The definition production code will check for redefinitions.
3132 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003133 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003134 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3135 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003136 //
3137 TFunction *prevDec =
3138 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303139
Martin Radevda6254b2016-12-14 17:00:36 +02003140 if (getShaderVersion() >= 300 &&
3141 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3142 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303143 {
Martin Radevda6254b2016-12-14 17:00:36 +02003144 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303145 // Therefore overloading or redefining builtin functions is an error.
3146 error(location, "Name of a built-in function cannot be redeclared as function",
3147 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303148 }
3149 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003150 {
3151 if (prevDec->getReturnType() != function->getReturnType())
3152 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003153 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003154 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003155 }
3156 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3157 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003158 if (prevDec->getParam(i).type->getQualifier() !=
3159 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003160 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003161 error(location,
3162 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003163 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003164 }
3165 }
3166 }
3167
3168 //
3169 // Check for previously declared variables using the same name.
3170 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003171 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003172 if (prevSym)
3173 {
3174 if (!prevSym->isFunction())
3175 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003176 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003177 }
3178 }
3179 else
3180 {
3181 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003182 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003183 }
3184
3185 // We're at the inner scope level of the function's arguments and body statement.
3186 // Add the function prototype to the surrounding scope instead.
3187 symbolTable.getOuterLevel()->insert(function);
3188
Olli Etuaho78d13742017-01-18 13:06:10 +00003189 // Raise error message if main function takes any parameters or return anything other than void
3190 if (function->getName() == "main")
3191 {
3192 if (function->getParamCount() > 0)
3193 {
3194 error(location, "function cannot take any parameter(s)", "main");
3195 }
3196 if (function->getReturnType().getBasicType() != EbtVoid)
3197 {
3198 error(location, "main function cannot return a value",
3199 function->getReturnType().getBasicString());
3200 }
3201 }
3202
Jamie Madill185fb402015-06-12 15:48:48 -04003203 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003204 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3205 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003206 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3207 //
3208 return function;
3209}
3210
Olli Etuaho9de84a52016-06-14 17:36:01 +03003211TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3212 const TString *name,
3213 const TSourceLoc &location)
3214{
3215 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3216 {
3217 error(location, "no qualifiers allowed for function return",
3218 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003219 }
3220 if (!type.layoutQualifier.isEmpty())
3221 {
3222 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003223 }
jchen10cc2a10e2017-05-03 14:05:12 +08003224 // make sure an opaque type is not involved as well...
3225 std::string reason(getBasicString(type.getBasicType()));
3226 reason += "s can't be function return values";
3227 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003228 if (mShaderVersion < 300)
3229 {
3230 // Array return values are forbidden, but there's also no valid syntax for declaring array
3231 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003232 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003233
3234 if (type.isStructureContainingArrays())
3235 {
3236 // ESSL 1.00.17 section 6.1 Function Definitions
3237 error(location, "structures containing arrays can't be function return values",
3238 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003239 }
3240 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003241
3242 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003243 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003244}
3245
Olli Etuahocce89652017-06-19 16:04:09 +03003246TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3247{
Olli Etuahocce89652017-06-19 16:04:09 +03003248 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003249 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003250}
3251
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003252TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003253{
Olli Etuahocce89652017-06-19 16:04:09 +03003254 if (mShaderVersion < 300 && publicType.array)
3255 {
3256 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3257 "[]");
3258 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003259 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003260 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003261 error(publicType.getLine(), "constructor can't be a structure definition",
3262 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003263 }
3264
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003265 TType *type = new TType(publicType);
3266 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003267 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003268 error(publicType.getLine(), "cannot construct this type",
3269 getBasicString(publicType.getBasicType()));
3270 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003271 }
3272
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003273 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003274}
3275
Olli Etuahocce89652017-06-19 16:04:09 +03003276TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3277 const TString *name,
3278 const TSourceLoc &nameLoc)
3279{
3280 if (publicType.getBasicType() == EbtVoid)
3281 {
3282 error(nameLoc, "illegal use of type 'void'", name->c_str());
3283 }
3284 checkIsNotReserved(nameLoc, *name);
3285 TType *type = new TType(publicType);
3286 TParameter param = {name, type};
3287 return param;
3288}
3289
3290TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3291 const TSourceLoc &identifierLoc,
3292 TIntermTyped *arraySize,
3293 const TSourceLoc &arrayLoc,
3294 TPublicType *type)
3295{
3296 checkIsValidTypeForArray(arrayLoc, *type);
3297 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3298 type->setArraySize(size);
3299 return parseParameterDeclarator(*type, identifier, identifierLoc);
3300}
3301
Jamie Madillb98c3a82015-07-23 14:26:04 -04003302// This function is used to test for the correctness of the parameters passed to various constructor
3303// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003304//
Olli Etuaho856c4972016-08-08 11:38:39 +03003305// 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 +00003306//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003307TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003308 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303309 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003310{
Olli Etuaho856c4972016-08-08 11:38:39 +03003311 if (type.isUnsizedArray())
3312 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003313 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003314 {
3315 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3316 type.setArraySize(1u);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003317 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003318 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003319 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03003320 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003321
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003322 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003323 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003324 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003325 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003326
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003327 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003328 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003329
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003330 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3331 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003332 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003333 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003334 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003335 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003336}
3337
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003338//
3339// Interface/uniform blocks
3340//
Olli Etuaho13389b62016-10-16 11:48:18 +01003341TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003342 const TTypeQualifierBuilder &typeQualifierBuilder,
3343 const TSourceLoc &nameLine,
3344 const TString &blockName,
3345 TFieldList *fieldList,
3346 const TString *instanceName,
3347 const TSourceLoc &instanceLine,
3348 TIntermTyped *arrayIndex,
3349 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003350{
Olli Etuaho856c4972016-08-08 11:38:39 +03003351 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003352
Olli Etuaho77ba4082016-12-16 12:01:18 +00003353 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003354
Jiajia Qinbc585152017-06-23 15:42:17 +08003355 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003356 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003357 error(typeQualifier.line,
3358 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3359 "3.10",
3360 getQualifierString(typeQualifier.qualifier));
3361 }
3362 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3363 {
3364 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003365 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003366 }
3367
Martin Radev70866b82016-07-22 15:27:42 +03003368 if (typeQualifier.invariant)
3369 {
3370 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3371 }
3372
Jiajia Qinbc585152017-06-23 15:42:17 +08003373 if (typeQualifier.qualifier != EvqBuffer)
3374 {
3375 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3376 }
Olli Etuaho43364892017-02-13 16:00:12 +00003377
jchen10af713a22017-04-19 09:10:56 +08003378 // add array index
3379 unsigned int arraySize = 0;
3380 if (arrayIndex != nullptr)
3381 {
3382 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3383 }
3384
3385 if (mShaderVersion < 310)
3386 {
3387 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3388 }
3389 else
3390 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003391 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3392 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003393 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003394
Andrei Volykhina5527072017-03-22 16:46:30 +03003395 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3396
Jamie Madill099c0f32013-06-20 11:55:52 -04003397 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003398 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003399
Jamie Madill099c0f32013-06-20 11:55:52 -04003400 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3401 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003402 if (typeQualifier.qualifier == EvqUniform)
3403 {
3404 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3405 }
3406 else if (typeQualifier.qualifier == EvqBuffer)
3407 {
3408 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3409 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003410 }
3411
Jamie Madill1566ef72013-06-20 11:55:54 -04003412 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3413 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003414 if (typeQualifier.qualifier == EvqUniform)
3415 {
3416 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3417 }
3418 else if (typeQualifier.qualifier == EvqBuffer)
3419 {
3420 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3421 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003422 }
3423
Olli Etuaho856c4972016-08-08 11:38:39 +03003424 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003425
Martin Radev2cc85b32016-08-05 16:22:53 +03003426 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3427
Olli Etuaho0f684632017-07-13 12:42:15 +03003428 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303429 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003430 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003431 }
3432
Jamie Madill98493dd2013-07-08 14:39:03 -04003433 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303434 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3435 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003436 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303437 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003438 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303439 {
jchen10cc2a10e2017-05-03 14:05:12 +08003440 std::string reason("unsupported type - ");
3441 reason += fieldType->getBasicString();
3442 reason += " types are not allowed in interface blocks";
3443 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003444 }
3445
Jamie Madill98493dd2013-07-08 14:39:03 -04003446 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003447 switch (qualifier)
3448 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003449 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003450 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003451 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003452 if (typeQualifier.qualifier == EvqBuffer)
3453 {
3454 error(field->line(), "invalid qualifier on shader storage block member",
3455 getQualifierString(qualifier));
3456 }
3457 break;
3458 case EvqBuffer:
3459 if (typeQualifier.qualifier == EvqUniform)
3460 {
3461 error(field->line(), "invalid qualifier on uniform block member",
3462 getQualifierString(qualifier));
3463 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003464 break;
3465 default:
3466 error(field->line(), "invalid qualifier on interface block member",
3467 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003468 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003469 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003470
Martin Radev70866b82016-07-22 15:27:42 +03003471 if (fieldType->isInvariant())
3472 {
3473 error(field->line(), "invalid qualifier on interface block member", "invariant");
3474 }
3475
Jamie Madilla5efff92013-06-06 11:56:47 -04003476 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003477 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003478 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003479 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003480
Jamie Madill98493dd2013-07-08 14:39:03 -04003481 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003482 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003483 error(field->line(), "invalid layout qualifier: cannot be used here",
3484 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003485 }
3486
Jamie Madill98493dd2013-07-08 14:39:03 -04003487 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003488 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003489 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003490 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003491 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003492 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003493 warning(field->line(),
3494 "extraneous layout qualifier: only has an effect on matrix types",
3495 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003496 }
3497
Jamie Madill98493dd2013-07-08 14:39:03 -04003498 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003499
3500 if (typeQualifier.qualifier == EvqBuffer)
3501 {
3502 // set memory qualifiers
3503 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3504 // qualified with a memory qualifier, it is as if all of its members were declared with
3505 // the same memory qualifier.
3506 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3507 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3508 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3509 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3510 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3511 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3512 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3513 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3514 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3515 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3516 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003517 }
3518
Jamie Madillb98c3a82015-07-23 14:26:04 -04003519 TInterfaceBlock *interfaceBlock =
3520 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
3521 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
3522 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003523
3524 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003525 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003526
Jamie Madill98493dd2013-07-08 14:39:03 -04003527 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003528 {
3529 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003530 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3531 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003532 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303533 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003534
3535 // set parent pointer of the field variable
3536 fieldType->setInterfaceBlock(interfaceBlock);
3537
Olli Etuaho0f684632017-07-13 12:42:15 +03003538 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003539
Olli Etuaho0f684632017-07-13 12:42:15 +03003540 if (fieldVariable)
3541 {
3542 fieldVariable->setQualifier(typeQualifier.qualifier);
3543 }
3544 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303545 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003546 error(field->line(), "redefinition of an interface block member name",
3547 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003548 }
3549 }
3550 }
3551 else
3552 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003553 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003554
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003555 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003556 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3557 if (instanceTypeDef)
3558 {
3559 instanceTypeDef->setQualifier(typeQualifier.qualifier);
3560 symbolId = instanceTypeDef->getUniqueId();
3561 }
3562 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303563 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003564 error(instanceLine, "redefinition of an interface block instance name",
3565 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003566 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003567 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003568 }
3569
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003570 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3571 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003572 TIntermDeclaration *declaration = new TIntermDeclaration();
3573 declaration->appendDeclarator(blockSymbol);
3574 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003575
3576 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003577 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003578}
3579
Olli Etuaho383b7912016-08-05 11:22:59 +03003580void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003581{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003582 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003583
3584 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003585 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303586 if (mStructNestingLevel > 1)
3587 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003588 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003589 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003590}
3591
3592void TParseContext::exitStructDeclaration()
3593{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003594 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003595}
3596
Olli Etuaho8a176262016-08-16 14:23:01 +03003597void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003598{
Jamie Madillacb4b812016-11-07 13:50:29 -05003599 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303600 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003601 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003602 }
3603
Arun Patole7e7e68d2015-05-22 12:02:25 +05303604 if (field.type()->getBasicType() != EbtStruct)
3605 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003606 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003607 }
3608
3609 // We're already inside a structure definition at this point, so add
3610 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303611 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3612 {
Jamie Madill41a49272014-03-18 16:10:13 -04003613 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003614 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3615 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003616 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003617 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003618 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003619 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003620}
3621
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003622//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003623// Parse an array index expression
3624//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003625TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3626 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303627 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003628{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003629 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3630 {
3631 if (baseExpression->getAsSymbolNode())
3632 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303633 error(location, " left of '[' is not of type array, matrix, or vector ",
3634 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003635 }
3636 else
3637 {
3638 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3639 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003640
Olli Etuaho3ec75682017-07-05 17:02:55 +03003641 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003642 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003643
Jamie Madill21c1e452014-12-29 11:33:41 -05003644 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3645
Olli Etuaho36b05142015-11-12 13:10:42 +02003646 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3647 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3648 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3649 // index is a constant expression.
3650 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3651 {
3652 if (baseExpression->isInterfaceBlock())
3653 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003654 error(location,
3655 "array indexes for interface blocks arrays must be constant integral expressions",
3656 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003657 }
3658 else if (baseExpression->getQualifier() == EvqFragmentOut)
3659 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003660 error(location,
3661 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003662 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003663 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3664 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003665 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003666 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003667 }
3668
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003669 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003670 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003671 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3672 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3673 // constant fold expressions that are not constant expressions). The most compatible way to
3674 // handle this case is to report a warning instead of an error and force the index to be in
3675 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003676 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003677 int index = 0;
3678 if (indexConstantUnion->getBasicType() == EbtInt)
3679 {
3680 index = indexConstantUnion->getIConst(0);
3681 }
3682 else if (indexConstantUnion->getBasicType() == EbtUInt)
3683 {
3684 index = static_cast<int>(indexConstantUnion->getUConst(0));
3685 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003686
3687 int safeIndex = -1;
3688
3689 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003690 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003691 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003692 {
Olli Etuahodaaff1c2017-07-05 18:03:26 +03003693 if (!isExtensionEnabled("GL_EXT_draw_buffers"))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003694 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003695 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003696 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003697 "GL_EXT_draw_buffers is disabled",
3698 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003699 safeIndex = 0;
3700 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003701 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003702 // Only do generic out-of-range check if similar error hasn't already been reported.
3703 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003704 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003705 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3706 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003707 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003708 }
3709 }
3710 else if (baseExpression->isMatrix())
3711 {
3712 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003713 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003714 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003715 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003716 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003717 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003718 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3719 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003720 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003721 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003722
3723 ASSERT(safeIndex >= 0);
3724 // Data of constant unions can't be changed, because it may be shared with other
3725 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3726 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003727 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003728 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003729 TConstantUnion *safeConstantUnion = new TConstantUnion();
3730 safeConstantUnion->setIConst(safeIndex);
3731 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003732 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003733 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003734
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003735 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3736 node->setLine(location);
3737 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003738 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003739 else
3740 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003741 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3742 node->setLine(location);
3743 // Indirect indexing can never be constant folded.
3744 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003745 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003746}
3747
Olli Etuaho90892fb2016-07-14 14:44:51 +03003748int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3749 const TSourceLoc &location,
3750 int index,
3751 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003752 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003753{
3754 if (index >= arraySize || index < 0)
3755 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003756 std::stringstream reasonStream;
3757 reasonStream << reason << " '" << index << "'";
3758 std::string token = reasonStream.str();
3759 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003760 if (index < 0)
3761 {
3762 return 0;
3763 }
3764 else
3765 {
3766 return arraySize - 1;
3767 }
3768 }
3769 return index;
3770}
3771
Jamie Madillb98c3a82015-07-23 14:26:04 -04003772TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3773 const TSourceLoc &dotLocation,
3774 const TString &fieldString,
3775 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003776{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003777 if (baseExpression->isArray())
3778 {
3779 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003780 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003781 }
3782
3783 if (baseExpression->isVector())
3784 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003785 TVector<int> fieldOffsets;
3786 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3787 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003788 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003789 fieldOffsets.resize(1);
3790 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003791 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003792 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3793 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003794
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003795 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003796 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003797 else if (baseExpression->getBasicType() == EbtStruct)
3798 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303799 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003800 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003801 {
3802 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003803 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003804 }
3805 else
3806 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003807 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003808 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003809 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003810 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003811 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003812 {
3813 fieldFound = true;
3814 break;
3815 }
3816 }
3817 if (fieldFound)
3818 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003819 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003820 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003821 TIntermBinary *node =
3822 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3823 node->setLine(dotLocation);
3824 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003825 }
3826 else
3827 {
3828 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003829 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003830 }
3831 }
3832 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003833 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003834 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303835 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003836 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003837 {
3838 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003839 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003840 }
3841 else
3842 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003843 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003844 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003845 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003846 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003847 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003848 {
3849 fieldFound = true;
3850 break;
3851 }
3852 }
3853 if (fieldFound)
3854 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003855 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003856 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003857 TIntermBinary *node =
3858 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3859 node->setLine(dotLocation);
3860 // Indexing interface blocks can never be constant folded.
3861 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003862 }
3863 else
3864 {
3865 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003866 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003867 }
3868 }
3869 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003870 else
3871 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003872 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003873 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003874 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303875 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003876 }
3877 else
3878 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303879 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003880 " field selection requires structure, vector, or interface block on left hand "
3881 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303882 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003883 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003884 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003885 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003886}
3887
Jamie Madillb98c3a82015-07-23 14:26:04 -04003888TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3889 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003890{
Martin Radev802abe02016-08-04 17:48:32 +03003891 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003892
3893 if (qualifierType == "shared")
3894 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003895 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003896 {
3897 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3898 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003899 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003900 }
3901 else if (qualifierType == "packed")
3902 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003903 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003904 {
3905 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3906 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003907 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003908 }
3909 else if (qualifierType == "std140")
3910 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003911 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003912 }
3913 else if (qualifierType == "row_major")
3914 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003915 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003916 }
3917 else if (qualifierType == "column_major")
3918 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003919 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003920 }
3921 else if (qualifierType == "location")
3922 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003923 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3924 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003925 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003926 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3927 mShaderType == GL_FRAGMENT_SHADER)
3928 {
3929 qualifier.yuv = true;
3930 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003931 else if (qualifierType == "rgba32f")
3932 {
3933 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3934 qualifier.imageInternalFormat = EiifRGBA32F;
3935 }
3936 else if (qualifierType == "rgba16f")
3937 {
3938 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3939 qualifier.imageInternalFormat = EiifRGBA16F;
3940 }
3941 else if (qualifierType == "r32f")
3942 {
3943 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3944 qualifier.imageInternalFormat = EiifR32F;
3945 }
3946 else if (qualifierType == "rgba8")
3947 {
3948 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3949 qualifier.imageInternalFormat = EiifRGBA8;
3950 }
3951 else if (qualifierType == "rgba8_snorm")
3952 {
3953 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3954 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3955 }
3956 else if (qualifierType == "rgba32i")
3957 {
3958 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3959 qualifier.imageInternalFormat = EiifRGBA32I;
3960 }
3961 else if (qualifierType == "rgba16i")
3962 {
3963 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3964 qualifier.imageInternalFormat = EiifRGBA16I;
3965 }
3966 else if (qualifierType == "rgba8i")
3967 {
3968 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3969 qualifier.imageInternalFormat = EiifRGBA8I;
3970 }
3971 else if (qualifierType == "r32i")
3972 {
3973 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3974 qualifier.imageInternalFormat = EiifR32I;
3975 }
3976 else if (qualifierType == "rgba32ui")
3977 {
3978 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3979 qualifier.imageInternalFormat = EiifRGBA32UI;
3980 }
3981 else if (qualifierType == "rgba16ui")
3982 {
3983 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3984 qualifier.imageInternalFormat = EiifRGBA16UI;
3985 }
3986 else if (qualifierType == "rgba8ui")
3987 {
3988 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3989 qualifier.imageInternalFormat = EiifRGBA8UI;
3990 }
3991 else if (qualifierType == "r32ui")
3992 {
3993 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3994 qualifier.imageInternalFormat = EiifR32UI;
3995 }
Shaob5cc1192017-07-06 10:47:20 +08003996 else if (qualifierType == "points" && isExtensionEnabled("GL_OES_geometry_shader") &&
3997 mShaderType == GL_GEOMETRY_SHADER_OES)
3998 {
3999 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4000 qualifier.primitiveType = EptPoints;
4001 }
4002 else if (qualifierType == "lines" && isExtensionEnabled("GL_OES_geometry_shader") &&
4003 mShaderType == GL_GEOMETRY_SHADER_OES)
4004 {
4005 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4006 qualifier.primitiveType = EptLines;
4007 }
4008 else if (qualifierType == "lines_adjacency" && isExtensionEnabled("GL_OES_geometry_shader") &&
4009 mShaderType == GL_GEOMETRY_SHADER_OES)
4010 {
4011 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4012 qualifier.primitiveType = EptLinesAdjacency;
4013 }
4014 else if (qualifierType == "triangles" && isExtensionEnabled("GL_OES_geometry_shader") &&
4015 mShaderType == GL_GEOMETRY_SHADER_OES)
4016 {
4017 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4018 qualifier.primitiveType = EptTriangles;
4019 }
4020 else if (qualifierType == "triangles_adjacency" &&
4021 isExtensionEnabled("GL_OES_geometry_shader") && mShaderType == GL_GEOMETRY_SHADER_OES)
4022 {
4023 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4024 qualifier.primitiveType = EptTrianglesAdjacency;
4025 }
4026 else if (qualifierType == "line_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4027 mShaderType == GL_GEOMETRY_SHADER_OES)
4028 {
4029 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4030 qualifier.primitiveType = EptLineStrip;
4031 }
4032 else if (qualifierType == "triangle_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4033 mShaderType == GL_GEOMETRY_SHADER_OES)
4034 {
4035 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4036 qualifier.primitiveType = EptTriangleStrip;
4037 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004038
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004039 else
4040 {
4041 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004042 }
4043
Jamie Madilla5efff92013-06-06 11:56:47 -04004044 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004045}
4046
Martin Radev802abe02016-08-04 17:48:32 +03004047void TParseContext::parseLocalSize(const TString &qualifierType,
4048 const TSourceLoc &qualifierTypeLine,
4049 int intValue,
4050 const TSourceLoc &intValueLine,
4051 const std::string &intValueString,
4052 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004053 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004054{
Olli Etuaho856c4972016-08-08 11:38:39 +03004055 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004056 if (intValue < 1)
4057 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004058 std::stringstream reasonStream;
4059 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4060 std::string reason = reasonStream.str();
4061 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004062 }
4063 (*localSize)[index] = intValue;
4064}
4065
Olli Etuaho09b04a22016-12-15 13:30:26 +00004066void TParseContext::parseNumViews(int intValue,
4067 const TSourceLoc &intValueLine,
4068 const std::string &intValueString,
4069 int *numViews)
4070{
4071 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4072 // specification.
4073 if (intValue < 1)
4074 {
4075 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4076 }
4077 *numViews = intValue;
4078}
4079
Shaob5cc1192017-07-06 10:47:20 +08004080void TParseContext::parseInvocations(int intValue,
4081 const TSourceLoc &intValueLine,
4082 const std::string &intValueString,
4083 int *numInvocations)
4084{
4085 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4086 // it doesn't make sense to accept invocations <= 0.
4087 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4088 {
4089 error(intValueLine,
4090 "out of range: invocations must be in the range of [1, "
4091 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4092 intValueString.c_str());
4093 }
4094 else
4095 {
4096 *numInvocations = intValue;
4097 }
4098}
4099
4100void TParseContext::parseMaxVertices(int intValue,
4101 const TSourceLoc &intValueLine,
4102 const std::string &intValueString,
4103 int *maxVertices)
4104{
4105 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4106 // it doesn't make sense to accept max_vertices < 0.
4107 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4108 {
4109 error(
4110 intValueLine,
4111 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4112 intValueString.c_str());
4113 }
4114 else
4115 {
4116 *maxVertices = intValue;
4117 }
4118}
4119
Jamie Madillb98c3a82015-07-23 14:26:04 -04004120TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4121 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004122 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304123 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004124{
Martin Radev802abe02016-08-04 17:48:32 +03004125 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004126
Martin Radev802abe02016-08-04 17:48:32 +03004127 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004128
Martin Radev802abe02016-08-04 17:48:32 +03004129 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004130 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004131 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004132 if (intValue < 0)
4133 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004134 error(intValueLine, "out of range: location must be non-negative",
4135 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004136 }
4137 else
4138 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004139 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004140 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004141 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004142 }
Olli Etuaho43364892017-02-13 16:00:12 +00004143 else if (qualifierType == "binding")
4144 {
4145 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4146 if (intValue < 0)
4147 {
4148 error(intValueLine, "out of range: binding must be non-negative",
4149 intValueString.c_str());
4150 }
4151 else
4152 {
4153 qualifier.binding = intValue;
4154 }
4155 }
jchen104cdac9e2017-05-08 11:01:20 +08004156 else if (qualifierType == "offset")
4157 {
4158 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4159 if (intValue < 0)
4160 {
4161 error(intValueLine, "out of range: offset must be non-negative",
4162 intValueString.c_str());
4163 }
4164 else
4165 {
4166 qualifier.offset = intValue;
4167 }
4168 }
Martin Radev802abe02016-08-04 17:48:32 +03004169 else if (qualifierType == "local_size_x")
4170 {
4171 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4172 &qualifier.localSize);
4173 }
4174 else if (qualifierType == "local_size_y")
4175 {
4176 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4177 &qualifier.localSize);
4178 }
4179 else if (qualifierType == "local_size_z")
4180 {
4181 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4182 &qualifier.localSize);
4183 }
Olli Etuaho95468d12017-05-04 11:14:34 +03004184 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004185 mShaderType == GL_VERTEX_SHADER)
4186 {
4187 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4188 }
Shaob5cc1192017-07-06 10:47:20 +08004189 else if (qualifierType == "invocations" && isExtensionEnabled("GL_OES_geometry_shader") &&
4190 mShaderType == GL_GEOMETRY_SHADER_OES)
4191 {
4192 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4193 }
4194 else if (qualifierType == "max_vertices" && isExtensionEnabled("GL_OES_geometry_shader") &&
4195 mShaderType == GL_GEOMETRY_SHADER_OES)
4196 {
4197 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4198 }
4199
Martin Radev802abe02016-08-04 17:48:32 +03004200 else
4201 {
4202 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004203 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004204
Jamie Madilla5efff92013-06-06 11:56:47 -04004205 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004206}
4207
Olli Etuaho613b9592016-09-05 12:05:53 +03004208TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4209{
4210 return new TTypeQualifierBuilder(
4211 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4212 mShaderVersion);
4213}
4214
Olli Etuahocce89652017-06-19 16:04:09 +03004215TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4216 const TSourceLoc &loc)
4217{
4218 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4219 return new TStorageQualifierWrapper(qualifier, loc);
4220}
4221
4222TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4223{
4224 if (getShaderType() == GL_VERTEX_SHADER)
4225 {
4226 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4227 }
4228 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4229}
4230
4231TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4232{
4233 if (declaringFunction())
4234 {
4235 return new TStorageQualifierWrapper(EvqIn, loc);
4236 }
Shaob5cc1192017-07-06 10:47:20 +08004237
4238 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004239 {
Shaob5cc1192017-07-06 10:47:20 +08004240 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004241 {
Shaob5cc1192017-07-06 10:47:20 +08004242 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
4243 {
4244 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4245 }
4246 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004247 }
Shaob5cc1192017-07-06 10:47:20 +08004248 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004249 {
Shaob5cc1192017-07-06 10:47:20 +08004250 if (mShaderVersion < 300)
4251 {
4252 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4253 }
4254 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004255 }
Shaob5cc1192017-07-06 10:47:20 +08004256 case GL_COMPUTE_SHADER:
4257 {
4258 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4259 }
4260 case GL_GEOMETRY_SHADER_OES:
4261 {
4262 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4263 }
4264 default:
4265 {
4266 UNREACHABLE();
4267 return new TStorageQualifierWrapper(EvqLast, loc);
4268 }
Olli Etuahocce89652017-06-19 16:04:09 +03004269 }
Olli Etuahocce89652017-06-19 16:04:09 +03004270}
4271
4272TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4273{
4274 if (declaringFunction())
4275 {
4276 return new TStorageQualifierWrapper(EvqOut, loc);
4277 }
Shaob5cc1192017-07-06 10:47:20 +08004278 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004279 {
Shaob5cc1192017-07-06 10:47:20 +08004280 case GL_VERTEX_SHADER:
4281 {
4282 if (mShaderVersion < 300)
4283 {
4284 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4285 }
4286 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4287 }
4288 case GL_FRAGMENT_SHADER:
4289 {
4290 if (mShaderVersion < 300)
4291 {
4292 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4293 }
4294 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4295 }
4296 case GL_COMPUTE_SHADER:
4297 {
4298 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4299 return new TStorageQualifierWrapper(EvqLast, loc);
4300 }
4301 case GL_GEOMETRY_SHADER_OES:
4302 {
4303 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4304 }
4305 default:
4306 {
4307 UNREACHABLE();
4308 return new TStorageQualifierWrapper(EvqLast, loc);
4309 }
Olli Etuahocce89652017-06-19 16:04:09 +03004310 }
Olli Etuahocce89652017-06-19 16:04:09 +03004311}
4312
4313TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4314{
4315 if (!declaringFunction())
4316 {
4317 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4318 }
4319 return new TStorageQualifierWrapper(EvqInOut, loc);
4320}
4321
Jamie Madillb98c3a82015-07-23 14:26:04 -04004322TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004323 TLayoutQualifier rightQualifier,
4324 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004325{
Martin Radevc28888b2016-07-22 15:27:42 +03004326 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004327 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004328}
4329
Olli Etuahocce89652017-06-19 16:04:09 +03004330TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4331{
4332 checkIsNotReserved(loc, *identifier);
4333 TType *type = new TType(EbtVoid, EbpUndefined);
4334 return new TField(type, identifier, loc);
4335}
4336
4337TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4338 const TSourceLoc &loc,
4339 TIntermTyped *arraySize,
4340 const TSourceLoc &arraySizeLoc)
4341{
4342 checkIsNotReserved(loc, *identifier);
4343
4344 TType *type = new TType(EbtVoid, EbpUndefined);
4345 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
4346 type->setArraySize(size);
4347
4348 return new TField(type, identifier, loc);
4349}
4350
Olli Etuaho4de340a2016-12-16 09:32:03 +00004351TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4352 const TFieldList *newlyAddedFields,
4353 const TSourceLoc &location)
4354{
4355 for (TField *field : *newlyAddedFields)
4356 {
4357 for (TField *oldField : *processedFields)
4358 {
4359 if (oldField->name() == field->name())
4360 {
4361 error(location, "duplicate field name in structure", field->name().c_str());
4362 }
4363 }
4364 processedFields->push_back(field);
4365 }
4366 return processedFields;
4367}
4368
Martin Radev70866b82016-07-22 15:27:42 +03004369TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4370 const TTypeQualifierBuilder &typeQualifierBuilder,
4371 TPublicType *typeSpecifier,
4372 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004373{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004374 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004375
Martin Radev70866b82016-07-22 15:27:42 +03004376 typeSpecifier->qualifier = typeQualifier.qualifier;
4377 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004378 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004379 typeSpecifier->invariant = typeQualifier.invariant;
4380 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304381 {
Martin Radev70866b82016-07-22 15:27:42 +03004382 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004383 }
Martin Radev70866b82016-07-22 15:27:42 +03004384 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004385}
4386
Jamie Madillb98c3a82015-07-23 14:26:04 -04004387TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
4388 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004389{
Martin Radev4a9cd802016-09-01 16:51:51 +03004390 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4391 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004392
Martin Radev4a9cd802016-09-01 16:51:51 +03004393 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004394
Martin Radev4a9cd802016-09-01 16:51:51 +03004395 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004396
Arun Patole7e7e68d2015-05-22 12:02:25 +05304397 for (unsigned int i = 0; i < fieldList->size(); ++i)
4398 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004399 //
4400 // Careful not to replace already known aspects of type, like array-ness
4401 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304402 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03004403 type->setBasicType(typeSpecifier.getBasicType());
4404 type->setPrimarySize(typeSpecifier.getPrimarySize());
4405 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004406 type->setPrecision(typeSpecifier.precision);
4407 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04004408 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03004409 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03004410 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004411
4412 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05304413 if (type->isArray())
4414 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004415 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004416 }
4417 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03004418 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03004419 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304420 {
Olli Etuaho0f684632017-07-13 12:42:15 +03004421 type->setStruct(typeSpecifier.getUserDef());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004422 }
4423
Martin Radev4a9cd802016-09-01 16:51:51 +03004424 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004425 }
4426
Jamie Madill98493dd2013-07-08 14:39:03 -04004427 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004428}
4429
Martin Radev4a9cd802016-09-01 16:51:51 +03004430TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4431 const TSourceLoc &nameLine,
4432 const TString *structName,
4433 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004434{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004435 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004436
Jamie Madill9b820842015-02-12 10:40:10 -05004437 // Store a bool in the struct if we're at global scope, to allow us to
4438 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004439 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004440
Jamie Madill98493dd2013-07-08 14:39:03 -04004441 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004442 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004443 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004444 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304445 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004446 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004447 }
4448 }
4449
4450 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004451 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004452 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004453 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004454 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004455 switch (qualifier)
4456 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004457 case EvqGlobal:
4458 case EvqTemporary:
4459 break;
4460 default:
4461 error(field.line(), "invalid qualifier on struct member",
4462 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004463 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004464 }
Martin Radev70866b82016-07-22 15:27:42 +03004465 if (field.type()->isInvariant())
4466 {
4467 error(field.line(), "invalid qualifier on struct member", "invariant");
4468 }
jchen104cdac9e2017-05-08 11:01:20 +08004469 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4470 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004471 {
4472 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4473 }
4474
Olli Etuaho43364892017-02-13 16:00:12 +00004475 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4476
4477 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004478
4479 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004480 }
4481
Martin Radev4a9cd802016-09-01 16:51:51 +03004482 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004483 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004484 exitStructDeclaration();
4485
Martin Radev4a9cd802016-09-01 16:51:51 +03004486 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004487}
4488
Jamie Madillb98c3a82015-07-23 14:26:04 -04004489TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004490 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004491 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004492{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004493 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004494 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004495 init->isVector())
4496 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004497 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4498 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004499 return nullptr;
4500 }
4501
Olli Etuahoac5274d2015-02-20 10:19:08 +02004502 if (statementList)
4503 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004504 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004505 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004506 return nullptr;
4507 }
4508 }
4509
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004510 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4511 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004512 return node;
4513}
4514
4515TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4516{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004517 if (mSwitchNestingLevel == 0)
4518 {
4519 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004520 return nullptr;
4521 }
4522 if (condition == nullptr)
4523 {
4524 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004525 return nullptr;
4526 }
4527 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004528 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004529 {
4530 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004531 }
4532 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004533 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4534 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4535 // fold in case labels.
4536 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004537 {
4538 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004539 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004540 TIntermCase *node = new TIntermCase(condition);
4541 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004542 return node;
4543}
4544
4545TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4546{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004547 if (mSwitchNestingLevel == 0)
4548 {
4549 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004550 return nullptr;
4551 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004552 TIntermCase *node = new TIntermCase(nullptr);
4553 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004554 return node;
4555}
4556
Jamie Madillb98c3a82015-07-23 14:26:04 -04004557TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4558 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004559 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004560{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004561 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004562
4563 switch (op)
4564 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004565 case EOpLogicalNot:
4566 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4567 child->isVector())
4568 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004569 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004570 return nullptr;
4571 }
4572 break;
4573 case EOpBitwiseNot:
4574 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4575 child->isMatrix() || child->isArray())
4576 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004577 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004578 return nullptr;
4579 }
4580 break;
4581 case EOpPostIncrement:
4582 case EOpPreIncrement:
4583 case EOpPostDecrement:
4584 case EOpPreDecrement:
4585 case EOpNegative:
4586 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004587 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4588 child->getBasicType() == EbtBool || child->isArray() ||
4589 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004590 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004591 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004592 return nullptr;
4593 }
4594 // Operators for built-ins are already type checked against their prototype.
4595 default:
4596 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004597 }
4598
Jiajia Qinbc585152017-06-23 15:42:17 +08004599 if (child->getMemoryQualifier().writeonly)
4600 {
4601 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4602 return nullptr;
4603 }
4604
Olli Etuahof119a262016-08-19 15:54:22 +03004605 TIntermUnary *node = new TIntermUnary(op, child);
4606 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004607
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004608 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004609}
4610
Olli Etuaho09b22472015-02-11 11:47:26 +02004611TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4612{
Olli Etuahocce89652017-06-19 16:04:09 +03004613 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004614 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004615 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004616 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004617 return child;
4618 }
4619 return node;
4620}
4621
Jamie Madillb98c3a82015-07-23 14:26:04 -04004622TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4623 TIntermTyped *child,
4624 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004625{
Olli Etuaho856c4972016-08-08 11:38:39 +03004626 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004627 return addUnaryMath(op, child, loc);
4628}
4629
Jamie Madillb98c3a82015-07-23 14:26:04 -04004630bool TParseContext::binaryOpCommonCheck(TOperator op,
4631 TIntermTyped *left,
4632 TIntermTyped *right,
4633 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004634{
jchen10b4cf5652017-05-05 18:51:17 +08004635 // Check opaque types are not allowed to be operands in expressions other than array indexing
4636 // and structure member selection.
4637 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4638 {
4639 switch (op)
4640 {
4641 case EOpIndexDirect:
4642 case EOpIndexIndirect:
4643 break;
4644 case EOpIndexDirectStruct:
4645 UNREACHABLE();
4646
4647 default:
4648 error(loc, "Invalid operation for variables with an opaque type",
4649 GetOperatorString(op));
4650 return false;
4651 }
4652 }
jchen10cc2a10e2017-05-03 14:05:12 +08004653
Jiajia Qinbc585152017-06-23 15:42:17 +08004654 if (right->getMemoryQualifier().writeonly)
4655 {
4656 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4657 return false;
4658 }
4659
4660 if (left->getMemoryQualifier().writeonly)
4661 {
4662 switch (op)
4663 {
4664 case EOpAssign:
4665 case EOpInitialize:
4666 case EOpIndexDirect:
4667 case EOpIndexIndirect:
4668 case EOpIndexDirectStruct:
4669 case EOpIndexDirectInterfaceBlock:
4670 break;
4671 default:
4672 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4673 return false;
4674 }
4675 }
4676
Olli Etuaho244be012016-08-18 15:26:02 +03004677 if (left->getType().getStruct() || right->getType().getStruct())
4678 {
4679 switch (op)
4680 {
4681 case EOpIndexDirectStruct:
4682 ASSERT(left->getType().getStruct());
4683 break;
4684 case EOpEqual:
4685 case EOpNotEqual:
4686 case EOpAssign:
4687 case EOpInitialize:
4688 if (left->getType() != right->getType())
4689 {
4690 return false;
4691 }
4692 break;
4693 default:
4694 error(loc, "Invalid operation for structs", GetOperatorString(op));
4695 return false;
4696 }
4697 }
4698
Olli Etuaho94050052017-05-08 14:17:44 +03004699 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4700 {
4701 switch (op)
4702 {
4703 case EOpIndexDirectInterfaceBlock:
4704 ASSERT(left->getType().getInterfaceBlock());
4705 break;
4706 default:
4707 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4708 return false;
4709 }
4710 }
4711
Olli Etuahod6b14282015-03-17 14:31:35 +02004712 if (left->isArray() || right->isArray())
4713 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004714 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004715 {
4716 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4717 return false;
4718 }
4719
4720 if (left->isArray() != right->isArray())
4721 {
4722 error(loc, "array / non-array mismatch", GetOperatorString(op));
4723 return false;
4724 }
4725
4726 switch (op)
4727 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004728 case EOpEqual:
4729 case EOpNotEqual:
4730 case EOpAssign:
4731 case EOpInitialize:
4732 break;
4733 default:
4734 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4735 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004736 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004737 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02004738 if (left->getArraySize() != right->getArraySize())
4739 {
4740 error(loc, "array size mismatch", GetOperatorString(op));
4741 return false;
4742 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004743 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004744
4745 // Check ops which require integer / ivec parameters
4746 bool isBitShift = false;
4747 switch (op)
4748 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004749 case EOpBitShiftLeft:
4750 case EOpBitShiftRight:
4751 case EOpBitShiftLeftAssign:
4752 case EOpBitShiftRightAssign:
4753 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4754 // check that the basic type is an integer type.
4755 isBitShift = true;
4756 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4757 {
4758 return false;
4759 }
4760 break;
4761 case EOpBitwiseAnd:
4762 case EOpBitwiseXor:
4763 case EOpBitwiseOr:
4764 case EOpBitwiseAndAssign:
4765 case EOpBitwiseXorAssign:
4766 case EOpBitwiseOrAssign:
4767 // It is enough to check the type of only one operand, since later it
4768 // is checked that the operand types match.
4769 if (!IsInteger(left->getBasicType()))
4770 {
4771 return false;
4772 }
4773 break;
4774 default:
4775 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004776 }
4777
4778 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4779 // So the basic type should usually match.
4780 if (!isBitShift && left->getBasicType() != right->getBasicType())
4781 {
4782 return false;
4783 }
4784
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004785 // Check that:
4786 // 1. Type sizes match exactly on ops that require that.
4787 // 2. Restrictions for structs that contain arrays or samplers are respected.
4788 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004789 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004790 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004791 case EOpAssign:
4792 case EOpInitialize:
4793 case EOpEqual:
4794 case EOpNotEqual:
4795 // ESSL 1.00 sections 5.7, 5.8, 5.9
4796 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4797 {
4798 error(loc, "undefined operation for structs containing arrays",
4799 GetOperatorString(op));
4800 return false;
4801 }
4802 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4803 // we interpret the spec so that this extends to structs containing samplers,
4804 // similarly to ESSL 1.00 spec.
4805 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4806 left->getType().isStructureContainingSamplers())
4807 {
4808 error(loc, "undefined operation for structs containing samplers",
4809 GetOperatorString(op));
4810 return false;
4811 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004812
Olli Etuahoe1805592017-01-02 16:41:20 +00004813 if ((left->getNominalSize() != right->getNominalSize()) ||
4814 (left->getSecondarySize() != right->getSecondarySize()))
4815 {
4816 error(loc, "dimension mismatch", GetOperatorString(op));
4817 return false;
4818 }
4819 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004820 case EOpLessThan:
4821 case EOpGreaterThan:
4822 case EOpLessThanEqual:
4823 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004824 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004825 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004826 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004827 return false;
4828 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004829 break;
4830 case EOpAdd:
4831 case EOpSub:
4832 case EOpDiv:
4833 case EOpIMod:
4834 case EOpBitShiftLeft:
4835 case EOpBitShiftRight:
4836 case EOpBitwiseAnd:
4837 case EOpBitwiseXor:
4838 case EOpBitwiseOr:
4839 case EOpAddAssign:
4840 case EOpSubAssign:
4841 case EOpDivAssign:
4842 case EOpIModAssign:
4843 case EOpBitShiftLeftAssign:
4844 case EOpBitShiftRightAssign:
4845 case EOpBitwiseAndAssign:
4846 case EOpBitwiseXorAssign:
4847 case EOpBitwiseOrAssign:
4848 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4849 {
4850 return false;
4851 }
4852
4853 // Are the sizes compatible?
4854 if (left->getNominalSize() != right->getNominalSize() ||
4855 left->getSecondarySize() != right->getSecondarySize())
4856 {
4857 // If the nominal sizes of operands do not match:
4858 // One of them must be a scalar.
4859 if (!left->isScalar() && !right->isScalar())
4860 return false;
4861
4862 // In the case of compound assignment other than multiply-assign,
4863 // the right side needs to be a scalar. Otherwise a vector/matrix
4864 // would be assigned to a scalar. A scalar can't be shifted by a
4865 // vector either.
4866 if (!right->isScalar() &&
4867 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4868 return false;
4869 }
4870 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004871 default:
4872 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004873 }
4874
Olli Etuahod6b14282015-03-17 14:31:35 +02004875 return true;
4876}
4877
Olli Etuaho1dded802016-08-18 18:13:13 +03004878bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4879 const TType &left,
4880 const TType &right)
4881{
4882 switch (op)
4883 {
4884 case EOpMul:
4885 case EOpMulAssign:
4886 return left.getNominalSize() == right.getNominalSize() &&
4887 left.getSecondarySize() == right.getSecondarySize();
4888 case EOpVectorTimesScalar:
4889 return true;
4890 case EOpVectorTimesScalarAssign:
4891 ASSERT(!left.isMatrix() && !right.isMatrix());
4892 return left.isVector() && !right.isVector();
4893 case EOpVectorTimesMatrix:
4894 return left.getNominalSize() == right.getRows();
4895 case EOpVectorTimesMatrixAssign:
4896 ASSERT(!left.isMatrix() && right.isMatrix());
4897 return left.isVector() && left.getNominalSize() == right.getRows() &&
4898 left.getNominalSize() == right.getCols();
4899 case EOpMatrixTimesVector:
4900 return left.getCols() == right.getNominalSize();
4901 case EOpMatrixTimesScalar:
4902 return true;
4903 case EOpMatrixTimesScalarAssign:
4904 ASSERT(left.isMatrix() && !right.isMatrix());
4905 return !right.isVector();
4906 case EOpMatrixTimesMatrix:
4907 return left.getCols() == right.getRows();
4908 case EOpMatrixTimesMatrixAssign:
4909 ASSERT(left.isMatrix() && right.isMatrix());
4910 // We need to check two things:
4911 // 1. The matrix multiplication step is valid.
4912 // 2. The result will have the same number of columns as the lvalue.
4913 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4914
4915 default:
4916 UNREACHABLE();
4917 return false;
4918 }
4919}
4920
Jamie Madillb98c3a82015-07-23 14:26:04 -04004921TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4922 TIntermTyped *left,
4923 TIntermTyped *right,
4924 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004925{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004926 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004927 return nullptr;
4928
Olli Etuahofc1806e2015-03-17 13:03:11 +02004929 switch (op)
4930 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004931 case EOpEqual:
4932 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004933 case EOpLessThan:
4934 case EOpGreaterThan:
4935 case EOpLessThanEqual:
4936 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004937 break;
4938 case EOpLogicalOr:
4939 case EOpLogicalXor:
4940 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004941 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4942 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004943 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004944 {
4945 return nullptr;
4946 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004947 // Basic types matching should have been already checked.
4948 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004949 break;
4950 case EOpAdd:
4951 case EOpSub:
4952 case EOpDiv:
4953 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004954 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4955 !right->getType().getStruct());
4956 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004957 {
4958 return nullptr;
4959 }
4960 break;
4961 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004962 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4963 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004964 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004965 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004966 {
4967 return nullptr;
4968 }
4969 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004970 default:
4971 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004972 }
4973
Olli Etuaho1dded802016-08-18 18:13:13 +03004974 if (op == EOpMul)
4975 {
4976 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4977 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4978 {
4979 return nullptr;
4980 }
4981 }
4982
Olli Etuaho3fdec912016-08-18 15:08:06 +03004983 TIntermBinary *node = new TIntermBinary(op, left, right);
4984 node->setLine(loc);
4985
Olli Etuaho3fdec912016-08-18 15:08:06 +03004986 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004987 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02004988}
4989
Jamie Madillb98c3a82015-07-23 14:26:04 -04004990TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4991 TIntermTyped *left,
4992 TIntermTyped *right,
4993 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004994{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004995 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004996 if (node == 0)
4997 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004998 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4999 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005000 return left;
5001 }
5002 return node;
5003}
5004
Jamie Madillb98c3a82015-07-23 14:26:04 -04005005TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5006 TIntermTyped *left,
5007 TIntermTyped *right,
5008 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005009{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005010 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005011 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005012 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005013 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5014 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005015 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005016 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005017 }
5018 return node;
5019}
5020
Olli Etuaho13389b62016-10-16 11:48:18 +01005021TIntermBinary *TParseContext::createAssign(TOperator op,
5022 TIntermTyped *left,
5023 TIntermTyped *right,
5024 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005025{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005026 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005027 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005028 if (op == EOpMulAssign)
5029 {
5030 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5031 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5032 {
5033 return nullptr;
5034 }
5035 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005036 TIntermBinary *node = new TIntermBinary(op, left, right);
5037 node->setLine(loc);
5038
Olli Etuaho3fdec912016-08-18 15:08:06 +03005039 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005040 }
5041 return nullptr;
5042}
5043
Jamie Madillb98c3a82015-07-23 14:26:04 -04005044TIntermTyped *TParseContext::addAssign(TOperator op,
5045 TIntermTyped *left,
5046 TIntermTyped *right,
5047 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005048{
Olli Etuahocce89652017-06-19 16:04:09 +03005049 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005050 TIntermTyped *node = createAssign(op, left, right, loc);
5051 if (node == nullptr)
5052 {
5053 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005054 return left;
5055 }
5056 return node;
5057}
5058
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005059TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5060 TIntermTyped *right,
5061 const TSourceLoc &loc)
5062{
Corentin Wallez0d959252016-07-12 17:26:32 -04005063 // WebGL2 section 5.26, the following results in an error:
5064 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005065 if (mShaderSpec == SH_WEBGL2_SPEC &&
5066 (left->isArray() || left->getBasicType() == EbtVoid ||
5067 left->getType().isStructureContainingArrays() || right->isArray() ||
5068 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005069 {
5070 error(loc,
5071 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5072 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005073 }
5074
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005075 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5076 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5077 commaNode->getTypePointer()->setQualifier(resultQualifier);
5078 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005079}
5080
Olli Etuaho49300862015-02-20 14:54:49 +02005081TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5082{
5083 switch (op)
5084 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005085 case EOpContinue:
5086 if (mLoopNestingLevel <= 0)
5087 {
5088 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005089 }
5090 break;
5091 case EOpBreak:
5092 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5093 {
5094 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005095 }
5096 break;
5097 case EOpReturn:
5098 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5099 {
5100 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005101 }
5102 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005103 case EOpKill:
5104 if (mShaderType != GL_FRAGMENT_SHADER)
5105 {
5106 error(loc, "discard supported in fragment shaders only", "discard");
5107 }
5108 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005109 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005110 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005111 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005112 }
Olli Etuahocce89652017-06-19 16:04:09 +03005113 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005114}
5115
Jamie Madillb98c3a82015-07-23 14:26:04 -04005116TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005117 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005118 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005119{
Olli Etuahocce89652017-06-19 16:04:09 +03005120 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005121 {
Olli Etuahocce89652017-06-19 16:04:09 +03005122 ASSERT(op == EOpReturn);
5123 mFunctionReturnsValue = true;
5124 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5125 {
5126 error(loc, "void function cannot return a value", "return");
5127 }
5128 else if (*mCurrentFunctionType != expression->getType())
5129 {
5130 error(loc, "function return is not matching type:", "return");
5131 }
Olli Etuaho49300862015-02-20 14:54:49 +02005132 }
Olli Etuahocce89652017-06-19 16:04:09 +03005133 TIntermBranch *node = new TIntermBranch(op, expression);
5134 node->setLine(loc);
5135 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005136}
5137
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005138void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5139{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005140 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005141 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005142 TIntermNode *offset = nullptr;
5143 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03005144 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5145 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5146 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005147 {
5148 offset = arguments->back();
5149 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005150 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005151 {
5152 // A bias parameter might follow the offset parameter.
5153 ASSERT(arguments->size() >= 3);
5154 offset = (*arguments)[2];
5155 }
5156 if (offset != nullptr)
5157 {
5158 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5159 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5160 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005161 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005162 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005163 }
5164 else
5165 {
5166 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5167 size_t size = offsetConstantUnion->getType().getObjectSize();
5168 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
5169 for (size_t i = 0u; i < size; ++i)
5170 {
5171 int offsetValue = values[i].getIConst();
5172 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
5173 {
5174 std::stringstream tokenStream;
5175 tokenStream << offsetValue;
5176 std::string token = tokenStream.str();
5177 error(offset->getLine(), "Texture offset value out of valid range",
5178 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005179 }
5180 }
5181 }
5182 }
5183}
5184
Martin Radev2cc85b32016-08-05 16:22:53 +03005185// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5186void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5187{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005188 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005189 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5190
5191 if (name.compare(0, 5, "image") == 0)
5192 {
5193 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005194 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005195
Olli Etuaho485eefd2017-02-14 17:40:06 +00005196 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005197
5198 if (name.compare(5, 5, "Store") == 0)
5199 {
5200 if (memoryQualifier.readonly)
5201 {
5202 error(imageNode->getLine(),
5203 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005204 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005205 }
5206 }
5207 else if (name.compare(5, 4, "Load") == 0)
5208 {
5209 if (memoryQualifier.writeonly)
5210 {
5211 error(imageNode->getLine(),
5212 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005213 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005214 }
5215 }
5216 }
5217}
5218
5219// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5220void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5221 const TFunction *functionDefinition,
5222 const TIntermAggregate *functionCall)
5223{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005224 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005225
5226 const TIntermSequence &arguments = *functionCall->getSequence();
5227
5228 ASSERT(functionDefinition->getParamCount() == arguments.size());
5229
5230 for (size_t i = 0; i < arguments.size(); ++i)
5231 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005232 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5233 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005234 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5235 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5236
5237 if (IsImage(functionArgumentType.getBasicType()))
5238 {
5239 const TMemoryQualifier &functionArgumentMemoryQualifier =
5240 functionArgumentType.getMemoryQualifier();
5241 const TMemoryQualifier &functionParameterMemoryQualifier =
5242 functionParameterType.getMemoryQualifier();
5243 if (functionArgumentMemoryQualifier.readonly &&
5244 !functionParameterMemoryQualifier.readonly)
5245 {
5246 error(functionCall->getLine(),
5247 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005248 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005249 }
5250
5251 if (functionArgumentMemoryQualifier.writeonly &&
5252 !functionParameterMemoryQualifier.writeonly)
5253 {
5254 error(functionCall->getLine(),
5255 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005256 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005257 }
Martin Radev049edfa2016-11-11 14:35:37 +02005258
5259 if (functionArgumentMemoryQualifier.coherent &&
5260 !functionParameterMemoryQualifier.coherent)
5261 {
5262 error(functionCall->getLine(),
5263 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005264 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005265 }
5266
5267 if (functionArgumentMemoryQualifier.volatileQualifier &&
5268 !functionParameterMemoryQualifier.volatileQualifier)
5269 {
5270 error(functionCall->getLine(),
5271 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005272 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005273 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005274 }
5275 }
5276}
5277
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005278TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005279{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005280 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005281}
5282
5283TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005284 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005285 TIntermNode *thisNode,
5286 const TSourceLoc &loc)
5287{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005288 if (thisNode != nullptr)
5289 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005290 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005291 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005292
5293 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005294 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005295 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005296 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005297 }
5298 else
5299 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005300 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005301 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5302 }
5303}
5304
5305TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5306 TIntermSequence *arguments,
5307 TIntermNode *thisNode,
5308 const TSourceLoc &loc)
5309{
5310 TConstantUnion *unionArray = new TConstantUnion[1];
5311 int arraySize = 0;
5312 TIntermTyped *typedThis = thisNode->getAsTyped();
5313 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5314 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5315 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5316 // So accessing fnCall->getName() below is safe.
5317 if (fnCall->getName() != "length")
5318 {
5319 error(loc, "invalid method", fnCall->getName().c_str());
5320 }
5321 else if (!arguments->empty())
5322 {
5323 error(loc, "method takes no parameters", "length");
5324 }
5325 else if (typedThis == nullptr || !typedThis->isArray())
5326 {
5327 error(loc, "length can only be called on arrays", "length");
5328 }
5329 else
5330 {
5331 arraySize = typedThis->getArraySize();
5332 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00005333 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005334 // This code path can be hit with expressions like these:
5335 // (a = b).length()
5336 // (func()).length()
5337 // (int[3](0, 1, 2)).length()
5338 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
5339 // expression.
5340 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
5341 // spec section 5.9 which allows "An array, vector or matrix expression with the
5342 // length method applied".
5343 error(loc, "length can only be called on array names, not on array expressions",
5344 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00005345 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005346 }
5347 unionArray->setIConst(arraySize);
Olli Etuaho56229f12017-07-10 14:16:33 +03005348 TIntermConstantUnion *node =
5349 new TIntermConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst));
5350 node->setLine(loc);
5351 return node;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005352}
5353
5354TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5355 TIntermSequence *arguments,
5356 const TSourceLoc &loc)
5357{
5358 // First find by unmangled name to check whether the function name has been
5359 // hidden by a variable name or struct typename.
5360 // If a function is found, check for one with a matching argument list.
5361 bool builtIn;
5362 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5363 if (symbol != nullptr && !symbol->isFunction())
5364 {
5365 error(loc, "function name expected", fnCall->getName().c_str());
5366 }
5367 else
5368 {
5369 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5370 mShaderVersion, &builtIn);
5371 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005372 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005373 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5374 }
5375 else
5376 {
5377 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005378 //
5379 // A declared function.
5380 //
Olli Etuaho383b7912016-08-05 11:22:59 +03005381 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005382 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005383 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005384 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005385 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005386 if (builtIn && op != EOpNull)
5387 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005388 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005389 if (fnCandidate->getParamCount() == 1)
5390 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005391 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005392 TIntermNode *unaryParamNode = arguments->front();
5393 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005394 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005395 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005396 }
5397 else
5398 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005399 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005400 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005401 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005402
5403 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005404 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305405
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005406 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305407 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005408 // See if we can constant fold a built-in. Note that this may be possible
5409 // even if it is not const-qualified.
5410 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305411 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005412 else
5413 {
5414 return callNode;
5415 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005416 }
5417 }
5418 else
5419 {
5420 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005421 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005422
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005423 // If builtIn == false, the function is user defined - could be an overloaded
5424 // built-in as well.
5425 // if builtIn == true, it's a builtIn function with no op associated with it.
5426 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005427 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005428 {
Olli Etuahofe486322017-03-21 09:30:54 +00005429 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005430 checkTextureOffsetConst(callNode);
5431 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005432 }
5433 else
5434 {
Olli Etuahofe486322017-03-21 09:30:54 +00005435 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005436 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005437 }
5438
Jiajia Qinbc585152017-06-23 15:42:17 +08005439 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005440
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005441 callNode->setLine(loc);
5442
5443 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005444 }
5445 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005446 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005447
5448 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005449 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005450}
5451
Jamie Madillb98c3a82015-07-23 14:26:04 -04005452TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005453 TIntermTyped *trueExpression,
5454 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005455 const TSourceLoc &loc)
5456{
Olli Etuaho56229f12017-07-10 14:16:33 +03005457 if (!checkIsScalarBool(loc, cond))
5458 {
5459 return falseExpression;
5460 }
Olli Etuaho52901742015-04-15 13:42:45 +03005461
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005462 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005463 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005464 std::stringstream reasonStream;
5465 reasonStream << "mismatching ternary operator operand types '"
5466 << trueExpression->getCompleteString() << " and '"
5467 << falseExpression->getCompleteString() << "'";
5468 std::string reason = reasonStream.str();
5469 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005470 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005471 }
Olli Etuahode318b22016-10-25 16:18:25 +01005472 if (IsOpaqueType(trueExpression->getBasicType()))
5473 {
5474 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005475 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005476 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5477 // Note that structs containing opaque types don't need to be checked as structs are
5478 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005479 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005480 return falseExpression;
5481 }
5482
Jiajia Qinbc585152017-06-23 15:42:17 +08005483 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5484 falseExpression->getMemoryQualifier().writeonly)
5485 {
5486 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5487 return falseExpression;
5488 }
5489
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005490 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005491 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005492 // ESSL 3.00.6 section 5.7:
5493 // Ternary operator support is optional for arrays. No certainty that it works across all
5494 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5495 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005496 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005497 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005498 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005499 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005500 }
Olli Etuaho94050052017-05-08 14:17:44 +03005501 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5502 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005503 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005504 return falseExpression;
5505 }
5506
Corentin Wallez0d959252016-07-12 17:26:32 -04005507 // WebGL2 section 5.26, the following results in an error:
5508 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005509 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005510 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005511 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005512 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005513 }
5514
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005515 // Note that the node resulting from here can be a constant union without being qualified as
5516 // constant.
5517 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5518 node->setLine(loc);
5519
5520 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005521}
Olli Etuaho49300862015-02-20 14:54:49 +02005522
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005523//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005524// Parse an array of strings using yyparse.
5525//
5526// Returns 0 for success.
5527//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005528int PaParseStrings(size_t count,
5529 const char *const string[],
5530 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305531 TParseContext *context)
5532{
Yunchao He4f285442017-04-21 12:15:49 +08005533 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005534 return 1;
5535
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005536 if (glslang_initialize(context))
5537 return 1;
5538
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005539 int error = glslang_scan(count, string, length, context);
5540 if (!error)
5541 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005542
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005543 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005544
alokp@chromium.org6b495712012-06-29 00:06:58 +00005545 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005546}
Jamie Madill45bcc782016-11-07 13:58:48 -05005547
5548} // namespace sh