blob: da64488b308f94e99cc269439589a89dbb27f2f4 [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
Jiawei Shaod8105a02017-08-08 09:54:36 +080098// Map input primitive types to input array sizes in a geometry shader.
99GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
100{
101 switch (primitiveType)
102 {
103 case EptPoints:
104 return 1u;
105 case EptLines:
106 return 2u;
107 case EptTriangles:
108 return 3u;
109 case EptLinesAdjacency:
110 return 4u;
111 case EptTrianglesAdjacency:
112 return 6u;
113 default:
114 UNREACHABLE();
115 return 0u;
116 }
117}
118
Martin Radev2cc85b32016-08-05 16:22:53 +0300119} // namespace
120
jchen104cdac9e2017-05-08 11:01:20 +0800121// This tracks each binding point's current default offset for inheritance of subsequent
122// variables using the same binding, and keeps offsets unique and non overlapping.
123// See GLSL ES 3.1, section 4.4.6.
124class TParseContext::AtomicCounterBindingState
125{
126 public:
127 AtomicCounterBindingState() : mDefaultOffset(0) {}
128 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
129 // newly inserted span.
130 int insertSpan(int start, size_t length)
131 {
132 gl::RangeI newSpan(start, start + static_cast<int>(length));
133 for (const auto &span : mSpans)
134 {
135 if (newSpan.intersects(span))
136 {
137 return -1;
138 }
139 }
140 mSpans.push_back(newSpan);
141 mDefaultOffset = newSpan.high();
142 return start;
143 }
144 // Inserts a new span starting from the default offset.
145 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
146 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
147
148 private:
149 int mDefaultOffset;
150 std::vector<gl::RangeI> mSpans;
151};
152
Jamie Madillacb4b812016-11-07 13:50:29 -0500153TParseContext::TParseContext(TSymbolTable &symt,
154 TExtensionBehavior &ext,
155 sh::GLenum type,
156 ShShaderSpec spec,
157 ShCompileOptions options,
158 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000159 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500160 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300161 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300162 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500163 mShaderType(type),
164 mShaderSpec(spec),
165 mCompileOptions(options),
166 mShaderVersion(100),
167 mTreeRoot(nullptr),
168 mLoopNestingLevel(0),
169 mStructNestingLevel(0),
170 mSwitchNestingLevel(0),
171 mCurrentFunctionType(nullptr),
172 mFunctionReturnsValue(false),
173 mChecksPrecisionErrors(checksPrecErrors),
174 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800175 mDefaultUniformMatrixPacking(EmpColumnMajor),
176 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
177 mDefaultBufferMatrixPacking(EmpColumnMajor),
178 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000179 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500180 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000181 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500182 mShaderVersion,
183 mShaderType,
184 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000185 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500186 mScanner(nullptr),
187 mUsesFragData(false),
188 mUsesFragColor(false),
189 mUsesSecondaryOutputs(false),
190 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
191 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
192 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000193 mNumViews(-1),
194 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000195 mMaxImageUnits(resources.MaxImageUnits),
196 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000197 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800198 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800199 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800200 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800201 mDeclaringFunction(false),
202 mGeometryShaderInputPrimitiveType(EptUndefined),
203 mGeometryShaderOutputPrimitiveType(EptUndefined),
204 mGeometryShaderInvocations(0),
205 mGeometryShaderMaxVertices(-1),
206 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800207 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
208 mGeometryShaderInputArraySize(0)
Jamie Madillacb4b812016-11-07 13:50:29 -0500209{
210 mComputeShaderLocalSize.fill(-1);
211}
212
jchen104cdac9e2017-05-08 11:01:20 +0800213TParseContext::~TParseContext()
214{
215}
216
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300217bool TParseContext::parseVectorFields(const TSourceLoc &line,
218 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400219 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300220 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300222 ASSERT(fieldOffsets);
223 size_t fieldCount = compString.size();
224 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530225 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000226 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000227 return false;
228 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300229 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230
Jamie Madillb98c3a82015-07-23 14:26:04 -0400231 enum
232 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000233 exyzw,
234 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000235 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000236 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000237
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300238 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530239 {
240 switch (compString[i])
241 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400242 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300243 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400244 fieldSet[i] = exyzw;
245 break;
246 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300247 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400248 fieldSet[i] = ergba;
249 break;
250 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300251 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400252 fieldSet[i] = estpq;
253 break;
254 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300255 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400256 fieldSet[i] = exyzw;
257 break;
258 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300259 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400260 fieldSet[i] = ergba;
261 break;
262 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300263 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400264 fieldSet[i] = estpq;
265 break;
266 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300267 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268 fieldSet[i] = exyzw;
269 break;
270 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300271 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400272 fieldSet[i] = ergba;
273 break;
274 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300275 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400276 fieldSet[i] = estpq;
277 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530278
Jamie Madillb98c3a82015-07-23 14:26:04 -0400279 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300280 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400281 fieldSet[i] = exyzw;
282 break;
283 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300284 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400285 fieldSet[i] = ergba;
286 break;
287 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300288 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400289 fieldSet[i] = estpq;
290 break;
291 default:
292 error(line, "illegal vector field selection", compString.c_str());
293 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000294 }
295 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300297 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530298 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300299 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530300 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400301 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 return false;
303 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
Arun Patole7e7e68d2015-05-22 12:02:25 +0530305 if (i > 0)
306 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400307 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530308 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400309 error(line, "illegal - vector component fields not from the same set",
310 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000311 return false;
312 }
313 }
314 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000316 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317}
318
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319///////////////////////////////////////////////////////////////////////
320//
321// Errors
322//
323////////////////////////////////////////////////////////////////////////
324
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325//
326// Used by flex/bison to output all syntax and parsing errors.
327//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000328void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000330 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331}
332
Olli Etuaho4de340a2016-12-16 09:32:03 +0000333void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530334{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000335 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000336}
337
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200338void TParseContext::outOfRangeError(bool isError,
339 const TSourceLoc &loc,
340 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000341 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200342{
343 if (isError)
344 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000345 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200346 }
347 else
348 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000349 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200350 }
351}
352
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353//
354// Same error message for all places assignments don't work.
355//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530356void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000358 std::stringstream reasonStream;
359 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
360 std::string reason = reasonStream.str();
361 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362}
363
364//
365// Same error message for all places unary operations don't work.
366//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530367void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000369 std::stringstream reasonStream;
370 reasonStream << "wrong operand type - no operation '" << op
371 << "' exists that takes an operand of type " << operand
372 << " (or there is no acceptable conversion)";
373 std::string reason = reasonStream.str();
374 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000375}
376
377//
378// Same error message for all binary operations don't work.
379//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400380void TParseContext::binaryOpError(const TSourceLoc &line,
381 const char *op,
382 TString left,
383 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000384{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000385 std::stringstream reasonStream;
386 reasonStream << "wrong operand types - no operation '" << op
387 << "' exists that takes a left-hand operand of type '" << left
388 << "' and a right operand of type '" << right
389 << "' (or there is no acceptable conversion)";
390 std::string reason = reasonStream.str();
391 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392}
393
Olli Etuaho856c4972016-08-08 11:38:39 +0300394void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
395 TPrecision precision,
396 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530397{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400398 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300399 return;
Martin Radev70866b82016-07-22 15:27:42 +0300400
401 if (precision != EbpUndefined && !SupportsPrecision(type))
402 {
403 error(line, "illegal type for precision qualifier", getBasicString(type));
404 }
405
Olli Etuaho183d7e22015-11-20 15:59:09 +0200406 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530407 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200408 switch (type)
409 {
410 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400411 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300412 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200413 case EbtInt:
414 case EbtUInt:
415 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400416 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300417 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200418 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800419 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200420 {
jchen10cc2a10e2017-05-03 14:05:12 +0800421 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300422 return;
423 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200424 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000425 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000426}
427
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428// Both test and if necessary, spit out an error, to see if the node is really
429// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300430bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500432 TIntermSymbol *symNode = node->getAsSymbolNode();
433 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100434 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
435
436 if (swizzleNode)
437 {
438 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
439 if (ok && swizzleNode->hasDuplicateOffsets())
440 {
441 error(line, " l-value of swizzle cannot have duplicate components", op);
442 return false;
443 }
444 return ok;
445 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000446
Arun Patole7e7e68d2015-05-22 12:02:25 +0530447 if (binaryNode)
448 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400449 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530450 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400451 case EOpIndexDirect:
452 case EOpIndexIndirect:
453 case EOpIndexDirectStruct:
454 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300455 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400456 default:
457 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000458 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000459 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300460 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000461 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000462
jchen10cc2a10e2017-05-03 14:05:12 +0800463 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530464 switch (node->getQualifier())
465 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400466 case EvqConst:
467 message = "can't modify a const";
468 break;
469 case EvqConstReadOnly:
470 message = "can't modify a const";
471 break;
472 case EvqAttribute:
473 message = "can't modify an attribute";
474 break;
475 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400476 case EvqVertexIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800477 case EvqFlatIn:
478 case EvqSmoothIn:
479 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400480 message = "can't modify an input";
481 break;
482 case EvqUniform:
483 message = "can't modify a uniform";
484 break;
485 case EvqVaryingIn:
486 message = "can't modify a varying";
487 break;
488 case EvqFragCoord:
489 message = "can't modify gl_FragCoord";
490 break;
491 case EvqFrontFacing:
492 message = "can't modify gl_FrontFacing";
493 break;
494 case EvqPointCoord:
495 message = "can't modify gl_PointCoord";
496 break;
Martin Radevb0883602016-08-04 17:48:58 +0300497 case EvqNumWorkGroups:
498 message = "can't modify gl_NumWorkGroups";
499 break;
500 case EvqWorkGroupSize:
501 message = "can't modify gl_WorkGroupSize";
502 break;
503 case EvqWorkGroupID:
504 message = "can't modify gl_WorkGroupID";
505 break;
506 case EvqLocalInvocationID:
507 message = "can't modify gl_LocalInvocationID";
508 break;
509 case EvqGlobalInvocationID:
510 message = "can't modify gl_GlobalInvocationID";
511 break;
512 case EvqLocalInvocationIndex:
513 message = "can't modify gl_LocalInvocationIndex";
514 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300515 case EvqViewIDOVR:
516 message = "can't modify gl_ViewID_OVR";
517 break;
Martin Radev802abe02016-08-04 17:48:32 +0300518 case EvqComputeIn:
519 message = "can't modify work group size variable";
520 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800521 case EvqPerVertexIn:
522 message = "can't modify any member in gl_in";
523 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800524 case EvqPrimitiveIDIn:
525 message = "can't modify gl_PrimitiveIDIn";
526 break;
527 case EvqInvocationID:
528 message = "can't modify gl_InvocationID";
529 break;
530 case EvqPrimitiveID:
531 if (mShaderType == GL_FRAGMENT_SHADER)
532 {
533 message = "can't modify gl_PrimitiveID in a fragment shader";
534 }
535 break;
536 case EvqLayer:
537 if (mShaderType == GL_FRAGMENT_SHADER)
538 {
539 message = "can't modify gl_Layer in a fragment shader";
540 }
541 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400542 default:
543 //
544 // Type that can't be written to?
545 //
546 if (node->getBasicType() == EbtVoid)
547 {
548 message = "can't modify void";
549 }
jchen10cc2a10e2017-05-03 14:05:12 +0800550 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400551 {
jchen10cc2a10e2017-05-03 14:05:12 +0800552 message = "can't modify a variable with type ";
553 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300554 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800555 else if (node->getMemoryQualifier().readonly)
556 {
557 message = "can't modify a readonly variable";
558 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000560
jchen10cc2a10e2017-05-03 14:05:12 +0800561 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530562 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000563 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564
Olli Etuaho8a176262016-08-16 14:23:01 +0300565 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000566 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000568 //
569 // Everything else is okay, no error.
570 //
jchen10cc2a10e2017-05-03 14:05:12 +0800571 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300572 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000573
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000574 //
575 // If we get here, we have an error and a message.
576 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530577 if (symNode)
578 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000579 const char *symbol = symNode->getSymbol().c_str();
580 std::stringstream reasonStream;
581 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
582 std::string reason = reasonStream.str();
583 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000584 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530585 else
586 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000587 std::stringstream reasonStream;
588 reasonStream << "l-value required (" << message << ")";
589 std::string reason = reasonStream.str();
590 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000591 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592
Olli Etuaho8a176262016-08-16 14:23:01 +0300593 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594}
595
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596// Both test, and if necessary spit out an error, to see if the node is really
597// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300598void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000599{
Olli Etuaho383b7912016-08-05 11:22:59 +0300600 if (node->getQualifier() != EvqConst)
601 {
602 error(node->getLine(), "constant expression required", "");
603 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604}
605
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606// Both test, and if necessary spit out an error, to see if the node is really
607// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300608void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609{
Olli Etuaho383b7912016-08-05 11:22:59 +0300610 if (!node->isScalarInt())
611 {
612 error(node->getLine(), "integer expression required", token);
613 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614}
615
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000616// Both test, and if necessary spit out an error, to see if we are currently
617// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800618bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619{
Olli Etuaho856c4972016-08-08 11:38:39 +0300620 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300621 {
622 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800623 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300624 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800625 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626}
627
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300628// ESSL 3.00.5 sections 3.8 and 3.9.
629// If it starts "gl_" or contains two consecutive underscores, it's reserved.
630// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300631bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530633 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300634 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530635 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300636 error(line, reservedErrMsg, "gl_");
637 return false;
638 }
639 if (sh::IsWebGLBasedSpec(mShaderSpec))
640 {
641 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530642 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300643 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300644 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300646 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530647 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300648 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300649 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000650 }
651 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300652 if (identifier.find("__") != TString::npos)
653 {
654 error(line,
655 "identifiers containing two consecutive underscores (__) are reserved as "
656 "possible future keywords",
657 identifier.c_str());
658 return false;
659 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300660 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661}
662
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300663// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300664bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800665 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300666 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800668 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530669 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200670 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300671 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000672 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200673
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300674 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530675 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300676 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200677 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300678 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200679 {
jchen10cc2a10e2017-05-03 14:05:12 +0800680 std::string reason("cannot convert a variable with type ");
681 reason += getBasicString(argTyped->getBasicType());
682 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300683 return false;
684 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800685 else if (argTyped->getMemoryQualifier().writeonly)
686 {
687 error(line, "cannot convert a variable with writeonly", "constructor");
688 return false;
689 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200690 if (argTyped->getBasicType() == EbtVoid)
691 {
692 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300693 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200694 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000695 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696
Olli Etuaho856c4972016-08-08 11:38:39 +0300697 if (type.isArray())
698 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300699 // The size of an unsized constructor should already have been determined.
700 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300701 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300702 {
703 error(line, "array constructor needs one argument per array element", "constructor");
704 return false;
705 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300706 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
707 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800708 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300709 {
710 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500711 if (argType.isArray())
712 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300713 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500714 return false;
715 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300716 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300717 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000718 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300719 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300720 }
721 }
722 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300723 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300724 {
725 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300726 if (fields.size() != arguments->size())
727 {
728 error(line,
729 "Number of constructor parameters does not match the number of structure fields",
730 "constructor");
731 return false;
732 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300733
734 for (size_t i = 0; i < fields.size(); i++)
735 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800736 if (i >= arguments->size() ||
737 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300738 {
739 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000740 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300741 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300742 }
743 }
744 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300745 else
746 {
747 // We're constructing a scalar, vector, or matrix.
748
749 // Note: It's okay to have too many components available, but not okay to have unused
750 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
751 // there is an extra argument, so 'overFull' will become true.
752
753 size_t size = 0;
754 bool full = false;
755 bool overFull = false;
756 bool matrixArg = false;
757 for (TIntermNode *arg : *arguments)
758 {
759 const TIntermTyped *argTyped = arg->getAsTyped();
760 ASSERT(argTyped != nullptr);
761
Olli Etuaho487b63a2017-05-23 15:55:09 +0300762 if (argTyped->getBasicType() == EbtStruct)
763 {
764 error(line, "a struct cannot be used as a constructor argument for this type",
765 "constructor");
766 return false;
767 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300768 if (argTyped->getType().isArray())
769 {
770 error(line, "constructing from a non-dereferenced array", "constructor");
771 return false;
772 }
773 if (argTyped->getType().isMatrix())
774 {
775 matrixArg = true;
776 }
777
778 size += argTyped->getType().getObjectSize();
779 if (full)
780 {
781 overFull = true;
782 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300783 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300784 {
785 full = true;
786 }
787 }
788
789 if (type.isMatrix() && matrixArg)
790 {
791 if (arguments->size() != 1)
792 {
793 error(line, "constructing matrix from matrix can only take one argument",
794 "constructor");
795 return false;
796 }
797 }
798 else
799 {
800 if (size != 1 && size < type.getObjectSize())
801 {
802 error(line, "not enough data provided for construction", "constructor");
803 return false;
804 }
805 if (overFull)
806 {
807 error(line, "too many arguments", "constructor");
808 return false;
809 }
810 }
811 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300812
Olli Etuaho8a176262016-08-16 14:23:01 +0300813 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814}
815
Jamie Madillb98c3a82015-07-23 14:26:04 -0400816// This function checks to see if a void variable has been declared and raise an error message for
817// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818//
819// returns true in case of an error
820//
Olli Etuaho856c4972016-08-08 11:38:39 +0300821bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400822 const TString &identifier,
823 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300825 if (type == EbtVoid)
826 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000827 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300828 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300829 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830
Olli Etuaho8a176262016-08-16 14:23:01 +0300831 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832}
833
Jamie Madillb98c3a82015-07-23 14:26:04 -0400834// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300835// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300836bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000837{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300838 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530839 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000840 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300841 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530842 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300843 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844}
845
Jamie Madillb98c3a82015-07-23 14:26:04 -0400846// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300847// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300848void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849{
Martin Radev4a9cd802016-09-01 16:51:51 +0300850 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530851 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000852 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530853 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854}
855
jchen10cc2a10e2017-05-03 14:05:12 +0800856bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
857 const TTypeSpecifierNonArray &pType,
858 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530860 if (pType.type == EbtStruct)
861 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300862 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530863 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000864 std::stringstream reasonStream;
865 reasonStream << reason << " (structure contains a sampler)";
866 std::string reasonStr = reasonStream.str();
867 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300868 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000869 }
jchen10cc2a10e2017-05-03 14:05:12 +0800870 // only samplers need to be checked from structs, since other opaque types can't be struct
871 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300872 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530873 }
jchen10cc2a10e2017-05-03 14:05:12 +0800874 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530875 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000876 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300877 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000878 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879
Olli Etuaho8a176262016-08-16 14:23:01 +0300880 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881}
882
Olli Etuaho856c4972016-08-08 11:38:39 +0300883void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
884 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400885{
886 if (pType.layoutQualifier.location != -1)
887 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400888 error(line, "location must only be specified for a single input or output variable",
889 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400890 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400891}
892
Olli Etuaho856c4972016-08-08 11:38:39 +0300893void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
894 const TLayoutQualifier &layoutQualifier)
895{
896 if (layoutQualifier.location != -1)
897 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000898 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
899 if (mShaderVersion >= 310)
900 {
901 errorMsg =
902 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
903 }
904 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300905 }
906}
907
Martin Radev2cc85b32016-08-05 16:22:53 +0300908void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
909 TQualifier qualifier,
910 const TType &type)
911{
Martin Radev2cc85b32016-08-05 16:22:53 +0300912 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800913 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530914 {
jchen10cc2a10e2017-05-03 14:05:12 +0800915 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000916 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917}
918
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300920unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530922 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000923
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200924 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
925 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
926 // fold as array size.
927 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000928 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000929 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300930 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932
Olli Etuaho856c4972016-08-08 11:38:39 +0300933 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400934
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000935 if (constant->getBasicType() == EbtUInt)
936 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300937 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000938 }
939 else
940 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300941 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000942
Olli Etuaho856c4972016-08-08 11:38:39 +0300943 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000944 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400945 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300946 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000947 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400948
Olli Etuaho856c4972016-08-08 11:38:39 +0300949 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400950 }
951
Olli Etuaho856c4972016-08-08 11:38:39 +0300952 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400953 {
954 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300955 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400956 }
957
958 // The size of arrays is restricted here to prevent issues further down the
959 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
960 // 4096 registers so this should be reasonable even for aggressively optimizable code.
961 const unsigned int sizeLimit = 65536;
962
Olli Etuaho856c4972016-08-08 11:38:39 +0300963 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400964 {
965 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300966 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300968
969 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970}
971
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300973bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
974 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975{
Olli Etuaho8a176262016-08-16 14:23:01 +0300976 if ((elementQualifier.qualifier == EvqAttribute) ||
977 (elementQualifier.qualifier == EvqVertexIn) ||
978 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300979 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400980 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300981 TType(elementQualifier).getQualifierString());
982 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000983 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984
Olli Etuaho8a176262016-08-16 14:23:01 +0300985 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986}
987
Olli Etuaho8a176262016-08-16 14:23:01 +0300988// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +0300989bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
990 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991{
Olli Etuaho8a176262016-08-16 14:23:01 +0300992 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400993 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300994 error(line, "cannot declare arrays of arrays",
995 TType(elementType).getCompleteString().c_str());
996 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000997 }
Olli Etuahoe0803872017-08-23 15:30:23 +0300998 return true;
999}
1000
1001// Check if this qualified element type can be formed into an array. This is only called when array
1002// brackets are associated with an identifier in a declaration, like this:
1003// float a[2];
1004// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1005// are associated with the type, like this:
1006// float[2] a;
1007bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1008 const TPublicType &elementType)
1009{
1010 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1011 {
1012 return false;
1013 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001014 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1015 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1016 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001017 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001018 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001019 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001020 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001021 TType(elementType).getCompleteString().c_str());
1022 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001023 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001024 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025}
1026
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001028void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1029 const TString &identifier,
1030 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031{
Olli Etuaho3739d232015-04-08 12:23:44 +03001032 ASSERT(type != nullptr);
1033 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001034 {
1035 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +03001036 type->qualifier = EvqTemporary;
1037
1038 // Generate informative error messages for ESSL1.
1039 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001040 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001041 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301042 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001043 "structures containing arrays may not be declared constant since they cannot be "
1044 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301045 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001046 }
1047 else
1048 {
1049 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1050 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001051 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001052 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001053 if (type->isUnsizedArray())
1054 {
1055 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001056 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057}
1058
Olli Etuaho2935c582015-04-08 14:32:06 +03001059// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060// and update the symbol table.
1061//
Olli Etuaho2935c582015-04-08 14:32:06 +03001062// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001064bool TParseContext::declareVariable(const TSourceLoc &line,
1065 const TString &identifier,
1066 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001067 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068{
Olli Etuaho2935c582015-04-08 14:32:06 +03001069 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070
Olli Etuaho43364892017-02-13 16:00:12 +00001071 checkBindingIsValid(line, type);
1072
Olli Etuaho856c4972016-08-08 11:38:39 +03001073 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074
Olli Etuaho2935c582015-04-08 14:32:06 +03001075 // gl_LastFragData may be redeclared with a new precision qualifier
1076 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1077 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001078 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1079 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001080 if (type.isArrayOfArrays())
1081 {
1082 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1083 identifier.c_str());
1084 return false;
1085 }
1086 else if (static_cast<int>(type.getOutermostArraySize()) ==
1087 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001088 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001089 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001090 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001091 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001092 }
1093 }
1094 else
1095 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001096 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1097 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001098 return false;
1099 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101
Olli Etuaho8a176262016-08-16 14:23:01 +03001102 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001103 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104
Olli Etuaho0f684632017-07-13 12:42:15 +03001105 (*variable) = symbolTable.declareVariable(&identifier, type);
1106 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001107 {
1108 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001109 return false;
1110 }
1111
Olli Etuaho8a176262016-08-16 14:23:01 +03001112 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001113 return false;
1114
1115 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116}
1117
Martin Radev70866b82016-07-22 15:27:42 +03001118void TParseContext::checkIsParameterQualifierValid(
1119 const TSourceLoc &line,
1120 const TTypeQualifierBuilder &typeQualifierBuilder,
1121 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301122{
Olli Etuahocce89652017-06-19 16:04:09 +03001123 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001124 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001125
1126 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301127 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001128 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1129 }
1130
1131 if (!IsImage(type->getBasicType()))
1132 {
Olli Etuaho43364892017-02-13 16:00:12 +00001133 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001134 }
1135 else
1136 {
1137 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001138 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001139
Martin Radev70866b82016-07-22 15:27:42 +03001140 type->setQualifier(typeQualifier.qualifier);
1141
1142 if (typeQualifier.precision != EbpUndefined)
1143 {
1144 type->setPrecision(typeQualifier.precision);
1145 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146}
1147
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001148bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001149{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001150 ASSERT(extension != TExtension::UNDEFINED);
Jamie Madillb98c3a82015-07-23 14:26:04 -04001151 const TExtensionBehavior &extBehavior = extensionBehavior();
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001152 TExtensionBehavior::const_iterator iter = extBehavior.find(extension);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301153 if (iter == extBehavior.end())
1154 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001155 error(line, "extension is not supported", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001156 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001157 }
zmo@google.comf5450912011-09-09 01:37:19 +00001158 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301159 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1160 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001161 error(line, "extension is disabled", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001162 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001163 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301164 if (iter->second == EBhWarn)
1165 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001166 warning(line, "extension is being used", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001167 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001168 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001169
Olli Etuaho8a176262016-08-16 14:23:01 +03001170 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001171}
1172
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001173// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1174// compile-time or link-time errors are the same whether or not the declaration is empty".
1175// This function implements all the checks that are done on qualifiers regardless of if the
1176// declaration is empty.
1177void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1178 const sh::TLayoutQualifier &layoutQualifier,
1179 const TSourceLoc &location)
1180{
1181 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1182 {
1183 error(location, "Shared memory declarations cannot have layout specified", "layout");
1184 }
1185
1186 if (layoutQualifier.matrixPacking != EmpUnspecified)
1187 {
1188 error(location, "layout qualifier only valid for interface blocks",
1189 getMatrixPackingString(layoutQualifier.matrixPacking));
1190 return;
1191 }
1192
1193 if (layoutQualifier.blockStorage != EbsUnspecified)
1194 {
1195 error(location, "layout qualifier only valid for interface blocks",
1196 getBlockStorageString(layoutQualifier.blockStorage));
1197 return;
1198 }
1199
1200 if (qualifier == EvqFragmentOut)
1201 {
1202 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1203 {
1204 error(location, "invalid layout qualifier combination", "yuv");
1205 return;
1206 }
1207 }
1208 else
1209 {
1210 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1211 }
1212
Olli Etuaho95468d12017-05-04 11:14:34 +03001213 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1214 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001215 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1216 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001217 {
1218 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1219 }
1220
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001221 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1222 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1223 {
1224 canHaveLocation = true;
1225 // We're not checking whether the uniform location is in range here since that depends on
1226 // the type of the variable.
1227 // The type can only be fully determined for non-empty declarations.
1228 }
1229 if (!canHaveLocation)
1230 {
1231 checkLocationIsNotSpecified(location, layoutQualifier);
1232 }
1233}
1234
jchen104cdac9e2017-05-08 11:01:20 +08001235void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1236 const TSourceLoc &location)
1237{
1238 if (publicType.precision != EbpHigh)
1239 {
1240 error(location, "Can only be highp", "atomic counter");
1241 }
1242 // dEQP enforces compile error if location is specified. See uniform_location.test.
1243 if (publicType.layoutQualifier.location != -1)
1244 {
1245 error(location, "location must not be set for atomic_uint", "layout");
1246 }
1247 if (publicType.layoutQualifier.binding == -1)
1248 {
1249 error(location, "no binding specified", "atomic counter");
1250 }
1251}
1252
Martin Radevb8b01222016-11-20 23:25:53 +02001253void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1254 const TSourceLoc &location)
1255{
1256 if (publicType.isUnsizedArray())
1257 {
1258 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1259 // error. It is assumed that this applies to empty declarations as well.
1260 error(location, "empty array declaration needs to specify a size", "");
1261 }
Martin Radevb8b01222016-11-20 23:25:53 +02001262}
1263
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001264// These checks are done for all declarations that are non-empty. They're done for non-empty
1265// declarations starting a declarator list, and declarators that follow an empty declaration.
1266void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1267 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001268{
Olli Etuahofa33d582015-04-09 14:33:12 +03001269 switch (publicType.qualifier)
1270 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001271 case EvqVaryingIn:
1272 case EvqVaryingOut:
1273 case EvqAttribute:
1274 case EvqVertexIn:
1275 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001276 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001277 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001278 {
1279 error(identifierLocation, "cannot be used with a structure",
1280 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001281 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001282 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001283 break;
1284 case EvqBuffer:
1285 if (publicType.getBasicType() != EbtInterfaceBlock)
1286 {
1287 error(identifierLocation,
1288 "cannot declare buffer variables at global scope(outside a block)",
1289 getQualifierString(publicType.qualifier));
1290 return;
1291 }
1292 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001293 default:
1294 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001295 }
jchen10cc2a10e2017-05-03 14:05:12 +08001296 std::string reason(getBasicString(publicType.getBasicType()));
1297 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001298 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001299 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001300 {
1301 return;
1302 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001303
Andrei Volykhina5527072017-03-22 16:46:30 +03001304 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1305 publicType.qualifier != EvqConst) &&
1306 publicType.getBasicType() == EbtYuvCscStandardEXT)
1307 {
1308 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1309 getQualifierString(publicType.qualifier));
1310 return;
1311 }
1312
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001313 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1314 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001315 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1316 // But invalid shaders may still reach here with an unsized array declaration.
1317 if (!publicType.isUnsizedArray())
1318 {
1319 TType type(publicType);
1320 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1321 publicType.layoutQualifier);
1322 }
1323 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001324
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001325 // check for layout qualifier issues
1326 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001327
Martin Radev2cc85b32016-08-05 16:22:53 +03001328 if (IsImage(publicType.getBasicType()))
1329 {
1330
1331 switch (layoutQualifier.imageInternalFormat)
1332 {
1333 case EiifRGBA32F:
1334 case EiifRGBA16F:
1335 case EiifR32F:
1336 case EiifRGBA8:
1337 case EiifRGBA8_SNORM:
1338 if (!IsFloatImage(publicType.getBasicType()))
1339 {
1340 error(identifierLocation,
1341 "internal image format requires a floating image type",
1342 getBasicString(publicType.getBasicType()));
1343 return;
1344 }
1345 break;
1346 case EiifRGBA32I:
1347 case EiifRGBA16I:
1348 case EiifRGBA8I:
1349 case EiifR32I:
1350 if (!IsIntegerImage(publicType.getBasicType()))
1351 {
1352 error(identifierLocation,
1353 "internal image format requires an integer image type",
1354 getBasicString(publicType.getBasicType()));
1355 return;
1356 }
1357 break;
1358 case EiifRGBA32UI:
1359 case EiifRGBA16UI:
1360 case EiifRGBA8UI:
1361 case EiifR32UI:
1362 if (!IsUnsignedImage(publicType.getBasicType()))
1363 {
1364 error(identifierLocation,
1365 "internal image format requires an unsigned image type",
1366 getBasicString(publicType.getBasicType()));
1367 return;
1368 }
1369 break;
1370 case EiifUnspecified:
1371 error(identifierLocation, "layout qualifier", "No image internal format specified");
1372 return;
1373 default:
1374 error(identifierLocation, "layout qualifier", "unrecognized token");
1375 return;
1376 }
1377
1378 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1379 switch (layoutQualifier.imageInternalFormat)
1380 {
1381 case EiifR32F:
1382 case EiifR32I:
1383 case EiifR32UI:
1384 break;
1385 default:
1386 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1387 {
1388 error(identifierLocation, "layout qualifier",
1389 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1390 "image variables must be qualified readonly and/or writeonly");
1391 return;
1392 }
1393 break;
1394 }
1395 }
1396 else
1397 {
Olli Etuaho43364892017-02-13 16:00:12 +00001398 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001399 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1400 }
jchen104cdac9e2017-05-08 11:01:20 +08001401
1402 if (IsAtomicCounter(publicType.getBasicType()))
1403 {
1404 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1405 }
1406 else
1407 {
1408 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1409 }
Olli Etuaho43364892017-02-13 16:00:12 +00001410}
Martin Radev2cc85b32016-08-05 16:22:53 +03001411
Olli Etuaho43364892017-02-13 16:00:12 +00001412void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1413{
1414 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001415 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1416 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1417 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1418 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1419 // when it comes to which shaders are accepted by the compiler.
1420 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001421 if (IsImage(type.getBasicType()))
1422 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001423 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1424 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001425 }
1426 else if (IsSampler(type.getBasicType()))
1427 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001428 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1429 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001430 }
jchen104cdac9e2017-05-08 11:01:20 +08001431 else if (IsAtomicCounter(type.getBasicType()))
1432 {
1433 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1434 }
Olli Etuaho43364892017-02-13 16:00:12 +00001435 else
1436 {
1437 ASSERT(!IsOpaqueType(type.getBasicType()));
1438 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001439 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001440}
1441
Olli Etuaho856c4972016-08-08 11:38:39 +03001442void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1443 const TString &layoutQualifierName,
1444 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001445{
1446
1447 if (mShaderVersion < versionRequired)
1448 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001449 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001450 }
1451}
1452
Olli Etuaho856c4972016-08-08 11:38:39 +03001453bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1454 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001455{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001456 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001457 for (size_t i = 0u; i < localSize.size(); ++i)
1458 {
1459 if (localSize[i] != -1)
1460 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001461 error(location,
1462 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1463 "global layout declaration",
1464 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001465 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001466 }
1467 }
1468
Olli Etuaho8a176262016-08-16 14:23:01 +03001469 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001470}
1471
Olli Etuaho43364892017-02-13 16:00:12 +00001472void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001473 TLayoutImageInternalFormat internalFormat)
1474{
1475 if (internalFormat != EiifUnspecified)
1476 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001477 error(location, "invalid layout qualifier: only valid when used with images",
1478 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001479 }
Olli Etuaho43364892017-02-13 16:00:12 +00001480}
1481
1482void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1483{
1484 if (binding != -1)
1485 {
1486 error(location,
1487 "invalid layout qualifier: only valid when used with opaque types or blocks",
1488 "binding");
1489 }
1490}
1491
jchen104cdac9e2017-05-08 11:01:20 +08001492void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1493{
1494 if (offset != -1)
1495 {
1496 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1497 "offset");
1498 }
1499}
1500
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001501void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1502 int binding,
1503 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001504{
1505 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001506 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001507 {
1508 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1509 }
1510}
1511
1512void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1513 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001514 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001515{
1516 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001517 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001518 {
1519 error(location, "sampler binding greater than maximum texture units", "binding");
1520 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001521}
1522
Jiajia Qinbc585152017-06-23 15:42:17 +08001523void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1524 const TQualifier &qualifier,
1525 int binding,
1526 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001527{
1528 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001529 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001530 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001531 if (binding + size > mMaxUniformBufferBindings)
1532 {
1533 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1534 "binding");
1535 }
1536 }
1537 else if (qualifier == EvqBuffer)
1538 {
1539 if (binding + size > mMaxShaderStorageBufferBindings)
1540 {
1541 error(location,
1542 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1543 "binding");
1544 }
jchen10af713a22017-04-19 09:10:56 +08001545 }
1546}
jchen104cdac9e2017-05-08 11:01:20 +08001547void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1548{
1549 if (binding >= mMaxAtomicCounterBindings)
1550 {
1551 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1552 "binding");
1553 }
1554}
jchen10af713a22017-04-19 09:10:56 +08001555
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001556void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1557 int objectLocationCount,
1558 const TLayoutQualifier &layoutQualifier)
1559{
1560 int loc = layoutQualifier.location;
1561 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1562 {
1563 error(location, "Uniform location out of range", "location");
1564 }
1565}
1566
Andrei Volykhina5527072017-03-22 16:46:30 +03001567void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1568{
1569 if (yuv != false)
1570 {
1571 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1572 }
1573}
1574
Jiajia Qinbc585152017-06-23 15:42:17 +08001575void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1576 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001577{
1578 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1579 {
1580 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001581 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1582 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1583 qual == EvqInOut || qual == EvqConstReadOnly))
1584 {
1585 if (argument->getMemoryQualifier().writeonly)
1586 {
1587 error(argument->getLine(),
1588 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1589 fnCall->getFunctionSymbolInfo()->getName().c_str());
1590 return;
1591 }
1592 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001593 if (qual == EvqOut || qual == EvqInOut)
1594 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001595 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001596 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001597 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001598 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001599 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001600 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001601 }
1602 }
1603 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001604}
1605
Martin Radev70866b82016-07-22 15:27:42 +03001606void TParseContext::checkInvariantVariableQualifier(bool invariant,
1607 const TQualifier qualifier,
1608 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001609{
Martin Radev70866b82016-07-22 15:27:42 +03001610 if (!invariant)
1611 return;
1612
1613 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001614 {
Martin Radev70866b82016-07-22 15:27:42 +03001615 // input variables in the fragment shader can be also qualified as invariant
1616 if (!sh::CanBeInvariantESSL1(qualifier))
1617 {
1618 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1619 }
1620 }
1621 else
1622 {
1623 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1624 {
1625 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1626 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001627 }
1628}
1629
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001630bool TParseContext::supportsExtension(TExtension extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001631{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001632 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001633 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1634 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001635}
1636
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001637bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001638{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001639 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001640}
1641
Jamie Madillb98c3a82015-07-23 14:26:04 -04001642void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1643 const char *extName,
1644 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001645{
1646 pp::SourceLocation srcLoc;
1647 srcLoc.file = loc.first_file;
1648 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001649 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001650}
1651
Jamie Madillb98c3a82015-07-23 14:26:04 -04001652void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1653 const char *name,
1654 const char *value,
1655 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001656{
1657 pp::SourceLocation srcLoc;
1658 srcLoc.file = loc.first_file;
1659 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001660 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001661}
1662
Martin Radev4c4c8e72016-08-04 12:25:34 +03001663sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001664{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001665 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001666 for (size_t i = 0u; i < result.size(); ++i)
1667 {
1668 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1669 {
1670 result[i] = 1;
1671 }
1672 else
1673 {
1674 result[i] = mComputeShaderLocalSize[i];
1675 }
1676 }
1677 return result;
1678}
1679
Olli Etuaho56229f12017-07-10 14:16:33 +03001680TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1681 const TSourceLoc &line)
1682{
1683 TIntermConstantUnion *node = new TIntermConstantUnion(
1684 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1685 node->setLine(line);
1686 return node;
1687}
1688
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689/////////////////////////////////////////////////////////////////////////////////
1690//
1691// Non-Errors.
1692//
1693/////////////////////////////////////////////////////////////////////////////////
1694
Jamie Madill5c097022014-08-20 16:38:32 -04001695const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1696 const TString *name,
1697 const TSymbol *symbol)
1698{
Jamie Madill5c097022014-08-20 16:38:32 -04001699 if (!symbol)
1700 {
1701 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001702 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001703 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001704
1705 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001706 {
1707 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001708 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001709 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001710
1711 const TVariable *variable = static_cast<const TVariable *>(symbol);
1712
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001713 if (variable->getExtension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001714 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001715 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001716 }
1717
Olli Etuaho0f684632017-07-13 12:42:15 +03001718 // Reject shaders using both gl_FragData and gl_FragColor
1719 TQualifier qualifier = variable->getType().getQualifier();
1720 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001721 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001722 mUsesFragData = true;
1723 }
1724 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1725 {
1726 mUsesFragColor = true;
1727 }
1728 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1729 {
1730 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001731 }
1732
Olli Etuaho0f684632017-07-13 12:42:15 +03001733 // This validation is not quite correct - it's only an error to write to
1734 // both FragData and FragColor. For simplicity, and because users shouldn't
1735 // be rewarded for reading from undefined varaibles, return an error
1736 // if they are both referenced, rather than assigned.
1737 if (mUsesFragData && mUsesFragColor)
1738 {
1739 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1740 if (mUsesSecondaryOutputs)
1741 {
1742 errorMessage =
1743 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1744 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1745 }
1746 error(location, errorMessage, name->c_str());
1747 }
1748
1749 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1750 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1751 qualifier == EvqWorkGroupSize)
1752 {
1753 error(location,
1754 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1755 "gl_WorkGroupSize");
1756 }
Jamie Madill5c097022014-08-20 16:38:32 -04001757 return variable;
1758}
1759
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001760TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1761 const TString *name,
1762 const TSymbol *symbol)
1763{
1764 const TVariable *variable = getNamedVariable(location, name, symbol);
1765
Olli Etuaho0f684632017-07-13 12:42:15 +03001766 if (!variable)
1767 {
1768 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1769 node->setLine(location);
1770 return node;
1771 }
1772
Olli Etuaho56229f12017-07-10 14:16:33 +03001773 TIntermTyped *node = nullptr;
1774
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001775 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001776 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001777 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001778 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001779 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001780 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1781 mComputeShaderLocalSizeDeclared)
1782 {
1783 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1784 // needs to be added to the AST as a constant and not as a symbol.
1785 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1786 TConstantUnion *constArray = new TConstantUnion[3];
1787 for (size_t i = 0; i < 3; ++i)
1788 {
1789 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1790 }
1791
1792 ASSERT(variable->getType().getBasicType() == EbtUInt);
1793 ASSERT(variable->getType().getObjectSize() == 3);
1794
1795 TType type(variable->getType());
1796 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001797 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001798 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08001799 // TODO(jiawei.shao@intel.com): set array sizes for user-defined geometry shader inputs.
1800 else if (variable->getType().getQualifier() == EvqPerVertexIn)
1801 {
1802 TType type(variable->getType());
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001803 type.setArraySize(0, mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001804 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), type);
1805 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001806 else
1807 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001808 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001809 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001810 ASSERT(node != nullptr);
1811 node->setLine(location);
1812 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001813}
1814
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001815// Initializers show up in several places in the grammar. Have one set of
1816// code to handle them here.
1817//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001818// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001819bool TParseContext::executeInitializer(const TSourceLoc &line,
1820 const TString &identifier,
1821 const TPublicType &pType,
1822 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001823 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824{
Olli Etuaho13389b62016-10-16 11:48:18 +01001825 ASSERT(initNode != nullptr);
1826 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001827 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828
Olli Etuaho2935c582015-04-08 14:32:06 +03001829 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001830 if (type.isUnsizedArray())
1831 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001832 // In case initializer is not an array or type has more dimensions than initializer, this
1833 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1834 // actually is an array or not. Having a non-array initializer for an unsized array will
1835 // result in an error later, so we don't generate an error message here.
1836 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001837 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001838 if (!declareVariable(line, identifier, type, &variable))
1839 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001840 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001841 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842
Olli Etuahob0c645e2015-05-12 14:25:36 +03001843 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001844 if (symbolTable.atGlobalLevel() &&
1845 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001846 {
1847 // Error message does not completely match behavior with ESSL 1.00, but
1848 // we want to steer developers towards only using constant expressions.
1849 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001850 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001851 }
1852 if (globalInitWarning)
1853 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001854 warning(
1855 line,
1856 "global variable initializers should be constant expressions "
1857 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1858 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001859 }
1860
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001861 //
1862 // identifier must be of type constant, a global, or a temporary
1863 //
1864 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301865 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1866 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001867 error(line, " cannot initialize this type of qualifier ",
1868 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001869 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001870 }
1871 //
1872 // test for and propagate constant
1873 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874
Arun Patole7e7e68d2015-05-22 12:02:25 +05301875 if (qualifier == EvqConst)
1876 {
1877 if (qualifier != initializer->getType().getQualifier())
1878 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001879 std::stringstream reasonStream;
1880 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1881 << "'";
1882 std::string reason = reasonStream.str();
1883 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001884 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001885 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001886 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301887 if (type != initializer->getType())
1888 {
1889 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001890 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001891 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001892 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001893 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001894
1895 // Save the constant folded value to the variable if possible. For example array
1896 // initializers are not folded, since that way copying the array literal to multiple places
1897 // in the shader is avoided.
1898 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1899 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301900 if (initializer->getAsConstantUnion())
1901 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001902 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001903 ASSERT(*initNode == nullptr);
1904 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301905 }
1906 else if (initializer->getAsSymbolNode())
1907 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001908 const TSymbol *symbol =
1909 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1910 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001912 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001913 if (constArray)
1914 {
1915 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001916 ASSERT(*initNode == nullptr);
1917 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001918 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001919 }
1920 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001921
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001922 TIntermSymbol *intermSymbol =
1923 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1924 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001925 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1926 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001927 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001928 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001929 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001930 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001931
Olli Etuaho914b79a2017-06-19 16:03:19 +03001932 return true;
1933}
1934
1935TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1936 const TString &identifier,
1937 TIntermTyped *initializer,
1938 const TSourceLoc &loc)
1939{
1940 checkIsScalarBool(loc, pType);
1941 TIntermBinary *initNode = nullptr;
1942 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1943 {
1944 // The initializer is valid. The init condition needs to have a node - either the
1945 // initializer node, or a constant node in case the initialized variable is const and won't
1946 // be recorded in the AST.
1947 if (initNode == nullptr)
1948 {
1949 return initializer;
1950 }
1951 else
1952 {
1953 TIntermDeclaration *declaration = new TIntermDeclaration();
1954 declaration->appendDeclarator(initNode);
1955 return declaration;
1956 }
1957 }
1958 return nullptr;
1959}
1960
1961TIntermNode *TParseContext::addLoop(TLoopType type,
1962 TIntermNode *init,
1963 TIntermNode *cond,
1964 TIntermTyped *expr,
1965 TIntermNode *body,
1966 const TSourceLoc &line)
1967{
1968 TIntermNode *node = nullptr;
1969 TIntermTyped *typedCond = nullptr;
1970 if (cond)
1971 {
1972 typedCond = cond->getAsTyped();
1973 }
1974 if (cond == nullptr || typedCond)
1975 {
Olli Etuahocce89652017-06-19 16:04:09 +03001976 if (type == ELoopDoWhile)
1977 {
1978 checkIsScalarBool(line, typedCond);
1979 }
1980 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1981 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1982 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1983 !typedCond->isVector()));
1984
Olli Etuaho3ec75682017-07-05 17:02:55 +03001985 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001986 node->setLine(line);
1987 return node;
1988 }
1989
Olli Etuahocce89652017-06-19 16:04:09 +03001990 ASSERT(type != ELoopDoWhile);
1991
Olli Etuaho914b79a2017-06-19 16:03:19 +03001992 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1993 ASSERT(declaration);
1994 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1995 ASSERT(declarator->getLeft()->getAsSymbolNode());
1996
1997 // The condition is a declaration. In the AST representation we don't support declarations as
1998 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1999 // the loop.
2000 TIntermBlock *block = new TIntermBlock();
2001
2002 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2003 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2004 block->appendStatement(declareCondition);
2005
2006 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2007 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002008 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002009 block->appendStatement(loop);
2010 loop->setLine(line);
2011 block->setLine(line);
2012 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013}
2014
Olli Etuahocce89652017-06-19 16:04:09 +03002015TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2016 TIntermNodePair code,
2017 const TSourceLoc &loc)
2018{
Olli Etuaho56229f12017-07-10 14:16:33 +03002019 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002020
2021 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002022 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002023 {
2024 if (cond->getAsConstantUnion()->getBConst(0) == true)
2025 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002026 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002027 }
2028 else
2029 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002030 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002031 }
2032 }
2033
Olli Etuaho3ec75682017-07-05 17:02:55 +03002034 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002035 node->setLine(loc);
2036
2037 return node;
2038}
2039
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002040void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2041{
2042 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2043 typeSpecifier->getBasicType());
2044
2045 if (mShaderVersion < 300 && typeSpecifier->array)
2046 {
2047 error(typeSpecifier->getLine(), "not supported", "first-class array");
2048 typeSpecifier->clearArrayness();
2049 }
2050}
2051
Martin Radev70866b82016-07-22 15:27:42 +03002052TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302053 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002054{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002055 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002056
Martin Radev70866b82016-07-22 15:27:42 +03002057 TPublicType returnType = typeSpecifier;
2058 returnType.qualifier = typeQualifier.qualifier;
2059 returnType.invariant = typeQualifier.invariant;
2060 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002061 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002062 returnType.precision = typeSpecifier.precision;
2063
2064 if (typeQualifier.precision != EbpUndefined)
2065 {
2066 returnType.precision = typeQualifier.precision;
2067 }
2068
Martin Radev4a9cd802016-09-01 16:51:51 +03002069 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2070 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002071
Martin Radev4a9cd802016-09-01 16:51:51 +03002072 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2073 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002074
Martin Radev4a9cd802016-09-01 16:51:51 +03002075 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002076
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002077 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002078 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002079 if (typeSpecifier.array)
2080 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002081 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002082 returnType.clearArrayness();
2083 }
2084
Martin Radev70866b82016-07-22 15:27:42 +03002085 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002086 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002087 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002088 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002089 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002090 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002091
Martin Radev70866b82016-07-22 15:27:42 +03002092 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002093 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002094 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002095 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002096 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002097 }
2098 }
2099 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002100 {
Martin Radev70866b82016-07-22 15:27:42 +03002101 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002102 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002103 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002104 }
Martin Radev70866b82016-07-22 15:27:42 +03002105 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2106 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002107 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002108 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2109 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002110 }
Martin Radev70866b82016-07-22 15:27:42 +03002111 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002112 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002113 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002114 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002115 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002116 }
2117
2118 return returnType;
2119}
2120
Olli Etuaho856c4972016-08-08 11:38:39 +03002121void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2122 const TPublicType &type,
2123 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002124{
2125 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002126 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002127 {
2128 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002129 }
2130
2131 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2132 switch (qualifier)
2133 {
2134 case EvqVertexIn:
2135 // ESSL 3.00 section 4.3.4
2136 if (type.array)
2137 {
2138 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002139 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002140 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002141 return;
2142 case EvqFragmentOut:
2143 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002144 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002145 {
2146 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002147 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002148 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002149 return;
2150 default:
2151 break;
2152 }
2153
2154 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2155 // restrictions.
2156 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002157 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2158 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002159 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2160 {
2161 error(qualifierLocation, "must use 'flat' interpolation here",
2162 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002163 }
2164
Martin Radev4a9cd802016-09-01 16:51:51 +03002165 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002166 {
2167 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2168 // These restrictions are only implied by the ESSL 3.00 spec, but
2169 // the ESSL 3.10 spec lists these restrictions explicitly.
2170 if (type.array)
2171 {
2172 error(qualifierLocation, "cannot be an array of structures",
2173 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002174 }
2175 if (type.isStructureContainingArrays())
2176 {
2177 error(qualifierLocation, "cannot be a structure containing an array",
2178 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002179 }
2180 if (type.isStructureContainingType(EbtStruct))
2181 {
2182 error(qualifierLocation, "cannot be a structure containing a structure",
2183 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002184 }
2185 if (type.isStructureContainingType(EbtBool))
2186 {
2187 error(qualifierLocation, "cannot be a structure containing a bool",
2188 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002189 }
2190 }
2191}
2192
Martin Radev2cc85b32016-08-05 16:22:53 +03002193void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2194{
2195 if (qualifier.getType() == QtStorage)
2196 {
2197 const TStorageQualifierWrapper &storageQualifier =
2198 static_cast<const TStorageQualifierWrapper &>(qualifier);
2199 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2200 !symbolTable.atGlobalLevel())
2201 {
2202 error(storageQualifier.getLine(),
2203 "Local variables can only use the const storage qualifier.",
2204 storageQualifier.getQualifierString().c_str());
2205 }
2206 }
2207}
2208
Olli Etuaho43364892017-02-13 16:00:12 +00002209void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002210 const TSourceLoc &location)
2211{
Jiajia Qinbc585152017-06-23 15:42:17 +08002212 const std::string reason(
2213 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2214 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002215 if (memoryQualifier.readonly)
2216 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002217 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002218 }
2219 if (memoryQualifier.writeonly)
2220 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002221 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002222 }
Martin Radev049edfa2016-11-11 14:35:37 +02002223 if (memoryQualifier.coherent)
2224 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002225 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002226 }
2227 if (memoryQualifier.restrictQualifier)
2228 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002229 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002230 }
2231 if (memoryQualifier.volatileQualifier)
2232 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002233 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002234 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002235}
2236
jchen104cdac9e2017-05-08 11:01:20 +08002237// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2238// intermediate tree.
2239void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2240 size_t size,
2241 bool forceAppend,
2242 const TSourceLoc &loc,
2243 TType &type)
2244{
2245 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2246 int offset;
2247 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2248 {
2249 offset = bindingState.appendSpan(size);
2250 }
2251 else
2252 {
2253 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2254 }
2255 if (offset == -1)
2256 {
2257 error(loc, "Offset overlapping", "atomic counter");
2258 return;
2259 }
2260 TLayoutQualifier qualifier = type.getLayoutQualifier();
2261 qualifier.offset = offset;
2262 type.setLayoutQualifier(qualifier);
2263}
2264
Olli Etuaho13389b62016-10-16 11:48:18 +01002265TIntermDeclaration *TParseContext::parseSingleDeclaration(
2266 TPublicType &publicType,
2267 const TSourceLoc &identifierOrTypeLocation,
2268 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002269{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002270 TType type(publicType);
2271 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2272 mDirectiveHandler.pragma().stdgl.invariantAll)
2273 {
2274 TQualifier qualifier = type.getQualifier();
2275
2276 // The directive handler has already taken care of rejecting invalid uses of this pragma
2277 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2278 // affected variable declarations:
2279 //
2280 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2281 // elsewhere, in TranslatorGLSL.)
2282 //
2283 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2284 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2285 // the way this is currently implemented we have to enable this compiler option before
2286 // parsing the shader and determining the shading language version it uses. If this were
2287 // implemented as a post-pass, the workaround could be more targeted.
2288 //
2289 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2290 // the specification, but there are desktop OpenGL drivers that expect that this is the
2291 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2292 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2293 {
2294 type.setInvariant(true);
2295 }
2296 }
2297
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002298 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2299 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002300
Olli Etuahobab4c082015-04-24 16:38:49 +03002301 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002302 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002303
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002304 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002305 if (emptyDeclaration)
2306 {
Martin Radevb8b01222016-11-20 23:25:53 +02002307 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002308 // In most cases we don't need to create a symbol node for an empty declaration.
2309 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2310 if (type.getBasicType() == EbtStruct)
2311 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002312 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002313 }
jchen104cdac9e2017-05-08 11:01:20 +08002314 else if (IsAtomicCounter(publicType.getBasicType()))
2315 {
2316 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2317 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002318 }
2319 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002320 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002321 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002322
Olli Etuaho856c4972016-08-08 11:38:39 +03002323 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002324
jchen104cdac9e2017-05-08 11:01:20 +08002325 if (IsAtomicCounter(publicType.getBasicType()))
2326 {
2327
2328 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2329 identifierOrTypeLocation, type);
2330 }
2331
Olli Etuaho2935c582015-04-08 14:32:06 +03002332 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002333 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002334
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002335 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002336 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002337 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002338 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002339 }
2340
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002341 TIntermDeclaration *declaration = new TIntermDeclaration();
2342 declaration->setLine(identifierOrTypeLocation);
2343 if (symbol)
2344 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002345 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002346 declaration->appendDeclarator(symbol);
2347 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002348 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002349}
2350
Olli Etuaho13389b62016-10-16 11:48:18 +01002351TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2352 const TSourceLoc &identifierLocation,
2353 const TString &identifier,
2354 const TSourceLoc &indexLocation,
2355 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002356{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002357 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002358
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002359 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2360 identifierLocation);
2361
2362 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002363
Olli Etuaho856c4972016-08-08 11:38:39 +03002364 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002365
Olli Etuaho8a176262016-08-16 14:23:01 +03002366 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002367
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002368 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002369
Olli Etuaho856c4972016-08-08 11:38:39 +03002370 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002371 // Make the type an array even if size check failed.
2372 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002373 arrayType.makeArray(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002374
jchen104cdac9e2017-05-08 11:01:20 +08002375 if (IsAtomicCounter(publicType.getBasicType()))
2376 {
2377 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2378 identifierLocation, arrayType);
2379 }
2380
Olli Etuaho2935c582015-04-08 14:32:06 +03002381 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002382 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002383
Olli Etuaho13389b62016-10-16 11:48:18 +01002384 TIntermDeclaration *declaration = new TIntermDeclaration();
2385 declaration->setLine(identifierLocation);
2386
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002387 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002388 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002389 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2390 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002391 declaration->appendDeclarator(symbol);
2392 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002393
Olli Etuaho13389b62016-10-16 11:48:18 +01002394 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002395}
2396
Olli Etuaho13389b62016-10-16 11:48:18 +01002397TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2398 const TSourceLoc &identifierLocation,
2399 const TString &identifier,
2400 const TSourceLoc &initLocation,
2401 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002402{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002403 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002404
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002405 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2406 identifierLocation);
2407
2408 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002409
Olli Etuaho13389b62016-10-16 11:48:18 +01002410 TIntermDeclaration *declaration = new TIntermDeclaration();
2411 declaration->setLine(identifierLocation);
2412
2413 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002414 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002415 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002416 if (initNode)
2417 {
2418 declaration->appendDeclarator(initNode);
2419 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002420 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002421 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002422}
2423
Olli Etuaho13389b62016-10-16 11:48:18 +01002424TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002425 TPublicType &publicType,
2426 const TSourceLoc &identifierLocation,
2427 const TString &identifier,
2428 const TSourceLoc &indexLocation,
2429 TIntermTyped *indexExpression,
2430 const TSourceLoc &initLocation,
2431 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002432{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002433 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002434
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002435 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2436 identifierLocation);
2437
2438 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002439
Olli Etuaho8a176262016-08-16 14:23:01 +03002440 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002441
2442 TPublicType arrayType(publicType);
2443
Olli Etuaho856c4972016-08-08 11:38:39 +03002444 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002445 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2446 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002447 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002448 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002449 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002450 }
2451 // Make the type an array even if size check failed.
2452 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2453 arrayType.setArraySize(size);
2454
Olli Etuaho13389b62016-10-16 11:48:18 +01002455 TIntermDeclaration *declaration = new TIntermDeclaration();
2456 declaration->setLine(identifierLocation);
2457
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002458 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002459 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002460 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002461 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002462 if (initNode)
2463 {
2464 declaration->appendDeclarator(initNode);
2465 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002466 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002467
2468 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002469}
2470
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002471TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002472 const TTypeQualifierBuilder &typeQualifierBuilder,
2473 const TSourceLoc &identifierLoc,
2474 const TString *identifier,
2475 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002476{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002477 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002478
Martin Radev70866b82016-07-22 15:27:42 +03002479 if (!typeQualifier.invariant)
2480 {
2481 error(identifierLoc, "Expected invariant", identifier->c_str());
2482 return nullptr;
2483 }
2484 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2485 {
2486 return nullptr;
2487 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002488 if (!symbol)
2489 {
2490 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002491 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002492 }
Martin Radev70866b82016-07-22 15:27:42 +03002493 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002494 {
Martin Radev70866b82016-07-22 15:27:42 +03002495 error(identifierLoc, "invariant declaration specifies qualifier",
2496 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002497 }
Martin Radev70866b82016-07-22 15:27:42 +03002498 if (typeQualifier.precision != EbpUndefined)
2499 {
2500 error(identifierLoc, "invariant declaration specifies precision",
2501 getPrecisionString(typeQualifier.precision));
2502 }
2503 if (!typeQualifier.layoutQualifier.isEmpty())
2504 {
2505 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2506 }
2507
2508 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002509 if (!variable)
2510 {
2511 return nullptr;
2512 }
Martin Radev70866b82016-07-22 15:27:42 +03002513 const TType &type = variable->getType();
2514
2515 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2516 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002517 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002518
2519 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2520
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002521 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2522 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002523
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002524 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002525}
2526
Olli Etuaho13389b62016-10-16 11:48:18 +01002527void TParseContext::parseDeclarator(TPublicType &publicType,
2528 const TSourceLoc &identifierLocation,
2529 const TString &identifier,
2530 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002531{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002532 // If the declaration starting this declarator list was empty (example: int,), some checks were
2533 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002534 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002535 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002536 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2537 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002538 }
2539
Olli Etuaho856c4972016-08-08 11:38:39 +03002540 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002541
Olli Etuaho856c4972016-08-08 11:38:39 +03002542 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002543
Olli Etuaho2935c582015-04-08 14:32:06 +03002544 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002545 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002546 if (IsAtomicCounter(publicType.getBasicType()))
2547 {
2548 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2549 identifierLocation, type);
2550 }
Olli Etuaho43364892017-02-13 16:00:12 +00002551 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002552
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002553 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002554 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002555 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2556 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002557 declarationOut->appendDeclarator(symbol);
2558 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002559}
2560
Olli Etuaho13389b62016-10-16 11:48:18 +01002561void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2562 const TSourceLoc &identifierLocation,
2563 const TString &identifier,
2564 const TSourceLoc &arrayLocation,
2565 TIntermTyped *indexExpression,
2566 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002567{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002568 // If the declaration starting this declarator list was empty (example: int,), some checks were
2569 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002570 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002571 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002572 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2573 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002574 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002575
Olli Etuaho856c4972016-08-08 11:38:39 +03002576 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002577
Olli Etuaho856c4972016-08-08 11:38:39 +03002578 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002579
Olli Etuaho8a176262016-08-16 14:23:01 +03002580 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002581 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002582 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002583 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002584 arrayType.makeArray(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002585
jchen104cdac9e2017-05-08 11:01:20 +08002586 if (IsAtomicCounter(publicType.getBasicType()))
2587 {
2588 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2589 true, identifierLocation, arrayType);
2590 }
2591
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002592 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002593 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002594
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002595 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002596 {
2597 TIntermSymbol *symbol =
2598 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2599 symbol->setLine(identifierLocation);
2600 declarationOut->appendDeclarator(symbol);
2601 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002602 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002603}
2604
Olli Etuaho13389b62016-10-16 11:48:18 +01002605void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2606 const TSourceLoc &identifierLocation,
2607 const TString &identifier,
2608 const TSourceLoc &initLocation,
2609 TIntermTyped *initializer,
2610 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002611{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002612 // If the declaration starting this declarator list was empty (example: int,), some checks were
2613 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002614 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002615 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002616 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2617 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002618 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002619
Olli Etuaho856c4972016-08-08 11:38:39 +03002620 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002621
Olli Etuaho13389b62016-10-16 11:48:18 +01002622 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002623 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002624 {
2625 //
2626 // build the intermediate representation
2627 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002628 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002629 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002630 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002631 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002632 }
2633}
2634
Olli Etuaho13389b62016-10-16 11:48:18 +01002635void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2636 const TSourceLoc &identifierLocation,
2637 const TString &identifier,
2638 const TSourceLoc &indexLocation,
2639 TIntermTyped *indexExpression,
2640 const TSourceLoc &initLocation,
2641 TIntermTyped *initializer,
2642 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002643{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002644 // If the declaration starting this declarator list was empty (example: int,), some checks were
2645 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002646 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002647 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002648 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2649 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002650 }
2651
Olli Etuaho856c4972016-08-08 11:38:39 +03002652 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002653
Olli Etuaho8a176262016-08-16 14:23:01 +03002654 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002655
2656 TPublicType arrayType(publicType);
2657
Olli Etuaho856c4972016-08-08 11:38:39 +03002658 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002659 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2660 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002661 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002662 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002663 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002664 }
2665 // Make the type an array even if size check failed.
2666 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2667 arrayType.setArraySize(size);
2668
2669 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002670 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002671 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002672 {
2673 if (initNode)
2674 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002675 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002676 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002677 }
2678}
2679
jchen104cdac9e2017-05-08 11:01:20 +08002680void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2681 const TSourceLoc &location)
2682{
2683 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2684 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2685 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2686 {
2687 error(location, "Requires both binding and offset", "layout");
2688 return;
2689 }
2690 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2691}
2692
Olli Etuahocce89652017-06-19 16:04:09 +03002693void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2694 const TPublicType &type,
2695 const TSourceLoc &loc)
2696{
2697 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2698 !getFragmentPrecisionHigh())
2699 {
2700 error(loc, "precision is not supported in fragment shader", "highp");
2701 }
2702
2703 if (!CanSetDefaultPrecisionOnType(type))
2704 {
2705 error(loc, "illegal type argument for default precision qualifier",
2706 getBasicString(type.getBasicType()));
2707 return;
2708 }
2709 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2710}
2711
Shaob5cc1192017-07-06 10:47:20 +08002712bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2713{
2714 switch (typeQualifier.layoutQualifier.primitiveType)
2715 {
2716 case EptLines:
2717 case EptLinesAdjacency:
2718 case EptTriangles:
2719 case EptTrianglesAdjacency:
2720 return typeQualifier.qualifier == EvqGeometryIn;
2721
2722 case EptLineStrip:
2723 case EptTriangleStrip:
2724 return typeQualifier.qualifier == EvqGeometryOut;
2725
2726 case EptPoints:
2727 return true;
2728
2729 default:
2730 UNREACHABLE();
2731 return false;
2732 }
2733}
2734
Jiawei Shaod8105a02017-08-08 09:54:36 +08002735void TParseContext::setGeometryShaderInputArraySizes()
2736{
2737 // TODO(jiawei.shao@intel.com): check former input array sizes match the input primitive
2738 // declaration.
2739 ASSERT(mGeometryShaderInputArraySize == 0);
2740 mGeometryShaderInputArraySize =
2741 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType);
2742}
2743
Shaob5cc1192017-07-06 10:47:20 +08002744bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2745{
2746 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2747
2748 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2749
2750 if (layoutQualifier.maxVertices != -1)
2751 {
2752 error(typeQualifier.line,
2753 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2754 return false;
2755 }
2756
2757 // Set mGeometryInputPrimitiveType if exists
2758 if (layoutQualifier.primitiveType != EptUndefined)
2759 {
2760 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2761 {
2762 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2763 return false;
2764 }
2765
2766 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2767 {
2768 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shaod8105a02017-08-08 09:54:36 +08002769 setGeometryShaderInputArraySizes();
Shaob5cc1192017-07-06 10:47:20 +08002770 }
2771 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2772 {
2773 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2774 "layout");
2775 return false;
2776 }
2777 }
2778
2779 // Set mGeometryInvocations if exists
2780 if (layoutQualifier.invocations > 0)
2781 {
2782 if (mGeometryShaderInvocations == 0)
2783 {
2784 mGeometryShaderInvocations = layoutQualifier.invocations;
2785 }
2786 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2787 {
2788 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2789 "layout");
2790 return false;
2791 }
2792 }
2793
2794 return true;
2795}
2796
2797bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2798{
2799 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2800
2801 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2802
2803 if (layoutQualifier.invocations > 0)
2804 {
2805 error(typeQualifier.line,
2806 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2807 return false;
2808 }
2809
2810 // Set mGeometryOutputPrimitiveType if exists
2811 if (layoutQualifier.primitiveType != EptUndefined)
2812 {
2813 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2814 {
2815 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2816 return false;
2817 }
2818
2819 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2820 {
2821 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2822 }
2823 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2824 {
2825 error(typeQualifier.line,
2826 "primitive doesn't match earlier output primitive declaration", "layout");
2827 return false;
2828 }
2829 }
2830
2831 // Set mGeometryMaxVertices if exists
2832 if (layoutQualifier.maxVertices > -1)
2833 {
2834 if (mGeometryShaderMaxVertices == -1)
2835 {
2836 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2837 }
2838 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2839 {
2840 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2841 "layout");
2842 return false;
2843 }
2844 }
2845
2846 return true;
2847}
2848
Martin Radev70866b82016-07-22 15:27:42 +03002849void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002850{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002851 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002852 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002853
Martin Radev70866b82016-07-22 15:27:42 +03002854 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2855 typeQualifier.line);
2856
Jamie Madillc2128ff2016-07-04 10:26:17 -04002857 // It should never be the case, but some strange parser errors can send us here.
2858 if (layoutQualifier.isEmpty())
2859 {
2860 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002861 return;
2862 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002863
Martin Radev802abe02016-08-04 17:48:32 +03002864 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002865 {
Olli Etuaho43364892017-02-13 16:00:12 +00002866 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002867 return;
2868 }
2869
Olli Etuaho43364892017-02-13 16:00:12 +00002870 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2871
2872 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002873
2874 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2875
Andrei Volykhina5527072017-03-22 16:46:30 +03002876 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2877
jchen104cdac9e2017-05-08 11:01:20 +08002878 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2879
Martin Radev802abe02016-08-04 17:48:32 +03002880 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002881 {
Martin Radev802abe02016-08-04 17:48:32 +03002882 if (mComputeShaderLocalSizeDeclared &&
2883 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2884 {
2885 error(typeQualifier.line, "Work group size does not match the previous declaration",
2886 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002887 return;
2888 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002889
Martin Radev802abe02016-08-04 17:48:32 +03002890 if (mShaderVersion < 310)
2891 {
2892 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002893 return;
2894 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002895
Martin Radev4c4c8e72016-08-04 12:25:34 +03002896 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002897 {
2898 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002899 return;
2900 }
2901
2902 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2903 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2904
2905 const TConstantUnion *maxComputeWorkGroupSizeData =
2906 maxComputeWorkGroupSize->getConstPointer();
2907
2908 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2909 {
2910 if (layoutQualifier.localSize[i] != -1)
2911 {
2912 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2913 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2914 if (mComputeShaderLocalSize[i] < 1 ||
2915 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2916 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002917 std::stringstream reasonStream;
2918 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2919 << maxComputeWorkGroupSizeValue;
2920 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002921
Olli Etuaho4de340a2016-12-16 09:32:03 +00002922 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002923 return;
2924 }
2925 }
2926 }
2927
2928 mComputeShaderLocalSizeDeclared = true;
2929 }
Shaob5cc1192017-07-06 10:47:20 +08002930 else if (typeQualifier.qualifier == EvqGeometryIn)
2931 {
2932 if (mShaderVersion < 310)
2933 {
2934 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2935 return;
2936 }
2937
2938 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2939 {
2940 return;
2941 }
2942 }
2943 else if (typeQualifier.qualifier == EvqGeometryOut)
2944 {
2945 if (mShaderVersion < 310)
2946 {
2947 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2948 "layout");
2949 return;
2950 }
2951
2952 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2953 {
2954 return;
2955 }
2956 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03002957 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
2958 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002959 {
2960 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2961 // specification.
2962 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2963 {
2964 error(typeQualifier.line, "Number of views does not match the previous declaration",
2965 "layout");
2966 return;
2967 }
2968
2969 if (layoutQualifier.numViews == -1)
2970 {
2971 error(typeQualifier.line, "No num_views specified", "layout");
2972 return;
2973 }
2974
2975 if (layoutQualifier.numViews > mMaxNumViews)
2976 {
2977 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2978 "layout");
2979 return;
2980 }
2981
2982 mNumViews = layoutQualifier.numViews;
2983 }
Martin Radev802abe02016-08-04 17:48:32 +03002984 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002985 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002986 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002987 {
Martin Radev802abe02016-08-04 17:48:32 +03002988 return;
2989 }
2990
Jiajia Qinbc585152017-06-23 15:42:17 +08002991 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03002992 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002993 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00002994 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002995 return;
2996 }
2997
2998 if (mShaderVersion < 300)
2999 {
3000 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3001 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003002 return;
3003 }
3004
Olli Etuaho09b04a22016-12-15 13:30:26 +00003005 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003006
3007 if (layoutQualifier.matrixPacking != EmpUnspecified)
3008 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003009 if (typeQualifier.qualifier == EvqUniform)
3010 {
3011 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3012 }
3013 else if (typeQualifier.qualifier == EvqBuffer)
3014 {
3015 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3016 }
Martin Radev802abe02016-08-04 17:48:32 +03003017 }
3018
3019 if (layoutQualifier.blockStorage != EbsUnspecified)
3020 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003021 if (typeQualifier.qualifier == EvqUniform)
3022 {
3023 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3024 }
3025 else if (typeQualifier.qualifier == EvqBuffer)
3026 {
3027 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3028 }
Martin Radev802abe02016-08-04 17:48:32 +03003029 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003030 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003031}
3032
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003033TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3034 const TFunction &function,
3035 const TSourceLoc &location,
3036 bool insertParametersToSymbolTable)
3037{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003038 checkIsNotReserved(location, function.getName());
3039
Olli Etuahofe486322017-03-21 09:30:54 +00003040 TIntermFunctionPrototype *prototype =
3041 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003042 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3043 // point to the data that already exists in the symbol table.
3044 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3045 prototype->setLine(location);
3046
3047 for (size_t i = 0; i < function.getParamCount(); i++)
3048 {
3049 const TConstParameter &param = function.getParam(i);
3050
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003051 TIntermSymbol *symbol = nullptr;
3052
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003053 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3054 // be used for unused args).
3055 if (param.name != nullptr)
3056 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003057 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003058 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003059 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003060 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3061 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003062 {
3063 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3064 variable->getType());
3065 }
3066 else
3067 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003068 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003069 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003070 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003071 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003072 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003073 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003074 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3075 // symbol.
3076 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003077 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003078 symbol->setLine(location);
3079 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003080 }
3081 return prototype;
3082}
3083
Olli Etuaho16c745a2017-01-16 17:02:27 +00003084TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3085 const TFunction &parsedFunction,
3086 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003087{
Olli Etuaho476197f2016-10-11 13:59:08 +01003088 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3089 // first declaration. Either way the instance in the symbol table is used to track whether the
3090 // function is declared multiple times.
3091 TFunction *function = static_cast<TFunction *>(
3092 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3093 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003094 {
3095 // ESSL 1.00.17 section 4.2.7.
3096 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3097 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003098 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003099 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003100
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003101 TIntermFunctionPrototype *prototype =
3102 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003103
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003104 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003105
3106 if (!symbolTable.atGlobalLevel())
3107 {
3108 // ESSL 3.00.4 section 4.2.4.
3109 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003110 }
3111
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003112 return prototype;
3113}
3114
Olli Etuaho336b1472016-10-05 16:37:55 +01003115TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003116 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003117 TIntermBlock *functionBody,
3118 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003119{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003120 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003121 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3122 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003123 error(location, "function does not return a value:",
3124 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003125 }
3126
Olli Etuahof51fdd22016-10-03 10:03:40 +01003127 if (functionBody == nullptr)
3128 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003129 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003130 functionBody->setLine(location);
3131 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003132 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003133 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003134 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003135
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003136 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003137 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003138}
3139
Olli Etuaho476197f2016-10-11 13:59:08 +01003140void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3141 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003142 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003143{
Olli Etuaho476197f2016-10-11 13:59:08 +01003144 ASSERT(function);
3145 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003146 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003147 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003148
3149 if (builtIn)
3150 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003151 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003152 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003153 else
Jamie Madill185fb402015-06-12 15:48:48 -04003154 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003155 TFunction *prevDec = static_cast<TFunction *>(
3156 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3157
3158 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3159 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3160 // occurance.
3161 if (*function != prevDec)
3162 {
3163 // Swap the parameters of the previous declaration to the parameters of the function
3164 // definition (parameter names may differ).
3165 prevDec->swapParameters(**function);
3166
3167 // The function definition will share the same symbol as any previous declaration.
3168 *function = prevDec;
3169 }
3170
3171 if ((*function)->isDefined())
3172 {
3173 error(location, "function already has a body", (*function)->getName().c_str());
3174 }
3175
3176 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003177 }
Jamie Madill185fb402015-06-12 15:48:48 -04003178
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003179 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003180 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003181 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003182
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003183 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003184 setLoopNestingLevel(0);
3185}
3186
Jamie Madillb98c3a82015-07-23 14:26:04 -04003187TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003188{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003189 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003190 // We don't know at this point whether this is a function definition or a prototype.
3191 // The definition production code will check for redefinitions.
3192 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003193 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003194 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3195 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003196 //
3197 TFunction *prevDec =
3198 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303199
Martin Radevda6254b2016-12-14 17:00:36 +02003200 if (getShaderVersion() >= 300 &&
3201 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3202 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303203 {
Martin Radevda6254b2016-12-14 17:00:36 +02003204 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303205 // Therefore overloading or redefining builtin functions is an error.
3206 error(location, "Name of a built-in function cannot be redeclared as function",
3207 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303208 }
3209 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003210 {
3211 if (prevDec->getReturnType() != function->getReturnType())
3212 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003213 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003214 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003215 }
3216 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3217 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003218 if (prevDec->getParam(i).type->getQualifier() !=
3219 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003220 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003221 error(location,
3222 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003223 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003224 }
3225 }
3226 }
3227
3228 //
3229 // Check for previously declared variables using the same name.
3230 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003231 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003232 if (prevSym)
3233 {
3234 if (!prevSym->isFunction())
3235 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003236 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003237 }
3238 }
3239 else
3240 {
3241 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003242 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003243 }
3244
3245 // We're at the inner scope level of the function's arguments and body statement.
3246 // Add the function prototype to the surrounding scope instead.
3247 symbolTable.getOuterLevel()->insert(function);
3248
Olli Etuaho78d13742017-01-18 13:06:10 +00003249 // Raise error message if main function takes any parameters or return anything other than void
3250 if (function->getName() == "main")
3251 {
3252 if (function->getParamCount() > 0)
3253 {
3254 error(location, "function cannot take any parameter(s)", "main");
3255 }
3256 if (function->getReturnType().getBasicType() != EbtVoid)
3257 {
3258 error(location, "main function cannot return a value",
3259 function->getReturnType().getBasicString());
3260 }
3261 }
3262
Jamie Madill185fb402015-06-12 15:48:48 -04003263 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003264 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3265 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003266 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3267 //
3268 return function;
3269}
3270
Olli Etuaho9de84a52016-06-14 17:36:01 +03003271TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3272 const TString *name,
3273 const TSourceLoc &location)
3274{
3275 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3276 {
3277 error(location, "no qualifiers allowed for function return",
3278 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003279 }
3280 if (!type.layoutQualifier.isEmpty())
3281 {
3282 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003283 }
jchen10cc2a10e2017-05-03 14:05:12 +08003284 // make sure an opaque type is not involved as well...
3285 std::string reason(getBasicString(type.getBasicType()));
3286 reason += "s can't be function return values";
3287 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003288 if (mShaderVersion < 300)
3289 {
3290 // Array return values are forbidden, but there's also no valid syntax for declaring array
3291 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003292 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003293
3294 if (type.isStructureContainingArrays())
3295 {
3296 // ESSL 1.00.17 section 6.1 Function Definitions
3297 error(location, "structures containing arrays can't be function return values",
3298 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003299 }
3300 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003301
3302 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003303 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003304}
3305
Olli Etuahocce89652017-06-19 16:04:09 +03003306TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3307{
Olli Etuahocce89652017-06-19 16:04:09 +03003308 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003309 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003310}
3311
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003312TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003313{
Olli Etuahocce89652017-06-19 16:04:09 +03003314 if (mShaderVersion < 300 && publicType.array)
3315 {
3316 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3317 "[]");
3318 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003319 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003320 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003321 error(publicType.getLine(), "constructor can't be a structure definition",
3322 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003323 }
3324
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003325 TType *type = new TType(publicType);
3326 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003327 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003328 error(publicType.getLine(), "cannot construct this type",
3329 getBasicString(publicType.getBasicType()));
3330 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003331 }
3332
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003333 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003334}
3335
Olli Etuahocce89652017-06-19 16:04:09 +03003336TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3337 const TString *name,
3338 const TSourceLoc &nameLoc)
3339{
3340 if (publicType.getBasicType() == EbtVoid)
3341 {
3342 error(nameLoc, "illegal use of type 'void'", name->c_str());
3343 }
3344 checkIsNotReserved(nameLoc, *name);
3345 TType *type = new TType(publicType);
3346 TParameter param = {name, type};
3347 return param;
3348}
3349
3350TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3351 const TSourceLoc &identifierLoc,
3352 TIntermTyped *arraySize,
3353 const TSourceLoc &arrayLoc,
3354 TPublicType *type)
3355{
Olli Etuahoe0803872017-08-23 15:30:23 +03003356 checkArrayElementIsNotArray(arrayLoc, *type);
Olli Etuahocce89652017-06-19 16:04:09 +03003357 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3358 type->setArraySize(size);
3359 return parseParameterDeclarator(*type, identifier, identifierLoc);
3360}
3361
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003362bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3363 TType type,
3364 const TSourceLoc &line)
3365{
3366 if (arguments->empty())
3367 {
3368 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3369 return false;
3370 }
3371 for (TIntermNode *arg : *arguments)
3372 {
3373 TIntermTyped *element = arg->getAsTyped();
3374 ASSERT(element);
3375 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3376 if (dimensionalityFromElement > type.getArraySizes().size())
3377 {
3378 error(line, "constructing from a non-dereferenced array", "constructor");
3379 return false;
3380 }
3381 else if (dimensionalityFromElement < type.getArraySizes().size())
3382 {
3383 if (dimensionalityFromElement == 1u)
3384 {
3385 error(line, "implicitly sized array of arrays constructor argument is not an array",
3386 "constructor");
3387 }
3388 else
3389 {
3390 error(line,
3391 "implicitly sized array of arrays constructor argument dimensionality is too "
3392 "low",
3393 "constructor");
3394 }
3395 return false;
3396 }
3397 }
3398 return true;
3399}
3400
Jamie Madillb98c3a82015-07-23 14:26:04 -04003401// This function is used to test for the correctness of the parameters passed to various constructor
3402// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003403//
Olli Etuaho856c4972016-08-08 11:38:39 +03003404// 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 +00003405//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003406TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003407 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303408 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003409{
Olli Etuaho856c4972016-08-08 11:38:39 +03003410 if (type.isUnsizedArray())
3411 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003412 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003413 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003414 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003415 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003416 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003417 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3418 ASSERT(firstElement);
3419 type.setArraySize(type.getArraySizes().size() - 1u,
3420 static_cast<unsigned int>(arguments->size()));
3421 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3422 {
3423 if (type.getArraySizes()[i] == 0u)
3424 {
3425 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3426 }
3427 }
3428 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003429 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003430
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003431 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003432 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003433 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003434 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003435
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003436 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003437 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003438
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003439 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3440 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003441 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003442 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003443 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003444 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003445}
3446
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003447//
3448// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003449// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003450//
Olli Etuaho13389b62016-10-16 11:48:18 +01003451TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003452 const TTypeQualifierBuilder &typeQualifierBuilder,
3453 const TSourceLoc &nameLine,
3454 const TString &blockName,
3455 TFieldList *fieldList,
3456 const TString *instanceName,
3457 const TSourceLoc &instanceLine,
3458 TIntermTyped *arrayIndex,
3459 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003460{
Olli Etuaho856c4972016-08-08 11:38:39 +03003461 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003462
Olli Etuaho77ba4082016-12-16 12:01:18 +00003463 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003464
Jiajia Qinbc585152017-06-23 15:42:17 +08003465 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003466 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003467 error(typeQualifier.line,
3468 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3469 "3.10",
3470 getQualifierString(typeQualifier.qualifier));
3471 }
3472 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3473 {
3474 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003475 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003476 }
3477
Martin Radev70866b82016-07-22 15:27:42 +03003478 if (typeQualifier.invariant)
3479 {
3480 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3481 }
3482
Jiajia Qinbc585152017-06-23 15:42:17 +08003483 if (typeQualifier.qualifier != EvqBuffer)
3484 {
3485 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3486 }
Olli Etuaho43364892017-02-13 16:00:12 +00003487
jchen10af713a22017-04-19 09:10:56 +08003488 // add array index
3489 unsigned int arraySize = 0;
3490 if (arrayIndex != nullptr)
3491 {
3492 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3493 }
3494
3495 if (mShaderVersion < 310)
3496 {
3497 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3498 }
3499 else
3500 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003501 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3502 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003503 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003504
Andrei Volykhina5527072017-03-22 16:46:30 +03003505 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3506
Jamie Madill099c0f32013-06-20 11:55:52 -04003507 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003508 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003509
Jamie Madill099c0f32013-06-20 11:55:52 -04003510 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3511 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003512 if (typeQualifier.qualifier == EvqUniform)
3513 {
3514 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3515 }
3516 else if (typeQualifier.qualifier == EvqBuffer)
3517 {
3518 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3519 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003520 }
3521
Jamie Madill1566ef72013-06-20 11:55:54 -04003522 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3523 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003524 if (typeQualifier.qualifier == EvqUniform)
3525 {
3526 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3527 }
3528 else if (typeQualifier.qualifier == EvqBuffer)
3529 {
3530 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3531 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003532 }
3533
Olli Etuaho856c4972016-08-08 11:38:39 +03003534 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003535
Martin Radev2cc85b32016-08-05 16:22:53 +03003536 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3537
Olli Etuaho0f684632017-07-13 12:42:15 +03003538 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303539 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003540 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003541 }
3542
Jamie Madill98493dd2013-07-08 14:39:03 -04003543 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303544 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3545 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003546 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303547 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003548 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303549 {
jchen10cc2a10e2017-05-03 14:05:12 +08003550 std::string reason("unsupported type - ");
3551 reason += fieldType->getBasicString();
3552 reason += " types are not allowed in interface blocks";
3553 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003554 }
3555
Jamie Madill98493dd2013-07-08 14:39:03 -04003556 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003557 switch (qualifier)
3558 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003559 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003560 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003561 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003562 if (typeQualifier.qualifier == EvqBuffer)
3563 {
3564 error(field->line(), "invalid qualifier on shader storage block member",
3565 getQualifierString(qualifier));
3566 }
3567 break;
3568 case EvqBuffer:
3569 if (typeQualifier.qualifier == EvqUniform)
3570 {
3571 error(field->line(), "invalid qualifier on uniform block member",
3572 getQualifierString(qualifier));
3573 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003574 break;
3575 default:
3576 error(field->line(), "invalid qualifier on interface block member",
3577 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003578 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003579 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003580
Martin Radev70866b82016-07-22 15:27:42 +03003581 if (fieldType->isInvariant())
3582 {
3583 error(field->line(), "invalid qualifier on interface block member", "invariant");
3584 }
3585
Jamie Madilla5efff92013-06-06 11:56:47 -04003586 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003587 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003588 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003589 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003590
Jamie Madill98493dd2013-07-08 14:39:03 -04003591 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003592 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003593 error(field->line(), "invalid layout qualifier: cannot be used here",
3594 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003595 }
3596
Jamie Madill98493dd2013-07-08 14:39:03 -04003597 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003598 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003599 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003600 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003601 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003602 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003603 warning(field->line(),
3604 "extraneous layout qualifier: only has an effect on matrix types",
3605 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003606 }
3607
Jamie Madill98493dd2013-07-08 14:39:03 -04003608 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003609
3610 if (typeQualifier.qualifier == EvqBuffer)
3611 {
3612 // set memory qualifiers
3613 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3614 // qualified with a memory qualifier, it is as if all of its members were declared with
3615 // the same memory qualifier.
3616 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3617 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3618 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3619 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3620 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3621 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3622 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3623 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3624 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3625 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3626 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003627 }
3628
Jamie Madillb98c3a82015-07-23 14:26:04 -04003629 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003630 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003631 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3632 if (arrayIndex != nullptr)
3633 {
3634 interfaceBlockType.makeArray(arraySize);
3635 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003636
3637 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003638 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003639
Jamie Madill98493dd2013-07-08 14:39:03 -04003640 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003641 {
3642 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003643 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3644 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003645 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303646 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003647
3648 // set parent pointer of the field variable
3649 fieldType->setInterfaceBlock(interfaceBlock);
3650
Olli Etuaho0f684632017-07-13 12:42:15 +03003651 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003652
Olli Etuaho0f684632017-07-13 12:42:15 +03003653 if (fieldVariable)
3654 {
3655 fieldVariable->setQualifier(typeQualifier.qualifier);
3656 }
3657 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303658 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003659 error(field->line(), "redefinition of an interface block member name",
3660 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003661 }
3662 }
3663 }
3664 else
3665 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003666 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003667
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003668 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003669 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3670 if (instanceTypeDef)
3671 {
3672 instanceTypeDef->setQualifier(typeQualifier.qualifier);
3673 symbolId = instanceTypeDef->getUniqueId();
3674 }
3675 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303676 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003677 error(instanceLine, "redefinition of an interface block instance name",
3678 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003679 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003680 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003681 }
3682
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003683 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3684 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003685 TIntermDeclaration *declaration = new TIntermDeclaration();
3686 declaration->appendDeclarator(blockSymbol);
3687 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003688
3689 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003690 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003691}
3692
Olli Etuaho383b7912016-08-05 11:22:59 +03003693void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003694{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003695 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003696
3697 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003698 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303699 if (mStructNestingLevel > 1)
3700 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003701 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003702 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003703}
3704
3705void TParseContext::exitStructDeclaration()
3706{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003707 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003708}
3709
Olli Etuaho8a176262016-08-16 14:23:01 +03003710void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003711{
Jamie Madillacb4b812016-11-07 13:50:29 -05003712 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303713 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003714 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003715 }
3716
Arun Patole7e7e68d2015-05-22 12:02:25 +05303717 if (field.type()->getBasicType() != EbtStruct)
3718 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003719 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003720 }
3721
3722 // We're already inside a structure definition at this point, so add
3723 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303724 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3725 {
Jamie Madill41a49272014-03-18 16:10:13 -04003726 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003727 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3728 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003729 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003730 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003731 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003732 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003733}
3734
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003735//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003736// Parse an array index expression
3737//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003738TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3739 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303740 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003741{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003742 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3743 {
3744 if (baseExpression->getAsSymbolNode())
3745 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303746 error(location, " left of '[' is not of type array, matrix, or vector ",
3747 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003748 }
3749 else
3750 {
3751 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3752 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003753
Olli Etuaho3ec75682017-07-05 17:02:55 +03003754 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003755 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003756
Jiawei Shaod8105a02017-08-08 09:54:36 +08003757 if (baseExpression->getQualifier() == EvqPerVertexIn)
3758 {
3759 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3760 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3761 {
3762 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3763 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3764 }
3765 }
3766
Jamie Madill21c1e452014-12-29 11:33:41 -05003767 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3768
Olli Etuaho36b05142015-11-12 13:10:42 +02003769 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3770 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3771 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3772 // index is a constant expression.
3773 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3774 {
3775 if (baseExpression->isInterfaceBlock())
3776 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003777 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3778 switch (baseExpression->getQualifier())
3779 {
3780 case EvqPerVertexIn:
3781 break;
3782 case EvqUniform:
3783 case EvqBuffer:
3784 error(location,
3785 "array indexes for uniform block arrays and shader storage block arrays "
3786 "must be constant integral expressions",
3787 "[");
3788 break;
3789 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003790 // We can reach here only in error cases.
3791 ASSERT(mDiagnostics->numErrors() > 0);
3792 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003793 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003794 }
3795 else if (baseExpression->getQualifier() == EvqFragmentOut)
3796 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003797 error(location,
3798 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003799 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003800 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3801 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003802 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003803 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003804 }
3805
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003806 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003807 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003808 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3809 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3810 // constant fold expressions that are not constant expressions). The most compatible way to
3811 // handle this case is to report a warning instead of an error and force the index to be in
3812 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003813 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003814 int index = 0;
3815 if (indexConstantUnion->getBasicType() == EbtInt)
3816 {
3817 index = indexConstantUnion->getIConst(0);
3818 }
3819 else if (indexConstantUnion->getBasicType() == EbtUInt)
3820 {
3821 index = static_cast<int>(indexConstantUnion->getUConst(0));
3822 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003823
3824 int safeIndex = -1;
3825
3826 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003827 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003828 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003829 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003830 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003831 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003832 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003833 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003834 "GL_EXT_draw_buffers is disabled",
3835 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003836 safeIndex = 0;
3837 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003838 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003839 // Only do generic out-of-range check if similar error hasn't already been reported.
3840 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003841 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003842 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003843 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003844 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003845 }
3846 }
3847 else if (baseExpression->isMatrix())
3848 {
3849 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003850 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003851 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003852 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003853 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003854 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003855 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3856 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003857 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003858 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003859
3860 ASSERT(safeIndex >= 0);
3861 // Data of constant unions can't be changed, because it may be shared with other
3862 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3863 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003864 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003865 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003866 TConstantUnion *safeConstantUnion = new TConstantUnion();
3867 safeConstantUnion->setIConst(safeIndex);
3868 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003869 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003870 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003871
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003872 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3873 node->setLine(location);
3874 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003875 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003876 else
3877 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003878 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3879 node->setLine(location);
3880 // Indirect indexing can never be constant folded.
3881 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003882 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003883}
3884
Olli Etuaho90892fb2016-07-14 14:44:51 +03003885int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3886 const TSourceLoc &location,
3887 int index,
3888 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003889 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003890{
3891 if (index >= arraySize || index < 0)
3892 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003893 std::stringstream reasonStream;
3894 reasonStream << reason << " '" << index << "'";
3895 std::string token = reasonStream.str();
3896 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003897 if (index < 0)
3898 {
3899 return 0;
3900 }
3901 else
3902 {
3903 return arraySize - 1;
3904 }
3905 }
3906 return index;
3907}
3908
Jamie Madillb98c3a82015-07-23 14:26:04 -04003909TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3910 const TSourceLoc &dotLocation,
3911 const TString &fieldString,
3912 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003913{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003914 if (baseExpression->isArray())
3915 {
3916 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003917 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003918 }
3919
3920 if (baseExpression->isVector())
3921 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003922 TVector<int> fieldOffsets;
3923 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3924 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003925 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003926 fieldOffsets.resize(1);
3927 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003928 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003929 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3930 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003931
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003932 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003933 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003934 else if (baseExpression->getBasicType() == EbtStruct)
3935 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303936 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003937 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003938 {
3939 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003940 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003941 }
3942 else
3943 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003944 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003945 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003946 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003947 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003948 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003949 {
3950 fieldFound = true;
3951 break;
3952 }
3953 }
3954 if (fieldFound)
3955 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003956 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003957 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003958 TIntermBinary *node =
3959 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3960 node->setLine(dotLocation);
3961 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003962 }
3963 else
3964 {
3965 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003966 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003967 }
3968 }
3969 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003970 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003971 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303972 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003973 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003974 {
3975 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003976 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003977 }
3978 else
3979 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003980 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003981 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003982 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003983 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003984 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003985 {
3986 fieldFound = true;
3987 break;
3988 }
3989 }
3990 if (fieldFound)
3991 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003992 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003993 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003994 TIntermBinary *node =
3995 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3996 node->setLine(dotLocation);
3997 // Indexing interface blocks can never be constant folded.
3998 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003999 }
4000 else
4001 {
4002 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004003 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004004 }
4005 }
4006 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004007 else
4008 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004009 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004010 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004011 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304012 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004013 }
4014 else
4015 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304016 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004017 " field selection requires structure, vector, or interface block on left hand "
4018 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304019 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004020 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004021 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004022 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004023}
4024
Jamie Madillb98c3a82015-07-23 14:26:04 -04004025TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4026 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004027{
Martin Radev802abe02016-08-04 17:48:32 +03004028 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004029
4030 if (qualifierType == "shared")
4031 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004032 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004033 {
4034 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4035 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004036 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004037 }
4038 else if (qualifierType == "packed")
4039 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004040 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004041 {
4042 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4043 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004044 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004045 }
4046 else if (qualifierType == "std140")
4047 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004048 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004049 }
4050 else if (qualifierType == "row_major")
4051 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004052 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004053 }
4054 else if (qualifierType == "column_major")
4055 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004056 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004057 }
4058 else if (qualifierType == "location")
4059 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004060 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4061 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004062 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004063 else if (qualifierType == "yuv" && isExtensionEnabled(TExtension::EXT_YUV_target) &&
Andrei Volykhina5527072017-03-22 16:46:30 +03004064 mShaderType == GL_FRAGMENT_SHADER)
4065 {
4066 qualifier.yuv = true;
4067 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004068 else if (qualifierType == "rgba32f")
4069 {
4070 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4071 qualifier.imageInternalFormat = EiifRGBA32F;
4072 }
4073 else if (qualifierType == "rgba16f")
4074 {
4075 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4076 qualifier.imageInternalFormat = EiifRGBA16F;
4077 }
4078 else if (qualifierType == "r32f")
4079 {
4080 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4081 qualifier.imageInternalFormat = EiifR32F;
4082 }
4083 else if (qualifierType == "rgba8")
4084 {
4085 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4086 qualifier.imageInternalFormat = EiifRGBA8;
4087 }
4088 else if (qualifierType == "rgba8_snorm")
4089 {
4090 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4091 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4092 }
4093 else if (qualifierType == "rgba32i")
4094 {
4095 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4096 qualifier.imageInternalFormat = EiifRGBA32I;
4097 }
4098 else if (qualifierType == "rgba16i")
4099 {
4100 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4101 qualifier.imageInternalFormat = EiifRGBA16I;
4102 }
4103 else if (qualifierType == "rgba8i")
4104 {
4105 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4106 qualifier.imageInternalFormat = EiifRGBA8I;
4107 }
4108 else if (qualifierType == "r32i")
4109 {
4110 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4111 qualifier.imageInternalFormat = EiifR32I;
4112 }
4113 else if (qualifierType == "rgba32ui")
4114 {
4115 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4116 qualifier.imageInternalFormat = EiifRGBA32UI;
4117 }
4118 else if (qualifierType == "rgba16ui")
4119 {
4120 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4121 qualifier.imageInternalFormat = EiifRGBA16UI;
4122 }
4123 else if (qualifierType == "rgba8ui")
4124 {
4125 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4126 qualifier.imageInternalFormat = EiifRGBA8UI;
4127 }
4128 else if (qualifierType == "r32ui")
4129 {
4130 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4131 qualifier.imageInternalFormat = EiifR32UI;
4132 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004133 else if (qualifierType == "points" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004134 mShaderType == GL_GEOMETRY_SHADER_OES)
4135 {
4136 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4137 qualifier.primitiveType = EptPoints;
4138 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004139 else if (qualifierType == "lines" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004140 mShaderType == GL_GEOMETRY_SHADER_OES)
4141 {
4142 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4143 qualifier.primitiveType = EptLines;
4144 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004145 else if (qualifierType == "lines_adjacency" &&
4146 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004147 mShaderType == GL_GEOMETRY_SHADER_OES)
4148 {
4149 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4150 qualifier.primitiveType = EptLinesAdjacency;
4151 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004152 else if (qualifierType == "triangles" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004153 mShaderType == GL_GEOMETRY_SHADER_OES)
4154 {
4155 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4156 qualifier.primitiveType = EptTriangles;
4157 }
4158 else if (qualifierType == "triangles_adjacency" &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004159 isExtensionEnabled(TExtension::OES_geometry_shader) &&
4160 mShaderType == GL_GEOMETRY_SHADER_OES)
Shaob5cc1192017-07-06 10:47:20 +08004161 {
4162 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4163 qualifier.primitiveType = EptTrianglesAdjacency;
4164 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004165 else if (qualifierType == "line_strip" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004166 mShaderType == GL_GEOMETRY_SHADER_OES)
4167 {
4168 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4169 qualifier.primitiveType = EptLineStrip;
4170 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004171 else if (qualifierType == "triangle_strip" &&
4172 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004173 mShaderType == GL_GEOMETRY_SHADER_OES)
4174 {
4175 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4176 qualifier.primitiveType = EptTriangleStrip;
4177 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004178
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004179 else
4180 {
4181 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004182 }
4183
Jamie Madilla5efff92013-06-06 11:56:47 -04004184 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004185}
4186
Martin Radev802abe02016-08-04 17:48:32 +03004187void TParseContext::parseLocalSize(const TString &qualifierType,
4188 const TSourceLoc &qualifierTypeLine,
4189 int intValue,
4190 const TSourceLoc &intValueLine,
4191 const std::string &intValueString,
4192 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004193 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004194{
Olli Etuaho856c4972016-08-08 11:38:39 +03004195 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004196 if (intValue < 1)
4197 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004198 std::stringstream reasonStream;
4199 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4200 std::string reason = reasonStream.str();
4201 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004202 }
4203 (*localSize)[index] = intValue;
4204}
4205
Olli Etuaho09b04a22016-12-15 13:30:26 +00004206void TParseContext::parseNumViews(int intValue,
4207 const TSourceLoc &intValueLine,
4208 const std::string &intValueString,
4209 int *numViews)
4210{
4211 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4212 // specification.
4213 if (intValue < 1)
4214 {
4215 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4216 }
4217 *numViews = intValue;
4218}
4219
Shaob5cc1192017-07-06 10:47:20 +08004220void TParseContext::parseInvocations(int intValue,
4221 const TSourceLoc &intValueLine,
4222 const std::string &intValueString,
4223 int *numInvocations)
4224{
4225 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4226 // it doesn't make sense to accept invocations <= 0.
4227 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4228 {
4229 error(intValueLine,
4230 "out of range: invocations must be in the range of [1, "
4231 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4232 intValueString.c_str());
4233 }
4234 else
4235 {
4236 *numInvocations = intValue;
4237 }
4238}
4239
4240void TParseContext::parseMaxVertices(int intValue,
4241 const TSourceLoc &intValueLine,
4242 const std::string &intValueString,
4243 int *maxVertices)
4244{
4245 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4246 // it doesn't make sense to accept max_vertices < 0.
4247 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4248 {
4249 error(
4250 intValueLine,
4251 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4252 intValueString.c_str());
4253 }
4254 else
4255 {
4256 *maxVertices = intValue;
4257 }
4258}
4259
Jamie Madillb98c3a82015-07-23 14:26:04 -04004260TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4261 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004262 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304263 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004264{
Martin Radev802abe02016-08-04 17:48:32 +03004265 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004266
Martin Radev802abe02016-08-04 17:48:32 +03004267 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004268
Martin Radev802abe02016-08-04 17:48:32 +03004269 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004270 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004271 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004272 if (intValue < 0)
4273 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004274 error(intValueLine, "out of range: location must be non-negative",
4275 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004276 }
4277 else
4278 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004279 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004280 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004281 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004282 }
Olli Etuaho43364892017-02-13 16:00:12 +00004283 else if (qualifierType == "binding")
4284 {
4285 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4286 if (intValue < 0)
4287 {
4288 error(intValueLine, "out of range: binding must be non-negative",
4289 intValueString.c_str());
4290 }
4291 else
4292 {
4293 qualifier.binding = intValue;
4294 }
4295 }
jchen104cdac9e2017-05-08 11:01:20 +08004296 else if (qualifierType == "offset")
4297 {
4298 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4299 if (intValue < 0)
4300 {
4301 error(intValueLine, "out of range: offset must be non-negative",
4302 intValueString.c_str());
4303 }
4304 else
4305 {
4306 qualifier.offset = intValue;
4307 }
4308 }
Martin Radev802abe02016-08-04 17:48:32 +03004309 else if (qualifierType == "local_size_x")
4310 {
4311 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4312 &qualifier.localSize);
4313 }
4314 else if (qualifierType == "local_size_y")
4315 {
4316 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4317 &qualifier.localSize);
4318 }
4319 else if (qualifierType == "local_size_z")
4320 {
4321 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4322 &qualifier.localSize);
4323 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004324 else if (qualifierType == "num_views" && isExtensionEnabled(TExtension::OVR_multiview) &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004325 mShaderType == GL_VERTEX_SHADER)
4326 {
4327 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4328 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004329 else if (qualifierType == "invocations" &&
4330 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004331 mShaderType == GL_GEOMETRY_SHADER_OES)
4332 {
4333 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4334 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004335 else if (qualifierType == "max_vertices" &&
4336 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004337 mShaderType == GL_GEOMETRY_SHADER_OES)
4338 {
4339 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4340 }
4341
Martin Radev802abe02016-08-04 17:48:32 +03004342 else
4343 {
4344 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004345 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004346
Jamie Madilla5efff92013-06-06 11:56:47 -04004347 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004348}
4349
Olli Etuaho613b9592016-09-05 12:05:53 +03004350TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4351{
4352 return new TTypeQualifierBuilder(
4353 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4354 mShaderVersion);
4355}
4356
Olli Etuahocce89652017-06-19 16:04:09 +03004357TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4358 const TSourceLoc &loc)
4359{
4360 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4361 return new TStorageQualifierWrapper(qualifier, loc);
4362}
4363
4364TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4365{
4366 if (getShaderType() == GL_VERTEX_SHADER)
4367 {
4368 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4369 }
4370 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4371}
4372
4373TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4374{
4375 if (declaringFunction())
4376 {
4377 return new TStorageQualifierWrapper(EvqIn, loc);
4378 }
Shaob5cc1192017-07-06 10:47:20 +08004379
4380 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004381 {
Shaob5cc1192017-07-06 10:47:20 +08004382 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004383 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004384 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004385 {
4386 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4387 }
4388 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004389 }
Shaob5cc1192017-07-06 10:47:20 +08004390 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004391 {
Shaob5cc1192017-07-06 10:47:20 +08004392 if (mShaderVersion < 300)
4393 {
4394 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4395 }
4396 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004397 }
Shaob5cc1192017-07-06 10:47:20 +08004398 case GL_COMPUTE_SHADER:
4399 {
4400 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4401 }
4402 case GL_GEOMETRY_SHADER_OES:
4403 {
4404 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4405 }
4406 default:
4407 {
4408 UNREACHABLE();
4409 return new TStorageQualifierWrapper(EvqLast, loc);
4410 }
Olli Etuahocce89652017-06-19 16:04:09 +03004411 }
Olli Etuahocce89652017-06-19 16:04:09 +03004412}
4413
4414TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4415{
4416 if (declaringFunction())
4417 {
4418 return new TStorageQualifierWrapper(EvqOut, loc);
4419 }
Shaob5cc1192017-07-06 10:47:20 +08004420 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004421 {
Shaob5cc1192017-07-06 10:47:20 +08004422 case GL_VERTEX_SHADER:
4423 {
4424 if (mShaderVersion < 300)
4425 {
4426 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4427 }
4428 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4429 }
4430 case GL_FRAGMENT_SHADER:
4431 {
4432 if (mShaderVersion < 300)
4433 {
4434 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4435 }
4436 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4437 }
4438 case GL_COMPUTE_SHADER:
4439 {
4440 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4441 return new TStorageQualifierWrapper(EvqLast, loc);
4442 }
4443 case GL_GEOMETRY_SHADER_OES:
4444 {
4445 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4446 }
4447 default:
4448 {
4449 UNREACHABLE();
4450 return new TStorageQualifierWrapper(EvqLast, loc);
4451 }
Olli Etuahocce89652017-06-19 16:04:09 +03004452 }
Olli Etuahocce89652017-06-19 16:04:09 +03004453}
4454
4455TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4456{
4457 if (!declaringFunction())
4458 {
4459 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4460 }
4461 return new TStorageQualifierWrapper(EvqInOut, loc);
4462}
4463
Jamie Madillb98c3a82015-07-23 14:26:04 -04004464TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004465 TLayoutQualifier rightQualifier,
4466 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004467{
Martin Radevc28888b2016-07-22 15:27:42 +03004468 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004469 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004470}
4471
Olli Etuahocce89652017-06-19 16:04:09 +03004472TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4473{
4474 checkIsNotReserved(loc, *identifier);
4475 TType *type = new TType(EbtVoid, EbpUndefined);
4476 return new TField(type, identifier, loc);
4477}
4478
4479TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4480 const TSourceLoc &loc,
4481 TIntermTyped *arraySize,
4482 const TSourceLoc &arraySizeLoc)
4483{
4484 checkIsNotReserved(loc, *identifier);
4485
4486 TType *type = new TType(EbtVoid, EbpUndefined);
4487 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004488 type->makeArray(size);
Olli Etuahocce89652017-06-19 16:04:09 +03004489
4490 return new TField(type, identifier, loc);
4491}
4492
Olli Etuaho4de340a2016-12-16 09:32:03 +00004493TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4494 const TFieldList *newlyAddedFields,
4495 const TSourceLoc &location)
4496{
4497 for (TField *field : *newlyAddedFields)
4498 {
4499 for (TField *oldField : *processedFields)
4500 {
4501 if (oldField->name() == field->name())
4502 {
4503 error(location, "duplicate field name in structure", field->name().c_str());
4504 }
4505 }
4506 processedFields->push_back(field);
4507 }
4508 return processedFields;
4509}
4510
Martin Radev70866b82016-07-22 15:27:42 +03004511TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4512 const TTypeQualifierBuilder &typeQualifierBuilder,
4513 TPublicType *typeSpecifier,
4514 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004515{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004516 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004517
Martin Radev70866b82016-07-22 15:27:42 +03004518 typeSpecifier->qualifier = typeQualifier.qualifier;
4519 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004520 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004521 typeSpecifier->invariant = typeQualifier.invariant;
4522 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304523 {
Martin Radev70866b82016-07-22 15:27:42 +03004524 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004525 }
Martin Radev70866b82016-07-22 15:27:42 +03004526 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004527}
4528
Jamie Madillb98c3a82015-07-23 14:26:04 -04004529TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004530 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004531{
Martin Radev4a9cd802016-09-01 16:51:51 +03004532 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4533 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004534
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004535 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4536 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004537
Martin Radev4a9cd802016-09-01 16:51:51 +03004538 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004539
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004540 for (unsigned int i = 0; i < declaratorList->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304541 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004542 auto declaratorArraySizes = (*declaratorList)[i]->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004543 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004544 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304545 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004546 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004547 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004548
4549 TType *type = (*declaratorList)[i]->type();
4550 *type = TType(typeSpecifier);
4551 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304552 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004553 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004554 }
4555
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004556 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*declaratorList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004557 }
4558
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004559 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004560}
4561
Martin Radev4a9cd802016-09-01 16:51:51 +03004562TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4563 const TSourceLoc &nameLine,
4564 const TString *structName,
4565 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004566{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004567 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004568
Jamie Madill9b820842015-02-12 10:40:10 -05004569 // Store a bool in the struct if we're at global scope, to allow us to
4570 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004571 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004572
Jamie Madill98493dd2013-07-08 14:39:03 -04004573 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004574 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004575 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004576 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304577 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004578 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004579 }
4580 }
4581
4582 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004583 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004584 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004585 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004586 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004587 switch (qualifier)
4588 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004589 case EvqGlobal:
4590 case EvqTemporary:
4591 break;
4592 default:
4593 error(field.line(), "invalid qualifier on struct member",
4594 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004595 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004596 }
Martin Radev70866b82016-07-22 15:27:42 +03004597 if (field.type()->isInvariant())
4598 {
4599 error(field.line(), "invalid qualifier on struct member", "invariant");
4600 }
jchen104cdac9e2017-05-08 11:01:20 +08004601 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4602 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004603 {
4604 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4605 }
4606
Olli Etuaho43364892017-02-13 16:00:12 +00004607 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4608
4609 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004610
4611 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004612 }
4613
Martin Radev4a9cd802016-09-01 16:51:51 +03004614 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004615 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004616 exitStructDeclaration();
4617
Martin Radev4a9cd802016-09-01 16:51:51 +03004618 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004619}
4620
Jamie Madillb98c3a82015-07-23 14:26:04 -04004621TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004622 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004623 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004624{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004625 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004626 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004627 init->isVector())
4628 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004629 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4630 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004631 return nullptr;
4632 }
4633
Olli Etuahoac5274d2015-02-20 10:19:08 +02004634 if (statementList)
4635 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004636 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004637 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004638 return nullptr;
4639 }
4640 }
4641
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004642 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4643 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004644 return node;
4645}
4646
4647TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4648{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004649 if (mSwitchNestingLevel == 0)
4650 {
4651 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004652 return nullptr;
4653 }
4654 if (condition == nullptr)
4655 {
4656 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004657 return nullptr;
4658 }
4659 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004660 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004661 {
4662 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004663 }
4664 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004665 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4666 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4667 // fold in case labels.
4668 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004669 {
4670 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004671 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004672 TIntermCase *node = new TIntermCase(condition);
4673 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004674 return node;
4675}
4676
4677TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4678{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004679 if (mSwitchNestingLevel == 0)
4680 {
4681 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004682 return nullptr;
4683 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004684 TIntermCase *node = new TIntermCase(nullptr);
4685 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004686 return node;
4687}
4688
Jamie Madillb98c3a82015-07-23 14:26:04 -04004689TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4690 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004691 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004692{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004693 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004694
4695 switch (op)
4696 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004697 case EOpLogicalNot:
4698 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4699 child->isVector())
4700 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004701 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004702 return nullptr;
4703 }
4704 break;
4705 case EOpBitwiseNot:
4706 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4707 child->isMatrix() || child->isArray())
4708 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004709 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004710 return nullptr;
4711 }
4712 break;
4713 case EOpPostIncrement:
4714 case EOpPreIncrement:
4715 case EOpPostDecrement:
4716 case EOpPreDecrement:
4717 case EOpNegative:
4718 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004719 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4720 child->getBasicType() == EbtBool || child->isArray() ||
4721 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004722 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004723 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004724 return nullptr;
4725 }
4726 // Operators for built-ins are already type checked against their prototype.
4727 default:
4728 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004729 }
4730
Jiajia Qinbc585152017-06-23 15:42:17 +08004731 if (child->getMemoryQualifier().writeonly)
4732 {
4733 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4734 return nullptr;
4735 }
4736
Olli Etuahof119a262016-08-19 15:54:22 +03004737 TIntermUnary *node = new TIntermUnary(op, child);
4738 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004739
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004740 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004741}
4742
Olli Etuaho09b22472015-02-11 11:47:26 +02004743TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4744{
Olli Etuahocce89652017-06-19 16:04:09 +03004745 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004746 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004747 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004748 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004749 return child;
4750 }
4751 return node;
4752}
4753
Jamie Madillb98c3a82015-07-23 14:26:04 -04004754TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4755 TIntermTyped *child,
4756 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004757{
Olli Etuaho856c4972016-08-08 11:38:39 +03004758 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004759 return addUnaryMath(op, child, loc);
4760}
4761
Jamie Madillb98c3a82015-07-23 14:26:04 -04004762bool TParseContext::binaryOpCommonCheck(TOperator op,
4763 TIntermTyped *left,
4764 TIntermTyped *right,
4765 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004766{
jchen10b4cf5652017-05-05 18:51:17 +08004767 // Check opaque types are not allowed to be operands in expressions other than array indexing
4768 // and structure member selection.
4769 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4770 {
4771 switch (op)
4772 {
4773 case EOpIndexDirect:
4774 case EOpIndexIndirect:
4775 break;
4776 case EOpIndexDirectStruct:
4777 UNREACHABLE();
4778
4779 default:
4780 error(loc, "Invalid operation for variables with an opaque type",
4781 GetOperatorString(op));
4782 return false;
4783 }
4784 }
jchen10cc2a10e2017-05-03 14:05:12 +08004785
Jiajia Qinbc585152017-06-23 15:42:17 +08004786 if (right->getMemoryQualifier().writeonly)
4787 {
4788 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4789 return false;
4790 }
4791
4792 if (left->getMemoryQualifier().writeonly)
4793 {
4794 switch (op)
4795 {
4796 case EOpAssign:
4797 case EOpInitialize:
4798 case EOpIndexDirect:
4799 case EOpIndexIndirect:
4800 case EOpIndexDirectStruct:
4801 case EOpIndexDirectInterfaceBlock:
4802 break;
4803 default:
4804 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4805 return false;
4806 }
4807 }
4808
Olli Etuaho244be012016-08-18 15:26:02 +03004809 if (left->getType().getStruct() || right->getType().getStruct())
4810 {
4811 switch (op)
4812 {
4813 case EOpIndexDirectStruct:
4814 ASSERT(left->getType().getStruct());
4815 break;
4816 case EOpEqual:
4817 case EOpNotEqual:
4818 case EOpAssign:
4819 case EOpInitialize:
4820 if (left->getType() != right->getType())
4821 {
4822 return false;
4823 }
4824 break;
4825 default:
4826 error(loc, "Invalid operation for structs", GetOperatorString(op));
4827 return false;
4828 }
4829 }
4830
Olli Etuaho94050052017-05-08 14:17:44 +03004831 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4832 {
4833 switch (op)
4834 {
4835 case EOpIndexDirectInterfaceBlock:
4836 ASSERT(left->getType().getInterfaceBlock());
4837 break;
4838 default:
4839 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4840 return false;
4841 }
4842 }
4843
Olli Etuahod6b14282015-03-17 14:31:35 +02004844 if (left->isArray() || right->isArray())
4845 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004846 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004847 {
4848 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4849 return false;
4850 }
4851
4852 if (left->isArray() != right->isArray())
4853 {
4854 error(loc, "array / non-array mismatch", GetOperatorString(op));
4855 return false;
4856 }
4857
4858 switch (op)
4859 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004860 case EOpEqual:
4861 case EOpNotEqual:
4862 case EOpAssign:
4863 case EOpInitialize:
4864 break;
4865 default:
4866 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4867 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004868 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004869 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004870 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004871 {
4872 error(loc, "array size mismatch", GetOperatorString(op));
4873 return false;
4874 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004875 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004876
4877 // Check ops which require integer / ivec parameters
4878 bool isBitShift = false;
4879 switch (op)
4880 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004881 case EOpBitShiftLeft:
4882 case EOpBitShiftRight:
4883 case EOpBitShiftLeftAssign:
4884 case EOpBitShiftRightAssign:
4885 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4886 // check that the basic type is an integer type.
4887 isBitShift = true;
4888 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4889 {
4890 return false;
4891 }
4892 break;
4893 case EOpBitwiseAnd:
4894 case EOpBitwiseXor:
4895 case EOpBitwiseOr:
4896 case EOpBitwiseAndAssign:
4897 case EOpBitwiseXorAssign:
4898 case EOpBitwiseOrAssign:
4899 // It is enough to check the type of only one operand, since later it
4900 // is checked that the operand types match.
4901 if (!IsInteger(left->getBasicType()))
4902 {
4903 return false;
4904 }
4905 break;
4906 default:
4907 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004908 }
4909
4910 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4911 // So the basic type should usually match.
4912 if (!isBitShift && left->getBasicType() != right->getBasicType())
4913 {
4914 return false;
4915 }
4916
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004917 // Check that:
4918 // 1. Type sizes match exactly on ops that require that.
4919 // 2. Restrictions for structs that contain arrays or samplers are respected.
4920 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004921 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004922 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004923 case EOpAssign:
4924 case EOpInitialize:
4925 case EOpEqual:
4926 case EOpNotEqual:
4927 // ESSL 1.00 sections 5.7, 5.8, 5.9
4928 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4929 {
4930 error(loc, "undefined operation for structs containing arrays",
4931 GetOperatorString(op));
4932 return false;
4933 }
4934 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4935 // we interpret the spec so that this extends to structs containing samplers,
4936 // similarly to ESSL 1.00 spec.
4937 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4938 left->getType().isStructureContainingSamplers())
4939 {
4940 error(loc, "undefined operation for structs containing samplers",
4941 GetOperatorString(op));
4942 return false;
4943 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004944
Olli Etuahoe1805592017-01-02 16:41:20 +00004945 if ((left->getNominalSize() != right->getNominalSize()) ||
4946 (left->getSecondarySize() != right->getSecondarySize()))
4947 {
4948 error(loc, "dimension mismatch", GetOperatorString(op));
4949 return false;
4950 }
4951 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004952 case EOpLessThan:
4953 case EOpGreaterThan:
4954 case EOpLessThanEqual:
4955 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004956 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004957 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004958 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004959 return false;
4960 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004961 break;
4962 case EOpAdd:
4963 case EOpSub:
4964 case EOpDiv:
4965 case EOpIMod:
4966 case EOpBitShiftLeft:
4967 case EOpBitShiftRight:
4968 case EOpBitwiseAnd:
4969 case EOpBitwiseXor:
4970 case EOpBitwiseOr:
4971 case EOpAddAssign:
4972 case EOpSubAssign:
4973 case EOpDivAssign:
4974 case EOpIModAssign:
4975 case EOpBitShiftLeftAssign:
4976 case EOpBitShiftRightAssign:
4977 case EOpBitwiseAndAssign:
4978 case EOpBitwiseXorAssign:
4979 case EOpBitwiseOrAssign:
4980 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4981 {
4982 return false;
4983 }
4984
4985 // Are the sizes compatible?
4986 if (left->getNominalSize() != right->getNominalSize() ||
4987 left->getSecondarySize() != right->getSecondarySize())
4988 {
4989 // If the nominal sizes of operands do not match:
4990 // One of them must be a scalar.
4991 if (!left->isScalar() && !right->isScalar())
4992 return false;
4993
4994 // In the case of compound assignment other than multiply-assign,
4995 // the right side needs to be a scalar. Otherwise a vector/matrix
4996 // would be assigned to a scalar. A scalar can't be shifted by a
4997 // vector either.
4998 if (!right->isScalar() &&
4999 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5000 return false;
5001 }
5002 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005003 default:
5004 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005005 }
5006
Olli Etuahod6b14282015-03-17 14:31:35 +02005007 return true;
5008}
5009
Olli Etuaho1dded802016-08-18 18:13:13 +03005010bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5011 const TType &left,
5012 const TType &right)
5013{
5014 switch (op)
5015 {
5016 case EOpMul:
5017 case EOpMulAssign:
5018 return left.getNominalSize() == right.getNominalSize() &&
5019 left.getSecondarySize() == right.getSecondarySize();
5020 case EOpVectorTimesScalar:
5021 return true;
5022 case EOpVectorTimesScalarAssign:
5023 ASSERT(!left.isMatrix() && !right.isMatrix());
5024 return left.isVector() && !right.isVector();
5025 case EOpVectorTimesMatrix:
5026 return left.getNominalSize() == right.getRows();
5027 case EOpVectorTimesMatrixAssign:
5028 ASSERT(!left.isMatrix() && right.isMatrix());
5029 return left.isVector() && left.getNominalSize() == right.getRows() &&
5030 left.getNominalSize() == right.getCols();
5031 case EOpMatrixTimesVector:
5032 return left.getCols() == right.getNominalSize();
5033 case EOpMatrixTimesScalar:
5034 return true;
5035 case EOpMatrixTimesScalarAssign:
5036 ASSERT(left.isMatrix() && !right.isMatrix());
5037 return !right.isVector();
5038 case EOpMatrixTimesMatrix:
5039 return left.getCols() == right.getRows();
5040 case EOpMatrixTimesMatrixAssign:
5041 ASSERT(left.isMatrix() && right.isMatrix());
5042 // We need to check two things:
5043 // 1. The matrix multiplication step is valid.
5044 // 2. The result will have the same number of columns as the lvalue.
5045 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5046
5047 default:
5048 UNREACHABLE();
5049 return false;
5050 }
5051}
5052
Jamie Madillb98c3a82015-07-23 14:26:04 -04005053TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5054 TIntermTyped *left,
5055 TIntermTyped *right,
5056 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005057{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005058 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005059 return nullptr;
5060
Olli Etuahofc1806e2015-03-17 13:03:11 +02005061 switch (op)
5062 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005063 case EOpEqual:
5064 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005065 case EOpLessThan:
5066 case EOpGreaterThan:
5067 case EOpLessThanEqual:
5068 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005069 break;
5070 case EOpLogicalOr:
5071 case EOpLogicalXor:
5072 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005073 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5074 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005075 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005076 {
5077 return nullptr;
5078 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005079 // Basic types matching should have been already checked.
5080 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005081 break;
5082 case EOpAdd:
5083 case EOpSub:
5084 case EOpDiv:
5085 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005086 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5087 !right->getType().getStruct());
5088 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005089 {
5090 return nullptr;
5091 }
5092 break;
5093 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005094 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5095 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005096 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005097 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005098 {
5099 return nullptr;
5100 }
5101 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005102 default:
5103 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005104 }
5105
Olli Etuaho1dded802016-08-18 18:13:13 +03005106 if (op == EOpMul)
5107 {
5108 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5109 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5110 {
5111 return nullptr;
5112 }
5113 }
5114
Olli Etuaho3fdec912016-08-18 15:08:06 +03005115 TIntermBinary *node = new TIntermBinary(op, left, right);
5116 node->setLine(loc);
5117
Olli Etuaho3fdec912016-08-18 15:08:06 +03005118 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005119 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005120}
5121
Jamie Madillb98c3a82015-07-23 14:26:04 -04005122TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5123 TIntermTyped *left,
5124 TIntermTyped *right,
5125 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005126{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005127 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005128 if (node == 0)
5129 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005130 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5131 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005132 return left;
5133 }
5134 return node;
5135}
5136
Jamie Madillb98c3a82015-07-23 14:26:04 -04005137TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5138 TIntermTyped *left,
5139 TIntermTyped *right,
5140 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005141{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005142 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005143 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005144 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005145 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5146 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005147 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005148 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005149 }
5150 return node;
5151}
5152
Olli Etuaho13389b62016-10-16 11:48:18 +01005153TIntermBinary *TParseContext::createAssign(TOperator op,
5154 TIntermTyped *left,
5155 TIntermTyped *right,
5156 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005157{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005158 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005159 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005160 if (op == EOpMulAssign)
5161 {
5162 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5163 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5164 {
5165 return nullptr;
5166 }
5167 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005168 TIntermBinary *node = new TIntermBinary(op, left, right);
5169 node->setLine(loc);
5170
Olli Etuaho3fdec912016-08-18 15:08:06 +03005171 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005172 }
5173 return nullptr;
5174}
5175
Jamie Madillb98c3a82015-07-23 14:26:04 -04005176TIntermTyped *TParseContext::addAssign(TOperator op,
5177 TIntermTyped *left,
5178 TIntermTyped *right,
5179 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005180{
Olli Etuahocce89652017-06-19 16:04:09 +03005181 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005182 TIntermTyped *node = createAssign(op, left, right, loc);
5183 if (node == nullptr)
5184 {
5185 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005186 return left;
5187 }
5188 return node;
5189}
5190
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005191TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5192 TIntermTyped *right,
5193 const TSourceLoc &loc)
5194{
Corentin Wallez0d959252016-07-12 17:26:32 -04005195 // WebGL2 section 5.26, the following results in an error:
5196 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005197 if (mShaderSpec == SH_WEBGL2_SPEC &&
5198 (left->isArray() || left->getBasicType() == EbtVoid ||
5199 left->getType().isStructureContainingArrays() || right->isArray() ||
5200 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005201 {
5202 error(loc,
5203 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5204 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005205 }
5206
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005207 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5208 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5209 commaNode->getTypePointer()->setQualifier(resultQualifier);
5210 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005211}
5212
Olli Etuaho49300862015-02-20 14:54:49 +02005213TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5214{
5215 switch (op)
5216 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005217 case EOpContinue:
5218 if (mLoopNestingLevel <= 0)
5219 {
5220 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005221 }
5222 break;
5223 case EOpBreak:
5224 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5225 {
5226 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005227 }
5228 break;
5229 case EOpReturn:
5230 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5231 {
5232 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005233 }
5234 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005235 case EOpKill:
5236 if (mShaderType != GL_FRAGMENT_SHADER)
5237 {
5238 error(loc, "discard supported in fragment shaders only", "discard");
5239 }
5240 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005241 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005242 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005243 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005244 }
Olli Etuahocce89652017-06-19 16:04:09 +03005245 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005246}
5247
Jamie Madillb98c3a82015-07-23 14:26:04 -04005248TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005249 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005250 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005251{
Olli Etuahocce89652017-06-19 16:04:09 +03005252 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005253 {
Olli Etuahocce89652017-06-19 16:04:09 +03005254 ASSERT(op == EOpReturn);
5255 mFunctionReturnsValue = true;
5256 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5257 {
5258 error(loc, "void function cannot return a value", "return");
5259 }
5260 else if (*mCurrentFunctionType != expression->getType())
5261 {
5262 error(loc, "function return is not matching type:", "return");
5263 }
Olli Etuaho49300862015-02-20 14:54:49 +02005264 }
Olli Etuahocce89652017-06-19 16:04:09 +03005265 TIntermBranch *node = new TIntermBranch(op, expression);
5266 node->setLine(loc);
5267 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005268}
5269
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005270void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5271{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005272 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005273 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005274 TIntermNode *offset = nullptr;
5275 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03005276 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5277 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5278 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005279 {
5280 offset = arguments->back();
5281 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005282 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005283 {
5284 // A bias parameter might follow the offset parameter.
5285 ASSERT(arguments->size() >= 3);
5286 offset = (*arguments)[2];
5287 }
5288 if (offset != nullptr)
5289 {
5290 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5291 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5292 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005293 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005294 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005295 }
5296 else
5297 {
5298 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5299 size_t size = offsetConstantUnion->getType().getObjectSize();
5300 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
5301 for (size_t i = 0u; i < size; ++i)
5302 {
5303 int offsetValue = values[i].getIConst();
5304 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
5305 {
5306 std::stringstream tokenStream;
5307 tokenStream << offsetValue;
5308 std::string token = tokenStream.str();
5309 error(offset->getLine(), "Texture offset value out of valid range",
5310 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005311 }
5312 }
5313 }
5314 }
5315}
5316
Martin Radev2cc85b32016-08-05 16:22:53 +03005317// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5318void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5319{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005320 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005321 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5322
5323 if (name.compare(0, 5, "image") == 0)
5324 {
5325 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005326 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005327
Olli Etuaho485eefd2017-02-14 17:40:06 +00005328 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005329
5330 if (name.compare(5, 5, "Store") == 0)
5331 {
5332 if (memoryQualifier.readonly)
5333 {
5334 error(imageNode->getLine(),
5335 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005336 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005337 }
5338 }
5339 else if (name.compare(5, 4, "Load") == 0)
5340 {
5341 if (memoryQualifier.writeonly)
5342 {
5343 error(imageNode->getLine(),
5344 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005345 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005346 }
5347 }
5348 }
5349}
5350
5351// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5352void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5353 const TFunction *functionDefinition,
5354 const TIntermAggregate *functionCall)
5355{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005356 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005357
5358 const TIntermSequence &arguments = *functionCall->getSequence();
5359
5360 ASSERT(functionDefinition->getParamCount() == arguments.size());
5361
5362 for (size_t i = 0; i < arguments.size(); ++i)
5363 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005364 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5365 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005366 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5367 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5368
5369 if (IsImage(functionArgumentType.getBasicType()))
5370 {
5371 const TMemoryQualifier &functionArgumentMemoryQualifier =
5372 functionArgumentType.getMemoryQualifier();
5373 const TMemoryQualifier &functionParameterMemoryQualifier =
5374 functionParameterType.getMemoryQualifier();
5375 if (functionArgumentMemoryQualifier.readonly &&
5376 !functionParameterMemoryQualifier.readonly)
5377 {
5378 error(functionCall->getLine(),
5379 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005380 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005381 }
5382
5383 if (functionArgumentMemoryQualifier.writeonly &&
5384 !functionParameterMemoryQualifier.writeonly)
5385 {
5386 error(functionCall->getLine(),
5387 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005388 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005389 }
Martin Radev049edfa2016-11-11 14:35:37 +02005390
5391 if (functionArgumentMemoryQualifier.coherent &&
5392 !functionParameterMemoryQualifier.coherent)
5393 {
5394 error(functionCall->getLine(),
5395 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005396 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005397 }
5398
5399 if (functionArgumentMemoryQualifier.volatileQualifier &&
5400 !functionParameterMemoryQualifier.volatileQualifier)
5401 {
5402 error(functionCall->getLine(),
5403 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005404 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005405 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005406 }
5407 }
5408}
5409
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005410TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005411{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005412 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005413}
5414
5415TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005416 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005417 TIntermNode *thisNode,
5418 const TSourceLoc &loc)
5419{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005420 if (thisNode != nullptr)
5421 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005422 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005423 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005424
5425 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005426 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005427 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005428 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005429 }
5430 else
5431 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005432 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005433 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5434 }
5435}
5436
5437TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5438 TIntermSequence *arguments,
5439 TIntermNode *thisNode,
5440 const TSourceLoc &loc)
5441{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005442 TIntermTyped *typedThis = thisNode->getAsTyped();
5443 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5444 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5445 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5446 // So accessing fnCall->getName() below is safe.
5447 if (fnCall->getName() != "length")
5448 {
5449 error(loc, "invalid method", fnCall->getName().c_str());
5450 }
5451 else if (!arguments->empty())
5452 {
5453 error(loc, "method takes no parameters", "length");
5454 }
5455 else if (typedThis == nullptr || !typedThis->isArray())
5456 {
5457 error(loc, "length can only be called on arrays", "length");
5458 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005459 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5460 mGeometryShaderInputPrimitiveType == EptUndefined)
5461 {
5462 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5463 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005464 else
5465 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005466 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5467 node->setLine(loc);
5468 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005469 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005470 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005471}
5472
5473TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5474 TIntermSequence *arguments,
5475 const TSourceLoc &loc)
5476{
5477 // First find by unmangled name to check whether the function name has been
5478 // hidden by a variable name or struct typename.
5479 // If a function is found, check for one with a matching argument list.
5480 bool builtIn;
5481 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5482 if (symbol != nullptr && !symbol->isFunction())
5483 {
5484 error(loc, "function name expected", fnCall->getName().c_str());
5485 }
5486 else
5487 {
5488 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5489 mShaderVersion, &builtIn);
5490 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005491 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005492 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5493 }
5494 else
5495 {
5496 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005497 //
5498 // A declared function.
5499 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005500 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005501 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005502 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005503 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005504 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005505 if (builtIn && op != EOpNull)
5506 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005507 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005508 if (fnCandidate->getParamCount() == 1)
5509 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005510 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005511 TIntermNode *unaryParamNode = arguments->front();
5512 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005513 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005514 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005515 }
5516 else
5517 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005518 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005519 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005520 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005521
5522 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005523 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305524
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005525 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305526 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005527 // See if we can constant fold a built-in. Note that this may be possible
5528 // even if it is not const-qualified.
5529 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305530 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005531 else
5532 {
5533 return callNode;
5534 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005535 }
5536 }
5537 else
5538 {
5539 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005540 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005541
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005542 // If builtIn == false, the function is user defined - could be an overloaded
5543 // built-in as well.
5544 // if builtIn == true, it's a builtIn function with no op associated with it.
5545 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005546 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005547 {
Olli Etuahofe486322017-03-21 09:30:54 +00005548 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005549 checkTextureOffsetConst(callNode);
5550 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005551 }
5552 else
5553 {
Olli Etuahofe486322017-03-21 09:30:54 +00005554 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005555 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005556 }
5557
Jiajia Qinbc585152017-06-23 15:42:17 +08005558 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005559
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005560 callNode->setLine(loc);
5561
5562 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005563 }
5564 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005565 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005566
5567 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005568 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005569}
5570
Jamie Madillb98c3a82015-07-23 14:26:04 -04005571TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005572 TIntermTyped *trueExpression,
5573 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005574 const TSourceLoc &loc)
5575{
Olli Etuaho56229f12017-07-10 14:16:33 +03005576 if (!checkIsScalarBool(loc, cond))
5577 {
5578 return falseExpression;
5579 }
Olli Etuaho52901742015-04-15 13:42:45 +03005580
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005581 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005582 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005583 std::stringstream reasonStream;
5584 reasonStream << "mismatching ternary operator operand types '"
5585 << trueExpression->getCompleteString() << " and '"
5586 << falseExpression->getCompleteString() << "'";
5587 std::string reason = reasonStream.str();
5588 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005589 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005590 }
Olli Etuahode318b22016-10-25 16:18:25 +01005591 if (IsOpaqueType(trueExpression->getBasicType()))
5592 {
5593 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005594 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005595 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5596 // Note that structs containing opaque types don't need to be checked as structs are
5597 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005598 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005599 return falseExpression;
5600 }
5601
Jiajia Qinbc585152017-06-23 15:42:17 +08005602 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5603 falseExpression->getMemoryQualifier().writeonly)
5604 {
5605 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5606 return falseExpression;
5607 }
5608
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005609 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005610 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005611 // ESSL 3.00.6 section 5.7:
5612 // Ternary operator support is optional for arrays. No certainty that it works across all
5613 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5614 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005615 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005616 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005617 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005618 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005619 }
Olli Etuaho94050052017-05-08 14:17:44 +03005620 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5621 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005622 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005623 return falseExpression;
5624 }
5625
Corentin Wallez0d959252016-07-12 17:26:32 -04005626 // WebGL2 section 5.26, the following results in an error:
5627 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005628 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005629 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005630 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005631 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005632 }
5633
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005634 // Note that the node resulting from here can be a constant union without being qualified as
5635 // constant.
5636 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5637 node->setLine(loc);
5638
5639 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005640}
Olli Etuaho49300862015-02-20 14:54:49 +02005641
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005642//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005643// Parse an array of strings using yyparse.
5644//
5645// Returns 0 for success.
5646//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005647int PaParseStrings(size_t count,
5648 const char *const string[],
5649 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305650 TParseContext *context)
5651{
Yunchao He4f285442017-04-21 12:15:49 +08005652 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005653 return 1;
5654
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005655 if (glslang_initialize(context))
5656 return 1;
5657
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005658 int error = glslang_scan(count, string, length, context);
5659 if (!error)
5660 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005661
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005662 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005663
alokp@chromium.org6b495712012-06-29 00:06:58 +00005664 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005665}
Jamie Madill45bcc782016-11-07 13:58:48 -05005666
5667} // namespace sh