blob: c00a77b840619aa8ed2af58660d8bd8a6cc518f6 [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"
Olli Etuahod5f44c92017-11-29 17:15:40 +020014#include "compiler/translator/Declarator.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Kai Ninomiya614dd0f2017-11-22 14:04:48 -080016#include "compiler/translator/StaticType.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030017#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080018#include "compiler/translator/ValidateSwitch.h"
19#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030020#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021
Jamie Madill45bcc782016-11-07 13:58:48 -050022namespace sh
23{
24
alokp@chromium.org8b851c62012-06-15 16:25:11 +000025///////////////////////////////////////////////////////////////////////
26//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027// Sub- vector and matrix fields
28//
29////////////////////////////////////////////////////////////////////////
30
Martin Radev2cc85b32016-08-05 16:22:53 +030031namespace
32{
33
34const int kWebGLMaxStructNesting = 4;
35
Jiajia Qina3106c52017-11-03 09:39:39 +080036const std::array<const char *, 8> kAtomicBuiltin = {{"atomicAdd", "atomicMin", "atomicMax",
37 "atomicAnd", "atomicOr", "atomicXor",
38 "atomicExchange", "atomicCompSwap"}};
39
40bool IsAtomicBuiltin(const TString &name)
41{
42 for (size_t i = 0; i < kAtomicBuiltin.size(); ++i)
43 {
44 if (name.compare(kAtomicBuiltin[i]) == 0)
45 {
46 return true;
47 }
48 }
49 return false;
50}
51
Olli Etuaho0f684632017-07-13 12:42:15 +030052bool ContainsSampler(const TStructure *structType);
53
Martin Radev2cc85b32016-08-05 16:22:53 +030054bool ContainsSampler(const TType &type)
55{
56 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030057 {
Martin Radev2cc85b32016-08-05 16:22:53 +030058 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030059 }
jchen10cc2a10e2017-05-03 14:05:12 +080060 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030061 {
Olli Etuaho0f684632017-07-13 12:42:15 +030062 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030063 }
64
65 return false;
66}
67
Olli Etuaho0f684632017-07-13 12:42:15 +030068bool ContainsSampler(const TStructure *structType)
69{
70 for (const auto &field : structType->fields())
71 {
72 if (ContainsSampler(*field->type()))
73 return true;
74 }
75 return false;
76}
77
Olli Etuaho485eefd2017-02-14 17:40:06 +000078// Get a token from an image argument to use as an error message token.
79const char *GetImageArgumentToken(TIntermTyped *imageNode)
80{
81 ASSERT(IsImage(imageNode->getBasicType()));
82 while (imageNode->getAsBinaryNode() &&
83 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
84 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
85 {
86 imageNode = imageNode->getAsBinaryNode()->getLeft();
87 }
88 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
89 if (imageSymbol)
90 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020091 return imageSymbol->getName().c_str();
Olli Etuaho485eefd2017-02-14 17:40:06 +000092 }
93 return "image";
94}
95
Olli Etuahocce89652017-06-19 16:04:09 +030096bool CanSetDefaultPrecisionOnType(const TPublicType &type)
97{
98 if (!SupportsPrecision(type.getBasicType()))
99 {
100 return false;
101 }
102 if (type.getBasicType() == EbtUInt)
103 {
104 // ESSL 3.00.4 section 4.5.4
105 return false;
106 }
107 if (type.isAggregate())
108 {
109 // Not allowed to set for aggregate types
110 return false;
111 }
112 return true;
113}
114
Jiawei Shaod8105a02017-08-08 09:54:36 +0800115// Map input primitive types to input array sizes in a geometry shader.
116GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
117{
118 switch (primitiveType)
119 {
120 case EptPoints:
121 return 1u;
122 case EptLines:
123 return 2u;
124 case EptTriangles:
125 return 3u;
126 case EptLinesAdjacency:
127 return 4u;
128 case EptTrianglesAdjacency:
129 return 6u;
130 default:
131 UNREACHABLE();
132 return 0u;
133 }
134}
135
Jiajia Qina3106c52017-11-03 09:39:39 +0800136bool IsBufferOrSharedVariable(TIntermTyped *var)
137{
138 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
139 var->getQualifier() == EvqShared)
140 {
141 return true;
142 }
143 return false;
144}
145
Martin Radev2cc85b32016-08-05 16:22:53 +0300146} // namespace
147
jchen104cdac9e2017-05-08 11:01:20 +0800148// This tracks each binding point's current default offset for inheritance of subsequent
149// variables using the same binding, and keeps offsets unique and non overlapping.
150// See GLSL ES 3.1, section 4.4.6.
151class TParseContext::AtomicCounterBindingState
152{
153 public:
154 AtomicCounterBindingState() : mDefaultOffset(0) {}
155 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
156 // newly inserted span.
157 int insertSpan(int start, size_t length)
158 {
159 gl::RangeI newSpan(start, start + static_cast<int>(length));
160 for (const auto &span : mSpans)
161 {
162 if (newSpan.intersects(span))
163 {
164 return -1;
165 }
166 }
167 mSpans.push_back(newSpan);
168 mDefaultOffset = newSpan.high();
169 return start;
170 }
171 // Inserts a new span starting from the default offset.
172 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
173 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
174
175 private:
176 int mDefaultOffset;
177 std::vector<gl::RangeI> mSpans;
178};
179
Jamie Madillacb4b812016-11-07 13:50:29 -0500180TParseContext::TParseContext(TSymbolTable &symt,
181 TExtensionBehavior &ext,
182 sh::GLenum type,
183 ShShaderSpec spec,
184 ShCompileOptions options,
185 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000186 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500187 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300188 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300189 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500190 mShaderType(type),
191 mShaderSpec(spec),
192 mCompileOptions(options),
193 mShaderVersion(100),
194 mTreeRoot(nullptr),
195 mLoopNestingLevel(0),
196 mStructNestingLevel(0),
197 mSwitchNestingLevel(0),
198 mCurrentFunctionType(nullptr),
199 mFunctionReturnsValue(false),
200 mChecksPrecisionErrors(checksPrecErrors),
201 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800202 mDefaultUniformMatrixPacking(EmpColumnMajor),
203 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
204 mDefaultBufferMatrixPacking(EmpColumnMajor),
205 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000206 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500207 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000208 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500209 mShaderVersion,
210 mShaderType,
211 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000212 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500213 mScanner(nullptr),
214 mUsesFragData(false),
215 mUsesFragColor(false),
216 mUsesSecondaryOutputs(false),
217 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
218 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300219 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
220 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500221 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500222 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000223 mNumViews(-1),
224 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000225 mMaxImageUnits(resources.MaxImageUnits),
226 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000227 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800228 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800229 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800230 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800231 mDeclaringFunction(false),
232 mGeometryShaderInputPrimitiveType(EptUndefined),
233 mGeometryShaderOutputPrimitiveType(EptUndefined),
234 mGeometryShaderInvocations(0),
235 mGeometryShaderMaxVertices(-1),
236 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800237 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800238 mGeometryShaderInputArraySize(0u)
Jamie Madillacb4b812016-11-07 13:50:29 -0500239{
Jamie Madillacb4b812016-11-07 13:50:29 -0500240}
241
jchen104cdac9e2017-05-08 11:01:20 +0800242TParseContext::~TParseContext()
243{
244}
245
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300246bool TParseContext::parseVectorFields(const TSourceLoc &line,
247 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400248 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300249 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300251 ASSERT(fieldOffsets);
252 size_t fieldCount = compString.size();
253 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530254 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000255 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000256 return false;
257 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300258 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259
Jamie Madillb98c3a82015-07-23 14:26:04 -0400260 enum
261 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000262 exyzw,
263 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000264 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000265 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300267 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530268 {
269 switch (compString[i])
270 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400271 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300272 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400273 fieldSet[i] = exyzw;
274 break;
275 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300276 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 fieldSet[i] = ergba;
278 break;
279 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300280 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400281 fieldSet[i] = estpq;
282 break;
283 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300284 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400285 fieldSet[i] = exyzw;
286 break;
287 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300288 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400289 fieldSet[i] = ergba;
290 break;
291 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300292 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400293 fieldSet[i] = estpq;
294 break;
295 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300296 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400297 fieldSet[i] = exyzw;
298 break;
299 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300300 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400301 fieldSet[i] = ergba;
302 break;
303 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300304 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400305 fieldSet[i] = estpq;
306 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307
Jamie Madillb98c3a82015-07-23 14:26:04 -0400308 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300309 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400310 fieldSet[i] = exyzw;
311 break;
312 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300313 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400314 fieldSet[i] = ergba;
315 break;
316 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300317 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400318 fieldSet[i] = estpq;
319 break;
320 default:
321 error(line, "illegal vector field selection", compString.c_str());
322 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 }
324 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300326 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530327 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300328 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530329 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400330 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000331 return false;
332 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
Arun Patole7e7e68d2015-05-22 12:02:25 +0530334 if (i > 0)
335 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400336 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530337 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400338 error(line, "illegal - vector component fields not from the same set",
339 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000340 return false;
341 }
342 }
343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000345 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346}
347
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348///////////////////////////////////////////////////////////////////////
349//
350// Errors
351//
352////////////////////////////////////////////////////////////////////////
353
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354//
355// Used by flex/bison to output all syntax and parsing errors.
356//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000357void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000359 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
Olli Etuaho4de340a2016-12-16 09:32:03 +0000362void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530363{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000364 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000365}
366
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200367void TParseContext::outOfRangeError(bool isError,
368 const TSourceLoc &loc,
369 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000370 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200371{
372 if (isError)
373 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000374 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200375 }
376 else
377 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000378 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200379 }
380}
381
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382//
383// Same error message for all places assignments don't work.
384//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530385void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000387 std::stringstream reasonStream;
388 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
389 std::string reason = reasonStream.str();
390 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391}
392
393//
394// Same error message for all places unary operations don't work.
395//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530396void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000398 std::stringstream reasonStream;
399 reasonStream << "wrong operand type - no operation '" << op
400 << "' exists that takes an operand of type " << operand
401 << " (or there is no acceptable conversion)";
402 std::string reason = reasonStream.str();
403 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Same error message for all binary operations don't work.
408//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400409void TParseContext::binaryOpError(const TSourceLoc &line,
410 const char *op,
411 TString left,
412 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000414 std::stringstream reasonStream;
415 reasonStream << "wrong operand types - no operation '" << op
416 << "' exists that takes a left-hand operand of type '" << left
417 << "' and a right operand of type '" << right
418 << "' (or there is no acceptable conversion)";
419 std::string reason = reasonStream.str();
420 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
Olli Etuaho856c4972016-08-08 11:38:39 +0300423void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
424 TPrecision precision,
425 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530426{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400427 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300428 return;
Martin Radev70866b82016-07-22 15:27:42 +0300429
430 if (precision != EbpUndefined && !SupportsPrecision(type))
431 {
432 error(line, "illegal type for precision qualifier", getBasicString(type));
433 }
434
Olli Etuaho183d7e22015-11-20 15:59:09 +0200435 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530436 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200437 switch (type)
438 {
439 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400440 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300441 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200442 case EbtInt:
443 case EbtUInt:
444 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400445 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300446 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200447 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800448 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200449 {
jchen10cc2a10e2017-05-03 14:05:12 +0800450 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300451 return;
452 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200453 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000454 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000455}
456
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457// Both test and if necessary, spit out an error, to see if the node is really
458// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300459bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500461 TIntermSymbol *symNode = node->getAsSymbolNode();
462 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100463 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
464
465 if (swizzleNode)
466 {
467 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
468 if (ok && swizzleNode->hasDuplicateOffsets())
469 {
470 error(line, " l-value of swizzle cannot have duplicate components", op);
471 return false;
472 }
473 return ok;
474 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475
Arun Patole7e7e68d2015-05-22 12:02:25 +0530476 if (binaryNode)
477 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530479 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400480 case EOpIndexDirect:
481 case EOpIndexIndirect:
482 case EOpIndexDirectStruct:
483 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300484 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400485 default:
486 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000487 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000488 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300489 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000490 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491
jchen10cc2a10e2017-05-03 14:05:12 +0800492 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530493 switch (node->getQualifier())
494 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400495 case EvqConst:
496 message = "can't modify a const";
497 break;
498 case EvqConstReadOnly:
499 message = "can't modify a const";
500 break;
501 case EvqAttribute:
502 message = "can't modify an attribute";
503 break;
504 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400505 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800506 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800507 case EvqFlatIn:
508 case EvqSmoothIn:
509 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400510 message = "can't modify an input";
511 break;
512 case EvqUniform:
513 message = "can't modify a uniform";
514 break;
515 case EvqVaryingIn:
516 message = "can't modify a varying";
517 break;
518 case EvqFragCoord:
519 message = "can't modify gl_FragCoord";
520 break;
521 case EvqFrontFacing:
522 message = "can't modify gl_FrontFacing";
523 break;
524 case EvqPointCoord:
525 message = "can't modify gl_PointCoord";
526 break;
Martin Radevb0883602016-08-04 17:48:58 +0300527 case EvqNumWorkGroups:
528 message = "can't modify gl_NumWorkGroups";
529 break;
530 case EvqWorkGroupSize:
531 message = "can't modify gl_WorkGroupSize";
532 break;
533 case EvqWorkGroupID:
534 message = "can't modify gl_WorkGroupID";
535 break;
536 case EvqLocalInvocationID:
537 message = "can't modify gl_LocalInvocationID";
538 break;
539 case EvqGlobalInvocationID:
540 message = "can't modify gl_GlobalInvocationID";
541 break;
542 case EvqLocalInvocationIndex:
543 message = "can't modify gl_LocalInvocationIndex";
544 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300545 case EvqViewIDOVR:
546 message = "can't modify gl_ViewID_OVR";
547 break;
Martin Radev802abe02016-08-04 17:48:32 +0300548 case EvqComputeIn:
549 message = "can't modify work group size variable";
550 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800551 case EvqPerVertexIn:
552 message = "can't modify any member in gl_in";
553 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800554 case EvqPrimitiveIDIn:
555 message = "can't modify gl_PrimitiveIDIn";
556 break;
557 case EvqInvocationID:
558 message = "can't modify gl_InvocationID";
559 break;
560 case EvqPrimitiveID:
561 if (mShaderType == GL_FRAGMENT_SHADER)
562 {
563 message = "can't modify gl_PrimitiveID in a fragment shader";
564 }
565 break;
566 case EvqLayer:
567 if (mShaderType == GL_FRAGMENT_SHADER)
568 {
569 message = "can't modify gl_Layer in a fragment shader";
570 }
571 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400572 default:
573 //
574 // Type that can't be written to?
575 //
576 if (node->getBasicType() == EbtVoid)
577 {
578 message = "can't modify void";
579 }
jchen10cc2a10e2017-05-03 14:05:12 +0800580 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400581 {
jchen10cc2a10e2017-05-03 14:05:12 +0800582 message = "can't modify a variable with type ";
583 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300584 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800585 else if (node->getMemoryQualifier().readonly)
586 {
587 message = "can't modify a readonly variable";
588 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590
jchen10cc2a10e2017-05-03 14:05:12 +0800591 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530592 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000593 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594
Olli Etuaho8a176262016-08-16 14:23:01 +0300595 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 //
599 // Everything else is okay, no error.
600 //
jchen10cc2a10e2017-05-03 14:05:12 +0800601 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300602 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 //
605 // If we get here, we have an error and a message.
606 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530607 if (symNode)
608 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200609 // Symbol inside an expression can't be nameless.
610 ASSERT(symNode->variable().symbolType() != SymbolType::Empty);
611
612 const char *symbol = symNode->getName().c_str();
Olli Etuaho4de340a2016-12-16 09:32:03 +0000613 std::stringstream reasonStream;
614 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
615 std::string reason = reasonStream.str();
616 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000617 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530618 else
619 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000620 std::stringstream reasonStream;
621 reasonStream << "l-value required (" << message << ")";
622 std::string reason = reasonStream.str();
623 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000624 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625
Olli Etuaho8a176262016-08-16 14:23:01 +0300626 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627}
628
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629// Both test, and if necessary spit out an error, to see if the node is really
630// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300631void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632{
Olli Etuaho383b7912016-08-05 11:22:59 +0300633 if (node->getQualifier() != EvqConst)
634 {
635 error(node->getLine(), "constant expression required", "");
636 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637}
638
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639// Both test, and if necessary spit out an error, to see if the node is really
640// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300641void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642{
Olli Etuaho383b7912016-08-05 11:22:59 +0300643 if (!node->isScalarInt())
644 {
645 error(node->getLine(), "integer expression required", token);
646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647}
648
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649// Both test, and if necessary spit out an error, to see if we are currently
650// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800651bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652{
Olli Etuaho856c4972016-08-08 11:38:39 +0300653 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300654 {
655 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800656 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300657 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800658 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659}
660
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300661// ESSL 3.00.5 sections 3.8 and 3.9.
662// If it starts "gl_" or contains two consecutive underscores, it's reserved.
663// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300664bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530666 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300667 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530668 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300669 error(line, reservedErrMsg, "gl_");
670 return false;
671 }
672 if (sh::IsWebGLBasedSpec(mShaderSpec))
673 {
674 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530675 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300676 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300677 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000678 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300679 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530680 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300681 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300682 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000683 }
684 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300685 if (identifier.find("__") != TString::npos)
686 {
687 error(line,
688 "identifiers containing two consecutive underscores (__) are reserved as "
689 "possible future keywords",
690 identifier.c_str());
691 return false;
692 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300693 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000694}
695
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300696// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300697bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800698 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300699 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800701 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530702 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200703 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300704 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000705 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200706
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300707 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530708 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300709 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200710 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300711 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200712 {
jchen10cc2a10e2017-05-03 14:05:12 +0800713 std::string reason("cannot convert a variable with type ");
714 reason += getBasicString(argTyped->getBasicType());
715 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300716 return false;
717 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800718 else if (argTyped->getMemoryQualifier().writeonly)
719 {
720 error(line, "cannot convert a variable with writeonly", "constructor");
721 return false;
722 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200723 if (argTyped->getBasicType() == EbtVoid)
724 {
725 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300726 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200727 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000728 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000729
Olli Etuaho856c4972016-08-08 11:38:39 +0300730 if (type.isArray())
731 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300732 // The size of an unsized constructor should already have been determined.
733 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300734 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300735 {
736 error(line, "array constructor needs one argument per array element", "constructor");
737 return false;
738 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300739 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
740 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800741 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300742 {
743 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300744 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500745 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300746 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500747 return false;
748 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300749 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300750 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000751 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300752 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300753 }
754 }
755 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300756 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300757 {
758 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300759 if (fields.size() != arguments->size())
760 {
761 error(line,
762 "Number of constructor parameters does not match the number of structure fields",
763 "constructor");
764 return false;
765 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300766
767 for (size_t i = 0; i < fields.size(); i++)
768 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800769 if (i >= arguments->size() ||
770 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300771 {
772 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000773 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300774 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300775 }
776 }
777 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300778 else
779 {
780 // We're constructing a scalar, vector, or matrix.
781
782 // Note: It's okay to have too many components available, but not okay to have unused
783 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
784 // there is an extra argument, so 'overFull' will become true.
785
786 size_t size = 0;
787 bool full = false;
788 bool overFull = false;
789 bool matrixArg = false;
790 for (TIntermNode *arg : *arguments)
791 {
792 const TIntermTyped *argTyped = arg->getAsTyped();
793 ASSERT(argTyped != nullptr);
794
Olli Etuaho487b63a2017-05-23 15:55:09 +0300795 if (argTyped->getBasicType() == EbtStruct)
796 {
797 error(line, "a struct cannot be used as a constructor argument for this type",
798 "constructor");
799 return false;
800 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300801 if (argTyped->getType().isArray())
802 {
803 error(line, "constructing from a non-dereferenced array", "constructor");
804 return false;
805 }
806 if (argTyped->getType().isMatrix())
807 {
808 matrixArg = true;
809 }
810
811 size += argTyped->getType().getObjectSize();
812 if (full)
813 {
814 overFull = true;
815 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300816 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300817 {
818 full = true;
819 }
820 }
821
822 if (type.isMatrix() && matrixArg)
823 {
824 if (arguments->size() != 1)
825 {
826 error(line, "constructing matrix from matrix can only take one argument",
827 "constructor");
828 return false;
829 }
830 }
831 else
832 {
833 if (size != 1 && size < type.getObjectSize())
834 {
835 error(line, "not enough data provided for construction", "constructor");
836 return false;
837 }
838 if (overFull)
839 {
840 error(line, "too many arguments", "constructor");
841 return false;
842 }
843 }
844 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300845
Olli Etuaho8a176262016-08-16 14:23:01 +0300846 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
Jamie Madillb98c3a82015-07-23 14:26:04 -0400849// This function checks to see if a void variable has been declared and raise an error message for
850// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851//
852// returns true in case of an error
853//
Olli Etuaho856c4972016-08-08 11:38:39 +0300854bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400855 const TString &identifier,
856 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300858 if (type == EbtVoid)
859 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000860 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300861 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863
Olli Etuaho8a176262016-08-16 14:23:01 +0300864 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
Jamie Madillb98c3a82015-07-23 14:26:04 -0400867// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300868// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300869bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300871 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530872 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000873 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300874 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530875 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300876 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877}
878
Jamie Madillb98c3a82015-07-23 14:26:04 -0400879// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300880// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300881void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882{
Martin Radev4a9cd802016-09-01 16:51:51 +0300883 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530884 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000885 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530886 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
jchen10cc2a10e2017-05-03 14:05:12 +0800889bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
890 const TTypeSpecifierNonArray &pType,
891 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530893 if (pType.type == EbtStruct)
894 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300895 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530896 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000897 std::stringstream reasonStream;
898 reasonStream << reason << " (structure contains a sampler)";
899 std::string reasonStr = reasonStream.str();
900 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300901 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000902 }
jchen10cc2a10e2017-05-03 14:05:12 +0800903 // only samplers need to be checked from structs, since other opaque types can't be struct
904 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300905 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530906 }
jchen10cc2a10e2017-05-03 14:05:12 +0800907 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530908 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000909 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300910 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000911 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
Olli Etuaho8a176262016-08-16 14:23:01 +0300913 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914}
915
Olli Etuaho856c4972016-08-08 11:38:39 +0300916void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
917 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400918{
919 if (pType.layoutQualifier.location != -1)
920 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400921 error(line, "location must only be specified for a single input or output variable",
922 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400923 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400924}
925
Olli Etuaho856c4972016-08-08 11:38:39 +0300926void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
927 const TLayoutQualifier &layoutQualifier)
928{
929 if (layoutQualifier.location != -1)
930 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000931 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
932 if (mShaderVersion >= 310)
933 {
934 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800935 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000936 }
937 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300938 }
939}
940
Qin Jiajiaca68d982017-09-18 16:41:56 +0800941void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
942 const TLayoutBlockStorage &blockStorage,
943 const TQualifier &qualifier)
944{
945 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
946 {
947 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
948 }
949}
950
Martin Radev2cc85b32016-08-05 16:22:53 +0300951void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
952 TQualifier qualifier,
953 const TType &type)
954{
Martin Radev2cc85b32016-08-05 16:22:53 +0300955 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800956 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530957 {
jchen10cc2a10e2017-05-03 14:05:12 +0800958 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000959 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960}
961
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300963unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530965 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000966
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200967 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
968 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
969 // fold as array size.
970 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000971 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000972 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300973 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975
Olli Etuaho856c4972016-08-08 11:38:39 +0300976 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400977
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000978 if (constant->getBasicType() == EbtUInt)
979 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300980 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000981 }
982 else
983 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300984 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000985
Olli Etuaho856c4972016-08-08 11:38:39 +0300986 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000987 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400988 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300989 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000990 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400991
Olli Etuaho856c4972016-08-08 11:38:39 +0300992 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400993 }
994
Olli Etuaho856c4972016-08-08 11:38:39 +0300995 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400996 {
997 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300998 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400999 }
1000
1001 // The size of arrays is restricted here to prevent issues further down the
1002 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1003 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1004 const unsigned int sizeLimit = 65536;
1005
Olli Etuaho856c4972016-08-08 11:38:39 +03001006 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001007 {
1008 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001009 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001010 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001011
1012 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013}
1014
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001016bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1017 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018{
Olli Etuaho8a176262016-08-16 14:23:01 +03001019 if ((elementQualifier.qualifier == EvqAttribute) ||
1020 (elementQualifier.qualifier == EvqVertexIn) ||
1021 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001022 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001023 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001024 TType(elementQualifier).getQualifierString());
1025 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001026 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027
Olli Etuaho8a176262016-08-16 14:23:01 +03001028 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
Olli Etuaho8a176262016-08-16 14:23:01 +03001031// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001032bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1033 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001035 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001036 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001037 error(line, "cannot declare arrays of arrays",
1038 TType(elementType).getCompleteString().c_str());
1039 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001040 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001041 return true;
1042}
1043
1044// Check if this qualified element type can be formed into an array. This is only called when array
1045// brackets are associated with an identifier in a declaration, like this:
1046// float a[2];
1047// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1048// are associated with the type, like this:
1049// float[2] a;
1050bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1051 const TPublicType &elementType)
1052{
1053 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1054 {
1055 return false;
1056 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001057 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1058 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1059 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001060 // Geometry shader requires each user-defined input be declared as arrays or inside input
1061 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1062 // interface matching, such variables and blocks are treated as though they were not declared
1063 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001064 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001065 sh::IsVarying(elementType.qualifier) &&
1066 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001067 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001068 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001069 TType(elementType).getCompleteString().c_str());
1070 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001071 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001072 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073}
1074
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001076void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1077 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001078 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079{
Olli Etuaho3739d232015-04-08 12:23:44 +03001080 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001081 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001082 {
1083 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001084 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001085
1086 // Generate informative error messages for ESSL1.
1087 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001088 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001089 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301090 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001091 "structures containing arrays may not be declared constant since they cannot be "
1092 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301093 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001094 }
1095 else
1096 {
1097 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1098 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001099 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001100 // This will make the type sized if it isn't sized yet.
1101 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1102 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103}
1104
Olli Etuaho2935c582015-04-08 14:32:06 +03001105// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106// and update the symbol table.
1107//
Olli Etuaho2935c582015-04-08 14:32:06 +03001108// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001110bool TParseContext::declareVariable(const TSourceLoc &line,
1111 const TString &identifier,
1112 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001113 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114{
Olli Etuaho2935c582015-04-08 14:32:06 +03001115 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116
Olli Etuaho195be942017-12-04 23:40:14 +02001117 (*variable) = new TVariable(&symbolTable, &identifier, type, SymbolType::UserDefined);
1118
Olli Etuaho43364892017-02-13 16:00:12 +00001119 checkBindingIsValid(line, type);
1120
Olli Etuaho856c4972016-08-08 11:38:39 +03001121 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122
Olli Etuaho2935c582015-04-08 14:32:06 +03001123 // gl_LastFragData may be redeclared with a new precision qualifier
1124 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1125 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001126 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1127 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001128 if (type.isArrayOfArrays())
1129 {
1130 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1131 identifier.c_str());
1132 return false;
1133 }
1134 else if (static_cast<int>(type.getOutermostArraySize()) ==
1135 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001136 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001137 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001138 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001139 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001140 }
1141 }
1142 else
1143 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001144 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1145 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001146 return false;
1147 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001148 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001149
Olli Etuaho8a176262016-08-16 14:23:01 +03001150 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001151 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152
Olli Etuaho195be942017-12-04 23:40:14 +02001153 if (!symbolTable.declareVariable(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001154 {
1155 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001156 return false;
1157 }
1158
Olli Etuaho8a176262016-08-16 14:23:01 +03001159 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001160 return false;
1161
1162 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163}
1164
Martin Radev70866b82016-07-22 15:27:42 +03001165void TParseContext::checkIsParameterQualifierValid(
1166 const TSourceLoc &line,
1167 const TTypeQualifierBuilder &typeQualifierBuilder,
1168 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301169{
Olli Etuahocce89652017-06-19 16:04:09 +03001170 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001171 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001172
1173 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301174 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001175 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1176 }
1177
1178 if (!IsImage(type->getBasicType()))
1179 {
Olli Etuaho43364892017-02-13 16:00:12 +00001180 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001181 }
1182 else
1183 {
1184 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001185 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001186
Martin Radev70866b82016-07-22 15:27:42 +03001187 type->setQualifier(typeQualifier.qualifier);
1188
1189 if (typeQualifier.precision != EbpUndefined)
1190 {
1191 type->setPrecision(typeQualifier.precision);
1192 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001193}
1194
Olli Etuaho703671e2017-11-08 17:47:18 +02001195template <size_t size>
1196bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1197 const std::array<TExtension, size> &extensions)
1198{
1199 ASSERT(!extensions.empty());
1200 const TExtensionBehavior &extBehavior = extensionBehavior();
1201
1202 bool canUseWithWarning = false;
1203 bool canUseWithoutWarning = false;
1204
1205 const char *errorMsgString = "";
1206 TExtension errorMsgExtension = TExtension::UNDEFINED;
1207
1208 for (TExtension extension : extensions)
1209 {
1210 auto extIter = extBehavior.find(extension);
1211 if (canUseWithWarning)
1212 {
1213 // We already have an extension that we can use, but with a warning.
1214 // See if we can use the alternative extension without a warning.
1215 if (extIter == extBehavior.end())
1216 {
1217 continue;
1218 }
1219 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1220 {
1221 canUseWithoutWarning = true;
1222 break;
1223 }
1224 continue;
1225 }
1226 if (extIter == extBehavior.end())
1227 {
1228 errorMsgString = "extension is not supported";
1229 errorMsgExtension = extension;
1230 }
1231 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1232 {
1233 errorMsgString = "extension is disabled";
1234 errorMsgExtension = extension;
1235 }
1236 else if (extIter->second == EBhWarn)
1237 {
1238 errorMsgExtension = extension;
1239 canUseWithWarning = true;
1240 }
1241 else
1242 {
1243 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1244 canUseWithoutWarning = true;
1245 break;
1246 }
1247 }
1248
1249 if (canUseWithoutWarning)
1250 {
1251 return true;
1252 }
1253 if (canUseWithWarning)
1254 {
1255 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1256 return true;
1257 }
1258 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1259 return false;
1260}
1261
1262template bool TParseContext::checkCanUseOneOfExtensions(
1263 const TSourceLoc &line,
1264 const std::array<TExtension, 1> &extensions);
1265template bool TParseContext::checkCanUseOneOfExtensions(
1266 const TSourceLoc &line,
1267 const std::array<TExtension, 2> &extensions);
1268template bool TParseContext::checkCanUseOneOfExtensions(
1269 const TSourceLoc &line,
1270 const std::array<TExtension, 3> &extensions);
1271
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001272bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001273{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001274 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001275 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001276}
1277
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001278// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1279// compile-time or link-time errors are the same whether or not the declaration is empty".
1280// This function implements all the checks that are done on qualifiers regardless of if the
1281// declaration is empty.
1282void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1283 const sh::TLayoutQualifier &layoutQualifier,
1284 const TSourceLoc &location)
1285{
1286 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1287 {
1288 error(location, "Shared memory declarations cannot have layout specified", "layout");
1289 }
1290
1291 if (layoutQualifier.matrixPacking != EmpUnspecified)
1292 {
1293 error(location, "layout qualifier only valid for interface blocks",
1294 getMatrixPackingString(layoutQualifier.matrixPacking));
1295 return;
1296 }
1297
1298 if (layoutQualifier.blockStorage != EbsUnspecified)
1299 {
1300 error(location, "layout qualifier only valid for interface blocks",
1301 getBlockStorageString(layoutQualifier.blockStorage));
1302 return;
1303 }
1304
1305 if (qualifier == EvqFragmentOut)
1306 {
1307 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1308 {
1309 error(location, "invalid layout qualifier combination", "yuv");
1310 return;
1311 }
1312 }
1313 else
1314 {
1315 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1316 }
1317
Olli Etuaho95468d12017-05-04 11:14:34 +03001318 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1319 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001320 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1321 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001322 {
1323 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1324 }
1325
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001326 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001327 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001328 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001329 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001330 // We're not checking whether the uniform location is in range here since that depends on
1331 // the type of the variable.
1332 // The type can only be fully determined for non-empty declarations.
1333 }
1334 if (!canHaveLocation)
1335 {
1336 checkLocationIsNotSpecified(location, layoutQualifier);
1337 }
1338}
1339
jchen104cdac9e2017-05-08 11:01:20 +08001340void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1341 const TSourceLoc &location)
1342{
1343 if (publicType.precision != EbpHigh)
1344 {
1345 error(location, "Can only be highp", "atomic counter");
1346 }
1347 // dEQP enforces compile error if location is specified. See uniform_location.test.
1348 if (publicType.layoutQualifier.location != -1)
1349 {
1350 error(location, "location must not be set for atomic_uint", "layout");
1351 }
1352 if (publicType.layoutQualifier.binding == -1)
1353 {
1354 error(location, "no binding specified", "atomic counter");
1355 }
1356}
1357
Olli Etuaho55bde912017-10-25 13:41:13 +03001358void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001359{
Olli Etuaho55bde912017-10-25 13:41:13 +03001360 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001361 {
1362 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1363 // error. It is assumed that this applies to empty declarations as well.
1364 error(location, "empty array declaration needs to specify a size", "");
1365 }
Martin Radevb8b01222016-11-20 23:25:53 +02001366}
1367
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001368// These checks are done for all declarations that are non-empty. They're done for non-empty
1369// declarations starting a declarator list, and declarators that follow an empty declaration.
1370void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1371 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001372{
Olli Etuahofa33d582015-04-09 14:33:12 +03001373 switch (publicType.qualifier)
1374 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001375 case EvqVaryingIn:
1376 case EvqVaryingOut:
1377 case EvqAttribute:
1378 case EvqVertexIn:
1379 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001380 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001381 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001382 {
1383 error(identifierLocation, "cannot be used with a structure",
1384 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001385 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001386 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001387 break;
1388 case EvqBuffer:
1389 if (publicType.getBasicType() != EbtInterfaceBlock)
1390 {
1391 error(identifierLocation,
1392 "cannot declare buffer variables at global scope(outside a block)",
1393 getQualifierString(publicType.qualifier));
1394 return;
1395 }
1396 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001397 default:
1398 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001399 }
jchen10cc2a10e2017-05-03 14:05:12 +08001400 std::string reason(getBasicString(publicType.getBasicType()));
1401 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001402 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001403 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001404 {
1405 return;
1406 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001407
Andrei Volykhina5527072017-03-22 16:46:30 +03001408 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1409 publicType.qualifier != EvqConst) &&
1410 publicType.getBasicType() == EbtYuvCscStandardEXT)
1411 {
1412 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1413 getQualifierString(publicType.qualifier));
1414 return;
1415 }
1416
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001417 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1418 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001419 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1420 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001421 TType type(publicType);
1422 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001423 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001424 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1425 publicType.layoutQualifier);
1426 }
1427 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001428
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001429 // check for layout qualifier issues
1430 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001431
Martin Radev2cc85b32016-08-05 16:22:53 +03001432 if (IsImage(publicType.getBasicType()))
1433 {
1434
1435 switch (layoutQualifier.imageInternalFormat)
1436 {
1437 case EiifRGBA32F:
1438 case EiifRGBA16F:
1439 case EiifR32F:
1440 case EiifRGBA8:
1441 case EiifRGBA8_SNORM:
1442 if (!IsFloatImage(publicType.getBasicType()))
1443 {
1444 error(identifierLocation,
1445 "internal image format requires a floating image type",
1446 getBasicString(publicType.getBasicType()));
1447 return;
1448 }
1449 break;
1450 case EiifRGBA32I:
1451 case EiifRGBA16I:
1452 case EiifRGBA8I:
1453 case EiifR32I:
1454 if (!IsIntegerImage(publicType.getBasicType()))
1455 {
1456 error(identifierLocation,
1457 "internal image format requires an integer image type",
1458 getBasicString(publicType.getBasicType()));
1459 return;
1460 }
1461 break;
1462 case EiifRGBA32UI:
1463 case EiifRGBA16UI:
1464 case EiifRGBA8UI:
1465 case EiifR32UI:
1466 if (!IsUnsignedImage(publicType.getBasicType()))
1467 {
1468 error(identifierLocation,
1469 "internal image format requires an unsigned image type",
1470 getBasicString(publicType.getBasicType()));
1471 return;
1472 }
1473 break;
1474 case EiifUnspecified:
1475 error(identifierLocation, "layout qualifier", "No image internal format specified");
1476 return;
1477 default:
1478 error(identifierLocation, "layout qualifier", "unrecognized token");
1479 return;
1480 }
1481
1482 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1483 switch (layoutQualifier.imageInternalFormat)
1484 {
1485 case EiifR32F:
1486 case EiifR32I:
1487 case EiifR32UI:
1488 break;
1489 default:
1490 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1491 {
1492 error(identifierLocation, "layout qualifier",
1493 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1494 "image variables must be qualified readonly and/or writeonly");
1495 return;
1496 }
1497 break;
1498 }
1499 }
1500 else
1501 {
Olli Etuaho43364892017-02-13 16:00:12 +00001502 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001503 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1504 }
jchen104cdac9e2017-05-08 11:01:20 +08001505
1506 if (IsAtomicCounter(publicType.getBasicType()))
1507 {
1508 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1509 }
1510 else
1511 {
1512 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1513 }
Olli Etuaho43364892017-02-13 16:00:12 +00001514}
Martin Radev2cc85b32016-08-05 16:22:53 +03001515
Olli Etuaho43364892017-02-13 16:00:12 +00001516void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1517{
1518 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001519 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1520 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1521 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1522 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1523 // when it comes to which shaders are accepted by the compiler.
1524 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001525 if (IsImage(type.getBasicType()))
1526 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001527 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1528 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001529 }
1530 else if (IsSampler(type.getBasicType()))
1531 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001532 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1533 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001534 }
jchen104cdac9e2017-05-08 11:01:20 +08001535 else if (IsAtomicCounter(type.getBasicType()))
1536 {
1537 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1538 }
Olli Etuaho43364892017-02-13 16:00:12 +00001539 else
1540 {
1541 ASSERT(!IsOpaqueType(type.getBasicType()));
1542 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001543 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001544}
1545
Olli Etuaho856c4972016-08-08 11:38:39 +03001546void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1547 const TString &layoutQualifierName,
1548 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001549{
1550
1551 if (mShaderVersion < versionRequired)
1552 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001553 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001554 }
1555}
1556
Olli Etuaho856c4972016-08-08 11:38:39 +03001557bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1558 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001559{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001560 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001561 for (size_t i = 0u; i < localSize.size(); ++i)
1562 {
1563 if (localSize[i] != -1)
1564 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001565 error(location,
1566 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1567 "global layout declaration",
1568 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001569 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001570 }
1571 }
1572
Olli Etuaho8a176262016-08-16 14:23:01 +03001573 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001574}
1575
Olli Etuaho43364892017-02-13 16:00:12 +00001576void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001577 TLayoutImageInternalFormat internalFormat)
1578{
1579 if (internalFormat != EiifUnspecified)
1580 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001581 error(location, "invalid layout qualifier: only valid when used with images",
1582 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001583 }
Olli Etuaho43364892017-02-13 16:00:12 +00001584}
1585
1586void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1587{
1588 if (binding != -1)
1589 {
1590 error(location,
1591 "invalid layout qualifier: only valid when used with opaque types or blocks",
1592 "binding");
1593 }
1594}
1595
jchen104cdac9e2017-05-08 11:01:20 +08001596void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1597{
1598 if (offset != -1)
1599 {
1600 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1601 "offset");
1602 }
1603}
1604
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001605void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1606 int binding,
1607 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001608{
1609 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001610 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001611 {
1612 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1613 }
1614}
1615
1616void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1617 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001618 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001619{
1620 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001621 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001622 {
1623 error(location, "sampler binding greater than maximum texture units", "binding");
1624 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001625}
1626
Jiajia Qinbc585152017-06-23 15:42:17 +08001627void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1628 const TQualifier &qualifier,
1629 int binding,
1630 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001631{
1632 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001633 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001634 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001635 if (binding + size > mMaxUniformBufferBindings)
1636 {
1637 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1638 "binding");
1639 }
1640 }
1641 else if (qualifier == EvqBuffer)
1642 {
1643 if (binding + size > mMaxShaderStorageBufferBindings)
1644 {
1645 error(location,
1646 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1647 "binding");
1648 }
jchen10af713a22017-04-19 09:10:56 +08001649 }
1650}
jchen104cdac9e2017-05-08 11:01:20 +08001651void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1652{
1653 if (binding >= mMaxAtomicCounterBindings)
1654 {
1655 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1656 "binding");
1657 }
1658}
jchen10af713a22017-04-19 09:10:56 +08001659
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001660void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1661 int objectLocationCount,
1662 const TLayoutQualifier &layoutQualifier)
1663{
1664 int loc = layoutQualifier.location;
1665 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1666 {
1667 error(location, "Uniform location out of range", "location");
1668 }
1669}
1670
Andrei Volykhina5527072017-03-22 16:46:30 +03001671void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1672{
1673 if (yuv != false)
1674 {
1675 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1676 }
1677}
1678
Jiajia Qinbc585152017-06-23 15:42:17 +08001679void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1680 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001681{
1682 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1683 {
1684 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001685 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1686 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1687 qual == EvqInOut || qual == EvqConstReadOnly))
1688 {
1689 if (argument->getMemoryQualifier().writeonly)
1690 {
1691 error(argument->getLine(),
1692 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001693 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001694 return;
1695 }
1696 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001697 if (qual == EvqOut || qual == EvqInOut)
1698 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001699 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001700 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001701 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001702 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001703 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001704 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001705 }
1706 }
1707 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001708}
1709
Martin Radev70866b82016-07-22 15:27:42 +03001710void TParseContext::checkInvariantVariableQualifier(bool invariant,
1711 const TQualifier qualifier,
1712 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001713{
Martin Radev70866b82016-07-22 15:27:42 +03001714 if (!invariant)
1715 return;
1716
1717 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001718 {
Martin Radev70866b82016-07-22 15:27:42 +03001719 // input variables in the fragment shader can be also qualified as invariant
1720 if (!sh::CanBeInvariantESSL1(qualifier))
1721 {
1722 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1723 }
1724 }
1725 else
1726 {
1727 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1728 {
1729 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1730 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001731 }
1732}
1733
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001734bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001735{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001736 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001737}
1738
Jamie Madillb98c3a82015-07-23 14:26:04 -04001739void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1740 const char *extName,
1741 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001742{
1743 pp::SourceLocation srcLoc;
1744 srcLoc.file = loc.first_file;
1745 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001746 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001747}
1748
Jamie Madillb98c3a82015-07-23 14:26:04 -04001749void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1750 const char *name,
1751 const char *value,
1752 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001753{
1754 pp::SourceLocation srcLoc;
1755 srcLoc.file = loc.first_file;
1756 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001757 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001758}
1759
Martin Radev4c4c8e72016-08-04 12:25:34 +03001760sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001761{
Jamie Madill2f294c92017-11-20 14:47:26 -05001762 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001763 for (size_t i = 0u; i < result.size(); ++i)
1764 {
1765 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1766 {
1767 result[i] = 1;
1768 }
1769 else
1770 {
1771 result[i] = mComputeShaderLocalSize[i];
1772 }
1773 }
1774 return result;
1775}
1776
Olli Etuaho56229f12017-07-10 14:16:33 +03001777TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1778 const TSourceLoc &line)
1779{
1780 TIntermConstantUnion *node = new TIntermConstantUnion(
1781 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1782 node->setLine(line);
1783 return node;
1784}
1785
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786/////////////////////////////////////////////////////////////////////////////////
1787//
1788// Non-Errors.
1789//
1790/////////////////////////////////////////////////////////////////////////////////
1791
Jamie Madill5c097022014-08-20 16:38:32 -04001792const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1793 const TString *name,
1794 const TSymbol *symbol)
1795{
Jamie Madill5c097022014-08-20 16:38:32 -04001796 if (!symbol)
1797 {
1798 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001799 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001800 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001801
1802 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001803 {
1804 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001805 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001806 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001807
1808 const TVariable *variable = static_cast<const TVariable *>(symbol);
1809
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001810 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001811 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001812 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001813 }
1814
Olli Etuaho0f684632017-07-13 12:42:15 +03001815 // Reject shaders using both gl_FragData and gl_FragColor
1816 TQualifier qualifier = variable->getType().getQualifier();
1817 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001818 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001819 mUsesFragData = true;
1820 }
1821 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1822 {
1823 mUsesFragColor = true;
1824 }
1825 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1826 {
1827 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001828 }
1829
Olli Etuaho0f684632017-07-13 12:42:15 +03001830 // This validation is not quite correct - it's only an error to write to
1831 // both FragData and FragColor. For simplicity, and because users shouldn't
1832 // be rewarded for reading from undefined varaibles, return an error
1833 // if they are both referenced, rather than assigned.
1834 if (mUsesFragData && mUsesFragColor)
1835 {
1836 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1837 if (mUsesSecondaryOutputs)
1838 {
1839 errorMessage =
1840 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1841 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1842 }
1843 error(location, errorMessage, name->c_str());
1844 }
1845
1846 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1847 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1848 qualifier == EvqWorkGroupSize)
1849 {
1850 error(location,
1851 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1852 "gl_WorkGroupSize");
1853 }
Jamie Madill5c097022014-08-20 16:38:32 -04001854 return variable;
1855}
1856
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001857TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1858 const TString *name,
1859 const TSymbol *symbol)
1860{
1861 const TVariable *variable = getNamedVariable(location, name, symbol);
1862
Olli Etuaho0f684632017-07-13 12:42:15 +03001863 if (!variable)
1864 {
1865 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1866 node->setLine(location);
1867 return node;
1868 }
1869
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001870 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001871 TIntermTyped *node = nullptr;
1872
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001873 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001874 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001875 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001876 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001877 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001878 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001879 {
1880 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1881 // needs to be added to the AST as a constant and not as a symbol.
1882 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1883 TConstantUnion *constArray = new TConstantUnion[3];
1884 for (size_t i = 0; i < 3; ++i)
1885 {
1886 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1887 }
1888
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001889 ASSERT(variableType.getBasicType() == EbtUInt);
1890 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001891
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001892 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001893 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001894 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001895 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001896 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1897 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001898 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001899 ASSERT(mGeometryShaderInputArraySize > 0u);
1900
Olli Etuaho195be942017-12-04 23:40:14 +02001901 node = new TIntermSymbol(variable);
Olli Etuaho55bde912017-10-25 13:41:13 +03001902 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001903 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001904 else
1905 {
Olli Etuaho195be942017-12-04 23:40:14 +02001906 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001907 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001908 ASSERT(node != nullptr);
1909 node->setLine(location);
1910 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001911}
1912
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913// Initializers show up in several places in the grammar. Have one set of
1914// code to handle them here.
1915//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001916// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001917bool TParseContext::executeInitializer(const TSourceLoc &line,
1918 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001919 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001920 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001921 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922{
Olli Etuaho13389b62016-10-16 11:48:18 +01001923 ASSERT(initNode != nullptr);
1924 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925
Olli Etuaho376f1b52015-04-13 13:23:41 +03001926 if (type.isUnsizedArray())
1927 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001928 // In case initializer is not an array or type has more dimensions than initializer, this
1929 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1930 // actually is an array or not. Having a non-array initializer for an unsized array will
1931 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001932 auto *arraySizes = initializer->getType().getArraySizes();
1933 type.sizeUnsizedArrays(arraySizes);
Olli Etuaho376f1b52015-04-13 13:23:41 +03001934 }
Olli Etuaho195be942017-12-04 23:40:14 +02001935
1936 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001937 if (!declareVariable(line, identifier, type, &variable))
1938 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001939 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001940 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941
Olli Etuahob0c645e2015-05-12 14:25:36 +03001942 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001943 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001944 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001945 {
1946 // Error message does not completely match behavior with ESSL 1.00, but
1947 // we want to steer developers towards only using constant expressions.
1948 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001949 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001950 }
1951 if (globalInitWarning)
1952 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001953 warning(
1954 line,
1955 "global variable initializers should be constant expressions "
1956 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1957 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001958 }
1959
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001960 //
1961 // identifier must be of type constant, a global, or a temporary
1962 //
1963 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301964 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1965 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001966 error(line, " cannot initialize this type of qualifier ",
1967 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001968 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001969 }
1970 //
1971 // test for and propagate constant
1972 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973
Arun Patole7e7e68d2015-05-22 12:02:25 +05301974 if (qualifier == EvqConst)
1975 {
1976 if (qualifier != initializer->getType().getQualifier())
1977 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001978 std::stringstream reasonStream;
1979 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1980 << "'";
1981 std::string reason = reasonStream.str();
1982 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001983 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001984 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001985 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301986 if (type != initializer->getType())
1987 {
1988 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001989 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001990 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001991 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001992 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001993
1994 // Save the constant folded value to the variable if possible. For example array
1995 // initializers are not folded, since that way copying the array literal to multiple places
1996 // in the shader is avoided.
1997 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1998 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301999 if (initializer->getAsConstantUnion())
2000 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002001 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002002 ASSERT(*initNode == nullptr);
2003 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302004 }
2005 else if (initializer->getAsSymbolNode())
2006 {
Olli Etuahob6af22b2017-12-15 14:05:44 +02002007 const TVariable &var = initializer->getAsSymbolNode()->variable();
2008 const TConstantUnion *constArray = var.getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002009 if (constArray)
2010 {
2011 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002012 ASSERT(*initNode == nullptr);
2013 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002014 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002015 }
2016 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002017
Olli Etuaho195be942017-12-04 23:40:14 +02002018 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002019 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002020 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2021 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002022 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002023 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002024 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002025 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026
Olli Etuaho914b79a2017-06-19 16:03:19 +03002027 return true;
2028}
2029
2030TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2031 const TString &identifier,
2032 TIntermTyped *initializer,
2033 const TSourceLoc &loc)
2034{
2035 checkIsScalarBool(loc, pType);
2036 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002037 TType type(pType);
2038 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002039 {
2040 // The initializer is valid. The init condition needs to have a node - either the
2041 // initializer node, or a constant node in case the initialized variable is const and won't
2042 // be recorded in the AST.
2043 if (initNode == nullptr)
2044 {
2045 return initializer;
2046 }
2047 else
2048 {
2049 TIntermDeclaration *declaration = new TIntermDeclaration();
2050 declaration->appendDeclarator(initNode);
2051 return declaration;
2052 }
2053 }
2054 return nullptr;
2055}
2056
2057TIntermNode *TParseContext::addLoop(TLoopType type,
2058 TIntermNode *init,
2059 TIntermNode *cond,
2060 TIntermTyped *expr,
2061 TIntermNode *body,
2062 const TSourceLoc &line)
2063{
2064 TIntermNode *node = nullptr;
2065 TIntermTyped *typedCond = nullptr;
2066 if (cond)
2067 {
2068 typedCond = cond->getAsTyped();
2069 }
2070 if (cond == nullptr || typedCond)
2071 {
Olli Etuahocce89652017-06-19 16:04:09 +03002072 if (type == ELoopDoWhile)
2073 {
2074 checkIsScalarBool(line, typedCond);
2075 }
2076 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2077 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2078 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2079 !typedCond->isVector()));
2080
Olli Etuaho3ec75682017-07-05 17:02:55 +03002081 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002082 node->setLine(line);
2083 return node;
2084 }
2085
Olli Etuahocce89652017-06-19 16:04:09 +03002086 ASSERT(type != ELoopDoWhile);
2087
Olli Etuaho914b79a2017-06-19 16:03:19 +03002088 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2089 ASSERT(declaration);
2090 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2091 ASSERT(declarator->getLeft()->getAsSymbolNode());
2092
2093 // The condition is a declaration. In the AST representation we don't support declarations as
2094 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2095 // the loop.
2096 TIntermBlock *block = new TIntermBlock();
2097
2098 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2099 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2100 block->appendStatement(declareCondition);
2101
2102 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2103 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002104 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002105 block->appendStatement(loop);
2106 loop->setLine(line);
2107 block->setLine(line);
2108 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109}
2110
Olli Etuahocce89652017-06-19 16:04:09 +03002111TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2112 TIntermNodePair code,
2113 const TSourceLoc &loc)
2114{
Olli Etuaho56229f12017-07-10 14:16:33 +03002115 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002116
2117 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002118 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002119 {
2120 if (cond->getAsConstantUnion()->getBConst(0) == true)
2121 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002122 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002123 }
2124 else
2125 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002126 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002127 }
2128 }
2129
Olli Etuaho3ec75682017-07-05 17:02:55 +03002130 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002131 node->setLine(loc);
2132
2133 return node;
2134}
2135
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002136void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2137{
2138 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2139 typeSpecifier->getBasicType());
2140
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002141 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002142 {
2143 error(typeSpecifier->getLine(), "not supported", "first-class array");
2144 typeSpecifier->clearArrayness();
2145 }
2146}
2147
Martin Radev70866b82016-07-22 15:27:42 +03002148TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302149 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002150{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002151 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002152
Martin Radev70866b82016-07-22 15:27:42 +03002153 TPublicType returnType = typeSpecifier;
2154 returnType.qualifier = typeQualifier.qualifier;
2155 returnType.invariant = typeQualifier.invariant;
2156 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002157 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002158 returnType.precision = typeSpecifier.precision;
2159
2160 if (typeQualifier.precision != EbpUndefined)
2161 {
2162 returnType.precision = typeQualifier.precision;
2163 }
2164
Martin Radev4a9cd802016-09-01 16:51:51 +03002165 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2166 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002167
Martin Radev4a9cd802016-09-01 16:51:51 +03002168 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2169 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002170
Martin Radev4a9cd802016-09-01 16:51:51 +03002171 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002172
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002173 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002174 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002175 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002176 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002177 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002178 returnType.clearArrayness();
2179 }
2180
Martin Radev70866b82016-07-22 15:27:42 +03002181 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002182 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002183 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002184 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002185 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002186 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002187
Martin Radev70866b82016-07-22 15:27:42 +03002188 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002189 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002190 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002191 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002192 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002193 }
2194 }
2195 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002196 {
Martin Radev70866b82016-07-22 15:27:42 +03002197 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002198 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002199 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002200 }
Martin Radev70866b82016-07-22 15:27:42 +03002201 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2202 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002203 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002204 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2205 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002206 }
Martin Radev70866b82016-07-22 15:27:42 +03002207 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002208 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002209 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002210 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002211 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002212 }
2213
2214 return returnType;
2215}
2216
Olli Etuaho856c4972016-08-08 11:38:39 +03002217void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2218 const TPublicType &type,
2219 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002220{
2221 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002222 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002223 {
2224 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002225 }
2226
2227 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2228 switch (qualifier)
2229 {
2230 case EvqVertexIn:
2231 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002232 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002233 {
2234 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002235 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002236 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002237 return;
2238 case EvqFragmentOut:
2239 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002240 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002241 {
2242 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002243 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002244 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002245 return;
2246 default:
2247 break;
2248 }
2249
2250 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2251 // restrictions.
2252 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002253 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2254 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002255 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2256 {
2257 error(qualifierLocation, "must use 'flat' interpolation here",
2258 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002259 }
2260
Martin Radev4a9cd802016-09-01 16:51:51 +03002261 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002262 {
2263 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2264 // These restrictions are only implied by the ESSL 3.00 spec, but
2265 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002266 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 {
2268 error(qualifierLocation, "cannot be an array of structures",
2269 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002270 }
2271 if (type.isStructureContainingArrays())
2272 {
2273 error(qualifierLocation, "cannot be a structure containing an array",
2274 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002275 }
2276 if (type.isStructureContainingType(EbtStruct))
2277 {
2278 error(qualifierLocation, "cannot be a structure containing a structure",
2279 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002280 }
2281 if (type.isStructureContainingType(EbtBool))
2282 {
2283 error(qualifierLocation, "cannot be a structure containing a bool",
2284 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002285 }
2286 }
2287}
2288
Martin Radev2cc85b32016-08-05 16:22:53 +03002289void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2290{
2291 if (qualifier.getType() == QtStorage)
2292 {
2293 const TStorageQualifierWrapper &storageQualifier =
2294 static_cast<const TStorageQualifierWrapper &>(qualifier);
2295 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2296 !symbolTable.atGlobalLevel())
2297 {
2298 error(storageQualifier.getLine(),
2299 "Local variables can only use the const storage qualifier.",
2300 storageQualifier.getQualifierString().c_str());
2301 }
2302 }
2303}
2304
Olli Etuaho43364892017-02-13 16:00:12 +00002305void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002306 const TSourceLoc &location)
2307{
Jiajia Qinbc585152017-06-23 15:42:17 +08002308 const std::string reason(
2309 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2310 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002311 if (memoryQualifier.readonly)
2312 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002313 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002314 }
2315 if (memoryQualifier.writeonly)
2316 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002317 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002318 }
Martin Radev049edfa2016-11-11 14:35:37 +02002319 if (memoryQualifier.coherent)
2320 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002321 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002322 }
2323 if (memoryQualifier.restrictQualifier)
2324 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002325 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002326 }
2327 if (memoryQualifier.volatileQualifier)
2328 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002329 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002330 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002331}
2332
jchen104cdac9e2017-05-08 11:01:20 +08002333// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2334// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002335void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2336 const TSourceLoc &loc,
2337 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002338{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002339 if (!IsAtomicCounter(type->getBasicType()))
2340 {
2341 return;
2342 }
2343
2344 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2345 : kAtomicCounterSize;
2346 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2347 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002348 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002349 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002350 {
2351 offset = bindingState.appendSpan(size);
2352 }
2353 else
2354 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002355 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002356 }
2357 if (offset == -1)
2358 {
2359 error(loc, "Offset overlapping", "atomic counter");
2360 return;
2361 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002362 layoutQualifier.offset = offset;
2363 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002364}
2365
Olli Etuaho454c34c2017-10-25 16:35:56 +03002366void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2367 const char *token,
2368 TType *type)
2369{
2370 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2371 {
2372 if (type->isArray() && type->getOutermostArraySize() == 0u)
2373 {
2374 // Set size for the unsized geometry shader inputs if they are declared after a valid
2375 // input primitive declaration.
2376 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2377 {
2378 ASSERT(mGeometryShaderInputArraySize > 0u);
2379 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2380 }
2381 else
2382 {
2383 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2384 // An input can be declared without an array size if there is a previous layout
2385 // which specifies the size.
2386 error(location,
2387 "Missing a valid input primitive declaration before declaring an unsized "
2388 "array input",
2389 token);
2390 }
2391 }
2392 else if (type->isArray())
2393 {
2394 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2395 }
2396 else
2397 {
2398 error(location, "Geometry shader input variable must be declared as an array", token);
2399 }
2400 }
2401}
2402
Olli Etuaho13389b62016-10-16 11:48:18 +01002403TIntermDeclaration *TParseContext::parseSingleDeclaration(
2404 TPublicType &publicType,
2405 const TSourceLoc &identifierOrTypeLocation,
2406 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002407{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002408 TType type(publicType);
2409 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2410 mDirectiveHandler.pragma().stdgl.invariantAll)
2411 {
2412 TQualifier qualifier = type.getQualifier();
2413
2414 // The directive handler has already taken care of rejecting invalid uses of this pragma
2415 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2416 // affected variable declarations:
2417 //
2418 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2419 // elsewhere, in TranslatorGLSL.)
2420 //
2421 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2422 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2423 // the way this is currently implemented we have to enable this compiler option before
2424 // parsing the shader and determining the shading language version it uses. If this were
2425 // implemented as a post-pass, the workaround could be more targeted.
2426 //
2427 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2428 // the specification, but there are desktop OpenGL drivers that expect that this is the
2429 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2430 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2431 {
2432 type.setInvariant(true);
2433 }
2434 }
2435
Olli Etuaho454c34c2017-10-25 16:35:56 +03002436 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002437
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002438 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2439 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002440
Olli Etuahobab4c082015-04-24 16:38:49 +03002441 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002442 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002443
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002444 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002445 if (emptyDeclaration)
2446 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002447 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002448 // In most cases we don't need to create a symbol node for an empty declaration.
2449 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2450 if (type.getBasicType() == EbtStruct)
2451 {
Olli Etuaho195be942017-12-04 23:40:14 +02002452 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01002453 new TVariable(&symbolTable, nullptr, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002454 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002455 }
jchen104cdac9e2017-05-08 11:01:20 +08002456 else if (IsAtomicCounter(publicType.getBasicType()))
2457 {
2458 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2459 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002460 }
2461 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002462 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002463 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002464
Olli Etuaho55bde912017-10-25 13:41:13 +03002465 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002466
Olli Etuaho55bc9052017-10-25 17:33:06 +03002467 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002468
Olli Etuaho2935c582015-04-08 14:32:06 +03002469 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002470 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002471 {
Olli Etuaho195be942017-12-04 23:40:14 +02002472 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002473 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002474 }
2475
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002476 TIntermDeclaration *declaration = new TIntermDeclaration();
2477 declaration->setLine(identifierOrTypeLocation);
2478 if (symbol)
2479 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002480 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002481 declaration->appendDeclarator(symbol);
2482 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002483 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002484}
2485
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002486TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2487 TPublicType &elementType,
2488 const TSourceLoc &identifierLocation,
2489 const TString &identifier,
2490 const TSourceLoc &indexLocation,
2491 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002492{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002493 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002494
Olli Etuaho55bde912017-10-25 13:41:13 +03002495 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002496 identifierLocation);
2497
Olli Etuaho55bde912017-10-25 13:41:13 +03002498 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002499
Olli Etuaho55bde912017-10-25 13:41:13 +03002500 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002501
Olli Etuaho55bde912017-10-25 13:41:13 +03002502 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002503 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002504
Olli Etuaho454c34c2017-10-25 16:35:56 +03002505 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002506
2507 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2508
Olli Etuaho55bc9052017-10-25 17:33:06 +03002509 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002510
Olli Etuaho13389b62016-10-16 11:48:18 +01002511 TIntermDeclaration *declaration = new TIntermDeclaration();
2512 declaration->setLine(identifierLocation);
2513
Olli Etuaho195be942017-12-04 23:40:14 +02002514 TVariable *variable = nullptr;
2515 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002516 {
Olli Etuaho195be942017-12-04 23:40:14 +02002517 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002518 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002519 declaration->appendDeclarator(symbol);
2520 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002521
Olli Etuaho13389b62016-10-16 11:48:18 +01002522 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002523}
2524
Olli Etuaho13389b62016-10-16 11:48:18 +01002525TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2526 const TSourceLoc &identifierLocation,
2527 const TString &identifier,
2528 const TSourceLoc &initLocation,
2529 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002530{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002531 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002532
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002533 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2534 identifierLocation);
2535
2536 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002537
Olli Etuaho13389b62016-10-16 11:48:18 +01002538 TIntermDeclaration *declaration = new TIntermDeclaration();
2539 declaration->setLine(identifierLocation);
2540
2541 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002542 TType type(publicType);
2543 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002544 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002545 if (initNode)
2546 {
2547 declaration->appendDeclarator(initNode);
2548 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002549 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002550 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002551}
2552
Olli Etuaho13389b62016-10-16 11:48:18 +01002553TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002554 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002555 const TSourceLoc &identifierLocation,
2556 const TString &identifier,
2557 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002558 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002559 const TSourceLoc &initLocation,
2560 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002561{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002562 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002563
Olli Etuaho55bde912017-10-25 13:41:13 +03002564 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002565 identifierLocation);
2566
Olli Etuaho55bde912017-10-25 13:41:13 +03002567 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002568
Olli Etuaho55bde912017-10-25 13:41:13 +03002569 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002570
Olli Etuaho55bde912017-10-25 13:41:13 +03002571 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002572 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002573
Olli Etuaho13389b62016-10-16 11:48:18 +01002574 TIntermDeclaration *declaration = new TIntermDeclaration();
2575 declaration->setLine(identifierLocation);
2576
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002577 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002578 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002579 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002580 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002581 if (initNode)
2582 {
2583 declaration->appendDeclarator(initNode);
2584 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002585 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002586
2587 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002588}
2589
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002590TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002591 const TTypeQualifierBuilder &typeQualifierBuilder,
2592 const TSourceLoc &identifierLoc,
2593 const TString *identifier,
2594 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002595{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002596 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002597
Martin Radev70866b82016-07-22 15:27:42 +03002598 if (!typeQualifier.invariant)
2599 {
2600 error(identifierLoc, "Expected invariant", identifier->c_str());
2601 return nullptr;
2602 }
2603 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2604 {
2605 return nullptr;
2606 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002607 if (!symbol)
2608 {
2609 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002610 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002611 }
Martin Radev70866b82016-07-22 15:27:42 +03002612 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002613 {
Martin Radev70866b82016-07-22 15:27:42 +03002614 error(identifierLoc, "invariant declaration specifies qualifier",
2615 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002616 }
Martin Radev70866b82016-07-22 15:27:42 +03002617 if (typeQualifier.precision != EbpUndefined)
2618 {
2619 error(identifierLoc, "invariant declaration specifies precision",
2620 getPrecisionString(typeQualifier.precision));
2621 }
2622 if (!typeQualifier.layoutQualifier.isEmpty())
2623 {
2624 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2625 }
2626
2627 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002628 if (!variable)
2629 {
2630 return nullptr;
2631 }
Martin Radev70866b82016-07-22 15:27:42 +03002632 const TType &type = variable->getType();
2633
2634 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2635 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002636 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002637
2638 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2639
Olli Etuaho195be942017-12-04 23:40:14 +02002640 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002641 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002642
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002643 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002644}
2645
Olli Etuaho13389b62016-10-16 11:48:18 +01002646void TParseContext::parseDeclarator(TPublicType &publicType,
2647 const TSourceLoc &identifierLocation,
2648 const TString &identifier,
2649 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002650{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002651 // If the declaration starting this declarator list was empty (example: int,), some checks were
2652 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002653 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002654 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002655 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2656 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002657 }
2658
Olli Etuaho856c4972016-08-08 11:38:39 +03002659 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002660
Olli Etuaho43364892017-02-13 16:00:12 +00002661 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002662
2663 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2664
Olli Etuaho55bde912017-10-25 13:41:13 +03002665 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2666
Olli Etuaho55bc9052017-10-25 17:33:06 +03002667 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2668
Olli Etuaho195be942017-12-04 23:40:14 +02002669 TVariable *variable = nullptr;
2670 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002671 {
Olli Etuaho195be942017-12-04 23:40:14 +02002672 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002673 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002674 declarationOut->appendDeclarator(symbol);
2675 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002676}
2677
Olli Etuaho55bde912017-10-25 13:41:13 +03002678void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002679 const TSourceLoc &identifierLocation,
2680 const TString &identifier,
2681 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002682 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002683 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002684{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002685 // If the declaration starting this declarator list was empty (example: int,), some checks were
2686 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002687 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002688 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002689 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002690 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002691 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002692
Olli Etuaho55bde912017-10-25 13:41:13 +03002693 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002694
Olli Etuaho55bde912017-10-25 13:41:13 +03002695 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002696 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002697 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002698 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002699
Olli Etuaho454c34c2017-10-25 16:35:56 +03002700 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2701
Olli Etuaho55bde912017-10-25 13:41:13 +03002702 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2703
Olli Etuaho55bc9052017-10-25 17:33:06 +03002704 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002705
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002706 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002707 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002708 {
Olli Etuaho195be942017-12-04 23:40:14 +02002709 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002710 symbol->setLine(identifierLocation);
2711 declarationOut->appendDeclarator(symbol);
2712 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002713 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002714}
2715
Olli Etuaho13389b62016-10-16 11:48:18 +01002716void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2717 const TSourceLoc &identifierLocation,
2718 const TString &identifier,
2719 const TSourceLoc &initLocation,
2720 TIntermTyped *initializer,
2721 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002722{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002723 // If the declaration starting this declarator list was empty (example: int,), some checks were
2724 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002725 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002726 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002727 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2728 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002729 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002730
Olli Etuaho856c4972016-08-08 11:38:39 +03002731 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002732
Olli Etuaho13389b62016-10-16 11:48:18 +01002733 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002734 TType type(publicType);
2735 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002736 {
2737 //
2738 // build the intermediate representation
2739 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002740 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002741 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002742 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002743 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002744 }
2745}
2746
Olli Etuaho55bde912017-10-25 13:41:13 +03002747void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002748 const TSourceLoc &identifierLocation,
2749 const TString &identifier,
2750 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002751 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002752 const TSourceLoc &initLocation,
2753 TIntermTyped *initializer,
2754 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002755{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002756 // If the declaration starting this declarator list was empty (example: int,), some checks were
2757 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002758 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002759 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002760 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002761 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762 }
2763
Olli Etuaho55bde912017-10-25 13:41:13 +03002764 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002765
Olli Etuaho55bde912017-10-25 13:41:13 +03002766 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002767
Olli Etuaho55bde912017-10-25 13:41:13 +03002768 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002769 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002770
2771 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002772 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002773 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002774 {
2775 if (initNode)
2776 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002777 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002778 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002779 }
2780}
2781
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002782TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2783{
2784 // It's simpler to parse an empty statement as a constant expression rather than having a
2785 // different type of node just for empty statements, that will be pruned from the AST anyway.
2786 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2787 node->setLine(location);
2788 return node;
2789}
2790
jchen104cdac9e2017-05-08 11:01:20 +08002791void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2792 const TSourceLoc &location)
2793{
2794 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2795 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2796 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2797 {
2798 error(location, "Requires both binding and offset", "layout");
2799 return;
2800 }
2801 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2802}
2803
Olli Etuahocce89652017-06-19 16:04:09 +03002804void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2805 const TPublicType &type,
2806 const TSourceLoc &loc)
2807{
2808 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2809 !getFragmentPrecisionHigh())
2810 {
2811 error(loc, "precision is not supported in fragment shader", "highp");
2812 }
2813
2814 if (!CanSetDefaultPrecisionOnType(type))
2815 {
2816 error(loc, "illegal type argument for default precision qualifier",
2817 getBasicString(type.getBasicType()));
2818 return;
2819 }
2820 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2821}
2822
Shaob5cc1192017-07-06 10:47:20 +08002823bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2824{
2825 switch (typeQualifier.layoutQualifier.primitiveType)
2826 {
2827 case EptLines:
2828 case EptLinesAdjacency:
2829 case EptTriangles:
2830 case EptTrianglesAdjacency:
2831 return typeQualifier.qualifier == EvqGeometryIn;
2832
2833 case EptLineStrip:
2834 case EptTriangleStrip:
2835 return typeQualifier.qualifier == EvqGeometryOut;
2836
2837 case EptPoints:
2838 return true;
2839
2840 default:
2841 UNREACHABLE();
2842 return false;
2843 }
2844}
2845
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002846void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2847 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002848{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002849 if (mGeometryShaderInputArraySize == 0u)
2850 {
2851 mGeometryShaderInputArraySize = inputArraySize;
2852 }
2853 else if (mGeometryShaderInputArraySize != inputArraySize)
2854 {
2855 error(line,
2856 "Array size or input primitive declaration doesn't match the size of earlier sized "
2857 "array inputs.",
2858 "layout");
2859 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002860}
2861
Shaob5cc1192017-07-06 10:47:20 +08002862bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2863{
2864 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2865
2866 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2867
2868 if (layoutQualifier.maxVertices != -1)
2869 {
2870 error(typeQualifier.line,
2871 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2872 return false;
2873 }
2874
2875 // Set mGeometryInputPrimitiveType if exists
2876 if (layoutQualifier.primitiveType != EptUndefined)
2877 {
2878 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2879 {
2880 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2881 return false;
2882 }
2883
2884 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2885 {
2886 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002887 setGeometryShaderInputArraySize(
2888 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2889 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002890 }
2891 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2892 {
2893 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2894 "layout");
2895 return false;
2896 }
2897 }
2898
2899 // Set mGeometryInvocations if exists
2900 if (layoutQualifier.invocations > 0)
2901 {
2902 if (mGeometryShaderInvocations == 0)
2903 {
2904 mGeometryShaderInvocations = layoutQualifier.invocations;
2905 }
2906 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2907 {
2908 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2909 "layout");
2910 return false;
2911 }
2912 }
2913
2914 return true;
2915}
2916
2917bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2918{
2919 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2920
2921 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2922
2923 if (layoutQualifier.invocations > 0)
2924 {
2925 error(typeQualifier.line,
2926 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2927 return false;
2928 }
2929
2930 // Set mGeometryOutputPrimitiveType if exists
2931 if (layoutQualifier.primitiveType != EptUndefined)
2932 {
2933 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2934 {
2935 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2936 return false;
2937 }
2938
2939 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2940 {
2941 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2942 }
2943 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2944 {
2945 error(typeQualifier.line,
2946 "primitive doesn't match earlier output primitive declaration", "layout");
2947 return false;
2948 }
2949 }
2950
2951 // Set mGeometryMaxVertices if exists
2952 if (layoutQualifier.maxVertices > -1)
2953 {
2954 if (mGeometryShaderMaxVertices == -1)
2955 {
2956 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2957 }
2958 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2959 {
2960 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2961 "layout");
2962 return false;
2963 }
2964 }
2965
2966 return true;
2967}
2968
Martin Radev70866b82016-07-22 15:27:42 +03002969void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002970{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002971 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002972 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002973
Martin Radev70866b82016-07-22 15:27:42 +03002974 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2975 typeQualifier.line);
2976
Jamie Madillc2128ff2016-07-04 10:26:17 -04002977 // It should never be the case, but some strange parser errors can send us here.
2978 if (layoutQualifier.isEmpty())
2979 {
2980 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002981 return;
2982 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002983
Martin Radev802abe02016-08-04 17:48:32 +03002984 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002985 {
Olli Etuaho43364892017-02-13 16:00:12 +00002986 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002987 return;
2988 }
2989
Olli Etuaho43364892017-02-13 16:00:12 +00002990 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2991
2992 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002993
2994 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2995
Andrei Volykhina5527072017-03-22 16:46:30 +03002996 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2997
jchen104cdac9e2017-05-08 11:01:20 +08002998 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2999
Qin Jiajiaca68d982017-09-18 16:41:56 +08003000 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3001 typeQualifier.qualifier);
3002
Martin Radev802abe02016-08-04 17:48:32 +03003003 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003004 {
Martin Radev802abe02016-08-04 17:48:32 +03003005 if (mComputeShaderLocalSizeDeclared &&
3006 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3007 {
3008 error(typeQualifier.line, "Work group size does not match the previous declaration",
3009 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003010 return;
3011 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003012
Martin Radev802abe02016-08-04 17:48:32 +03003013 if (mShaderVersion < 310)
3014 {
3015 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003016 return;
3017 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003018
Martin Radev4c4c8e72016-08-04 12:25:34 +03003019 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003020 {
3021 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003022 return;
3023 }
3024
3025 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3026 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3027
3028 const TConstantUnion *maxComputeWorkGroupSizeData =
3029 maxComputeWorkGroupSize->getConstPointer();
3030
3031 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3032 {
3033 if (layoutQualifier.localSize[i] != -1)
3034 {
3035 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3036 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3037 if (mComputeShaderLocalSize[i] < 1 ||
3038 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3039 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003040 std::stringstream reasonStream;
3041 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3042 << maxComputeWorkGroupSizeValue;
3043 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003044
Olli Etuaho4de340a2016-12-16 09:32:03 +00003045 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003046 return;
3047 }
3048 }
3049 }
3050
3051 mComputeShaderLocalSizeDeclared = true;
3052 }
Shaob5cc1192017-07-06 10:47:20 +08003053 else if (typeQualifier.qualifier == EvqGeometryIn)
3054 {
3055 if (mShaderVersion < 310)
3056 {
3057 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3058 return;
3059 }
3060
3061 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3062 {
3063 return;
3064 }
3065 }
3066 else if (typeQualifier.qualifier == EvqGeometryOut)
3067 {
3068 if (mShaderVersion < 310)
3069 {
3070 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3071 "layout");
3072 return;
3073 }
3074
3075 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3076 {
3077 return;
3078 }
3079 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003080 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3081 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003082 {
3083 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3084 // specification.
3085 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3086 {
3087 error(typeQualifier.line, "Number of views does not match the previous declaration",
3088 "layout");
3089 return;
3090 }
3091
3092 if (layoutQualifier.numViews == -1)
3093 {
3094 error(typeQualifier.line, "No num_views specified", "layout");
3095 return;
3096 }
3097
3098 if (layoutQualifier.numViews > mMaxNumViews)
3099 {
3100 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3101 "layout");
3102 return;
3103 }
3104
3105 mNumViews = layoutQualifier.numViews;
3106 }
Martin Radev802abe02016-08-04 17:48:32 +03003107 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003108 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003109 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003110 {
Martin Radev802abe02016-08-04 17:48:32 +03003111 return;
3112 }
3113
Jiajia Qinbc585152017-06-23 15:42:17 +08003114 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003115 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003116 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003117 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003118 return;
3119 }
3120
3121 if (mShaderVersion < 300)
3122 {
3123 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3124 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003125 return;
3126 }
3127
Olli Etuaho09b04a22016-12-15 13:30:26 +00003128 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003129
3130 if (layoutQualifier.matrixPacking != EmpUnspecified)
3131 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003132 if (typeQualifier.qualifier == EvqUniform)
3133 {
3134 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3135 }
3136 else if (typeQualifier.qualifier == EvqBuffer)
3137 {
3138 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3139 }
Martin Radev802abe02016-08-04 17:48:32 +03003140 }
3141
3142 if (layoutQualifier.blockStorage != EbsUnspecified)
3143 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003144 if (typeQualifier.qualifier == EvqUniform)
3145 {
3146 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3147 }
3148 else if (typeQualifier.qualifier == EvqBuffer)
3149 {
3150 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3151 }
Martin Radev802abe02016-08-04 17:48:32 +03003152 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003153 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003154}
3155
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003156TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3157 const TFunction &function,
3158 const TSourceLoc &location,
3159 bool insertParametersToSymbolTable)
3160{
Olli Etuahobed35d72017-12-20 16:36:26 +02003161 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003162
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003163 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003164 prototype->setLine(location);
3165
3166 for (size_t i = 0; i < function.getParamCount(); i++)
3167 {
3168 const TConstParameter &param = function.getParam(i);
3169
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003170 TIntermSymbol *symbol = nullptr;
3171
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003172 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3173 // be used for unused args).
3174 if (param.name != nullptr)
3175 {
Olli Etuaho195be942017-12-04 23:40:14 +02003176 TVariable *variable =
3177 new TVariable(&symbolTable, param.name, *param.type, SymbolType::UserDefined);
3178 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003179 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003180 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003181 {
Olli Etuaho195be942017-12-04 23:40:14 +02003182 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003183 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003184 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003185 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003186 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003187 // Unsized type of a named parameter should have already been checked and sanitized.
3188 ASSERT(!param.type->isUnsizedArray());
3189 }
3190 else
3191 {
3192 if (param.type->isUnsizedArray())
3193 {
3194 error(location, "function parameter array must be sized at compile time", "[]");
3195 // We don't need to size the arrays since the parameter is unnamed and hence
3196 // inaccessible.
3197 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003198 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003199 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003200 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003201 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3202 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003203 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003204 new TVariable(&symbolTable, nullptr, *param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003205 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003206 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003207 symbol->setLine(location);
3208 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003209 }
3210 return prototype;
3211}
3212
Olli Etuaho16c745a2017-01-16 17:02:27 +00003213TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3214 const TFunction &parsedFunction,
3215 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003216{
Olli Etuaho476197f2016-10-11 13:59:08 +01003217 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3218 // first declaration. Either way the instance in the symbol table is used to track whether the
3219 // function is declared multiple times.
3220 TFunction *function = static_cast<TFunction *>(
3221 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3222 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003223 {
3224 // ESSL 1.00.17 section 4.2.7.
3225 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3226 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003227 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003228 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003229
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003230 TIntermFunctionPrototype *prototype =
3231 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003232
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003233 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003234
3235 if (!symbolTable.atGlobalLevel())
3236 {
3237 // ESSL 3.00.4 section 4.2.4.
3238 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003239 }
3240
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003241 return prototype;
3242}
3243
Olli Etuaho336b1472016-10-05 16:37:55 +01003244TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003245 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003246 TIntermBlock *functionBody,
3247 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003248{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003249 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003250 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3251 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003252 error(location, "function does not return a value:",
Olli Etuahobed35d72017-12-20 16:36:26 +02003253 functionPrototype->getFunction()->name().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003254 }
3255
Olli Etuahof51fdd22016-10-03 10:03:40 +01003256 if (functionBody == nullptr)
3257 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003258 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003259 functionBody->setLine(location);
3260 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003261 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003262 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003263 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003264
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003265 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003266 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003267}
3268
Olli Etuaho476197f2016-10-11 13:59:08 +01003269void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3270 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003271 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003272{
Olli Etuaho476197f2016-10-11 13:59:08 +01003273 ASSERT(function);
3274 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003275 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003276 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003277
3278 if (builtIn)
3279 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003280 error(location, "built-in functions cannot be redefined", (*function)->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003281 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003282 else
Jamie Madill185fb402015-06-12 15:48:48 -04003283 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003284 TFunction *prevDec = static_cast<TFunction *>(
3285 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3286
3287 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3288 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3289 // occurance.
3290 if (*function != prevDec)
3291 {
3292 // Swap the parameters of the previous declaration to the parameters of the function
3293 // definition (parameter names may differ).
3294 prevDec->swapParameters(**function);
3295
3296 // The function definition will share the same symbol as any previous declaration.
3297 *function = prevDec;
3298 }
3299
3300 if ((*function)->isDefined())
3301 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003302 error(location, "function already has a body", (*function)->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003303 }
3304
3305 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003306 }
Jamie Madill185fb402015-06-12 15:48:48 -04003307
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003308 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003309 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003310 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003311
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003312 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003313 setLoopNestingLevel(0);
3314}
3315
Jamie Madillb98c3a82015-07-23 14:26:04 -04003316TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003317{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003318 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003319 // We don't know at this point whether this is a function definition or a prototype.
3320 // The definition production code will check for redefinitions.
3321 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003322 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003323 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3324 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003325 //
3326 TFunction *prevDec =
3327 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303328
Olli Etuahod80f2942017-11-06 12:44:45 +02003329 for (size_t i = 0u; i < function->getParamCount(); ++i)
3330 {
3331 auto &param = function->getParam(i);
3332 if (param.type->isStructSpecifier())
3333 {
3334 // ESSL 3.00.6 section 12.10.
3335 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahobed35d72017-12-20 16:36:26 +02003336 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003337 }
3338 }
3339
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003340 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahobed35d72017-12-20 16:36:26 +02003341 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303342 {
Martin Radevda6254b2016-12-14 17:00:36 +02003343 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303344 // Therefore overloading or redefining builtin functions is an error.
3345 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahobed35d72017-12-20 16:36:26 +02003346 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303347 }
3348 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003349 {
3350 if (prevDec->getReturnType() != function->getReturnType())
3351 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003352 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003353 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003354 }
3355 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3356 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003357 if (prevDec->getParam(i).type->getQualifier() !=
3358 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003359 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003360 error(location,
3361 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003362 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003363 }
3364 }
3365 }
3366
3367 //
3368 // Check for previously declared variables using the same name.
3369 //
Olli Etuahobed35d72017-12-20 16:36:26 +02003370 TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003371 if (prevSym)
3372 {
3373 if (!prevSym->isFunction())
3374 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003375 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003376 }
3377 }
3378 else
3379 {
3380 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003381 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003382 }
3383
3384 // We're at the inner scope level of the function's arguments and body statement.
3385 // Add the function prototype to the surrounding scope instead.
3386 symbolTable.getOuterLevel()->insert(function);
3387
Olli Etuaho78d13742017-01-18 13:06:10 +00003388 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahobed35d72017-12-20 16:36:26 +02003389 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003390 {
3391 if (function->getParamCount() > 0)
3392 {
3393 error(location, "function cannot take any parameter(s)", "main");
3394 }
3395 if (function->getReturnType().getBasicType() != EbtVoid)
3396 {
3397 error(location, "main function cannot return a value",
3398 function->getReturnType().getBasicString());
3399 }
3400 }
3401
Jamie Madill185fb402015-06-12 15:48:48 -04003402 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003403 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3404 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003405 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3406 //
3407 return function;
3408}
3409
Olli Etuaho9de84a52016-06-14 17:36:01 +03003410TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3411 const TString *name,
3412 const TSourceLoc &location)
3413{
3414 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3415 {
3416 error(location, "no qualifiers allowed for function return",
3417 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003418 }
3419 if (!type.layoutQualifier.isEmpty())
3420 {
3421 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003422 }
jchen10cc2a10e2017-05-03 14:05:12 +08003423 // make sure an opaque type is not involved as well...
3424 std::string reason(getBasicString(type.getBasicType()));
3425 reason += "s can't be function return values";
3426 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003427 if (mShaderVersion < 300)
3428 {
3429 // Array return values are forbidden, but there's also no valid syntax for declaring array
3430 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003431 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003432
3433 if (type.isStructureContainingArrays())
3434 {
3435 // ESSL 1.00.17 section 6.1 Function Definitions
3436 error(location, "structures containing arrays can't be function return values",
3437 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003438 }
3439 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003440
3441 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003442 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003443}
3444
Olli Etuahocce89652017-06-19 16:04:09 +03003445TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3446{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003447 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003448 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3449 // really only interested in the mangled name of the function to look up the actual function
3450 // from the symbol table. If we just had the name string and the types of the parameters that
3451 // would be enough, but TFunction carries a lot of extra information in addition to that.
3452 // Besides function calls we do have to store constructor calls in the same data structure, for
3453 // them we need to store a TType.
Olli Etuaho0c371002017-12-13 17:00:25 +04003454 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved, false);
Olli Etuahocce89652017-06-19 16:04:09 +03003455}
3456
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003457TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003458{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003459 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003460 {
3461 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3462 "[]");
3463 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003464 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003465 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003466 error(publicType.getLine(), "constructor can't be a structure definition",
3467 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003468 }
3469
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003470 TType *type = new TType(publicType);
3471 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003472 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003473 error(publicType.getLine(), "cannot construct this type",
3474 getBasicString(publicType.getBasicType()));
3475 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003476 }
3477
Olli Etuaho0c371002017-12-13 17:00:25 +04003478 return new TFunction(&symbolTable, nullptr, type, SymbolType::NotResolved, true, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003479}
3480
Olli Etuaho55bde912017-10-25 13:41:13 +03003481void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3482 const char *errorMessage,
3483 const char *token,
3484 TType *arrayType)
3485{
3486 if (arrayType->isUnsizedArray())
3487 {
3488 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003489 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003490 }
3491}
3492
3493TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003494 const TString *name,
3495 const TSourceLoc &nameLoc)
3496{
Olli Etuaho55bde912017-10-25 13:41:13 +03003497 ASSERT(type);
3498 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3499 type);
3500 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003501 {
3502 error(nameLoc, "illegal use of type 'void'", name->c_str());
3503 }
3504 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003505 TParameter param = {name, type};
3506 return param;
3507}
3508
Olli Etuaho55bde912017-10-25 13:41:13 +03003509TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3510 const TString *name,
3511 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003512{
Olli Etuaho55bde912017-10-25 13:41:13 +03003513 TType *type = new TType(publicType);
3514 return parseParameterDeclarator(type, name, nameLoc);
3515}
3516
3517TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3518 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003519 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003520 const TSourceLoc &arrayLoc,
3521 TPublicType *elementType)
3522{
3523 checkArrayElementIsNotArray(arrayLoc, *elementType);
3524 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003525 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003526 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003527}
3528
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003529bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3530 TType type,
3531 const TSourceLoc &line)
3532{
3533 if (arguments->empty())
3534 {
3535 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3536 return false;
3537 }
3538 for (TIntermNode *arg : *arguments)
3539 {
3540 TIntermTyped *element = arg->getAsTyped();
3541 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003542 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3543 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003544 {
3545 error(line, "constructing from a non-dereferenced array", "constructor");
3546 return false;
3547 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003548 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003549 {
3550 if (dimensionalityFromElement == 1u)
3551 {
3552 error(line, "implicitly sized array of arrays constructor argument is not an array",
3553 "constructor");
3554 }
3555 else
3556 {
3557 error(line,
3558 "implicitly sized array of arrays constructor argument dimensionality is too "
3559 "low",
3560 "constructor");
3561 }
3562 return false;
3563 }
3564 }
3565 return true;
3566}
3567
Jamie Madillb98c3a82015-07-23 14:26:04 -04003568// This function is used to test for the correctness of the parameters passed to various constructor
3569// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003570//
Olli Etuaho856c4972016-08-08 11:38:39 +03003571// 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 +00003572//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003573TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003574 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303575 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003576{
Olli Etuaho856c4972016-08-08 11:38:39 +03003577 if (type.isUnsizedArray())
3578 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003579 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003580 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003581 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003582 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003583 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003584 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3585 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003586 if (type.getOutermostArraySize() == 0u)
3587 {
3588 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3589 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003590 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003591 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003592 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003593 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003594 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003595 }
3596 }
3597 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003598 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003599
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003600 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003601 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003602 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003603 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003604
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003605 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003606 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003607
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003608 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3609 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003610 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003611 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003612 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003613 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003614}
3615
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003616//
3617// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003618// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003619//
Olli Etuaho13389b62016-10-16 11:48:18 +01003620TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003621 const TTypeQualifierBuilder &typeQualifierBuilder,
3622 const TSourceLoc &nameLine,
3623 const TString &blockName,
3624 TFieldList *fieldList,
3625 const TString *instanceName,
3626 const TSourceLoc &instanceLine,
3627 TIntermTyped *arrayIndex,
3628 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003629{
Olli Etuaho856c4972016-08-08 11:38:39 +03003630 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003631
Olli Etuaho77ba4082016-12-16 12:01:18 +00003632 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003633
Jiajia Qinbc585152017-06-23 15:42:17 +08003634 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003635 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003636 error(typeQualifier.line,
3637 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3638 "3.10",
3639 getQualifierString(typeQualifier.qualifier));
3640 }
3641 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3642 {
3643 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003644 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003645 }
3646
Martin Radev70866b82016-07-22 15:27:42 +03003647 if (typeQualifier.invariant)
3648 {
3649 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3650 }
3651
Jiajia Qinbc585152017-06-23 15:42:17 +08003652 if (typeQualifier.qualifier != EvqBuffer)
3653 {
3654 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3655 }
Olli Etuaho43364892017-02-13 16:00:12 +00003656
jchen10af713a22017-04-19 09:10:56 +08003657 // add array index
3658 unsigned int arraySize = 0;
3659 if (arrayIndex != nullptr)
3660 {
3661 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3662 }
3663
3664 if (mShaderVersion < 310)
3665 {
3666 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3667 }
3668 else
3669 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003670 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3671 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003672 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003673
Andrei Volykhina5527072017-03-22 16:46:30 +03003674 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3675
Jamie Madill099c0f32013-06-20 11:55:52 -04003676 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003677 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003678 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3679 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003680
Jamie Madill099c0f32013-06-20 11:55:52 -04003681 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3682 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003683 if (typeQualifier.qualifier == EvqUniform)
3684 {
3685 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3686 }
3687 else if (typeQualifier.qualifier == EvqBuffer)
3688 {
3689 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3690 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003691 }
3692
Jamie Madill1566ef72013-06-20 11:55:54 -04003693 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3694 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003695 if (typeQualifier.qualifier == EvqUniform)
3696 {
3697 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3698 }
3699 else if (typeQualifier.qualifier == EvqBuffer)
3700 {
3701 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3702 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003703 }
3704
Olli Etuaho856c4972016-08-08 11:38:39 +03003705 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003706
Martin Radev2cc85b32016-08-05 16:22:53 +03003707 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3708
Jamie Madill98493dd2013-07-08 14:39:03 -04003709 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303710 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3711 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003712 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303713 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003714 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303715 {
jchen10cc2a10e2017-05-03 14:05:12 +08003716 std::string reason("unsupported type - ");
3717 reason += fieldType->getBasicString();
3718 reason += " types are not allowed in interface blocks";
3719 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003720 }
3721
Jamie Madill98493dd2013-07-08 14:39:03 -04003722 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003723 switch (qualifier)
3724 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003725 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003726 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003727 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003728 if (typeQualifier.qualifier == EvqBuffer)
3729 {
3730 error(field->line(), "invalid qualifier on shader storage block member",
3731 getQualifierString(qualifier));
3732 }
3733 break;
3734 case EvqBuffer:
3735 if (typeQualifier.qualifier == EvqUniform)
3736 {
3737 error(field->line(), "invalid qualifier on uniform block member",
3738 getQualifierString(qualifier));
3739 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003740 break;
3741 default:
3742 error(field->line(), "invalid qualifier on interface block member",
3743 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003744 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003745 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003746
Martin Radev70866b82016-07-22 15:27:42 +03003747 if (fieldType->isInvariant())
3748 {
3749 error(field->line(), "invalid qualifier on interface block member", "invariant");
3750 }
3751
Jamie Madilla5efff92013-06-06 11:56:47 -04003752 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003753 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003754 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003755 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003756
Jamie Madill98493dd2013-07-08 14:39:03 -04003757 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003758 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003759 error(field->line(), "invalid layout qualifier: cannot be used here",
3760 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003761 }
3762
Jamie Madill98493dd2013-07-08 14:39:03 -04003763 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003764 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003765 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003766 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003767 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003768 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003769 warning(field->line(),
3770 "extraneous layout qualifier: only has an effect on matrix types",
3771 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003772 }
3773
Jamie Madill98493dd2013-07-08 14:39:03 -04003774 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003775
Olli Etuahoebee5b32017-11-23 12:56:32 +02003776 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3777 typeQualifier.qualifier != EvqBuffer)
3778 {
3779 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3780 checkIsNotUnsizedArray(field->line(),
3781 "array members of interface blocks must specify a size",
3782 field->name().c_str(), field->type());
3783 }
3784
Jiajia Qinbc585152017-06-23 15:42:17 +08003785 if (typeQualifier.qualifier == EvqBuffer)
3786 {
3787 // set memory qualifiers
3788 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3789 // qualified with a memory qualifier, it is as if all of its members were declared with
3790 // the same memory qualifier.
3791 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3792 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3793 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3794 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3795 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3796 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3797 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3798 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3799 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3800 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3801 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003802 }
3803
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003804 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3805 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003806 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3807 {
3808 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3809 }
3810
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003811 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3812 if (arrayIndex != nullptr)
3813 {
3814 interfaceBlockType.makeArray(arraySize);
3815 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003816
Olli Etuaho195be942017-12-04 23:40:14 +02003817 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003818 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3819 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003820 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003821 new TVariable(&symbolTable, instanceName, interfaceBlockType,
3822 instanceName ? SymbolType::UserDefined : SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003823
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003824 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003825 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003826 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003827 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3828 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003829 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303830 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003831
3832 // set parent pointer of the field variable
3833 fieldType->setInterfaceBlock(interfaceBlock);
3834
Olli Etuaho195be942017-12-04 23:40:14 +02003835 TVariable *fieldVariable =
3836 new TVariable(&symbolTable, &field->name(), *fieldType, SymbolType::UserDefined);
3837 if (symbolTable.declareVariable(fieldVariable))
Olli Etuaho0f684632017-07-13 12:42:15 +03003838 {
3839 fieldVariable->setQualifier(typeQualifier.qualifier);
3840 }
3841 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303842 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003843 error(field->line(), "redefinition of an interface block member name",
3844 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003845 }
3846 }
3847 }
3848 else
3849 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003850 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003851
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003852 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003853 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303854 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003855 error(instanceLine, "redefinition of an interface block instance name",
3856 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003857 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003858 }
3859
Olli Etuaho195be942017-12-04 23:40:14 +02003860 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3861 blockSymbol->setLine(typeQualifier.line);
3862 TIntermDeclaration *declaration = new TIntermDeclaration();
3863 declaration->appendDeclarator(blockSymbol);
3864 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003865
3866 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003867 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003868}
3869
Olli Etuaho383b7912016-08-05 11:22:59 +03003870void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003871{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003872 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003873
3874 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003875 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303876 if (mStructNestingLevel > 1)
3877 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003878 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003879 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003880}
3881
3882void TParseContext::exitStructDeclaration()
3883{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003884 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003885}
3886
Olli Etuaho8a176262016-08-16 14:23:01 +03003887void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003888{
Jamie Madillacb4b812016-11-07 13:50:29 -05003889 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303890 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003891 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003892 }
3893
Arun Patole7e7e68d2015-05-22 12:02:25 +05303894 if (field.type()->getBasicType() != EbtStruct)
3895 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003896 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003897 }
3898
3899 // We're already inside a structure definition at this point, so add
3900 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303901 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3902 {
Jamie Madill41a49272014-03-18 16:10:13 -04003903 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003904 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3905 {
3906 // This may happen in case there are nested struct definitions. While they are also
3907 // invalid GLSL, they don't cause a syntax error.
3908 reasonStream << "Struct nesting";
3909 }
3910 else
3911 {
3912 reasonStream << "Reference of struct type "
Olli Etuahobed35d72017-12-20 16:36:26 +02003913 << field.type()->getStruct()->name().c_str();
Olli Etuahof0957992017-12-22 11:10:04 +02003914 }
3915 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003916 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003917 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003918 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003919 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003920}
3921
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003922//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003923// Parse an array index expression
3924//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003925TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3926 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303927 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003928{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003929 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3930 {
3931 if (baseExpression->getAsSymbolNode())
3932 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303933 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02003934 baseExpression->getAsSymbolNode()->getName().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003935 }
3936 else
3937 {
3938 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3939 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003940
Olli Etuaho3ec75682017-07-05 17:02:55 +03003941 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003942 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003943
Jiawei Shaod8105a02017-08-08 09:54:36 +08003944 if (baseExpression->getQualifier() == EvqPerVertexIn)
3945 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003946 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003947 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3948 {
3949 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3950 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3951 }
3952 }
3953
Jamie Madill21c1e452014-12-29 11:33:41 -05003954 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3955
Olli Etuaho36b05142015-11-12 13:10:42 +02003956 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3957 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3958 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3959 // index is a constant expression.
3960 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3961 {
3962 if (baseExpression->isInterfaceBlock())
3963 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003964 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003965 switch (baseExpression->getQualifier())
3966 {
3967 case EvqPerVertexIn:
3968 break;
3969 case EvqUniform:
3970 case EvqBuffer:
3971 error(location,
3972 "array indexes for uniform block arrays and shader storage block arrays "
3973 "must be constant integral expressions",
3974 "[");
3975 break;
3976 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003977 // We can reach here only in error cases.
3978 ASSERT(mDiagnostics->numErrors() > 0);
3979 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003980 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003981 }
3982 else if (baseExpression->getQualifier() == EvqFragmentOut)
3983 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003984 error(location,
3985 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003986 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003987 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3988 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003989 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003990 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003991 }
3992
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003993 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003994 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003995 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3996 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3997 // constant fold expressions that are not constant expressions). The most compatible way to
3998 // handle this case is to report a warning instead of an error and force the index to be in
3999 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004000 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03004001 int index = 0;
4002 if (indexConstantUnion->getBasicType() == EbtInt)
4003 {
4004 index = indexConstantUnion->getIConst(0);
4005 }
4006 else if (indexConstantUnion->getBasicType() == EbtUInt)
4007 {
4008 index = static_cast<int>(indexConstantUnion->getUConst(0));
4009 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004010
4011 int safeIndex = -1;
4012
Olli Etuahoebee5b32017-11-23 12:56:32 +02004013 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004014 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004015 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4016 safeIndex = 0;
4017 }
4018
4019 if (!baseExpression->getType().isUnsizedArray())
4020 {
4021 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004022 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004023 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004024 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004025 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4026 {
4027 outOfRangeError(outOfRangeIndexIsError, location,
4028 "array index for gl_FragData must be zero when "
4029 "GL_EXT_draw_buffers is disabled",
4030 "[]");
4031 safeIndex = 0;
4032 }
4033 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004034 }
4035 // Only do generic out-of-range check if similar error hasn't already been reported.
4036 if (safeIndex < 0)
4037 {
4038 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004039 {
4040 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4041 baseExpression->getOutermostArraySize(),
4042 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004043 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004044 else if (baseExpression->isMatrix())
4045 {
4046 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4047 baseExpression->getType().getCols(),
4048 "matrix field selection out of range");
4049 }
4050 else
4051 {
4052 ASSERT(baseExpression->isVector());
4053 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4054 baseExpression->getType().getNominalSize(),
4055 "vector field selection out of range");
4056 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004057 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004058
Olli Etuahoebee5b32017-11-23 12:56:32 +02004059 ASSERT(safeIndex >= 0);
4060 // Data of constant unions can't be changed, because it may be shared with other
4061 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4062 // sanitized object.
4063 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4064 {
4065 TConstantUnion *safeConstantUnion = new TConstantUnion();
4066 safeConstantUnion->setIConst(safeIndex);
4067 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4068 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4069 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004070
Olli Etuahoebee5b32017-11-23 12:56:32 +02004071 TIntermBinary *node =
4072 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4073 node->setLine(location);
4074 return node->fold(mDiagnostics);
4075 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004076 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004077
4078 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4079 node->setLine(location);
4080 // Indirect indexing can never be constant folded.
4081 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004082}
4083
Olli Etuahoebee5b32017-11-23 12:56:32 +02004084int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4085 const TSourceLoc &location,
4086 int index,
4087 int arraySize,
4088 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004089{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004090 // Should not reach here with an unsized / runtime-sized array.
4091 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004092 // A negative index should already have been checked.
4093 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004094 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004095 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004096 std::stringstream reasonStream;
4097 reasonStream << reason << " '" << index << "'";
4098 std::string token = reasonStream.str();
4099 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004100 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004101 }
4102 return index;
4103}
4104
Jamie Madillb98c3a82015-07-23 14:26:04 -04004105TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4106 const TSourceLoc &dotLocation,
4107 const TString &fieldString,
4108 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004109{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004110 if (baseExpression->isArray())
4111 {
4112 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004113 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004114 }
4115
4116 if (baseExpression->isVector())
4117 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004118 TVector<int> fieldOffsets;
4119 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4120 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004122 fieldOffsets.resize(1);
4123 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004125 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4126 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004127
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004128 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004129 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004130 else if (baseExpression->getBasicType() == EbtStruct)
4131 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304132 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004133 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004134 {
4135 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004136 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004137 }
4138 else
4139 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004140 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004141 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004142 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004143 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004144 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004145 {
4146 fieldFound = true;
4147 break;
4148 }
4149 }
4150 if (fieldFound)
4151 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004152 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004153 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004154 TIntermBinary *node =
4155 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4156 node->setLine(dotLocation);
4157 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004158 }
4159 else
4160 {
4161 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004162 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004163 }
4164 }
4165 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004166 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004167 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304168 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004169 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004170 {
4171 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004172 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004173 }
4174 else
4175 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004176 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004177 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004178 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004179 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004180 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004181 {
4182 fieldFound = true;
4183 break;
4184 }
4185 }
4186 if (fieldFound)
4187 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004188 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004189 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004190 TIntermBinary *node =
4191 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4192 node->setLine(dotLocation);
4193 // Indexing interface blocks can never be constant folded.
4194 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004195 }
4196 else
4197 {
4198 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004199 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004200 }
4201 }
4202 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004203 else
4204 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004205 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004206 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004207 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304208 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004209 }
4210 else
4211 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304212 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004213 " field selection requires structure, vector, or interface block on left hand "
4214 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304215 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004216 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004217 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004218 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004219}
4220
Jamie Madillb98c3a82015-07-23 14:26:04 -04004221TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4222 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004223{
Jamie Madill2f294c92017-11-20 14:47:26 -05004224 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004225
4226 if (qualifierType == "shared")
4227 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004228 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004229 {
4230 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4231 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004232 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004233 }
4234 else if (qualifierType == "packed")
4235 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004236 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004237 {
4238 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4239 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004240 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004241 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004242 else if (qualifierType == "std430")
4243 {
4244 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4245 qualifier.blockStorage = EbsStd430;
4246 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004247 else if (qualifierType == "std140")
4248 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004249 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004250 }
4251 else if (qualifierType == "row_major")
4252 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004253 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004254 }
4255 else if (qualifierType == "column_major")
4256 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004257 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004258 }
4259 else if (qualifierType == "location")
4260 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004261 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4262 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004263 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004264 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004265 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004266 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4267 {
4268 qualifier.yuv = true;
4269 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004270 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004271 else if (qualifierType == "rgba32f")
4272 {
4273 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4274 qualifier.imageInternalFormat = EiifRGBA32F;
4275 }
4276 else if (qualifierType == "rgba16f")
4277 {
4278 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4279 qualifier.imageInternalFormat = EiifRGBA16F;
4280 }
4281 else if (qualifierType == "r32f")
4282 {
4283 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4284 qualifier.imageInternalFormat = EiifR32F;
4285 }
4286 else if (qualifierType == "rgba8")
4287 {
4288 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4289 qualifier.imageInternalFormat = EiifRGBA8;
4290 }
4291 else if (qualifierType == "rgba8_snorm")
4292 {
4293 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4294 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4295 }
4296 else if (qualifierType == "rgba32i")
4297 {
4298 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4299 qualifier.imageInternalFormat = EiifRGBA32I;
4300 }
4301 else if (qualifierType == "rgba16i")
4302 {
4303 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4304 qualifier.imageInternalFormat = EiifRGBA16I;
4305 }
4306 else if (qualifierType == "rgba8i")
4307 {
4308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4309 qualifier.imageInternalFormat = EiifRGBA8I;
4310 }
4311 else if (qualifierType == "r32i")
4312 {
4313 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4314 qualifier.imageInternalFormat = EiifR32I;
4315 }
4316 else if (qualifierType == "rgba32ui")
4317 {
4318 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4319 qualifier.imageInternalFormat = EiifRGBA32UI;
4320 }
4321 else if (qualifierType == "rgba16ui")
4322 {
4323 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4324 qualifier.imageInternalFormat = EiifRGBA16UI;
4325 }
4326 else if (qualifierType == "rgba8ui")
4327 {
4328 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4329 qualifier.imageInternalFormat = EiifRGBA8UI;
4330 }
4331 else if (qualifierType == "r32ui")
4332 {
4333 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4334 qualifier.imageInternalFormat = EiifR32UI;
4335 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004336 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4337 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004338 {
4339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4340 qualifier.primitiveType = EptPoints;
4341 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004342 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4343 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004344 {
4345 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4346 qualifier.primitiveType = EptLines;
4347 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004348 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4349 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004350 {
4351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4352 qualifier.primitiveType = EptLinesAdjacency;
4353 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004354 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4355 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004356 {
4357 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4358 qualifier.primitiveType = EptTriangles;
4359 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004360 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4361 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004362 {
4363 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4364 qualifier.primitiveType = EptTrianglesAdjacency;
4365 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004366 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4367 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004368 {
4369 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4370 qualifier.primitiveType = EptLineStrip;
4371 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004372 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4373 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004374 {
4375 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4376 qualifier.primitiveType = EptTriangleStrip;
4377 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004378
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004379 else
4380 {
4381 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004382 }
4383
Jamie Madilla5efff92013-06-06 11:56:47 -04004384 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004385}
4386
Martin Radev802abe02016-08-04 17:48:32 +03004387void TParseContext::parseLocalSize(const TString &qualifierType,
4388 const TSourceLoc &qualifierTypeLine,
4389 int intValue,
4390 const TSourceLoc &intValueLine,
4391 const std::string &intValueString,
4392 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004393 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004394{
Olli Etuaho856c4972016-08-08 11:38:39 +03004395 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004396 if (intValue < 1)
4397 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004398 std::stringstream reasonStream;
4399 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4400 std::string reason = reasonStream.str();
4401 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004402 }
4403 (*localSize)[index] = intValue;
4404}
4405
Olli Etuaho09b04a22016-12-15 13:30:26 +00004406void TParseContext::parseNumViews(int intValue,
4407 const TSourceLoc &intValueLine,
4408 const std::string &intValueString,
4409 int *numViews)
4410{
4411 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4412 // specification.
4413 if (intValue < 1)
4414 {
4415 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4416 }
4417 *numViews = intValue;
4418}
4419
Shaob5cc1192017-07-06 10:47:20 +08004420void TParseContext::parseInvocations(int intValue,
4421 const TSourceLoc &intValueLine,
4422 const std::string &intValueString,
4423 int *numInvocations)
4424{
4425 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4426 // it doesn't make sense to accept invocations <= 0.
4427 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4428 {
4429 error(intValueLine,
4430 "out of range: invocations must be in the range of [1, "
4431 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4432 intValueString.c_str());
4433 }
4434 else
4435 {
4436 *numInvocations = intValue;
4437 }
4438}
4439
4440void TParseContext::parseMaxVertices(int intValue,
4441 const TSourceLoc &intValueLine,
4442 const std::string &intValueString,
4443 int *maxVertices)
4444{
4445 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4446 // it doesn't make sense to accept max_vertices < 0.
4447 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4448 {
4449 error(
4450 intValueLine,
4451 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4452 intValueString.c_str());
4453 }
4454 else
4455 {
4456 *maxVertices = intValue;
4457 }
4458}
4459
Jamie Madillb98c3a82015-07-23 14:26:04 -04004460TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4461 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004462 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304463 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004464{
Jamie Madill2f294c92017-11-20 14:47:26 -05004465 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004466
Martin Radev802abe02016-08-04 17:48:32 +03004467 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004468
Martin Radev802abe02016-08-04 17:48:32 +03004469 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004470 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004471 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004472 if (intValue < 0)
4473 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004474 error(intValueLine, "out of range: location must be non-negative",
4475 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004476 }
4477 else
4478 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004479 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004480 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004481 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004482 }
Olli Etuaho43364892017-02-13 16:00:12 +00004483 else if (qualifierType == "binding")
4484 {
4485 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4486 if (intValue < 0)
4487 {
4488 error(intValueLine, "out of range: binding must be non-negative",
4489 intValueString.c_str());
4490 }
4491 else
4492 {
4493 qualifier.binding = intValue;
4494 }
4495 }
jchen104cdac9e2017-05-08 11:01:20 +08004496 else if (qualifierType == "offset")
4497 {
4498 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4499 if (intValue < 0)
4500 {
4501 error(intValueLine, "out of range: offset must be non-negative",
4502 intValueString.c_str());
4503 }
4504 else
4505 {
4506 qualifier.offset = intValue;
4507 }
4508 }
Martin Radev802abe02016-08-04 17:48:32 +03004509 else if (qualifierType == "local_size_x")
4510 {
4511 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4512 &qualifier.localSize);
4513 }
4514 else if (qualifierType == "local_size_y")
4515 {
4516 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4517 &qualifier.localSize);
4518 }
4519 else if (qualifierType == "local_size_z")
4520 {
4521 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4522 &qualifier.localSize);
4523 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004524 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004525 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004526 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4527 {
4528 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4529 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004530 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004531 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4532 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004533 {
4534 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4535 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004536 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4537 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004538 {
4539 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4540 }
4541
Martin Radev802abe02016-08-04 17:48:32 +03004542 else
4543 {
4544 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004545 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004546
Jamie Madilla5efff92013-06-06 11:56:47 -04004547 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004548}
4549
Olli Etuaho613b9592016-09-05 12:05:53 +03004550TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4551{
4552 return new TTypeQualifierBuilder(
4553 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4554 mShaderVersion);
4555}
4556
Olli Etuahocce89652017-06-19 16:04:09 +03004557TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4558 const TSourceLoc &loc)
4559{
4560 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4561 return new TStorageQualifierWrapper(qualifier, loc);
4562}
4563
4564TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4565{
4566 if (getShaderType() == GL_VERTEX_SHADER)
4567 {
4568 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4569 }
4570 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4571}
4572
4573TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4574{
4575 if (declaringFunction())
4576 {
4577 return new TStorageQualifierWrapper(EvqIn, loc);
4578 }
Shaob5cc1192017-07-06 10:47:20 +08004579
4580 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004581 {
Shaob5cc1192017-07-06 10:47:20 +08004582 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004583 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004584 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004585 {
4586 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4587 }
4588 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004589 }
Shaob5cc1192017-07-06 10:47:20 +08004590 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004591 {
Shaob5cc1192017-07-06 10:47:20 +08004592 if (mShaderVersion < 300)
4593 {
4594 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4595 }
4596 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004597 }
Shaob5cc1192017-07-06 10:47:20 +08004598 case GL_COMPUTE_SHADER:
4599 {
4600 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4601 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004602 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004603 {
4604 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4605 }
4606 default:
4607 {
4608 UNREACHABLE();
4609 return new TStorageQualifierWrapper(EvqLast, loc);
4610 }
Olli Etuahocce89652017-06-19 16:04:09 +03004611 }
Olli Etuahocce89652017-06-19 16:04:09 +03004612}
4613
4614TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4615{
4616 if (declaringFunction())
4617 {
4618 return new TStorageQualifierWrapper(EvqOut, loc);
4619 }
Shaob5cc1192017-07-06 10:47:20 +08004620 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004621 {
Shaob5cc1192017-07-06 10:47:20 +08004622 case GL_VERTEX_SHADER:
4623 {
4624 if (mShaderVersion < 300)
4625 {
4626 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4627 }
4628 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4629 }
4630 case GL_FRAGMENT_SHADER:
4631 {
4632 if (mShaderVersion < 300)
4633 {
4634 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4635 }
4636 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4637 }
4638 case GL_COMPUTE_SHADER:
4639 {
4640 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4641 return new TStorageQualifierWrapper(EvqLast, loc);
4642 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004643 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004644 {
4645 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4646 }
4647 default:
4648 {
4649 UNREACHABLE();
4650 return new TStorageQualifierWrapper(EvqLast, loc);
4651 }
Olli Etuahocce89652017-06-19 16:04:09 +03004652 }
Olli Etuahocce89652017-06-19 16:04:09 +03004653}
4654
4655TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4656{
4657 if (!declaringFunction())
4658 {
4659 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4660 }
4661 return new TStorageQualifierWrapper(EvqInOut, loc);
4662}
4663
Jamie Madillb98c3a82015-07-23 14:26:04 -04004664TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004665 TLayoutQualifier rightQualifier,
4666 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004667{
Martin Radevc28888b2016-07-22 15:27:42 +03004668 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004669 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004670}
4671
Olli Etuahod5f44c92017-11-29 17:15:40 +02004672TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004673{
4674 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004675 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004676}
4677
Olli Etuahod5f44c92017-11-29 17:15:40 +02004678TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4679 const TSourceLoc &loc,
4680 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004681{
4682 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004683 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004684}
4685
Olli Etuaho722bfb52017-10-26 17:00:11 +03004686void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4687 const TFieldList::const_iterator end,
4688 const TString &name,
4689 const TSourceLoc &location)
4690{
4691 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4692 {
4693 if ((*fieldIter)->name() == name)
4694 {
4695 error(location, "duplicate field name in structure", name.c_str());
4696 }
4697 }
4698}
4699
4700TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4701{
4702 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4703 ++fieldIter)
4704 {
4705 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4706 location);
4707 }
4708 return fields;
4709}
4710
Olli Etuaho4de340a2016-12-16 09:32:03 +00004711TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4712 const TFieldList *newlyAddedFields,
4713 const TSourceLoc &location)
4714{
4715 for (TField *field : *newlyAddedFields)
4716 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004717 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4718 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004719 processedFields->push_back(field);
4720 }
4721 return processedFields;
4722}
4723
Martin Radev70866b82016-07-22 15:27:42 +03004724TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4725 const TTypeQualifierBuilder &typeQualifierBuilder,
4726 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004727 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004728{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004729 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004730
Martin Radev70866b82016-07-22 15:27:42 +03004731 typeSpecifier->qualifier = typeQualifier.qualifier;
4732 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004733 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004734 typeSpecifier->invariant = typeQualifier.invariant;
4735 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304736 {
Martin Radev70866b82016-07-22 15:27:42 +03004737 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004738 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004739 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004740}
4741
Jamie Madillb98c3a82015-07-23 14:26:04 -04004742TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004743 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004744{
Martin Radev4a9cd802016-09-01 16:51:51 +03004745 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4746 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004747
Olli Etuahod5f44c92017-11-29 17:15:40 +02004748 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004749 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004750
Martin Radev4a9cd802016-09-01 16:51:51 +03004751 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004752
Olli Etuahod5f44c92017-11-29 17:15:40 +02004753 TFieldList *fieldList = new TFieldList();
4754
4755 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304756 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004757 TType *type = new TType(typeSpecifier);
4758 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304759 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004760 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004761 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004762 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004763 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004764
Olli Etuahod5f44c92017-11-29 17:15:40 +02004765 TField *field = new TField(type, declarator->name(), declarator->line());
4766 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4767 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004768 }
4769
Olli Etuahod5f44c92017-11-29 17:15:40 +02004770 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004771}
4772
Martin Radev4a9cd802016-09-01 16:51:51 +03004773TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4774 const TSourceLoc &nameLine,
4775 const TString *structName,
4776 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004777{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004778 SymbolType structSymbolType = SymbolType::UserDefined;
4779 if (structName == nullptr)
4780 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004781 structSymbolType = SymbolType::Empty;
4782 }
4783 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004784
Jamie Madill9b820842015-02-12 10:40:10 -05004785 // Store a bool in the struct if we're at global scope, to allow us to
4786 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004787 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004788
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004789 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004790 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004791 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004792 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304793 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004794 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004795 }
4796 }
4797
4798 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004799 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004800 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004801 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004802 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004803 switch (qualifier)
4804 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004805 case EvqGlobal:
4806 case EvqTemporary:
4807 break;
4808 default:
4809 error(field.line(), "invalid qualifier on struct member",
4810 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004811 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004812 }
Martin Radev70866b82016-07-22 15:27:42 +03004813 if (field.type()->isInvariant())
4814 {
4815 error(field.line(), "invalid qualifier on struct member", "invariant");
4816 }
jchen104cdac9e2017-05-08 11:01:20 +08004817 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4818 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004819 {
4820 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4821 }
4822
Olli Etuahoebee5b32017-11-23 12:56:32 +02004823 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4824 field.name().c_str(), field.type());
4825
Olli Etuaho43364892017-02-13 16:00:12 +00004826 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4827
4828 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004829
4830 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004831 }
4832
Martin Radev4a9cd802016-09-01 16:51:51 +03004833 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004834 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004835 exitStructDeclaration();
4836
Martin Radev4a9cd802016-09-01 16:51:51 +03004837 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004838}
4839
Jamie Madillb98c3a82015-07-23 14:26:04 -04004840TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004841 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004842 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004843{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004844 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004845 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004846 init->isVector())
4847 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004848 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4849 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004850 return nullptr;
4851 }
4852
Olli Etuaho923ecef2017-10-11 12:01:38 +03004853 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004854 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004855 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004856 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004857 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004858 }
4859
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004860 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4861 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004862 return node;
4863}
4864
4865TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4866{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004867 if (mSwitchNestingLevel == 0)
4868 {
4869 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004870 return nullptr;
4871 }
4872 if (condition == nullptr)
4873 {
4874 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004875 return nullptr;
4876 }
4877 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004878 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004879 {
4880 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004881 }
4882 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004883 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4884 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4885 // fold in case labels.
4886 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004887 {
4888 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004889 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004890 TIntermCase *node = new TIntermCase(condition);
4891 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004892 return node;
4893}
4894
4895TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4896{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004897 if (mSwitchNestingLevel == 0)
4898 {
4899 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004900 return nullptr;
4901 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004902 TIntermCase *node = new TIntermCase(nullptr);
4903 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004904 return node;
4905}
4906
Jamie Madillb98c3a82015-07-23 14:26:04 -04004907TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4908 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004909 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004910{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004911 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004912
4913 switch (op)
4914 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004915 case EOpLogicalNot:
4916 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4917 child->isVector())
4918 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004919 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004920 return nullptr;
4921 }
4922 break;
4923 case EOpBitwiseNot:
4924 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4925 child->isMatrix() || child->isArray())
4926 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004927 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004928 return nullptr;
4929 }
4930 break;
4931 case EOpPostIncrement:
4932 case EOpPreIncrement:
4933 case EOpPostDecrement:
4934 case EOpPreDecrement:
4935 case EOpNegative:
4936 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004937 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4938 child->getBasicType() == EbtBool || child->isArray() ||
4939 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004940 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004941 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004942 return nullptr;
4943 }
4944 // Operators for built-ins are already type checked against their prototype.
4945 default:
4946 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004947 }
4948
Jiajia Qinbc585152017-06-23 15:42:17 +08004949 if (child->getMemoryQualifier().writeonly)
4950 {
4951 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4952 return nullptr;
4953 }
4954
Olli Etuahof119a262016-08-19 15:54:22 +03004955 TIntermUnary *node = new TIntermUnary(op, child);
4956 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004957
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004958 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004959}
4960
Olli Etuaho09b22472015-02-11 11:47:26 +02004961TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4962{
Olli Etuahocce89652017-06-19 16:04:09 +03004963 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004964 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004965 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004966 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004967 return child;
4968 }
4969 return node;
4970}
4971
Jamie Madillb98c3a82015-07-23 14:26:04 -04004972TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4973 TIntermTyped *child,
4974 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004975{
Olli Etuaho856c4972016-08-08 11:38:39 +03004976 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004977 return addUnaryMath(op, child, loc);
4978}
4979
Jamie Madillb98c3a82015-07-23 14:26:04 -04004980bool TParseContext::binaryOpCommonCheck(TOperator op,
4981 TIntermTyped *left,
4982 TIntermTyped *right,
4983 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004984{
jchen10b4cf5652017-05-05 18:51:17 +08004985 // Check opaque types are not allowed to be operands in expressions other than array indexing
4986 // and structure member selection.
4987 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4988 {
4989 switch (op)
4990 {
4991 case EOpIndexDirect:
4992 case EOpIndexIndirect:
4993 break;
4994 case EOpIndexDirectStruct:
4995 UNREACHABLE();
4996
4997 default:
4998 error(loc, "Invalid operation for variables with an opaque type",
4999 GetOperatorString(op));
5000 return false;
5001 }
5002 }
jchen10cc2a10e2017-05-03 14:05:12 +08005003
Jiajia Qinbc585152017-06-23 15:42:17 +08005004 if (right->getMemoryQualifier().writeonly)
5005 {
5006 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5007 return false;
5008 }
5009
5010 if (left->getMemoryQualifier().writeonly)
5011 {
5012 switch (op)
5013 {
5014 case EOpAssign:
5015 case EOpInitialize:
5016 case EOpIndexDirect:
5017 case EOpIndexIndirect:
5018 case EOpIndexDirectStruct:
5019 case EOpIndexDirectInterfaceBlock:
5020 break;
5021 default:
5022 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5023 return false;
5024 }
5025 }
5026
Olli Etuaho244be012016-08-18 15:26:02 +03005027 if (left->getType().getStruct() || right->getType().getStruct())
5028 {
5029 switch (op)
5030 {
5031 case EOpIndexDirectStruct:
5032 ASSERT(left->getType().getStruct());
5033 break;
5034 case EOpEqual:
5035 case EOpNotEqual:
5036 case EOpAssign:
5037 case EOpInitialize:
5038 if (left->getType() != right->getType())
5039 {
5040 return false;
5041 }
5042 break;
5043 default:
5044 error(loc, "Invalid operation for structs", GetOperatorString(op));
5045 return false;
5046 }
5047 }
5048
Olli Etuaho94050052017-05-08 14:17:44 +03005049 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5050 {
5051 switch (op)
5052 {
5053 case EOpIndexDirectInterfaceBlock:
5054 ASSERT(left->getType().getInterfaceBlock());
5055 break;
5056 default:
5057 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5058 return false;
5059 }
5060 }
5061
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005062 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005063 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005064 error(loc, "array / non-array mismatch", GetOperatorString(op));
5065 return false;
5066 }
5067
5068 if (left->isArray())
5069 {
5070 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005071 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005072 {
5073 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5074 return false;
5075 }
5076
Olli Etuahoe79904c2015-03-18 16:56:42 +02005077 switch (op)
5078 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005079 case EOpEqual:
5080 case EOpNotEqual:
5081 case EOpAssign:
5082 case EOpInitialize:
5083 break;
5084 default:
5085 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5086 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005087 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005088 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005089 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005090 {
5091 error(loc, "array size mismatch", GetOperatorString(op));
5092 return false;
5093 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005094 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005095
5096 // Check ops which require integer / ivec parameters
5097 bool isBitShift = false;
5098 switch (op)
5099 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005100 case EOpBitShiftLeft:
5101 case EOpBitShiftRight:
5102 case EOpBitShiftLeftAssign:
5103 case EOpBitShiftRightAssign:
5104 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5105 // check that the basic type is an integer type.
5106 isBitShift = true;
5107 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5108 {
5109 return false;
5110 }
5111 break;
5112 case EOpBitwiseAnd:
5113 case EOpBitwiseXor:
5114 case EOpBitwiseOr:
5115 case EOpBitwiseAndAssign:
5116 case EOpBitwiseXorAssign:
5117 case EOpBitwiseOrAssign:
5118 // It is enough to check the type of only one operand, since later it
5119 // is checked that the operand types match.
5120 if (!IsInteger(left->getBasicType()))
5121 {
5122 return false;
5123 }
5124 break;
5125 default:
5126 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005127 }
5128
5129 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5130 // So the basic type should usually match.
5131 if (!isBitShift && left->getBasicType() != right->getBasicType())
5132 {
5133 return false;
5134 }
5135
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005136 // Check that:
5137 // 1. Type sizes match exactly on ops that require that.
5138 // 2. Restrictions for structs that contain arrays or samplers are respected.
5139 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005140 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005141 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005142 case EOpAssign:
5143 case EOpInitialize:
5144 case EOpEqual:
5145 case EOpNotEqual:
5146 // ESSL 1.00 sections 5.7, 5.8, 5.9
5147 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5148 {
5149 error(loc, "undefined operation for structs containing arrays",
5150 GetOperatorString(op));
5151 return false;
5152 }
5153 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5154 // we interpret the spec so that this extends to structs containing samplers,
5155 // similarly to ESSL 1.00 spec.
5156 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5157 left->getType().isStructureContainingSamplers())
5158 {
5159 error(loc, "undefined operation for structs containing samplers",
5160 GetOperatorString(op));
5161 return false;
5162 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005163
Olli Etuahoe1805592017-01-02 16:41:20 +00005164 if ((left->getNominalSize() != right->getNominalSize()) ||
5165 (left->getSecondarySize() != right->getSecondarySize()))
5166 {
5167 error(loc, "dimension mismatch", GetOperatorString(op));
5168 return false;
5169 }
5170 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005171 case EOpLessThan:
5172 case EOpGreaterThan:
5173 case EOpLessThanEqual:
5174 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005175 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005176 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005177 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005178 return false;
5179 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005180 break;
5181 case EOpAdd:
5182 case EOpSub:
5183 case EOpDiv:
5184 case EOpIMod:
5185 case EOpBitShiftLeft:
5186 case EOpBitShiftRight:
5187 case EOpBitwiseAnd:
5188 case EOpBitwiseXor:
5189 case EOpBitwiseOr:
5190 case EOpAddAssign:
5191 case EOpSubAssign:
5192 case EOpDivAssign:
5193 case EOpIModAssign:
5194 case EOpBitShiftLeftAssign:
5195 case EOpBitShiftRightAssign:
5196 case EOpBitwiseAndAssign:
5197 case EOpBitwiseXorAssign:
5198 case EOpBitwiseOrAssign:
5199 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5200 {
5201 return false;
5202 }
5203
5204 // Are the sizes compatible?
5205 if (left->getNominalSize() != right->getNominalSize() ||
5206 left->getSecondarySize() != right->getSecondarySize())
5207 {
5208 // If the nominal sizes of operands do not match:
5209 // One of them must be a scalar.
5210 if (!left->isScalar() && !right->isScalar())
5211 return false;
5212
5213 // In the case of compound assignment other than multiply-assign,
5214 // the right side needs to be a scalar. Otherwise a vector/matrix
5215 // would be assigned to a scalar. A scalar can't be shifted by a
5216 // vector either.
5217 if (!right->isScalar() &&
5218 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5219 return false;
5220 }
5221 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005222 default:
5223 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005224 }
5225
Olli Etuahod6b14282015-03-17 14:31:35 +02005226 return true;
5227}
5228
Olli Etuaho1dded802016-08-18 18:13:13 +03005229bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5230 const TType &left,
5231 const TType &right)
5232{
5233 switch (op)
5234 {
5235 case EOpMul:
5236 case EOpMulAssign:
5237 return left.getNominalSize() == right.getNominalSize() &&
5238 left.getSecondarySize() == right.getSecondarySize();
5239 case EOpVectorTimesScalar:
5240 return true;
5241 case EOpVectorTimesScalarAssign:
5242 ASSERT(!left.isMatrix() && !right.isMatrix());
5243 return left.isVector() && !right.isVector();
5244 case EOpVectorTimesMatrix:
5245 return left.getNominalSize() == right.getRows();
5246 case EOpVectorTimesMatrixAssign:
5247 ASSERT(!left.isMatrix() && right.isMatrix());
5248 return left.isVector() && left.getNominalSize() == right.getRows() &&
5249 left.getNominalSize() == right.getCols();
5250 case EOpMatrixTimesVector:
5251 return left.getCols() == right.getNominalSize();
5252 case EOpMatrixTimesScalar:
5253 return true;
5254 case EOpMatrixTimesScalarAssign:
5255 ASSERT(left.isMatrix() && !right.isMatrix());
5256 return !right.isVector();
5257 case EOpMatrixTimesMatrix:
5258 return left.getCols() == right.getRows();
5259 case EOpMatrixTimesMatrixAssign:
5260 ASSERT(left.isMatrix() && right.isMatrix());
5261 // We need to check two things:
5262 // 1. The matrix multiplication step is valid.
5263 // 2. The result will have the same number of columns as the lvalue.
5264 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5265
5266 default:
5267 UNREACHABLE();
5268 return false;
5269 }
5270}
5271
Jamie Madillb98c3a82015-07-23 14:26:04 -04005272TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5273 TIntermTyped *left,
5274 TIntermTyped *right,
5275 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005276{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005277 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005278 return nullptr;
5279
Olli Etuahofc1806e2015-03-17 13:03:11 +02005280 switch (op)
5281 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005282 case EOpEqual:
5283 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005284 case EOpLessThan:
5285 case EOpGreaterThan:
5286 case EOpLessThanEqual:
5287 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005288 break;
5289 case EOpLogicalOr:
5290 case EOpLogicalXor:
5291 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005292 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5293 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005294 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005295 {
5296 return nullptr;
5297 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005298 // Basic types matching should have been already checked.
5299 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005300 break;
5301 case EOpAdd:
5302 case EOpSub:
5303 case EOpDiv:
5304 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005305 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5306 !right->getType().getStruct());
5307 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005308 {
5309 return nullptr;
5310 }
5311 break;
5312 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005313 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5314 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005315 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005316 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005317 {
5318 return nullptr;
5319 }
5320 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005321 default:
5322 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005323 }
5324
Olli Etuaho1dded802016-08-18 18:13:13 +03005325 if (op == EOpMul)
5326 {
5327 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5328 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5329 {
5330 return nullptr;
5331 }
5332 }
5333
Olli Etuaho3fdec912016-08-18 15:08:06 +03005334 TIntermBinary *node = new TIntermBinary(op, left, right);
5335 node->setLine(loc);
5336
Olli Etuaho3fdec912016-08-18 15:08:06 +03005337 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005338 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005339}
5340
Jamie Madillb98c3a82015-07-23 14:26:04 -04005341TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5342 TIntermTyped *left,
5343 TIntermTyped *right,
5344 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005345{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005346 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005347 if (node == 0)
5348 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005349 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5350 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005351 return left;
5352 }
5353 return node;
5354}
5355
Jamie Madillb98c3a82015-07-23 14:26:04 -04005356TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5357 TIntermTyped *left,
5358 TIntermTyped *right,
5359 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005360{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005361 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005362 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005363 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005364 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5365 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005366 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005367 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005368 }
5369 return node;
5370}
5371
Olli Etuaho13389b62016-10-16 11:48:18 +01005372TIntermBinary *TParseContext::createAssign(TOperator op,
5373 TIntermTyped *left,
5374 TIntermTyped *right,
5375 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005376{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005377 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005378 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005379 if (op == EOpMulAssign)
5380 {
5381 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5382 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5383 {
5384 return nullptr;
5385 }
5386 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005387 TIntermBinary *node = new TIntermBinary(op, left, right);
5388 node->setLine(loc);
5389
Olli Etuaho3fdec912016-08-18 15:08:06 +03005390 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005391 }
5392 return nullptr;
5393}
5394
Jamie Madillb98c3a82015-07-23 14:26:04 -04005395TIntermTyped *TParseContext::addAssign(TOperator op,
5396 TIntermTyped *left,
5397 TIntermTyped *right,
5398 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005399{
Olli Etuahocce89652017-06-19 16:04:09 +03005400 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005401 TIntermTyped *node = createAssign(op, left, right, loc);
5402 if (node == nullptr)
5403 {
5404 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005405 return left;
5406 }
5407 return node;
5408}
5409
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005410TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5411 TIntermTyped *right,
5412 const TSourceLoc &loc)
5413{
Corentin Wallez0d959252016-07-12 17:26:32 -04005414 // WebGL2 section 5.26, the following results in an error:
5415 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005416 if (mShaderSpec == SH_WEBGL2_SPEC &&
5417 (left->isArray() || left->getBasicType() == EbtVoid ||
5418 left->getType().isStructureContainingArrays() || right->isArray() ||
5419 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005420 {
5421 error(loc,
5422 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5423 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005424 }
5425
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005426 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5427 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5428 commaNode->getTypePointer()->setQualifier(resultQualifier);
5429 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005430}
5431
Olli Etuaho49300862015-02-20 14:54:49 +02005432TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5433{
5434 switch (op)
5435 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005436 case EOpContinue:
5437 if (mLoopNestingLevel <= 0)
5438 {
5439 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005440 }
5441 break;
5442 case EOpBreak:
5443 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5444 {
5445 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005446 }
5447 break;
5448 case EOpReturn:
5449 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5450 {
5451 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005452 }
5453 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005454 case EOpKill:
5455 if (mShaderType != GL_FRAGMENT_SHADER)
5456 {
5457 error(loc, "discard supported in fragment shaders only", "discard");
5458 }
5459 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005460 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005461 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005462 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005463 }
Olli Etuahocce89652017-06-19 16:04:09 +03005464 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005465}
5466
Jamie Madillb98c3a82015-07-23 14:26:04 -04005467TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005468 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005469 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005470{
Olli Etuahocce89652017-06-19 16:04:09 +03005471 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005472 {
Olli Etuahocce89652017-06-19 16:04:09 +03005473 ASSERT(op == EOpReturn);
5474 mFunctionReturnsValue = true;
5475 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5476 {
5477 error(loc, "void function cannot return a value", "return");
5478 }
5479 else if (*mCurrentFunctionType != expression->getType())
5480 {
5481 error(loc, "function return is not matching type:", "return");
5482 }
Olli Etuaho49300862015-02-20 14:54:49 +02005483 }
Olli Etuahocce89652017-06-19 16:04:09 +03005484 TIntermBranch *node = new TIntermBranch(op, expression);
5485 node->setLine(loc);
5486 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005487}
5488
Martin Radev84aa2dc2017-09-11 15:51:02 +03005489void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5490{
5491 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005492 const TString &name = functionCall->getFunction()->name();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005493 bool isTextureGather = (name == "textureGather");
5494 bool isTextureGatherOffset = (name == "textureGatherOffset");
5495 if (isTextureGather || isTextureGatherOffset)
5496 {
5497 TIntermNode *componentNode = nullptr;
5498 TIntermSequence *arguments = functionCall->getSequence();
5499 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5500 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5501 ASSERT(sampler != nullptr);
5502 switch (sampler->getBasicType())
5503 {
5504 case EbtSampler2D:
5505 case EbtISampler2D:
5506 case EbtUSampler2D:
5507 case EbtSampler2DArray:
5508 case EbtISampler2DArray:
5509 case EbtUSampler2DArray:
5510 if ((isTextureGather && arguments->size() == 3u) ||
5511 (isTextureGatherOffset && arguments->size() == 4u))
5512 {
5513 componentNode = arguments->back();
5514 }
5515 break;
5516 case EbtSamplerCube:
5517 case EbtISamplerCube:
5518 case EbtUSamplerCube:
5519 ASSERT(!isTextureGatherOffset);
5520 if (arguments->size() == 3u)
5521 {
5522 componentNode = arguments->back();
5523 }
5524 break;
5525 case EbtSampler2DShadow:
5526 case EbtSampler2DArrayShadow:
5527 case EbtSamplerCubeShadow:
5528 break;
5529 default:
5530 UNREACHABLE();
5531 break;
5532 }
5533 if (componentNode)
5534 {
5535 const TIntermConstantUnion *componentConstantUnion =
5536 componentNode->getAsConstantUnion();
5537 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5538 {
5539 error(functionCall->getLine(), "Texture component must be a constant expression",
5540 name.c_str());
5541 }
5542 else
5543 {
5544 int component = componentConstantUnion->getIConst(0);
5545 if (component < 0 || component > 3)
5546 {
5547 error(functionCall->getLine(), "Component must be in the range [0;3]",
5548 name.c_str());
5549 }
5550 }
5551 }
5552 }
5553}
5554
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005555void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5556{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005557 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005558 const TString &name = functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005559 TIntermNode *offset = nullptr;
5560 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005561 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005562 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5563 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5564 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005565 {
5566 offset = arguments->back();
5567 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005568 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005569 {
5570 // A bias parameter might follow the offset parameter.
5571 ASSERT(arguments->size() >= 3);
5572 offset = (*arguments)[2];
5573 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005574 else if (name == "textureGatherOffset")
5575 {
5576 ASSERT(arguments->size() >= 3u);
5577 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5578 ASSERT(sampler != nullptr);
5579 switch (sampler->getBasicType())
5580 {
5581 case EbtSampler2D:
5582 case EbtISampler2D:
5583 case EbtUSampler2D:
5584 case EbtSampler2DArray:
5585 case EbtISampler2DArray:
5586 case EbtUSampler2DArray:
5587 offset = (*arguments)[2];
5588 break;
5589 case EbtSampler2DShadow:
5590 case EbtSampler2DArrayShadow:
5591 offset = (*arguments)[3];
5592 break;
5593 default:
5594 UNREACHABLE();
5595 break;
5596 }
5597 useTextureGatherOffsetConstraints = true;
5598 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005599 if (offset != nullptr)
5600 {
5601 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5602 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5603 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005604 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005605 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005606 }
5607 else
5608 {
5609 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5610 size_t size = offsetConstantUnion->getType().getObjectSize();
5611 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005612 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5613 : mMinProgramTexelOffset;
5614 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5615 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005616 for (size_t i = 0u; i < size; ++i)
5617 {
5618 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005619 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005620 {
5621 std::stringstream tokenStream;
5622 tokenStream << offsetValue;
5623 std::string token = tokenStream.str();
5624 error(offset->getLine(), "Texture offset value out of valid range",
5625 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005626 }
5627 }
5628 }
5629 }
5630}
5631
Jiajia Qina3106c52017-11-03 09:39:39 +08005632void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5633{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005634 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005635 const TString &functionName = functionCall->getFunction()->name();
5636 if (IsAtomicBuiltin(functionName))
Jiajia Qina3106c52017-11-03 09:39:39 +08005637 {
5638 TIntermSequence *arguments = functionCall->getSequence();
5639 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5640
5641 if (IsBufferOrSharedVariable(memNode))
5642 {
5643 return;
5644 }
5645
5646 while (memNode->getAsBinaryNode())
5647 {
5648 memNode = memNode->getAsBinaryNode()->getLeft();
5649 if (IsBufferOrSharedVariable(memNode))
5650 {
5651 return;
5652 }
5653 }
5654
5655 error(memNode->getLine(),
5656 "The value passed to the mem argument of an atomic memory function does not "
5657 "correspond to a buffer or shared variable.",
Olli Etuahobed35d72017-12-20 16:36:26 +02005658 functionName.c_str());
Jiajia Qina3106c52017-11-03 09:39:39 +08005659 }
5660}
5661
Martin Radev2cc85b32016-08-05 16:22:53 +03005662// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5663void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5664{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005665 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005666 const TString &name = functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005667
5668 if (name.compare(0, 5, "image") == 0)
5669 {
5670 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005671 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005672
Olli Etuaho485eefd2017-02-14 17:40:06 +00005673 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005674
5675 if (name.compare(5, 5, "Store") == 0)
5676 {
5677 if (memoryQualifier.readonly)
5678 {
5679 error(imageNode->getLine(),
5680 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005681 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005682 }
5683 }
5684 else if (name.compare(5, 4, "Load") == 0)
5685 {
5686 if (memoryQualifier.writeonly)
5687 {
5688 error(imageNode->getLine(),
5689 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005690 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005691 }
5692 }
5693 }
5694}
5695
5696// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5697void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5698 const TFunction *functionDefinition,
5699 const TIntermAggregate *functionCall)
5700{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005701 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005702
5703 const TIntermSequence &arguments = *functionCall->getSequence();
5704
5705 ASSERT(functionDefinition->getParamCount() == arguments.size());
5706
5707 for (size_t i = 0; i < arguments.size(); ++i)
5708 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005709 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5710 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005711 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5712 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5713
5714 if (IsImage(functionArgumentType.getBasicType()))
5715 {
5716 const TMemoryQualifier &functionArgumentMemoryQualifier =
5717 functionArgumentType.getMemoryQualifier();
5718 const TMemoryQualifier &functionParameterMemoryQualifier =
5719 functionParameterType.getMemoryQualifier();
5720 if (functionArgumentMemoryQualifier.readonly &&
5721 !functionParameterMemoryQualifier.readonly)
5722 {
5723 error(functionCall->getLine(),
5724 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005725 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005726 }
5727
5728 if (functionArgumentMemoryQualifier.writeonly &&
5729 !functionParameterMemoryQualifier.writeonly)
5730 {
5731 error(functionCall->getLine(),
5732 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005733 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005734 }
Martin Radev049edfa2016-11-11 14:35:37 +02005735
5736 if (functionArgumentMemoryQualifier.coherent &&
5737 !functionParameterMemoryQualifier.coherent)
5738 {
5739 error(functionCall->getLine(),
5740 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005741 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005742 }
5743
5744 if (functionArgumentMemoryQualifier.volatileQualifier &&
5745 !functionParameterMemoryQualifier.volatileQualifier)
5746 {
5747 error(functionCall->getLine(),
5748 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005749 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005750 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005751 }
5752 }
5753}
5754
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005755TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005756{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005757 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005758}
5759
5760TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005761 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005762 TIntermNode *thisNode,
5763 const TSourceLoc &loc)
5764{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005765 if (thisNode != nullptr)
5766 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005767 return addMethod(fnCall->name(), arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005768 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005769
5770 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005771 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005772 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005773 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005774 }
5775 else
5776 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005777 ASSERT(op == EOpNull);
Olli Etuaho0c371002017-12-13 17:00:25 +04005778 return addNonConstructorFunctionCall(fnCall->name(), arguments, loc);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005779 }
5780}
5781
Olli Etuahobed35d72017-12-20 16:36:26 +02005782TIntermTyped *TParseContext::addMethod(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005783 TIntermSequence *arguments,
5784 TIntermNode *thisNode,
5785 const TSourceLoc &loc)
5786{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005787 TIntermTyped *typedThis = thisNode->getAsTyped();
5788 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5789 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5790 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005791 // So accessing fnCall->name() below is safe.
Olli Etuahobed35d72017-12-20 16:36:26 +02005792 if (name != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005793 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005794 error(loc, "invalid method", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005795 }
5796 else if (!arguments->empty())
5797 {
5798 error(loc, "method takes no parameters", "length");
5799 }
5800 else if (typedThis == nullptr || !typedThis->isArray())
5801 {
5802 error(loc, "length can only be called on arrays", "length");
5803 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005804 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5805 mGeometryShaderInputPrimitiveType == EptUndefined)
5806 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005807 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005808 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5809 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005810 else
5811 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005812 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5813 node->setLine(loc);
5814 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005815 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005816 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005817}
5818
Olli Etuahobed35d72017-12-20 16:36:26 +02005819TIntermTyped *TParseContext::addNonConstructorFunctionCall(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005820 TIntermSequence *arguments,
5821 const TSourceLoc &loc)
5822{
5823 // First find by unmangled name to check whether the function name has been
5824 // hidden by a variable name or struct typename.
5825 // If a function is found, check for one with a matching argument list.
5826 bool builtIn;
Olli Etuahobed35d72017-12-20 16:36:26 +02005827 const TSymbol *symbol = symbolTable.find(name, mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005828 if (symbol != nullptr && !symbol->isFunction())
5829 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005830 error(loc, "function name expected", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005831 }
5832 else
5833 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005834 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(name, *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005835 mShaderVersion, &builtIn);
5836 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005837 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005838 error(loc, "no matching overloaded function found", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005839 }
5840 else
5841 {
5842 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005843 //
5844 // A declared function.
5845 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005846 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005847 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005848 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005849 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005850 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005851 if (builtIn && op != EOpNull)
5852 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005853 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005854 if (fnCandidate->getParamCount() == 1)
5855 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005856 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005857 TIntermNode *unaryParamNode = arguments->front();
5858 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005859 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005860 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005861 }
5862 else
5863 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005864 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005865 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005866 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005867
5868 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005869 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305870
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005871 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305872 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005873 // See if we can constant fold a built-in. Note that this may be possible
5874 // even if it is not const-qualified.
5875 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305876 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005877 else
5878 {
5879 return callNode;
5880 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005881 }
5882 }
5883 else
5884 {
5885 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005886 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005887
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005888 // If builtIn == false, the function is user defined - could be an overloaded
5889 // built-in as well.
5890 // if builtIn == true, it's a builtIn function with no op associated with it.
5891 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005892 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005893 {
Olli Etuahofe486322017-03-21 09:30:54 +00005894 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005895 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005896 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005897 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005898 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005899 }
5900 else
5901 {
Olli Etuahofe486322017-03-21 09:30:54 +00005902 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005903 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005904 }
5905
Jiajia Qinbc585152017-06-23 15:42:17 +08005906 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005907
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005908 callNode->setLine(loc);
5909
5910 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005911 }
5912 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005913 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005914
5915 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005916 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005917}
5918
Jamie Madillb98c3a82015-07-23 14:26:04 -04005919TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005920 TIntermTyped *trueExpression,
5921 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005922 const TSourceLoc &loc)
5923{
Olli Etuaho56229f12017-07-10 14:16:33 +03005924 if (!checkIsScalarBool(loc, cond))
5925 {
5926 return falseExpression;
5927 }
Olli Etuaho52901742015-04-15 13:42:45 +03005928
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005929 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005930 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005931 std::stringstream reasonStream;
5932 reasonStream << "mismatching ternary operator operand types '"
5933 << trueExpression->getCompleteString() << " and '"
5934 << falseExpression->getCompleteString() << "'";
5935 std::string reason = reasonStream.str();
5936 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005937 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005938 }
Olli Etuahode318b22016-10-25 16:18:25 +01005939 if (IsOpaqueType(trueExpression->getBasicType()))
5940 {
5941 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005942 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005943 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5944 // Note that structs containing opaque types don't need to be checked as structs are
5945 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005946 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005947 return falseExpression;
5948 }
5949
Jiajia Qinbc585152017-06-23 15:42:17 +08005950 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5951 falseExpression->getMemoryQualifier().writeonly)
5952 {
5953 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5954 return falseExpression;
5955 }
5956
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005957 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005958 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005959 // ESSL 3.00.6 section 5.7:
5960 // Ternary operator support is optional for arrays. No certainty that it works across all
5961 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5962 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005963 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005964 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005965 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005966 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005967 }
Olli Etuaho94050052017-05-08 14:17:44 +03005968 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5969 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005970 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005971 return falseExpression;
5972 }
5973
Corentin Wallez0d959252016-07-12 17:26:32 -04005974 // WebGL2 section 5.26, the following results in an error:
5975 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005976 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005977 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005978 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005979 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005980 }
5981
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005982 // Note that the node resulting from here can be a constant union without being qualified as
5983 // constant.
5984 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5985 node->setLine(loc);
5986
5987 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005988}
Olli Etuaho49300862015-02-20 14:54:49 +02005989
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005990//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005991// Parse an array of strings using yyparse.
5992//
5993// Returns 0 for success.
5994//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005995int PaParseStrings(size_t count,
5996 const char *const string[],
5997 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305998 TParseContext *context)
5999{
Yunchao He4f285442017-04-21 12:15:49 +08006000 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006001 return 1;
6002
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006003 if (glslang_initialize(context))
6004 return 1;
6005
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006006 int error = glslang_scan(count, string, length, context);
6007 if (!error)
6008 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006009
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006010 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006011
alokp@chromium.org6b495712012-06-29 00:06:58 +00006012 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006013}
Jamie Madill45bcc782016-11-07 13:58:48 -05006014
6015} // namespace sh