blob: 13128a5b68b4fd3512bb1e83ee51fd69a5d4931e [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 {
91 return imageSymbol->getSymbol().c_str();
92 }
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 Etuaho4de340a2016-12-16 09:32:03 +0000609 const char *symbol = symNode->getSymbol().c_str();
610 std::stringstream reasonStream;
611 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
612 std::string reason = reasonStream.str();
613 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000614 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 else
616 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000617 std::stringstream reasonStream;
618 reasonStream << "l-value required (" << message << ")";
619 std::string reason = reasonStream.str();
620 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000621 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622
Olli Etuaho8a176262016-08-16 14:23:01 +0300623 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624}
625
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626// Both test, and if necessary spit out an error, to see if the node is really
627// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300628void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629{
Olli Etuaho383b7912016-08-05 11:22:59 +0300630 if (node->getQualifier() != EvqConst)
631 {
632 error(node->getLine(), "constant expression required", "");
633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634}
635
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636// Both test, and if necessary spit out an error, to see if the node is really
637// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300638void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639{
Olli Etuaho383b7912016-08-05 11:22:59 +0300640 if (!node->isScalarInt())
641 {
642 error(node->getLine(), "integer expression required", token);
643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644}
645
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646// Both test, and if necessary spit out an error, to see if we are currently
647// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800648bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649{
Olli Etuaho856c4972016-08-08 11:38:39 +0300650 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300651 {
652 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800653 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300654 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800655 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000656}
657
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300658// ESSL 3.00.5 sections 3.8 and 3.9.
659// If it starts "gl_" or contains two consecutive underscores, it's reserved.
660// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300661bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300664 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530665 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300666 error(line, reservedErrMsg, "gl_");
667 return false;
668 }
669 if (sh::IsWebGLBasedSpec(mShaderSpec))
670 {
671 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300673 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300674 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300676 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530677 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300678 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300679 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 }
681 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300682 if (identifier.find("__") != TString::npos)
683 {
684 error(line,
685 "identifiers containing two consecutive underscores (__) are reserved as "
686 "possible future keywords",
687 identifier.c_str());
688 return false;
689 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300690 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691}
692
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300693// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300694bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800695 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300696 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000697{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800698 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530699 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200700 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300701 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200703
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300704 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530705 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300706 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200707 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300708 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200709 {
jchen10cc2a10e2017-05-03 14:05:12 +0800710 std::string reason("cannot convert a variable with type ");
711 reason += getBasicString(argTyped->getBasicType());
712 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300713 return false;
714 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800715 else if (argTyped->getMemoryQualifier().writeonly)
716 {
717 error(line, "cannot convert a variable with writeonly", "constructor");
718 return false;
719 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200720 if (argTyped->getBasicType() == EbtVoid)
721 {
722 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300723 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200724 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000725 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726
Olli Etuaho856c4972016-08-08 11:38:39 +0300727 if (type.isArray())
728 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300729 // The size of an unsized constructor should already have been determined.
730 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300731 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300732 {
733 error(line, "array constructor needs one argument per array element", "constructor");
734 return false;
735 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300736 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
737 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800738 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300739 {
740 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300741 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500742 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300743 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500744 return false;
745 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300746 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300747 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000748 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300749 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300750 }
751 }
752 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300753 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300754 {
755 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300756 if (fields.size() != arguments->size())
757 {
758 error(line,
759 "Number of constructor parameters does not match the number of structure fields",
760 "constructor");
761 return false;
762 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300763
764 for (size_t i = 0; i < fields.size(); i++)
765 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800766 if (i >= arguments->size() ||
767 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300768 {
769 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000770 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300771 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300772 }
773 }
774 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300775 else
776 {
777 // We're constructing a scalar, vector, or matrix.
778
779 // Note: It's okay to have too many components available, but not okay to have unused
780 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
781 // there is an extra argument, so 'overFull' will become true.
782
783 size_t size = 0;
784 bool full = false;
785 bool overFull = false;
786 bool matrixArg = false;
787 for (TIntermNode *arg : *arguments)
788 {
789 const TIntermTyped *argTyped = arg->getAsTyped();
790 ASSERT(argTyped != nullptr);
791
Olli Etuaho487b63a2017-05-23 15:55:09 +0300792 if (argTyped->getBasicType() == EbtStruct)
793 {
794 error(line, "a struct cannot be used as a constructor argument for this type",
795 "constructor");
796 return false;
797 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300798 if (argTyped->getType().isArray())
799 {
800 error(line, "constructing from a non-dereferenced array", "constructor");
801 return false;
802 }
803 if (argTyped->getType().isMatrix())
804 {
805 matrixArg = true;
806 }
807
808 size += argTyped->getType().getObjectSize();
809 if (full)
810 {
811 overFull = true;
812 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300813 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300814 {
815 full = true;
816 }
817 }
818
819 if (type.isMatrix() && matrixArg)
820 {
821 if (arguments->size() != 1)
822 {
823 error(line, "constructing matrix from matrix can only take one argument",
824 "constructor");
825 return false;
826 }
827 }
828 else
829 {
830 if (size != 1 && size < type.getObjectSize())
831 {
832 error(line, "not enough data provided for construction", "constructor");
833 return false;
834 }
835 if (overFull)
836 {
837 error(line, "too many arguments", "constructor");
838 return false;
839 }
840 }
841 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300842
Olli Etuaho8a176262016-08-16 14:23:01 +0300843 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844}
845
Jamie Madillb98c3a82015-07-23 14:26:04 -0400846// This function checks to see if a void variable has been declared and raise an error message for
847// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848//
849// returns true in case of an error
850//
Olli Etuaho856c4972016-08-08 11:38:39 +0300851bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400852 const TString &identifier,
853 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300855 if (type == EbtVoid)
856 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000857 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300858 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300859 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860
Olli Etuaho8a176262016-08-16 14:23:01 +0300861 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
Jamie Madillb98c3a82015-07-23 14:26:04 -0400864// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300865// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300866bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300868 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530869 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000870 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300871 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530872 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300873 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874}
875
Jamie Madillb98c3a82015-07-23 14:26:04 -0400876// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300877// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300878void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879{
Martin Radev4a9cd802016-09-01 16:51:51 +0300880 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530881 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000882 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530883 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884}
885
jchen10cc2a10e2017-05-03 14:05:12 +0800886bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
887 const TTypeSpecifierNonArray &pType,
888 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530890 if (pType.type == EbtStruct)
891 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300892 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530893 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000894 std::stringstream reasonStream;
895 reasonStream << reason << " (structure contains a sampler)";
896 std::string reasonStr = reasonStream.str();
897 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300898 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000899 }
jchen10cc2a10e2017-05-03 14:05:12 +0800900 // only samplers need to be checked from structs, since other opaque types can't be struct
901 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300902 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530903 }
jchen10cc2a10e2017-05-03 14:05:12 +0800904 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530905 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000906 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300907 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000908 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909
Olli Etuaho8a176262016-08-16 14:23:01 +0300910 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911}
912
Olli Etuaho856c4972016-08-08 11:38:39 +0300913void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
914 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400915{
916 if (pType.layoutQualifier.location != -1)
917 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400918 error(line, "location must only be specified for a single input or output variable",
919 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400920 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400921}
922
Olli Etuaho856c4972016-08-08 11:38:39 +0300923void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
924 const TLayoutQualifier &layoutQualifier)
925{
926 if (layoutQualifier.location != -1)
927 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000928 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
929 if (mShaderVersion >= 310)
930 {
931 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800932 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000933 }
934 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300935 }
936}
937
Qin Jiajiaca68d982017-09-18 16:41:56 +0800938void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
939 const TLayoutBlockStorage &blockStorage,
940 const TQualifier &qualifier)
941{
942 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
943 {
944 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
945 }
946}
947
Martin Radev2cc85b32016-08-05 16:22:53 +0300948void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
949 TQualifier qualifier,
950 const TType &type)
951{
Martin Radev2cc85b32016-08-05 16:22:53 +0300952 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800953 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530954 {
jchen10cc2a10e2017-05-03 14:05:12 +0800955 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000956 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957}
958
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300960unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530962 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000963
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200964 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
965 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
966 // fold as array size.
967 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000968 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000969 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300970 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000971 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
Olli Etuaho856c4972016-08-08 11:38:39 +0300973 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400974
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000975 if (constant->getBasicType() == EbtUInt)
976 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300977 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000978 }
979 else
980 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300981 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000982
Olli Etuaho856c4972016-08-08 11:38:39 +0300983 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000984 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400985 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300986 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000987 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400988
Olli Etuaho856c4972016-08-08 11:38:39 +0300989 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400990 }
991
Olli Etuaho856c4972016-08-08 11:38:39 +0300992 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400993 {
994 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300995 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400996 }
997
998 // The size of arrays is restricted here to prevent issues further down the
999 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1000 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1001 const unsigned int sizeLimit = 65536;
1002
Olli Etuaho856c4972016-08-08 11:38:39 +03001003 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001004 {
1005 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001006 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001007 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001008
1009 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010}
1011
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001013bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1014 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015{
Olli Etuaho8a176262016-08-16 14:23:01 +03001016 if ((elementQualifier.qualifier == EvqAttribute) ||
1017 (elementQualifier.qualifier == EvqVertexIn) ||
1018 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001020 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001021 TType(elementQualifier).getQualifierString());
1022 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024
Olli Etuaho8a176262016-08-16 14:23:01 +03001025 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026}
1027
Olli Etuaho8a176262016-08-16 14:23:01 +03001028// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001029bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1030 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001032 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001033 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001034 error(line, "cannot declare arrays of arrays",
1035 TType(elementType).getCompleteString().c_str());
1036 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001037 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001038 return true;
1039}
1040
1041// Check if this qualified element type can be formed into an array. This is only called when array
1042// brackets are associated with an identifier in a declaration, like this:
1043// float a[2];
1044// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1045// are associated with the type, like this:
1046// float[2] a;
1047bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1048 const TPublicType &elementType)
1049{
1050 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1051 {
1052 return false;
1053 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001054 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1055 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1056 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001057 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001058 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001059 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001060 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001061 TType(elementType).getCompleteString().c_str());
1062 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001063 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001064 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065}
1066
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001068void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1069 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001070 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071{
Olli Etuaho3739d232015-04-08 12:23:44 +03001072 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001073 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001074 {
1075 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001076 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001077
1078 // Generate informative error messages for ESSL1.
1079 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001080 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001081 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301082 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001083 "structures containing arrays may not be declared constant since they cannot be "
1084 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301085 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001086 }
1087 else
1088 {
1089 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1090 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001092 // This will make the type sized if it isn't sized yet.
1093 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1094 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095}
1096
Olli Etuaho2935c582015-04-08 14:32:06 +03001097// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098// and update the symbol table.
1099//
Olli Etuaho2935c582015-04-08 14:32:06 +03001100// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001102bool TParseContext::declareVariable(const TSourceLoc &line,
1103 const TString &identifier,
1104 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001105 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106{
Olli Etuaho2935c582015-04-08 14:32:06 +03001107 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108
Olli Etuaho43364892017-02-13 16:00:12 +00001109 checkBindingIsValid(line, type);
1110
Olli Etuaho856c4972016-08-08 11:38:39 +03001111 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112
Olli Etuaho2935c582015-04-08 14:32:06 +03001113 // gl_LastFragData may be redeclared with a new precision qualifier
1114 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1115 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001116 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1117 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001118 if (type.isArrayOfArrays())
1119 {
1120 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1121 identifier.c_str());
1122 return false;
1123 }
1124 else if (static_cast<int>(type.getOutermostArraySize()) ==
1125 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001126 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001127 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001128 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001129 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001130 }
1131 }
1132 else
1133 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001134 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1135 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001136 return false;
1137 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001138 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001139
Olli Etuaho8a176262016-08-16 14:23:01 +03001140 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001141 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001142
Olli Etuaho0f684632017-07-13 12:42:15 +03001143 (*variable) = symbolTable.declareVariable(&identifier, type);
1144 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001145 {
1146 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001147 return false;
1148 }
1149
Olli Etuaho8a176262016-08-16 14:23:01 +03001150 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001151 return false;
1152
1153 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001154}
1155
Martin Radev70866b82016-07-22 15:27:42 +03001156void TParseContext::checkIsParameterQualifierValid(
1157 const TSourceLoc &line,
1158 const TTypeQualifierBuilder &typeQualifierBuilder,
1159 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301160{
Olli Etuahocce89652017-06-19 16:04:09 +03001161 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001162 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001163
1164 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301165 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001166 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1167 }
1168
1169 if (!IsImage(type->getBasicType()))
1170 {
Olli Etuaho43364892017-02-13 16:00:12 +00001171 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001172 }
1173 else
1174 {
1175 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001177
Martin Radev70866b82016-07-22 15:27:42 +03001178 type->setQualifier(typeQualifier.qualifier);
1179
1180 if (typeQualifier.precision != EbpUndefined)
1181 {
1182 type->setPrecision(typeQualifier.precision);
1183 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184}
1185
Olli Etuaho703671e2017-11-08 17:47:18 +02001186template <size_t size>
1187bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1188 const std::array<TExtension, size> &extensions)
1189{
1190 ASSERT(!extensions.empty());
1191 const TExtensionBehavior &extBehavior = extensionBehavior();
1192
1193 bool canUseWithWarning = false;
1194 bool canUseWithoutWarning = false;
1195
1196 const char *errorMsgString = "";
1197 TExtension errorMsgExtension = TExtension::UNDEFINED;
1198
1199 for (TExtension extension : extensions)
1200 {
1201 auto extIter = extBehavior.find(extension);
1202 if (canUseWithWarning)
1203 {
1204 // We already have an extension that we can use, but with a warning.
1205 // See if we can use the alternative extension without a warning.
1206 if (extIter == extBehavior.end())
1207 {
1208 continue;
1209 }
1210 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1211 {
1212 canUseWithoutWarning = true;
1213 break;
1214 }
1215 continue;
1216 }
1217 if (extIter == extBehavior.end())
1218 {
1219 errorMsgString = "extension is not supported";
1220 errorMsgExtension = extension;
1221 }
1222 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1223 {
1224 errorMsgString = "extension is disabled";
1225 errorMsgExtension = extension;
1226 }
1227 else if (extIter->second == EBhWarn)
1228 {
1229 errorMsgExtension = extension;
1230 canUseWithWarning = true;
1231 }
1232 else
1233 {
1234 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1235 canUseWithoutWarning = true;
1236 break;
1237 }
1238 }
1239
1240 if (canUseWithoutWarning)
1241 {
1242 return true;
1243 }
1244 if (canUseWithWarning)
1245 {
1246 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1247 return true;
1248 }
1249 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1250 return false;
1251}
1252
1253template bool TParseContext::checkCanUseOneOfExtensions(
1254 const TSourceLoc &line,
1255 const std::array<TExtension, 1> &extensions);
1256template bool TParseContext::checkCanUseOneOfExtensions(
1257 const TSourceLoc &line,
1258 const std::array<TExtension, 2> &extensions);
1259template bool TParseContext::checkCanUseOneOfExtensions(
1260 const TSourceLoc &line,
1261 const std::array<TExtension, 3> &extensions);
1262
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001263bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001264{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001265 ASSERT(extension != TExtension::UNDEFINED);
Jiawei Shao0e883132017-10-26 09:53:50 +08001266 ASSERT(extension != TExtension::EXT_geometry_shader);
Olli Etuaho703671e2017-11-08 17:47:18 +02001267 if (extension == TExtension::OES_geometry_shader)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301268 {
Olli Etuaho703671e2017-11-08 17:47:18 +02001269 // OES_geometry_shader and EXT_geometry_shader are always interchangeable.
1270 constexpr std::array<TExtension, 2u> extensions{
1271 {TExtension::EXT_geometry_shader, TExtension::OES_geometry_shader}};
1272 return checkCanUseOneOfExtensions(line, extensions);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001273 }
Corentin Wallez1d33c212017-11-13 10:21:39 -08001274 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001275}
1276
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001277// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1278// compile-time or link-time errors are the same whether or not the declaration is empty".
1279// This function implements all the checks that are done on qualifiers regardless of if the
1280// declaration is empty.
1281void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1282 const sh::TLayoutQualifier &layoutQualifier,
1283 const TSourceLoc &location)
1284{
1285 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1286 {
1287 error(location, "Shared memory declarations cannot have layout specified", "layout");
1288 }
1289
1290 if (layoutQualifier.matrixPacking != EmpUnspecified)
1291 {
1292 error(location, "layout qualifier only valid for interface blocks",
1293 getMatrixPackingString(layoutQualifier.matrixPacking));
1294 return;
1295 }
1296
1297 if (layoutQualifier.blockStorage != EbsUnspecified)
1298 {
1299 error(location, "layout qualifier only valid for interface blocks",
1300 getBlockStorageString(layoutQualifier.blockStorage));
1301 return;
1302 }
1303
1304 if (qualifier == EvqFragmentOut)
1305 {
1306 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1307 {
1308 error(location, "invalid layout qualifier combination", "yuv");
1309 return;
1310 }
1311 }
1312 else
1313 {
1314 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1315 }
1316
Olli Etuaho95468d12017-05-04 11:14:34 +03001317 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1318 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001319 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1320 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001321 {
1322 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1323 }
1324
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001325 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001326 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001327 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001328 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001329 // We're not checking whether the uniform location is in range here since that depends on
1330 // the type of the variable.
1331 // The type can only be fully determined for non-empty declarations.
1332 }
1333 if (!canHaveLocation)
1334 {
1335 checkLocationIsNotSpecified(location, layoutQualifier);
1336 }
1337}
1338
jchen104cdac9e2017-05-08 11:01:20 +08001339void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1340 const TSourceLoc &location)
1341{
1342 if (publicType.precision != EbpHigh)
1343 {
1344 error(location, "Can only be highp", "atomic counter");
1345 }
1346 // dEQP enforces compile error if location is specified. See uniform_location.test.
1347 if (publicType.layoutQualifier.location != -1)
1348 {
1349 error(location, "location must not be set for atomic_uint", "layout");
1350 }
1351 if (publicType.layoutQualifier.binding == -1)
1352 {
1353 error(location, "no binding specified", "atomic counter");
1354 }
1355}
1356
Olli Etuaho55bde912017-10-25 13:41:13 +03001357void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001358{
Olli Etuaho55bde912017-10-25 13:41:13 +03001359 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001360 {
1361 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1362 // error. It is assumed that this applies to empty declarations as well.
1363 error(location, "empty array declaration needs to specify a size", "");
1364 }
Martin Radevb8b01222016-11-20 23:25:53 +02001365}
1366
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001367// These checks are done for all declarations that are non-empty. They're done for non-empty
1368// declarations starting a declarator list, and declarators that follow an empty declaration.
1369void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1370 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001371{
Olli Etuahofa33d582015-04-09 14:33:12 +03001372 switch (publicType.qualifier)
1373 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001374 case EvqVaryingIn:
1375 case EvqVaryingOut:
1376 case EvqAttribute:
1377 case EvqVertexIn:
1378 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001379 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001380 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001381 {
1382 error(identifierLocation, "cannot be used with a structure",
1383 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001384 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001385 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001386 break;
1387 case EvqBuffer:
1388 if (publicType.getBasicType() != EbtInterfaceBlock)
1389 {
1390 error(identifierLocation,
1391 "cannot declare buffer variables at global scope(outside a block)",
1392 getQualifierString(publicType.qualifier));
1393 return;
1394 }
1395 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001396 default:
1397 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001398 }
jchen10cc2a10e2017-05-03 14:05:12 +08001399 std::string reason(getBasicString(publicType.getBasicType()));
1400 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001401 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001402 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001403 {
1404 return;
1405 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001406
Andrei Volykhina5527072017-03-22 16:46:30 +03001407 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1408 publicType.qualifier != EvqConst) &&
1409 publicType.getBasicType() == EbtYuvCscStandardEXT)
1410 {
1411 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1412 getQualifierString(publicType.qualifier));
1413 return;
1414 }
1415
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001416 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1417 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001418 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1419 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001420 TType type(publicType);
1421 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001422 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001423 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1424 publicType.layoutQualifier);
1425 }
1426 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001427
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001428 // check for layout qualifier issues
1429 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001430
Martin Radev2cc85b32016-08-05 16:22:53 +03001431 if (IsImage(publicType.getBasicType()))
1432 {
1433
1434 switch (layoutQualifier.imageInternalFormat)
1435 {
1436 case EiifRGBA32F:
1437 case EiifRGBA16F:
1438 case EiifR32F:
1439 case EiifRGBA8:
1440 case EiifRGBA8_SNORM:
1441 if (!IsFloatImage(publicType.getBasicType()))
1442 {
1443 error(identifierLocation,
1444 "internal image format requires a floating image type",
1445 getBasicString(publicType.getBasicType()));
1446 return;
1447 }
1448 break;
1449 case EiifRGBA32I:
1450 case EiifRGBA16I:
1451 case EiifRGBA8I:
1452 case EiifR32I:
1453 if (!IsIntegerImage(publicType.getBasicType()))
1454 {
1455 error(identifierLocation,
1456 "internal image format requires an integer image type",
1457 getBasicString(publicType.getBasicType()));
1458 return;
1459 }
1460 break;
1461 case EiifRGBA32UI:
1462 case EiifRGBA16UI:
1463 case EiifRGBA8UI:
1464 case EiifR32UI:
1465 if (!IsUnsignedImage(publicType.getBasicType()))
1466 {
1467 error(identifierLocation,
1468 "internal image format requires an unsigned image type",
1469 getBasicString(publicType.getBasicType()));
1470 return;
1471 }
1472 break;
1473 case EiifUnspecified:
1474 error(identifierLocation, "layout qualifier", "No image internal format specified");
1475 return;
1476 default:
1477 error(identifierLocation, "layout qualifier", "unrecognized token");
1478 return;
1479 }
1480
1481 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1482 switch (layoutQualifier.imageInternalFormat)
1483 {
1484 case EiifR32F:
1485 case EiifR32I:
1486 case EiifR32UI:
1487 break;
1488 default:
1489 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1490 {
1491 error(identifierLocation, "layout qualifier",
1492 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1493 "image variables must be qualified readonly and/or writeonly");
1494 return;
1495 }
1496 break;
1497 }
1498 }
1499 else
1500 {
Olli Etuaho43364892017-02-13 16:00:12 +00001501 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001502 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1503 }
jchen104cdac9e2017-05-08 11:01:20 +08001504
1505 if (IsAtomicCounter(publicType.getBasicType()))
1506 {
1507 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1508 }
1509 else
1510 {
1511 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1512 }
Olli Etuaho43364892017-02-13 16:00:12 +00001513}
Martin Radev2cc85b32016-08-05 16:22:53 +03001514
Olli Etuaho43364892017-02-13 16:00:12 +00001515void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1516{
1517 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001518 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1519 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1520 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1521 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1522 // when it comes to which shaders are accepted by the compiler.
1523 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001524 if (IsImage(type.getBasicType()))
1525 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001526 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1527 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001528 }
1529 else if (IsSampler(type.getBasicType()))
1530 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001531 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1532 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001533 }
jchen104cdac9e2017-05-08 11:01:20 +08001534 else if (IsAtomicCounter(type.getBasicType()))
1535 {
1536 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1537 }
Olli Etuaho43364892017-02-13 16:00:12 +00001538 else
1539 {
1540 ASSERT(!IsOpaqueType(type.getBasicType()));
1541 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001542 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001543}
1544
Olli Etuaho856c4972016-08-08 11:38:39 +03001545void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1546 const TString &layoutQualifierName,
1547 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001548{
1549
1550 if (mShaderVersion < versionRequired)
1551 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001552 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001553 }
1554}
1555
Olli Etuaho856c4972016-08-08 11:38:39 +03001556bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1557 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001558{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001559 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001560 for (size_t i = 0u; i < localSize.size(); ++i)
1561 {
1562 if (localSize[i] != -1)
1563 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001564 error(location,
1565 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1566 "global layout declaration",
1567 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001568 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001569 }
1570 }
1571
Olli Etuaho8a176262016-08-16 14:23:01 +03001572 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001573}
1574
Olli Etuaho43364892017-02-13 16:00:12 +00001575void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001576 TLayoutImageInternalFormat internalFormat)
1577{
1578 if (internalFormat != EiifUnspecified)
1579 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001580 error(location, "invalid layout qualifier: only valid when used with images",
1581 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001582 }
Olli Etuaho43364892017-02-13 16:00:12 +00001583}
1584
1585void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1586{
1587 if (binding != -1)
1588 {
1589 error(location,
1590 "invalid layout qualifier: only valid when used with opaque types or blocks",
1591 "binding");
1592 }
1593}
1594
jchen104cdac9e2017-05-08 11:01:20 +08001595void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1596{
1597 if (offset != -1)
1598 {
1599 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1600 "offset");
1601 }
1602}
1603
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001604void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1605 int binding,
1606 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001607{
1608 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001609 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001610 {
1611 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1612 }
1613}
1614
1615void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1616 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001617 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001618{
1619 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001620 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001621 {
1622 error(location, "sampler binding greater than maximum texture units", "binding");
1623 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001624}
1625
Jiajia Qinbc585152017-06-23 15:42:17 +08001626void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1627 const TQualifier &qualifier,
1628 int binding,
1629 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001630{
1631 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001632 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001633 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001634 if (binding + size > mMaxUniformBufferBindings)
1635 {
1636 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1637 "binding");
1638 }
1639 }
1640 else if (qualifier == EvqBuffer)
1641 {
1642 if (binding + size > mMaxShaderStorageBufferBindings)
1643 {
1644 error(location,
1645 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1646 "binding");
1647 }
jchen10af713a22017-04-19 09:10:56 +08001648 }
1649}
jchen104cdac9e2017-05-08 11:01:20 +08001650void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1651{
1652 if (binding >= mMaxAtomicCounterBindings)
1653 {
1654 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1655 "binding");
1656 }
1657}
jchen10af713a22017-04-19 09:10:56 +08001658
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001659void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1660 int objectLocationCount,
1661 const TLayoutQualifier &layoutQualifier)
1662{
1663 int loc = layoutQualifier.location;
1664 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1665 {
1666 error(location, "Uniform location out of range", "location");
1667 }
1668}
1669
Andrei Volykhina5527072017-03-22 16:46:30 +03001670void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1671{
1672 if (yuv != false)
1673 {
1674 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1675 }
1676}
1677
Jiajia Qinbc585152017-06-23 15:42:17 +08001678void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1679 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001680{
1681 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1682 {
1683 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001684 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1685 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1686 qual == EvqInOut || qual == EvqConstReadOnly))
1687 {
1688 if (argument->getMemoryQualifier().writeonly)
1689 {
1690 error(argument->getLine(),
1691 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1692 fnCall->getFunctionSymbolInfo()->getName().c_str());
1693 return;
1694 }
1695 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001696 if (qual == EvqOut || qual == EvqInOut)
1697 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001698 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001699 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001700 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001701 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001702 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001703 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001704 }
1705 }
1706 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001707}
1708
Martin Radev70866b82016-07-22 15:27:42 +03001709void TParseContext::checkInvariantVariableQualifier(bool invariant,
1710 const TQualifier qualifier,
1711 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001712{
Martin Radev70866b82016-07-22 15:27:42 +03001713 if (!invariant)
1714 return;
1715
1716 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001717 {
Martin Radev70866b82016-07-22 15:27:42 +03001718 // input variables in the fragment shader can be also qualified as invariant
1719 if (!sh::CanBeInvariantESSL1(qualifier))
1720 {
1721 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1722 }
1723 }
1724 else
1725 {
1726 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1727 {
1728 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1729 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001730 }
1731}
1732
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001733bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001734{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001735 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001736}
1737
Jamie Madillb98c3a82015-07-23 14:26:04 -04001738void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1739 const char *extName,
1740 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001741{
1742 pp::SourceLocation srcLoc;
1743 srcLoc.file = loc.first_file;
1744 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001745 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001746}
1747
Jamie Madillb98c3a82015-07-23 14:26:04 -04001748void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1749 const char *name,
1750 const char *value,
1751 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001752{
1753 pp::SourceLocation srcLoc;
1754 srcLoc.file = loc.first_file;
1755 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001756 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001757}
1758
Martin Radev4c4c8e72016-08-04 12:25:34 +03001759sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001760{
Jamie Madill2f294c92017-11-20 14:47:26 -05001761 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001762 for (size_t i = 0u; i < result.size(); ++i)
1763 {
1764 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1765 {
1766 result[i] = 1;
1767 }
1768 else
1769 {
1770 result[i] = mComputeShaderLocalSize[i];
1771 }
1772 }
1773 return result;
1774}
1775
Olli Etuaho56229f12017-07-10 14:16:33 +03001776TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1777 const TSourceLoc &line)
1778{
1779 TIntermConstantUnion *node = new TIntermConstantUnion(
1780 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1781 node->setLine(line);
1782 return node;
1783}
1784
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785/////////////////////////////////////////////////////////////////////////////////
1786//
1787// Non-Errors.
1788//
1789/////////////////////////////////////////////////////////////////////////////////
1790
Jamie Madill5c097022014-08-20 16:38:32 -04001791const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1792 const TString *name,
1793 const TSymbol *symbol)
1794{
Jamie Madill5c097022014-08-20 16:38:32 -04001795 if (!symbol)
1796 {
1797 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001798 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001799 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001800
1801 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001802 {
1803 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001804 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001805 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001806
1807 const TVariable *variable = static_cast<const TVariable *>(symbol);
1808
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001809 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001810 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001811 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001812 }
1813
Olli Etuaho0f684632017-07-13 12:42:15 +03001814 // Reject shaders using both gl_FragData and gl_FragColor
1815 TQualifier qualifier = variable->getType().getQualifier();
1816 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001817 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001818 mUsesFragData = true;
1819 }
1820 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1821 {
1822 mUsesFragColor = true;
1823 }
1824 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1825 {
1826 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001827 }
1828
Olli Etuaho0f684632017-07-13 12:42:15 +03001829 // This validation is not quite correct - it's only an error to write to
1830 // both FragData and FragColor. For simplicity, and because users shouldn't
1831 // be rewarded for reading from undefined varaibles, return an error
1832 // if they are both referenced, rather than assigned.
1833 if (mUsesFragData && mUsesFragColor)
1834 {
1835 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1836 if (mUsesSecondaryOutputs)
1837 {
1838 errorMessage =
1839 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1840 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1841 }
1842 error(location, errorMessage, name->c_str());
1843 }
1844
1845 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1846 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1847 qualifier == EvqWorkGroupSize)
1848 {
1849 error(location,
1850 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1851 "gl_WorkGroupSize");
1852 }
Jamie Madill5c097022014-08-20 16:38:32 -04001853 return variable;
1854}
1855
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001856TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1857 const TString *name,
1858 const TSymbol *symbol)
1859{
1860 const TVariable *variable = getNamedVariable(location, name, symbol);
1861
Olli Etuaho0f684632017-07-13 12:42:15 +03001862 if (!variable)
1863 {
1864 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1865 node->setLine(location);
1866 return node;
1867 }
1868
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001869 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001870 TIntermTyped *node = nullptr;
1871
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001872 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001873 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001874 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001875 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001876 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001877 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001878 {
1879 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1880 // needs to be added to the AST as a constant and not as a symbol.
1881 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1882 TConstantUnion *constArray = new TConstantUnion[3];
1883 for (size_t i = 0; i < 3; ++i)
1884 {
1885 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1886 }
1887
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001888 ASSERT(variableType.getBasicType() == EbtUInt);
1889 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001890
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001891 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001892 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001893 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001894 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001895 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1896 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001897 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001898 ASSERT(mGeometryShaderInputArraySize > 0u);
1899
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001900 node = new TIntermSymbol(variable->uniqueId(), variable->name(), variableType);
Olli Etuaho55bde912017-10-25 13:41:13 +03001901 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001902 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001903 else
1904 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001905 node = new TIntermSymbol(variable->uniqueId(), variable->name(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001906 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001907 ASSERT(node != nullptr);
1908 node->setLine(location);
1909 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001910}
1911
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912// Initializers show up in several places in the grammar. Have one set of
1913// code to handle them here.
1914//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001915// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001916bool TParseContext::executeInitializer(const TSourceLoc &line,
1917 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001918 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001919 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001920 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921{
Olli Etuaho13389b62016-10-16 11:48:18 +01001922 ASSERT(initNode != nullptr);
1923 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924
Olli Etuaho2935c582015-04-08 14:32:06 +03001925 TVariable *variable = nullptr;
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 Etuaho2935c582015-04-08 14:32:06 +03001935 if (!declareVariable(line, identifier, type, &variable))
1936 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001937 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939
Olli Etuahob0c645e2015-05-12 14:25:36 +03001940 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001941 if (symbolTable.atGlobalLevel() &&
1942 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001943 {
1944 // Error message does not completely match behavior with ESSL 1.00, but
1945 // we want to steer developers towards only using constant expressions.
1946 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001947 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001948 }
1949 if (globalInitWarning)
1950 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001951 warning(
1952 line,
1953 "global variable initializers should be constant expressions "
1954 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1955 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001956 }
1957
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001958 //
1959 // identifier must be of type constant, a global, or a temporary
1960 //
1961 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301962 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1963 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001964 error(line, " cannot initialize this type of qualifier ",
1965 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001966 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001967 }
1968 //
1969 // test for and propagate constant
1970 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971
Arun Patole7e7e68d2015-05-22 12:02:25 +05301972 if (qualifier == EvqConst)
1973 {
1974 if (qualifier != initializer->getType().getQualifier())
1975 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001976 std::stringstream reasonStream;
1977 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1978 << "'";
1979 std::string reason = reasonStream.str();
1980 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001981 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001982 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001983 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301984 if (type != initializer->getType())
1985 {
1986 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001987 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001988 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001989 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001990 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001991
1992 // Save the constant folded value to the variable if possible. For example array
1993 // initializers are not folded, since that way copying the array literal to multiple places
1994 // in the shader is avoided.
1995 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1996 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301997 if (initializer->getAsConstantUnion())
1998 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001999 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002000 ASSERT(*initNode == nullptr);
2001 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302002 }
2003 else if (initializer->getAsSymbolNode())
2004 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002005 const TSymbol *symbol =
2006 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
2007 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002008
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002009 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002010 if (constArray)
2011 {
2012 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002013 ASSERT(*initNode == nullptr);
2014 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002015 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002016 }
2017 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002018
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002019 TIntermSymbol *intermSymbol =
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002020 new TIntermSymbol(variable->uniqueId(), variable->name(), variable->getType());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002021 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002022 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2023 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002024 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002025 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002026 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002027 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028
Olli Etuaho914b79a2017-06-19 16:03:19 +03002029 return true;
2030}
2031
2032TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2033 const TString &identifier,
2034 TIntermTyped *initializer,
2035 const TSourceLoc &loc)
2036{
2037 checkIsScalarBool(loc, pType);
2038 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002039 TType type(pType);
2040 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002041 {
2042 // The initializer is valid. The init condition needs to have a node - either the
2043 // initializer node, or a constant node in case the initialized variable is const and won't
2044 // be recorded in the AST.
2045 if (initNode == nullptr)
2046 {
2047 return initializer;
2048 }
2049 else
2050 {
2051 TIntermDeclaration *declaration = new TIntermDeclaration();
2052 declaration->appendDeclarator(initNode);
2053 return declaration;
2054 }
2055 }
2056 return nullptr;
2057}
2058
2059TIntermNode *TParseContext::addLoop(TLoopType type,
2060 TIntermNode *init,
2061 TIntermNode *cond,
2062 TIntermTyped *expr,
2063 TIntermNode *body,
2064 const TSourceLoc &line)
2065{
2066 TIntermNode *node = nullptr;
2067 TIntermTyped *typedCond = nullptr;
2068 if (cond)
2069 {
2070 typedCond = cond->getAsTyped();
2071 }
2072 if (cond == nullptr || typedCond)
2073 {
Olli Etuahocce89652017-06-19 16:04:09 +03002074 if (type == ELoopDoWhile)
2075 {
2076 checkIsScalarBool(line, typedCond);
2077 }
2078 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2079 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2080 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2081 !typedCond->isVector()));
2082
Olli Etuaho3ec75682017-07-05 17:02:55 +03002083 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002084 node->setLine(line);
2085 return node;
2086 }
2087
Olli Etuahocce89652017-06-19 16:04:09 +03002088 ASSERT(type != ELoopDoWhile);
2089
Olli Etuaho914b79a2017-06-19 16:03:19 +03002090 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2091 ASSERT(declaration);
2092 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2093 ASSERT(declarator->getLeft()->getAsSymbolNode());
2094
2095 // The condition is a declaration. In the AST representation we don't support declarations as
2096 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2097 // the loop.
2098 TIntermBlock *block = new TIntermBlock();
2099
2100 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2101 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2102 block->appendStatement(declareCondition);
2103
2104 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2105 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002106 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002107 block->appendStatement(loop);
2108 loop->setLine(line);
2109 block->setLine(line);
2110 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111}
2112
Olli Etuahocce89652017-06-19 16:04:09 +03002113TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2114 TIntermNodePair code,
2115 const TSourceLoc &loc)
2116{
Olli Etuaho56229f12017-07-10 14:16:33 +03002117 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002118
2119 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002120 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002121 {
2122 if (cond->getAsConstantUnion()->getBConst(0) == true)
2123 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002124 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002125 }
2126 else
2127 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002128 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002129 }
2130 }
2131
Olli Etuaho3ec75682017-07-05 17:02:55 +03002132 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002133 node->setLine(loc);
2134
2135 return node;
2136}
2137
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002138void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2139{
2140 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2141 typeSpecifier->getBasicType());
2142
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002143 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002144 {
2145 error(typeSpecifier->getLine(), "not supported", "first-class array");
2146 typeSpecifier->clearArrayness();
2147 }
2148}
2149
Martin Radev70866b82016-07-22 15:27:42 +03002150TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302151 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002152{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002153 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002154
Martin Radev70866b82016-07-22 15:27:42 +03002155 TPublicType returnType = typeSpecifier;
2156 returnType.qualifier = typeQualifier.qualifier;
2157 returnType.invariant = typeQualifier.invariant;
2158 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002159 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002160 returnType.precision = typeSpecifier.precision;
2161
2162 if (typeQualifier.precision != EbpUndefined)
2163 {
2164 returnType.precision = typeQualifier.precision;
2165 }
2166
Martin Radev4a9cd802016-09-01 16:51:51 +03002167 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2168 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002169
Martin Radev4a9cd802016-09-01 16:51:51 +03002170 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2171 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002172
Martin Radev4a9cd802016-09-01 16:51:51 +03002173 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002174
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002175 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002176 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002177 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002178 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002179 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002180 returnType.clearArrayness();
2181 }
2182
Martin Radev70866b82016-07-22 15:27:42 +03002183 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002184 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002185 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002186 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002187 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002188 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002189
Martin Radev70866b82016-07-22 15:27:42 +03002190 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002191 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002192 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002193 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002194 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002195 }
2196 }
2197 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002198 {
Martin Radev70866b82016-07-22 15:27:42 +03002199 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002200 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002201 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002202 }
Martin Radev70866b82016-07-22 15:27:42 +03002203 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2204 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002205 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002206 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2207 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002208 }
Martin Radev70866b82016-07-22 15:27:42 +03002209 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002210 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002211 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002212 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002213 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002214 }
2215
2216 return returnType;
2217}
2218
Olli Etuaho856c4972016-08-08 11:38:39 +03002219void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2220 const TPublicType &type,
2221 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002222{
2223 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002224 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002225 {
2226 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002227 }
2228
2229 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2230 switch (qualifier)
2231 {
2232 case EvqVertexIn:
2233 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002234 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002235 {
2236 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002237 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002238 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002239 return;
2240 case EvqFragmentOut:
2241 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002242 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002243 {
2244 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002245 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002246 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002247 return;
2248 default:
2249 break;
2250 }
2251
2252 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2253 // restrictions.
2254 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002255 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2256 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002257 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2258 {
2259 error(qualifierLocation, "must use 'flat' interpolation here",
2260 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002261 }
2262
Martin Radev4a9cd802016-09-01 16:51:51 +03002263 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002264 {
2265 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2266 // These restrictions are only implied by the ESSL 3.00 spec, but
2267 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002268 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002269 {
2270 error(qualifierLocation, "cannot be an array of structures",
2271 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002272 }
2273 if (type.isStructureContainingArrays())
2274 {
2275 error(qualifierLocation, "cannot be a structure containing an array",
2276 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002277 }
2278 if (type.isStructureContainingType(EbtStruct))
2279 {
2280 error(qualifierLocation, "cannot be a structure containing a structure",
2281 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002282 }
2283 if (type.isStructureContainingType(EbtBool))
2284 {
2285 error(qualifierLocation, "cannot be a structure containing a bool",
2286 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002287 }
2288 }
2289}
2290
Martin Radev2cc85b32016-08-05 16:22:53 +03002291void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2292{
2293 if (qualifier.getType() == QtStorage)
2294 {
2295 const TStorageQualifierWrapper &storageQualifier =
2296 static_cast<const TStorageQualifierWrapper &>(qualifier);
2297 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2298 !symbolTable.atGlobalLevel())
2299 {
2300 error(storageQualifier.getLine(),
2301 "Local variables can only use the const storage qualifier.",
2302 storageQualifier.getQualifierString().c_str());
2303 }
2304 }
2305}
2306
Olli Etuaho43364892017-02-13 16:00:12 +00002307void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002308 const TSourceLoc &location)
2309{
Jiajia Qinbc585152017-06-23 15:42:17 +08002310 const std::string reason(
2311 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2312 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002313 if (memoryQualifier.readonly)
2314 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002315 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002316 }
2317 if (memoryQualifier.writeonly)
2318 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002319 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002320 }
Martin Radev049edfa2016-11-11 14:35:37 +02002321 if (memoryQualifier.coherent)
2322 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002323 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002324 }
2325 if (memoryQualifier.restrictQualifier)
2326 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002327 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002328 }
2329 if (memoryQualifier.volatileQualifier)
2330 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002331 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002332 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002333}
2334
jchen104cdac9e2017-05-08 11:01:20 +08002335// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2336// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002337void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2338 const TSourceLoc &loc,
2339 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002340{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002341 if (!IsAtomicCounter(type->getBasicType()))
2342 {
2343 return;
2344 }
2345
2346 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2347 : kAtomicCounterSize;
2348 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2349 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002350 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002351 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002352 {
2353 offset = bindingState.appendSpan(size);
2354 }
2355 else
2356 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002357 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002358 }
2359 if (offset == -1)
2360 {
2361 error(loc, "Offset overlapping", "atomic counter");
2362 return;
2363 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002364 layoutQualifier.offset = offset;
2365 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002366}
2367
Olli Etuaho454c34c2017-10-25 16:35:56 +03002368void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2369 const char *token,
2370 TType *type)
2371{
2372 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2373 {
2374 if (type->isArray() && type->getOutermostArraySize() == 0u)
2375 {
2376 // Set size for the unsized geometry shader inputs if they are declared after a valid
2377 // input primitive declaration.
2378 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2379 {
2380 ASSERT(mGeometryShaderInputArraySize > 0u);
2381 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2382 }
2383 else
2384 {
2385 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2386 // An input can be declared without an array size if there is a previous layout
2387 // which specifies the size.
2388 error(location,
2389 "Missing a valid input primitive declaration before declaring an unsized "
2390 "array input",
2391 token);
2392 }
2393 }
2394 else if (type->isArray())
2395 {
2396 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2397 }
2398 else
2399 {
2400 error(location, "Geometry shader input variable must be declared as an array", token);
2401 }
2402 }
2403}
2404
Olli Etuaho13389b62016-10-16 11:48:18 +01002405TIntermDeclaration *TParseContext::parseSingleDeclaration(
2406 TPublicType &publicType,
2407 const TSourceLoc &identifierOrTypeLocation,
2408 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002409{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002410 TType type(publicType);
2411 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2412 mDirectiveHandler.pragma().stdgl.invariantAll)
2413 {
2414 TQualifier qualifier = type.getQualifier();
2415
2416 // The directive handler has already taken care of rejecting invalid uses of this pragma
2417 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2418 // affected variable declarations:
2419 //
2420 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2421 // elsewhere, in TranslatorGLSL.)
2422 //
2423 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2424 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2425 // the way this is currently implemented we have to enable this compiler option before
2426 // parsing the shader and determining the shading language version it uses. If this were
2427 // implemented as a post-pass, the workaround could be more targeted.
2428 //
2429 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2430 // the specification, but there are desktop OpenGL drivers that expect that this is the
2431 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2432 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2433 {
2434 type.setInvariant(true);
2435 }
2436 }
2437
Olli Etuaho454c34c2017-10-25 16:35:56 +03002438 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002439
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002440 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2441 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002442
Olli Etuahobab4c082015-04-24 16:38:49 +03002443 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002444 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002445
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002446 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002447 if (emptyDeclaration)
2448 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002449 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002450 // In most cases we don't need to create a symbol node for an empty declaration.
2451 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2452 if (type.getBasicType() == EbtStruct)
2453 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002454 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
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;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002470 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002471
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002472 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002473 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002474 symbol = new TIntermSymbol(variable->uniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002475 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002476 }
2477
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002478 TIntermDeclaration *declaration = new TIntermDeclaration();
2479 declaration->setLine(identifierOrTypeLocation);
2480 if (symbol)
2481 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002482 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002483 declaration->appendDeclarator(symbol);
2484 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002485 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002486}
2487
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002488TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2489 TPublicType &elementType,
2490 const TSourceLoc &identifierLocation,
2491 const TString &identifier,
2492 const TSourceLoc &indexLocation,
2493 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002494{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002495 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002496
Olli Etuaho55bde912017-10-25 13:41:13 +03002497 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002498 identifierLocation);
2499
Olli Etuaho55bde912017-10-25 13:41:13 +03002500 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002501
Olli Etuaho55bde912017-10-25 13:41:13 +03002502 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002503
Olli Etuaho55bde912017-10-25 13:41:13 +03002504 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002505 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002506
Olli Etuaho454c34c2017-10-25 16:35:56 +03002507 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002508
2509 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2510
Olli Etuaho55bc9052017-10-25 17:33:06 +03002511 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002512
Olli Etuaho2935c582015-04-08 14:32:06 +03002513 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002514 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002515
Olli Etuaho13389b62016-10-16 11:48:18 +01002516 TIntermDeclaration *declaration = new TIntermDeclaration();
2517 declaration->setLine(identifierLocation);
2518
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002519 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002520 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002521 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, arrayType);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002522 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002523 declaration->appendDeclarator(symbol);
2524 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002525
Olli Etuaho13389b62016-10-16 11:48:18 +01002526 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002527}
2528
Olli Etuaho13389b62016-10-16 11:48:18 +01002529TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2530 const TSourceLoc &identifierLocation,
2531 const TString &identifier,
2532 const TSourceLoc &initLocation,
2533 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002534{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002535 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002536
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002537 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2538 identifierLocation);
2539
2540 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002541
Olli Etuaho13389b62016-10-16 11:48:18 +01002542 TIntermDeclaration *declaration = new TIntermDeclaration();
2543 declaration->setLine(identifierLocation);
2544
2545 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002546 TType type(publicType);
2547 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002548 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002549 if (initNode)
2550 {
2551 declaration->appendDeclarator(initNode);
2552 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002553 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002554 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002555}
2556
Olli Etuaho13389b62016-10-16 11:48:18 +01002557TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002558 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002559 const TSourceLoc &identifierLocation,
2560 const TString &identifier,
2561 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002562 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002563 const TSourceLoc &initLocation,
2564 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002566 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002567
Olli Etuaho55bde912017-10-25 13:41:13 +03002568 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002569 identifierLocation);
2570
Olli Etuaho55bde912017-10-25 13:41:13 +03002571 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572
Olli Etuaho55bde912017-10-25 13:41:13 +03002573 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002574
Olli Etuaho55bde912017-10-25 13:41:13 +03002575 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002576 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002577
Olli Etuaho13389b62016-10-16 11:48:18 +01002578 TIntermDeclaration *declaration = new TIntermDeclaration();
2579 declaration->setLine(identifierLocation);
2580
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002581 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002582 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002583 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002584 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002585 if (initNode)
2586 {
2587 declaration->appendDeclarator(initNode);
2588 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002589 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002590
2591 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002592}
2593
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002594TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002595 const TTypeQualifierBuilder &typeQualifierBuilder,
2596 const TSourceLoc &identifierLoc,
2597 const TString *identifier,
2598 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002599{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002600 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002601
Martin Radev70866b82016-07-22 15:27:42 +03002602 if (!typeQualifier.invariant)
2603 {
2604 error(identifierLoc, "Expected invariant", identifier->c_str());
2605 return nullptr;
2606 }
2607 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2608 {
2609 return nullptr;
2610 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002611 if (!symbol)
2612 {
2613 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002614 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002615 }
Martin Radev70866b82016-07-22 15:27:42 +03002616 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002617 {
Martin Radev70866b82016-07-22 15:27:42 +03002618 error(identifierLoc, "invariant declaration specifies qualifier",
2619 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002620 }
Martin Radev70866b82016-07-22 15:27:42 +03002621 if (typeQualifier.precision != EbpUndefined)
2622 {
2623 error(identifierLoc, "invariant declaration specifies precision",
2624 getPrecisionString(typeQualifier.precision));
2625 }
2626 if (!typeQualifier.layoutQualifier.isEmpty())
2627 {
2628 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2629 }
2630
2631 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002632 if (!variable)
2633 {
2634 return nullptr;
2635 }
Martin Radev70866b82016-07-22 15:27:42 +03002636 const TType &type = variable->getType();
2637
2638 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2639 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002640 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002641
2642 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2643
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002644 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->uniqueId(), *identifier, type);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002645 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002646
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002647 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002648}
2649
Olli Etuaho13389b62016-10-16 11:48:18 +01002650void TParseContext::parseDeclarator(TPublicType &publicType,
2651 const TSourceLoc &identifierLocation,
2652 const TString &identifier,
2653 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002654{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002655 // If the declaration starting this declarator list was empty (example: int,), some checks were
2656 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002657 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002658 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002659 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2660 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002661 }
2662
Olli Etuaho856c4972016-08-08 11:38:39 +03002663 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002664
Olli Etuaho2935c582015-04-08 14:32:06 +03002665 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002666 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002667
2668 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2669
Olli Etuaho55bde912017-10-25 13:41:13 +03002670 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2671
Olli Etuaho55bc9052017-10-25 17:33:06 +03002672 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2673
Olli Etuaho43364892017-02-13 16:00:12 +00002674 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002675
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002676 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002677 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002678 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, type);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002679 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002680 declarationOut->appendDeclarator(symbol);
2681 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002682}
2683
Olli Etuaho55bde912017-10-25 13:41:13 +03002684void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002685 const TSourceLoc &identifierLocation,
2686 const TString &identifier,
2687 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002688 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002689 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002690{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002691 // If the declaration starting this declarator list was empty (example: int,), some checks were
2692 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002693 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002694 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002695 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002696 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002697 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002698
Olli Etuaho55bde912017-10-25 13:41:13 +03002699 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002700
Olli Etuaho55bde912017-10-25 13:41:13 +03002701 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002702 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002703 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002704 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002705
Olli Etuaho454c34c2017-10-25 16:35:56 +03002706 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2707
Olli Etuaho55bde912017-10-25 13:41:13 +03002708 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2709
Olli Etuaho55bc9052017-10-25 17:33:06 +03002710 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002711
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002712 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002713 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002714
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002715 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002716 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002717 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, arrayType);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002718 symbol->setLine(identifierLocation);
2719 declarationOut->appendDeclarator(symbol);
2720 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002721 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002722}
2723
Olli Etuaho13389b62016-10-16 11:48:18 +01002724void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2725 const TSourceLoc &identifierLocation,
2726 const TString &identifier,
2727 const TSourceLoc &initLocation,
2728 TIntermTyped *initializer,
2729 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002730{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002731 // If the declaration starting this declarator list was empty (example: int,), some checks were
2732 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002733 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002734 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002735 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2736 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002737 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002738
Olli Etuaho856c4972016-08-08 11:38:39 +03002739 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002740
Olli Etuaho13389b62016-10-16 11:48:18 +01002741 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002742 TType type(publicType);
2743 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002744 {
2745 //
2746 // build the intermediate representation
2747 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002748 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002749 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002750 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002751 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002752 }
2753}
2754
Olli Etuaho55bde912017-10-25 13:41:13 +03002755void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002756 const TSourceLoc &identifierLocation,
2757 const TString &identifier,
2758 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002759 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002760 const TSourceLoc &initLocation,
2761 TIntermTyped *initializer,
2762 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002763{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002764 // If the declaration starting this declarator list was empty (example: int,), some checks were
2765 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002766 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002767 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002768 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002769 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002770 }
2771
Olli Etuaho55bde912017-10-25 13:41:13 +03002772 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002773
Olli Etuaho55bde912017-10-25 13:41:13 +03002774 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002775
Olli Etuaho55bde912017-10-25 13:41:13 +03002776 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002777 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002778
2779 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002780 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002781 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002782 {
2783 if (initNode)
2784 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002785 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002786 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002787 }
2788}
2789
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002790TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2791{
2792 // It's simpler to parse an empty statement as a constant expression rather than having a
2793 // different type of node just for empty statements, that will be pruned from the AST anyway.
2794 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2795 node->setLine(location);
2796 return node;
2797}
2798
jchen104cdac9e2017-05-08 11:01:20 +08002799void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2800 const TSourceLoc &location)
2801{
2802 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2803 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2804 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2805 {
2806 error(location, "Requires both binding and offset", "layout");
2807 return;
2808 }
2809 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2810}
2811
Olli Etuahocce89652017-06-19 16:04:09 +03002812void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2813 const TPublicType &type,
2814 const TSourceLoc &loc)
2815{
2816 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2817 !getFragmentPrecisionHigh())
2818 {
2819 error(loc, "precision is not supported in fragment shader", "highp");
2820 }
2821
2822 if (!CanSetDefaultPrecisionOnType(type))
2823 {
2824 error(loc, "illegal type argument for default precision qualifier",
2825 getBasicString(type.getBasicType()));
2826 return;
2827 }
2828 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2829}
2830
Shaob5cc1192017-07-06 10:47:20 +08002831bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2832{
2833 switch (typeQualifier.layoutQualifier.primitiveType)
2834 {
2835 case EptLines:
2836 case EptLinesAdjacency:
2837 case EptTriangles:
2838 case EptTrianglesAdjacency:
2839 return typeQualifier.qualifier == EvqGeometryIn;
2840
2841 case EptLineStrip:
2842 case EptTriangleStrip:
2843 return typeQualifier.qualifier == EvqGeometryOut;
2844
2845 case EptPoints:
2846 return true;
2847
2848 default:
2849 UNREACHABLE();
2850 return false;
2851 }
2852}
2853
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002854void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2855 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002856{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002857 if (mGeometryShaderInputArraySize == 0u)
2858 {
2859 mGeometryShaderInputArraySize = inputArraySize;
2860 }
2861 else if (mGeometryShaderInputArraySize != inputArraySize)
2862 {
2863 error(line,
2864 "Array size or input primitive declaration doesn't match the size of earlier sized "
2865 "array inputs.",
2866 "layout");
2867 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002868}
2869
Shaob5cc1192017-07-06 10:47:20 +08002870bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2871{
2872 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2873
2874 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2875
2876 if (layoutQualifier.maxVertices != -1)
2877 {
2878 error(typeQualifier.line,
2879 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2880 return false;
2881 }
2882
2883 // Set mGeometryInputPrimitiveType if exists
2884 if (layoutQualifier.primitiveType != EptUndefined)
2885 {
2886 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2887 {
2888 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2889 return false;
2890 }
2891
2892 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2893 {
2894 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002895 setGeometryShaderInputArraySize(
2896 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2897 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002898 }
2899 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2900 {
2901 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2902 "layout");
2903 return false;
2904 }
2905 }
2906
2907 // Set mGeometryInvocations if exists
2908 if (layoutQualifier.invocations > 0)
2909 {
2910 if (mGeometryShaderInvocations == 0)
2911 {
2912 mGeometryShaderInvocations = layoutQualifier.invocations;
2913 }
2914 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2915 {
2916 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2917 "layout");
2918 return false;
2919 }
2920 }
2921
2922 return true;
2923}
2924
2925bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2926{
2927 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2928
2929 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2930
2931 if (layoutQualifier.invocations > 0)
2932 {
2933 error(typeQualifier.line,
2934 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2935 return false;
2936 }
2937
2938 // Set mGeometryOutputPrimitiveType if exists
2939 if (layoutQualifier.primitiveType != EptUndefined)
2940 {
2941 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2942 {
2943 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2944 return false;
2945 }
2946
2947 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2948 {
2949 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2950 }
2951 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2952 {
2953 error(typeQualifier.line,
2954 "primitive doesn't match earlier output primitive declaration", "layout");
2955 return false;
2956 }
2957 }
2958
2959 // Set mGeometryMaxVertices if exists
2960 if (layoutQualifier.maxVertices > -1)
2961 {
2962 if (mGeometryShaderMaxVertices == -1)
2963 {
2964 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2965 }
2966 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2967 {
2968 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2969 "layout");
2970 return false;
2971 }
2972 }
2973
2974 return true;
2975}
2976
Martin Radev70866b82016-07-22 15:27:42 +03002977void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002978{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002979 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002980 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002981
Martin Radev70866b82016-07-22 15:27:42 +03002982 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2983 typeQualifier.line);
2984
Jamie Madillc2128ff2016-07-04 10:26:17 -04002985 // It should never be the case, but some strange parser errors can send us here.
2986 if (layoutQualifier.isEmpty())
2987 {
2988 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002989 return;
2990 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002991
Martin Radev802abe02016-08-04 17:48:32 +03002992 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002993 {
Olli Etuaho43364892017-02-13 16:00:12 +00002994 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002995 return;
2996 }
2997
Olli Etuaho43364892017-02-13 16:00:12 +00002998 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2999
3000 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003001
3002 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3003
Andrei Volykhina5527072017-03-22 16:46:30 +03003004 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3005
jchen104cdac9e2017-05-08 11:01:20 +08003006 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3007
Qin Jiajiaca68d982017-09-18 16:41:56 +08003008 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3009 typeQualifier.qualifier);
3010
Martin Radev802abe02016-08-04 17:48:32 +03003011 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003012 {
Martin Radev802abe02016-08-04 17:48:32 +03003013 if (mComputeShaderLocalSizeDeclared &&
3014 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3015 {
3016 error(typeQualifier.line, "Work group size does not match the previous declaration",
3017 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003018 return;
3019 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003020
Martin Radev802abe02016-08-04 17:48:32 +03003021 if (mShaderVersion < 310)
3022 {
3023 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003024 return;
3025 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003026
Martin Radev4c4c8e72016-08-04 12:25:34 +03003027 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003028 {
3029 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003030 return;
3031 }
3032
3033 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3034 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3035
3036 const TConstantUnion *maxComputeWorkGroupSizeData =
3037 maxComputeWorkGroupSize->getConstPointer();
3038
3039 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3040 {
3041 if (layoutQualifier.localSize[i] != -1)
3042 {
3043 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3044 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3045 if (mComputeShaderLocalSize[i] < 1 ||
3046 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3047 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003048 std::stringstream reasonStream;
3049 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3050 << maxComputeWorkGroupSizeValue;
3051 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003052
Olli Etuaho4de340a2016-12-16 09:32:03 +00003053 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003054 return;
3055 }
3056 }
3057 }
3058
3059 mComputeShaderLocalSizeDeclared = true;
3060 }
Shaob5cc1192017-07-06 10:47:20 +08003061 else if (typeQualifier.qualifier == EvqGeometryIn)
3062 {
3063 if (mShaderVersion < 310)
3064 {
3065 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3066 return;
3067 }
3068
3069 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3070 {
3071 return;
3072 }
3073 }
3074 else if (typeQualifier.qualifier == EvqGeometryOut)
3075 {
3076 if (mShaderVersion < 310)
3077 {
3078 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3079 "layout");
3080 return;
3081 }
3082
3083 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3084 {
3085 return;
3086 }
3087 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003088 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3089 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003090 {
3091 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3092 // specification.
3093 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3094 {
3095 error(typeQualifier.line, "Number of views does not match the previous declaration",
3096 "layout");
3097 return;
3098 }
3099
3100 if (layoutQualifier.numViews == -1)
3101 {
3102 error(typeQualifier.line, "No num_views specified", "layout");
3103 return;
3104 }
3105
3106 if (layoutQualifier.numViews > mMaxNumViews)
3107 {
3108 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3109 "layout");
3110 return;
3111 }
3112
3113 mNumViews = layoutQualifier.numViews;
3114 }
Martin Radev802abe02016-08-04 17:48:32 +03003115 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003116 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003117 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003118 {
Martin Radev802abe02016-08-04 17:48:32 +03003119 return;
3120 }
3121
Jiajia Qinbc585152017-06-23 15:42:17 +08003122 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003123 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003124 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003125 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003126 return;
3127 }
3128
3129 if (mShaderVersion < 300)
3130 {
3131 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3132 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003133 return;
3134 }
3135
Olli Etuaho09b04a22016-12-15 13:30:26 +00003136 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003137
3138 if (layoutQualifier.matrixPacking != EmpUnspecified)
3139 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003140 if (typeQualifier.qualifier == EvqUniform)
3141 {
3142 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3143 }
3144 else if (typeQualifier.qualifier == EvqBuffer)
3145 {
3146 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3147 }
Martin Radev802abe02016-08-04 17:48:32 +03003148 }
3149
3150 if (layoutQualifier.blockStorage != EbsUnspecified)
3151 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003152 if (typeQualifier.qualifier == EvqUniform)
3153 {
3154 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3155 }
3156 else if (typeQualifier.qualifier == EvqBuffer)
3157 {
3158 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3159 }
Martin Radev802abe02016-08-04 17:48:32 +03003160 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003161 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003162}
3163
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003164TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3165 const TFunction &function,
3166 const TSourceLoc &location,
3167 bool insertParametersToSymbolTable)
3168{
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003169 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003170
Olli Etuahofe486322017-03-21 09:30:54 +00003171 TIntermFunctionPrototype *prototype =
3172 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003173 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3174 // point to the data that already exists in the symbol table.
3175 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3176 prototype->setLine(location);
3177
3178 for (size_t i = 0; i < function.getParamCount(); i++)
3179 {
3180 const TConstParameter &param = function.getParam(i);
3181
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003182 TIntermSymbol *symbol = nullptr;
3183
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003184 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3185 // be used for unused args).
3186 if (param.name != nullptr)
3187 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003188 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003189 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003190 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003191 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3192 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003193 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003194 symbol = new TIntermSymbol(variable->uniqueId(), variable->name(),
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003195 variable->getType());
3196 }
3197 else
3198 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003199 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003200 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003201 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003202 // Unsized type of a named parameter should have already been checked and sanitized.
3203 ASSERT(!param.type->isUnsizedArray());
3204 }
3205 else
3206 {
3207 if (param.type->isUnsizedArray())
3208 {
3209 error(location, "function parameter array must be sized at compile time", "[]");
3210 // We don't need to size the arrays since the parameter is unnamed and hence
3211 // inaccessible.
3212 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003213 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003214 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003215 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003216 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3217 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003218 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003219 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003220 symbol->setLine(location);
3221 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003222 }
3223 return prototype;
3224}
3225
Olli Etuaho16c745a2017-01-16 17:02:27 +00003226TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3227 const TFunction &parsedFunction,
3228 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003229{
Olli Etuaho476197f2016-10-11 13:59:08 +01003230 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3231 // first declaration. Either way the instance in the symbol table is used to track whether the
3232 // function is declared multiple times.
3233 TFunction *function = static_cast<TFunction *>(
3234 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3235 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003236 {
3237 // ESSL 1.00.17 section 4.2.7.
3238 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3239 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003240 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003241 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003242
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003243 TIntermFunctionPrototype *prototype =
3244 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003245
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003246 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003247
3248 if (!symbolTable.atGlobalLevel())
3249 {
3250 // ESSL 3.00.4 section 4.2.4.
3251 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003252 }
3253
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003254 return prototype;
3255}
3256
Olli Etuaho336b1472016-10-05 16:37:55 +01003257TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003258 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003259 TIntermBlock *functionBody,
3260 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003261{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003262 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003263 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3264 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003265 error(location, "function does not return a value:",
3266 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003267 }
3268
Olli Etuahof51fdd22016-10-03 10:03:40 +01003269 if (functionBody == nullptr)
3270 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003271 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003272 functionBody->setLine(location);
3273 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003274 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003275 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003276 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003277
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003278 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003279 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003280}
3281
Olli Etuaho476197f2016-10-11 13:59:08 +01003282void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3283 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003284 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003285{
Olli Etuaho476197f2016-10-11 13:59:08 +01003286 ASSERT(function);
3287 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003288 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003289 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003290
3291 if (builtIn)
3292 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003293 error(location, "built-in functions cannot be redefined", (*function)->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003294 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003295 else
Jamie Madill185fb402015-06-12 15:48:48 -04003296 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003297 TFunction *prevDec = static_cast<TFunction *>(
3298 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3299
3300 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3301 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3302 // occurance.
3303 if (*function != prevDec)
3304 {
3305 // Swap the parameters of the previous declaration to the parameters of the function
3306 // definition (parameter names may differ).
3307 prevDec->swapParameters(**function);
3308
3309 // The function definition will share the same symbol as any previous declaration.
3310 *function = prevDec;
3311 }
3312
3313 if ((*function)->isDefined())
3314 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003315 error(location, "function already has a body", (*function)->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003316 }
3317
3318 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003319 }
Jamie Madill185fb402015-06-12 15:48:48 -04003320
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003321 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003322 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003323 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003324
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003325 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003326 setLoopNestingLevel(0);
3327}
3328
Jamie Madillb98c3a82015-07-23 14:26:04 -04003329TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003330{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003331 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003332 // We don't know at this point whether this is a function definition or a prototype.
3333 // The definition production code will check for redefinitions.
3334 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003335 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003336 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3337 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003338 //
3339 TFunction *prevDec =
3340 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303341
Olli Etuahod80f2942017-11-06 12:44:45 +02003342 for (size_t i = 0u; i < function->getParamCount(); ++i)
3343 {
3344 auto &param = function->getParam(i);
3345 if (param.type->isStructSpecifier())
3346 {
3347 // ESSL 3.00.6 section 12.10.
3348 error(location, "Function parameter type cannot be a structure definition",
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003349 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003350 }
3351 }
3352
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003353 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
3354 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303355 {
Martin Radevda6254b2016-12-14 17:00:36 +02003356 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303357 // Therefore overloading or redefining builtin functions is an error.
3358 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003359 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303360 }
3361 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003362 {
3363 if (prevDec->getReturnType() != function->getReturnType())
3364 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003365 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003366 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003367 }
3368 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3369 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003370 if (prevDec->getParam(i).type->getQualifier() !=
3371 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003372 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003373 error(location,
3374 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003375 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003376 }
3377 }
3378 }
3379
3380 //
3381 // Check for previously declared variables using the same name.
3382 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003383 TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003384 if (prevSym)
3385 {
3386 if (!prevSym->isFunction())
3387 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003388 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003389 }
3390 }
3391 else
3392 {
3393 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003394 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003395 }
3396
3397 // We're at the inner scope level of the function's arguments and body statement.
3398 // Add the function prototype to the surrounding scope instead.
3399 symbolTable.getOuterLevel()->insert(function);
3400
Olli Etuaho78d13742017-01-18 13:06:10 +00003401 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003402 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003403 {
3404 if (function->getParamCount() > 0)
3405 {
3406 error(location, "function cannot take any parameter(s)", "main");
3407 }
3408 if (function->getReturnType().getBasicType() != EbtVoid)
3409 {
3410 error(location, "main function cannot return a value",
3411 function->getReturnType().getBasicString());
3412 }
3413 }
3414
Jamie Madill185fb402015-06-12 15:48:48 -04003415 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003416 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3417 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003418 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3419 //
3420 return function;
3421}
3422
Olli Etuaho9de84a52016-06-14 17:36:01 +03003423TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3424 const TString *name,
3425 const TSourceLoc &location)
3426{
3427 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3428 {
3429 error(location, "no qualifiers allowed for function return",
3430 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003431 }
3432 if (!type.layoutQualifier.isEmpty())
3433 {
3434 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003435 }
jchen10cc2a10e2017-05-03 14:05:12 +08003436 // make sure an opaque type is not involved as well...
3437 std::string reason(getBasicString(type.getBasicType()));
3438 reason += "s can't be function return values";
3439 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003440 if (mShaderVersion < 300)
3441 {
3442 // Array return values are forbidden, but there's also no valid syntax for declaring array
3443 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003444 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003445
3446 if (type.isStructureContainingArrays())
3447 {
3448 // ESSL 1.00.17 section 6.1 Function Definitions
3449 error(location, "structures containing arrays can't be function return values",
3450 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003451 }
3452 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003453
3454 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003455 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003456}
3457
Olli Etuahocce89652017-06-19 16:04:09 +03003458TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3459{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003460 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003461 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3462 // really only interested in the mangled name of the function to look up the actual function
3463 // from the symbol table. If we just had the name string and the types of the parameters that
3464 // would be enough, but TFunction carries a lot of extra information in addition to that.
3465 // Besides function calls we do have to store constructor calls in the same data structure, for
3466 // them we need to store a TType.
3467 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved);
Olli Etuahocce89652017-06-19 16:04:09 +03003468}
3469
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003470TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003471{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003472 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003473 {
3474 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3475 "[]");
3476 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003477 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003478 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003479 error(publicType.getLine(), "constructor can't be a structure definition",
3480 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003481 }
3482
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003483 TType *type = new TType(publicType);
3484 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003485 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003486 error(publicType.getLine(), "cannot construct this type",
3487 getBasicString(publicType.getBasicType()));
3488 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003489 }
3490
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003491 return new TFunction(&symbolTable, nullptr, type, SymbolType::BuiltIn, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003492}
3493
Olli Etuaho55bde912017-10-25 13:41:13 +03003494void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3495 const char *errorMessage,
3496 const char *token,
3497 TType *arrayType)
3498{
3499 if (arrayType->isUnsizedArray())
3500 {
3501 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003502 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003503 }
3504}
3505
3506TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003507 const TString *name,
3508 const TSourceLoc &nameLoc)
3509{
Olli Etuaho55bde912017-10-25 13:41:13 +03003510 ASSERT(type);
3511 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3512 type);
3513 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003514 {
3515 error(nameLoc, "illegal use of type 'void'", name->c_str());
3516 }
3517 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003518 TParameter param = {name, type};
3519 return param;
3520}
3521
Olli Etuaho55bde912017-10-25 13:41:13 +03003522TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3523 const TString *name,
3524 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003525{
Olli Etuaho55bde912017-10-25 13:41:13 +03003526 TType *type = new TType(publicType);
3527 return parseParameterDeclarator(type, name, nameLoc);
3528}
3529
3530TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3531 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003532 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003533 const TSourceLoc &arrayLoc,
3534 TPublicType *elementType)
3535{
3536 checkArrayElementIsNotArray(arrayLoc, *elementType);
3537 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003538 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003539 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003540}
3541
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003542bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3543 TType type,
3544 const TSourceLoc &line)
3545{
3546 if (arguments->empty())
3547 {
3548 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3549 return false;
3550 }
3551 for (TIntermNode *arg : *arguments)
3552 {
3553 TIntermTyped *element = arg->getAsTyped();
3554 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003555 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3556 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003557 {
3558 error(line, "constructing from a non-dereferenced array", "constructor");
3559 return false;
3560 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003561 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003562 {
3563 if (dimensionalityFromElement == 1u)
3564 {
3565 error(line, "implicitly sized array of arrays constructor argument is not an array",
3566 "constructor");
3567 }
3568 else
3569 {
3570 error(line,
3571 "implicitly sized array of arrays constructor argument dimensionality is too "
3572 "low",
3573 "constructor");
3574 }
3575 return false;
3576 }
3577 }
3578 return true;
3579}
3580
Jamie Madillb98c3a82015-07-23 14:26:04 -04003581// This function is used to test for the correctness of the parameters passed to various constructor
3582// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003583//
Olli Etuaho856c4972016-08-08 11:38:39 +03003584// 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 +00003585//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003586TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003587 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303588 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003589{
Olli Etuaho856c4972016-08-08 11:38:39 +03003590 if (type.isUnsizedArray())
3591 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003592 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003593 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003594 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003595 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003596 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003597 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3598 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003599 if (type.getOutermostArraySize() == 0u)
3600 {
3601 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3602 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003603 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003604 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003605 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003606 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003607 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003608 }
3609 }
3610 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003611 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003612
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003613 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003614 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003615 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003616 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003617
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003618 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003619 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003620
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003621 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3622 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003623 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003624 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003625 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003626 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003627}
3628
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003629//
3630// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003631// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003632//
Olli Etuaho13389b62016-10-16 11:48:18 +01003633TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003634 const TTypeQualifierBuilder &typeQualifierBuilder,
3635 const TSourceLoc &nameLine,
3636 const TString &blockName,
3637 TFieldList *fieldList,
3638 const TString *instanceName,
3639 const TSourceLoc &instanceLine,
3640 TIntermTyped *arrayIndex,
3641 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003642{
Olli Etuaho856c4972016-08-08 11:38:39 +03003643 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003644
Olli Etuaho77ba4082016-12-16 12:01:18 +00003645 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003646
Jiajia Qinbc585152017-06-23 15:42:17 +08003647 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003648 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003649 error(typeQualifier.line,
3650 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3651 "3.10",
3652 getQualifierString(typeQualifier.qualifier));
3653 }
3654 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3655 {
3656 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003657 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003658 }
3659
Martin Radev70866b82016-07-22 15:27:42 +03003660 if (typeQualifier.invariant)
3661 {
3662 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3663 }
3664
Jiajia Qinbc585152017-06-23 15:42:17 +08003665 if (typeQualifier.qualifier != EvqBuffer)
3666 {
3667 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3668 }
Olli Etuaho43364892017-02-13 16:00:12 +00003669
jchen10af713a22017-04-19 09:10:56 +08003670 // add array index
3671 unsigned int arraySize = 0;
3672 if (arrayIndex != nullptr)
3673 {
3674 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3675 }
3676
3677 if (mShaderVersion < 310)
3678 {
3679 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3680 }
3681 else
3682 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003683 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3684 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003685 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003686
Andrei Volykhina5527072017-03-22 16:46:30 +03003687 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3688
Jamie Madill099c0f32013-06-20 11:55:52 -04003689 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003690 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003691 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3692 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003693
Jamie Madill099c0f32013-06-20 11:55:52 -04003694 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3695 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003696 if (typeQualifier.qualifier == EvqUniform)
3697 {
3698 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3699 }
3700 else if (typeQualifier.qualifier == EvqBuffer)
3701 {
3702 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3703 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003704 }
3705
Jamie Madill1566ef72013-06-20 11:55:54 -04003706 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3707 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003708 if (typeQualifier.qualifier == EvqUniform)
3709 {
3710 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3711 }
3712 else if (typeQualifier.qualifier == EvqBuffer)
3713 {
3714 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3715 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003716 }
3717
Olli Etuaho856c4972016-08-08 11:38:39 +03003718 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003719
Martin Radev2cc85b32016-08-05 16:22:53 +03003720 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3721
Jamie Madill98493dd2013-07-08 14:39:03 -04003722 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303723 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3724 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003725 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303726 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003727 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303728 {
jchen10cc2a10e2017-05-03 14:05:12 +08003729 std::string reason("unsupported type - ");
3730 reason += fieldType->getBasicString();
3731 reason += " types are not allowed in interface blocks";
3732 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003733 }
3734
Jamie Madill98493dd2013-07-08 14:39:03 -04003735 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003736 switch (qualifier)
3737 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003738 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003739 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003740 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003741 if (typeQualifier.qualifier == EvqBuffer)
3742 {
3743 error(field->line(), "invalid qualifier on shader storage block member",
3744 getQualifierString(qualifier));
3745 }
3746 break;
3747 case EvqBuffer:
3748 if (typeQualifier.qualifier == EvqUniform)
3749 {
3750 error(field->line(), "invalid qualifier on uniform block member",
3751 getQualifierString(qualifier));
3752 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003753 break;
3754 default:
3755 error(field->line(), "invalid qualifier on interface block member",
3756 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003757 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003758 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003759
Martin Radev70866b82016-07-22 15:27:42 +03003760 if (fieldType->isInvariant())
3761 {
3762 error(field->line(), "invalid qualifier on interface block member", "invariant");
3763 }
3764
Jamie Madilla5efff92013-06-06 11:56:47 -04003765 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003766 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003767 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003768 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003769
Jamie Madill98493dd2013-07-08 14:39:03 -04003770 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003771 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003772 error(field->line(), "invalid layout qualifier: cannot be used here",
3773 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003774 }
3775
Jamie Madill98493dd2013-07-08 14:39:03 -04003776 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003777 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003778 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003779 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003780 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003781 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003782 warning(field->line(),
3783 "extraneous layout qualifier: only has an effect on matrix types",
3784 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003785 }
3786
Jamie Madill98493dd2013-07-08 14:39:03 -04003787 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003788
Olli Etuahoebee5b32017-11-23 12:56:32 +02003789 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3790 typeQualifier.qualifier != EvqBuffer)
3791 {
3792 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3793 checkIsNotUnsizedArray(field->line(),
3794 "array members of interface blocks must specify a size",
3795 field->name().c_str(), field->type());
3796 }
3797
Jiajia Qinbc585152017-06-23 15:42:17 +08003798 if (typeQualifier.qualifier == EvqBuffer)
3799 {
3800 // set memory qualifiers
3801 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3802 // qualified with a memory qualifier, it is as if all of its members were declared with
3803 // the same memory qualifier.
3804 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3805 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3806 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3807 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3808 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3809 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3810 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3811 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3812 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3813 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3814 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003815 }
3816
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003817 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3818 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003819 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3820 {
3821 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3822 }
3823
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003824 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3825 if (arrayIndex != nullptr)
3826 {
3827 interfaceBlockType.makeArray(arraySize);
3828 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003829
3830 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003831 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003832
Jamie Madill98493dd2013-07-08 14:39:03 -04003833 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003834 {
3835 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003836 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3837 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003838 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303839 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003840
3841 // set parent pointer of the field variable
3842 fieldType->setInterfaceBlock(interfaceBlock);
3843
Olli Etuaho0f684632017-07-13 12:42:15 +03003844 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003845
Olli Etuaho0f684632017-07-13 12:42:15 +03003846 if (fieldVariable)
3847 {
3848 fieldVariable->setQualifier(typeQualifier.qualifier);
3849 }
3850 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303851 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003852 error(field->line(), "redefinition of an interface block member name",
3853 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003854 }
3855 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003856 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003857 }
3858 else
3859 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003860 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003861
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003862 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003863 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3864 if (instanceTypeDef)
3865 {
3866 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003867 symbolId = &instanceTypeDef->uniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003868 }
3869 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303870 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003871 error(instanceLine, "redefinition of an interface block instance name",
3872 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003873 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003874 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003875 }
3876
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003877 TIntermDeclaration *declaration = nullptr;
3878
3879 if (symbolId)
3880 {
3881 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3882 blockSymbol->setLine(typeQualifier.line);
3883 declaration = new TIntermDeclaration();
3884 declaration->appendDeclarator(blockSymbol);
3885 declaration->setLine(nameLine);
3886 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003887
3888 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003889 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003890}
3891
Olli Etuaho383b7912016-08-05 11:22:59 +03003892void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003893{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003894 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003895
3896 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003897 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303898 if (mStructNestingLevel > 1)
3899 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003900 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003901 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003902}
3903
3904void TParseContext::exitStructDeclaration()
3905{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003906 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003907}
3908
Olli Etuaho8a176262016-08-16 14:23:01 +03003909void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003910{
Jamie Madillacb4b812016-11-07 13:50:29 -05003911 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303912 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003913 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003914 }
3915
Arun Patole7e7e68d2015-05-22 12:02:25 +05303916 if (field.type()->getBasicType() != EbtStruct)
3917 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003918 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003919 }
3920
3921 // We're already inside a structure definition at this point, so add
3922 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303923 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3924 {
Jamie Madill41a49272014-03-18 16:10:13 -04003925 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003926 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3927 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003928 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003929 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003930 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003931 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003932}
3933
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003934//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003935// Parse an array index expression
3936//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003937TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3938 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303939 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003940{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003941 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3942 {
3943 if (baseExpression->getAsSymbolNode())
3944 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303945 error(location, " left of '[' is not of type array, matrix, or vector ",
3946 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003947 }
3948 else
3949 {
3950 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3951 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003952
Olli Etuaho3ec75682017-07-05 17:02:55 +03003953 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003954 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003955
Jiawei Shaod8105a02017-08-08 09:54:36 +08003956 if (baseExpression->getQualifier() == EvqPerVertexIn)
3957 {
3958 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3959 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3960 {
3961 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3962 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3963 }
3964 }
3965
Jamie Madill21c1e452014-12-29 11:33:41 -05003966 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3967
Olli Etuaho36b05142015-11-12 13:10:42 +02003968 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3969 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3970 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3971 // index is a constant expression.
3972 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3973 {
3974 if (baseExpression->isInterfaceBlock())
3975 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003976 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3977 switch (baseExpression->getQualifier())
3978 {
3979 case EvqPerVertexIn:
3980 break;
3981 case EvqUniform:
3982 case EvqBuffer:
3983 error(location,
3984 "array indexes for uniform block arrays and shader storage block arrays "
3985 "must be constant integral expressions",
3986 "[");
3987 break;
3988 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003989 // We can reach here only in error cases.
3990 ASSERT(mDiagnostics->numErrors() > 0);
3991 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003992 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003993 }
3994 else if (baseExpression->getQualifier() == EvqFragmentOut)
3995 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003996 error(location,
3997 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003998 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003999 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
4000 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004001 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02004002 }
Olli Etuaho36b05142015-11-12 13:10:42 +02004003 }
4004
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004005 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04004006 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004007 // If an out-of-range index is not qualified as constant, the behavior in the spec is
4008 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
4009 // constant fold expressions that are not constant expressions). The most compatible way to
4010 // handle this case is to report a warning instead of an error and force the index to be in
4011 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004012 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03004013 int index = 0;
4014 if (indexConstantUnion->getBasicType() == EbtInt)
4015 {
4016 index = indexConstantUnion->getIConst(0);
4017 }
4018 else if (indexConstantUnion->getBasicType() == EbtUInt)
4019 {
4020 index = static_cast<int>(indexConstantUnion->getUConst(0));
4021 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004022
4023 int safeIndex = -1;
4024
Olli Etuahoebee5b32017-11-23 12:56:32 +02004025 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004026 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004027 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4028 safeIndex = 0;
4029 }
4030
4031 if (!baseExpression->getType().isUnsizedArray())
4032 {
4033 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004034 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004035 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004036 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004037 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4038 {
4039 outOfRangeError(outOfRangeIndexIsError, location,
4040 "array index for gl_FragData must be zero when "
4041 "GL_EXT_draw_buffers is disabled",
4042 "[]");
4043 safeIndex = 0;
4044 }
4045 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004046 }
4047 // Only do generic out-of-range check if similar error hasn't already been reported.
4048 if (safeIndex < 0)
4049 {
4050 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004051 {
4052 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4053 baseExpression->getOutermostArraySize(),
4054 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004055 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004056 else if (baseExpression->isMatrix())
4057 {
4058 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4059 baseExpression->getType().getCols(),
4060 "matrix field selection out of range");
4061 }
4062 else
4063 {
4064 ASSERT(baseExpression->isVector());
4065 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4066 baseExpression->getType().getNominalSize(),
4067 "vector field selection out of range");
4068 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004069 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004070
Olli Etuahoebee5b32017-11-23 12:56:32 +02004071 ASSERT(safeIndex >= 0);
4072 // Data of constant unions can't be changed, because it may be shared with other
4073 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4074 // sanitized object.
4075 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4076 {
4077 TConstantUnion *safeConstantUnion = new TConstantUnion();
4078 safeConstantUnion->setIConst(safeIndex);
4079 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4080 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4081 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004082
Olli Etuahoebee5b32017-11-23 12:56:32 +02004083 TIntermBinary *node =
4084 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4085 node->setLine(location);
4086 return node->fold(mDiagnostics);
4087 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004088 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004089
4090 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4091 node->setLine(location);
4092 // Indirect indexing can never be constant folded.
4093 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004094}
4095
Olli Etuahoebee5b32017-11-23 12:56:32 +02004096int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4097 const TSourceLoc &location,
4098 int index,
4099 int arraySize,
4100 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004101{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004102 // Should not reach here with an unsized / runtime-sized array.
4103 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004104 // A negative index should already have been checked.
4105 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004106 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004107 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004108 std::stringstream reasonStream;
4109 reasonStream << reason << " '" << index << "'";
4110 std::string token = reasonStream.str();
4111 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004112 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004113 }
4114 return index;
4115}
4116
Jamie Madillb98c3a82015-07-23 14:26:04 -04004117TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4118 const TSourceLoc &dotLocation,
4119 const TString &fieldString,
4120 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004122 if (baseExpression->isArray())
4123 {
4124 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004125 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004126 }
4127
4128 if (baseExpression->isVector())
4129 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004130 TVector<int> fieldOffsets;
4131 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4132 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004133 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004134 fieldOffsets.resize(1);
4135 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004136 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004137 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4138 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004139
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004140 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004141 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004142 else if (baseExpression->getBasicType() == EbtStruct)
4143 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304144 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004145 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004146 {
4147 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004148 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004149 }
4150 else
4151 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004152 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004153 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004154 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004155 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004156 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004157 {
4158 fieldFound = true;
4159 break;
4160 }
4161 }
4162 if (fieldFound)
4163 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004164 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004165 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004166 TIntermBinary *node =
4167 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4168 node->setLine(dotLocation);
4169 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004170 }
4171 else
4172 {
4173 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004174 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004175 }
4176 }
4177 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004178 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004179 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304180 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004181 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004182 {
4183 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004184 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004185 }
4186 else
4187 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004188 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004189 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004190 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004191 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004192 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004193 {
4194 fieldFound = true;
4195 break;
4196 }
4197 }
4198 if (fieldFound)
4199 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004200 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004201 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004202 TIntermBinary *node =
4203 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4204 node->setLine(dotLocation);
4205 // Indexing interface blocks can never be constant folded.
4206 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004207 }
4208 else
4209 {
4210 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004211 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004212 }
4213 }
4214 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004215 else
4216 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004217 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004218 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004219 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304220 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004221 }
4222 else
4223 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304224 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004225 " field selection requires structure, vector, or interface block on left hand "
4226 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304227 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004228 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004229 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004230 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004231}
4232
Jamie Madillb98c3a82015-07-23 14:26:04 -04004233TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4234 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004235{
Jamie Madill2f294c92017-11-20 14:47:26 -05004236 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004237
4238 if (qualifierType == "shared")
4239 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004240 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004241 {
4242 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4243 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004244 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004245 }
4246 else if (qualifierType == "packed")
4247 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004248 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004249 {
4250 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4251 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004252 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004253 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004254 else if (qualifierType == "std430")
4255 {
4256 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4257 qualifier.blockStorage = EbsStd430;
4258 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004259 else if (qualifierType == "std140")
4260 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004261 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004262 }
4263 else if (qualifierType == "row_major")
4264 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004265 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004266 }
4267 else if (qualifierType == "column_major")
4268 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004269 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004270 }
4271 else if (qualifierType == "location")
4272 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004273 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4274 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004275 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004276 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004277 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004278 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4279 {
4280 qualifier.yuv = true;
4281 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004282 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004283 else if (qualifierType == "rgba32f")
4284 {
4285 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4286 qualifier.imageInternalFormat = EiifRGBA32F;
4287 }
4288 else if (qualifierType == "rgba16f")
4289 {
4290 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4291 qualifier.imageInternalFormat = EiifRGBA16F;
4292 }
4293 else if (qualifierType == "r32f")
4294 {
4295 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4296 qualifier.imageInternalFormat = EiifR32F;
4297 }
4298 else if (qualifierType == "rgba8")
4299 {
4300 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4301 qualifier.imageInternalFormat = EiifRGBA8;
4302 }
4303 else if (qualifierType == "rgba8_snorm")
4304 {
4305 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4306 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4307 }
4308 else if (qualifierType == "rgba32i")
4309 {
4310 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4311 qualifier.imageInternalFormat = EiifRGBA32I;
4312 }
4313 else if (qualifierType == "rgba16i")
4314 {
4315 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4316 qualifier.imageInternalFormat = EiifRGBA16I;
4317 }
4318 else if (qualifierType == "rgba8i")
4319 {
4320 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4321 qualifier.imageInternalFormat = EiifRGBA8I;
4322 }
4323 else if (qualifierType == "r32i")
4324 {
4325 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4326 qualifier.imageInternalFormat = EiifR32I;
4327 }
4328 else if (qualifierType == "rgba32ui")
4329 {
4330 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4331 qualifier.imageInternalFormat = EiifRGBA32UI;
4332 }
4333 else if (qualifierType == "rgba16ui")
4334 {
4335 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4336 qualifier.imageInternalFormat = EiifRGBA16UI;
4337 }
4338 else if (qualifierType == "rgba8ui")
4339 {
4340 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4341 qualifier.imageInternalFormat = EiifRGBA8UI;
4342 }
4343 else if (qualifierType == "r32ui")
4344 {
4345 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4346 qualifier.imageInternalFormat = EiifR32UI;
4347 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004348 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4349 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004350 {
4351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4352 qualifier.primitiveType = EptPoints;
4353 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004354 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4355 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004356 {
4357 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4358 qualifier.primitiveType = EptLines;
4359 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004360 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4361 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004362 {
4363 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4364 qualifier.primitiveType = EptLinesAdjacency;
4365 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004366 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4367 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004368 {
4369 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4370 qualifier.primitiveType = EptTriangles;
4371 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004372 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4373 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004374 {
4375 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4376 qualifier.primitiveType = EptTrianglesAdjacency;
4377 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004378 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4379 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004380 {
4381 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4382 qualifier.primitiveType = EptLineStrip;
4383 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004384 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4385 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004386 {
4387 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4388 qualifier.primitiveType = EptTriangleStrip;
4389 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004390
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004391 else
4392 {
4393 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004394 }
4395
Jamie Madilla5efff92013-06-06 11:56:47 -04004396 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004397}
4398
Martin Radev802abe02016-08-04 17:48:32 +03004399void TParseContext::parseLocalSize(const TString &qualifierType,
4400 const TSourceLoc &qualifierTypeLine,
4401 int intValue,
4402 const TSourceLoc &intValueLine,
4403 const std::string &intValueString,
4404 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004405 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004406{
Olli Etuaho856c4972016-08-08 11:38:39 +03004407 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004408 if (intValue < 1)
4409 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004410 std::stringstream reasonStream;
4411 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4412 std::string reason = reasonStream.str();
4413 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004414 }
4415 (*localSize)[index] = intValue;
4416}
4417
Olli Etuaho09b04a22016-12-15 13:30:26 +00004418void TParseContext::parseNumViews(int intValue,
4419 const TSourceLoc &intValueLine,
4420 const std::string &intValueString,
4421 int *numViews)
4422{
4423 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4424 // specification.
4425 if (intValue < 1)
4426 {
4427 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4428 }
4429 *numViews = intValue;
4430}
4431
Shaob5cc1192017-07-06 10:47:20 +08004432void TParseContext::parseInvocations(int intValue,
4433 const TSourceLoc &intValueLine,
4434 const std::string &intValueString,
4435 int *numInvocations)
4436{
4437 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4438 // it doesn't make sense to accept invocations <= 0.
4439 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4440 {
4441 error(intValueLine,
4442 "out of range: invocations must be in the range of [1, "
4443 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4444 intValueString.c_str());
4445 }
4446 else
4447 {
4448 *numInvocations = intValue;
4449 }
4450}
4451
4452void TParseContext::parseMaxVertices(int intValue,
4453 const TSourceLoc &intValueLine,
4454 const std::string &intValueString,
4455 int *maxVertices)
4456{
4457 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4458 // it doesn't make sense to accept max_vertices < 0.
4459 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4460 {
4461 error(
4462 intValueLine,
4463 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4464 intValueString.c_str());
4465 }
4466 else
4467 {
4468 *maxVertices = intValue;
4469 }
4470}
4471
Jamie Madillb98c3a82015-07-23 14:26:04 -04004472TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4473 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004474 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304475 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004476{
Jamie Madill2f294c92017-11-20 14:47:26 -05004477 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004478
Martin Radev802abe02016-08-04 17:48:32 +03004479 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004480
Martin Radev802abe02016-08-04 17:48:32 +03004481 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004482 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004483 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004484 if (intValue < 0)
4485 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004486 error(intValueLine, "out of range: location must be non-negative",
4487 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004488 }
4489 else
4490 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004491 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004492 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004493 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004494 }
Olli Etuaho43364892017-02-13 16:00:12 +00004495 else if (qualifierType == "binding")
4496 {
4497 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4498 if (intValue < 0)
4499 {
4500 error(intValueLine, "out of range: binding must be non-negative",
4501 intValueString.c_str());
4502 }
4503 else
4504 {
4505 qualifier.binding = intValue;
4506 }
4507 }
jchen104cdac9e2017-05-08 11:01:20 +08004508 else if (qualifierType == "offset")
4509 {
4510 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4511 if (intValue < 0)
4512 {
4513 error(intValueLine, "out of range: offset must be non-negative",
4514 intValueString.c_str());
4515 }
4516 else
4517 {
4518 qualifier.offset = intValue;
4519 }
4520 }
Martin Radev802abe02016-08-04 17:48:32 +03004521 else if (qualifierType == "local_size_x")
4522 {
4523 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4524 &qualifier.localSize);
4525 }
4526 else if (qualifierType == "local_size_y")
4527 {
4528 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4529 &qualifier.localSize);
4530 }
4531 else if (qualifierType == "local_size_z")
4532 {
4533 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4534 &qualifier.localSize);
4535 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004536 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004537 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004538 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4539 {
4540 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4541 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004542 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004543 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4544 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004545 {
4546 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4547 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004548 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4549 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004550 {
4551 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4552 }
4553
Martin Radev802abe02016-08-04 17:48:32 +03004554 else
4555 {
4556 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004557 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004558
Jamie Madilla5efff92013-06-06 11:56:47 -04004559 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004560}
4561
Olli Etuaho613b9592016-09-05 12:05:53 +03004562TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4563{
4564 return new TTypeQualifierBuilder(
4565 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4566 mShaderVersion);
4567}
4568
Olli Etuahocce89652017-06-19 16:04:09 +03004569TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4570 const TSourceLoc &loc)
4571{
4572 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4573 return new TStorageQualifierWrapper(qualifier, loc);
4574}
4575
4576TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4577{
4578 if (getShaderType() == GL_VERTEX_SHADER)
4579 {
4580 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4581 }
4582 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4583}
4584
4585TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4586{
4587 if (declaringFunction())
4588 {
4589 return new TStorageQualifierWrapper(EvqIn, loc);
4590 }
Shaob5cc1192017-07-06 10:47:20 +08004591
4592 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004593 {
Shaob5cc1192017-07-06 10:47:20 +08004594 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004595 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004596 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004597 {
4598 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4599 }
4600 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004601 }
Shaob5cc1192017-07-06 10:47:20 +08004602 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004603 {
Shaob5cc1192017-07-06 10:47:20 +08004604 if (mShaderVersion < 300)
4605 {
4606 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4607 }
4608 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004609 }
Shaob5cc1192017-07-06 10:47:20 +08004610 case GL_COMPUTE_SHADER:
4611 {
4612 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4613 }
4614 case GL_GEOMETRY_SHADER_OES:
4615 {
4616 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4617 }
4618 default:
4619 {
4620 UNREACHABLE();
4621 return new TStorageQualifierWrapper(EvqLast, loc);
4622 }
Olli Etuahocce89652017-06-19 16:04:09 +03004623 }
Olli Etuahocce89652017-06-19 16:04:09 +03004624}
4625
4626TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4627{
4628 if (declaringFunction())
4629 {
4630 return new TStorageQualifierWrapper(EvqOut, loc);
4631 }
Shaob5cc1192017-07-06 10:47:20 +08004632 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004633 {
Shaob5cc1192017-07-06 10:47:20 +08004634 case GL_VERTEX_SHADER:
4635 {
4636 if (mShaderVersion < 300)
4637 {
4638 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4639 }
4640 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4641 }
4642 case GL_FRAGMENT_SHADER:
4643 {
4644 if (mShaderVersion < 300)
4645 {
4646 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4647 }
4648 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4649 }
4650 case GL_COMPUTE_SHADER:
4651 {
4652 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4653 return new TStorageQualifierWrapper(EvqLast, loc);
4654 }
4655 case GL_GEOMETRY_SHADER_OES:
4656 {
4657 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4658 }
4659 default:
4660 {
4661 UNREACHABLE();
4662 return new TStorageQualifierWrapper(EvqLast, loc);
4663 }
Olli Etuahocce89652017-06-19 16:04:09 +03004664 }
Olli Etuahocce89652017-06-19 16:04:09 +03004665}
4666
4667TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4668{
4669 if (!declaringFunction())
4670 {
4671 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4672 }
4673 return new TStorageQualifierWrapper(EvqInOut, loc);
4674}
4675
Jamie Madillb98c3a82015-07-23 14:26:04 -04004676TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004677 TLayoutQualifier rightQualifier,
4678 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004679{
Martin Radevc28888b2016-07-22 15:27:42 +03004680 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004681 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004682}
4683
Olli Etuahod5f44c92017-11-29 17:15:40 +02004684TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004685{
4686 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004687 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004688}
4689
Olli Etuahod5f44c92017-11-29 17:15:40 +02004690TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4691 const TSourceLoc &loc,
4692 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004693{
4694 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004695 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004696}
4697
Olli Etuaho722bfb52017-10-26 17:00:11 +03004698void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4699 const TFieldList::const_iterator end,
4700 const TString &name,
4701 const TSourceLoc &location)
4702{
4703 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4704 {
4705 if ((*fieldIter)->name() == name)
4706 {
4707 error(location, "duplicate field name in structure", name.c_str());
4708 }
4709 }
4710}
4711
4712TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4713{
4714 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4715 ++fieldIter)
4716 {
4717 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4718 location);
4719 }
4720 return fields;
4721}
4722
Olli Etuaho4de340a2016-12-16 09:32:03 +00004723TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4724 const TFieldList *newlyAddedFields,
4725 const TSourceLoc &location)
4726{
4727 for (TField *field : *newlyAddedFields)
4728 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004729 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4730 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004731 processedFields->push_back(field);
4732 }
4733 return processedFields;
4734}
4735
Martin Radev70866b82016-07-22 15:27:42 +03004736TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4737 const TTypeQualifierBuilder &typeQualifierBuilder,
4738 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004739 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004740{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004741 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004742
Martin Radev70866b82016-07-22 15:27:42 +03004743 typeSpecifier->qualifier = typeQualifier.qualifier;
4744 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004745 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004746 typeSpecifier->invariant = typeQualifier.invariant;
4747 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304748 {
Martin Radev70866b82016-07-22 15:27:42 +03004749 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004750 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004751 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004752}
4753
Jamie Madillb98c3a82015-07-23 14:26:04 -04004754TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004755 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004756{
Martin Radev4a9cd802016-09-01 16:51:51 +03004757 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4758 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004759
Olli Etuahod5f44c92017-11-29 17:15:40 +02004760 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004761 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004762
Martin Radev4a9cd802016-09-01 16:51:51 +03004763 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004764
Olli Etuahod5f44c92017-11-29 17:15:40 +02004765 TFieldList *fieldList = new TFieldList();
4766
4767 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304768 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004769 TType *type = new TType(typeSpecifier);
4770 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304771 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004772 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004773 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004774 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004775 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004776
Olli Etuahod5f44c92017-11-29 17:15:40 +02004777 TField *field = new TField(type, declarator->name(), declarator->line());
4778 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4779 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004780 }
4781
Olli Etuahod5f44c92017-11-29 17:15:40 +02004782 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004783}
4784
Martin Radev4a9cd802016-09-01 16:51:51 +03004785TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4786 const TSourceLoc &nameLine,
4787 const TString *structName,
4788 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004789{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004790 SymbolType structSymbolType = SymbolType::UserDefined;
4791 if (structName == nullptr)
4792 {
4793 structName = NewPoolTString("");
4794 structSymbolType = SymbolType::Empty;
4795 }
4796 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004797
Jamie Madill9b820842015-02-12 10:40:10 -05004798 // Store a bool in the struct if we're at global scope, to allow us to
4799 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004800 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004801
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004802 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004803 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004804 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004805 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304806 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004807 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004808 }
4809 }
4810
4811 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004812 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004813 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004814 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004815 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004816 switch (qualifier)
4817 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004818 case EvqGlobal:
4819 case EvqTemporary:
4820 break;
4821 default:
4822 error(field.line(), "invalid qualifier on struct member",
4823 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004824 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004825 }
Martin Radev70866b82016-07-22 15:27:42 +03004826 if (field.type()->isInvariant())
4827 {
4828 error(field.line(), "invalid qualifier on struct member", "invariant");
4829 }
jchen104cdac9e2017-05-08 11:01:20 +08004830 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4831 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004832 {
4833 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4834 }
4835
Olli Etuahoebee5b32017-11-23 12:56:32 +02004836 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4837 field.name().c_str(), field.type());
4838
Olli Etuaho43364892017-02-13 16:00:12 +00004839 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4840
4841 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004842
4843 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004844 }
4845
Martin Radev4a9cd802016-09-01 16:51:51 +03004846 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004847 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004848 exitStructDeclaration();
4849
Martin Radev4a9cd802016-09-01 16:51:51 +03004850 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004851}
4852
Jamie Madillb98c3a82015-07-23 14:26:04 -04004853TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004854 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004855 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004856{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004857 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004858 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004859 init->isVector())
4860 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004861 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4862 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004863 return nullptr;
4864 }
4865
Olli Etuaho923ecef2017-10-11 12:01:38 +03004866 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004867 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004868 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004869 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004870 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004871 }
4872
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004873 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4874 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004875 return node;
4876}
4877
4878TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4879{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004880 if (mSwitchNestingLevel == 0)
4881 {
4882 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004883 return nullptr;
4884 }
4885 if (condition == nullptr)
4886 {
4887 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004888 return nullptr;
4889 }
4890 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004891 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004892 {
4893 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004894 }
4895 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004896 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4897 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4898 // fold in case labels.
4899 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004900 {
4901 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004902 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004903 TIntermCase *node = new TIntermCase(condition);
4904 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004905 return node;
4906}
4907
4908TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4909{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004910 if (mSwitchNestingLevel == 0)
4911 {
4912 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004913 return nullptr;
4914 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004915 TIntermCase *node = new TIntermCase(nullptr);
4916 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004917 return node;
4918}
4919
Jamie Madillb98c3a82015-07-23 14:26:04 -04004920TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4921 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004922 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004923{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004924 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004925
4926 switch (op)
4927 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004928 case EOpLogicalNot:
4929 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4930 child->isVector())
4931 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004932 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004933 return nullptr;
4934 }
4935 break;
4936 case EOpBitwiseNot:
4937 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4938 child->isMatrix() || child->isArray())
4939 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004940 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004941 return nullptr;
4942 }
4943 break;
4944 case EOpPostIncrement:
4945 case EOpPreIncrement:
4946 case EOpPostDecrement:
4947 case EOpPreDecrement:
4948 case EOpNegative:
4949 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004950 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4951 child->getBasicType() == EbtBool || child->isArray() ||
4952 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004953 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004954 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004955 return nullptr;
4956 }
4957 // Operators for built-ins are already type checked against their prototype.
4958 default:
4959 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004960 }
4961
Jiajia Qinbc585152017-06-23 15:42:17 +08004962 if (child->getMemoryQualifier().writeonly)
4963 {
4964 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4965 return nullptr;
4966 }
4967
Olli Etuahof119a262016-08-19 15:54:22 +03004968 TIntermUnary *node = new TIntermUnary(op, child);
4969 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004970
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004971 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004972}
4973
Olli Etuaho09b22472015-02-11 11:47:26 +02004974TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4975{
Olli Etuahocce89652017-06-19 16:04:09 +03004976 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004977 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004978 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004979 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004980 return child;
4981 }
4982 return node;
4983}
4984
Jamie Madillb98c3a82015-07-23 14:26:04 -04004985TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4986 TIntermTyped *child,
4987 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004988{
Olli Etuaho856c4972016-08-08 11:38:39 +03004989 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004990 return addUnaryMath(op, child, loc);
4991}
4992
Jamie Madillb98c3a82015-07-23 14:26:04 -04004993bool TParseContext::binaryOpCommonCheck(TOperator op,
4994 TIntermTyped *left,
4995 TIntermTyped *right,
4996 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004997{
jchen10b4cf5652017-05-05 18:51:17 +08004998 // Check opaque types are not allowed to be operands in expressions other than array indexing
4999 // and structure member selection.
5000 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
5001 {
5002 switch (op)
5003 {
5004 case EOpIndexDirect:
5005 case EOpIndexIndirect:
5006 break;
5007 case EOpIndexDirectStruct:
5008 UNREACHABLE();
5009
5010 default:
5011 error(loc, "Invalid operation for variables with an opaque type",
5012 GetOperatorString(op));
5013 return false;
5014 }
5015 }
jchen10cc2a10e2017-05-03 14:05:12 +08005016
Jiajia Qinbc585152017-06-23 15:42:17 +08005017 if (right->getMemoryQualifier().writeonly)
5018 {
5019 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5020 return false;
5021 }
5022
5023 if (left->getMemoryQualifier().writeonly)
5024 {
5025 switch (op)
5026 {
5027 case EOpAssign:
5028 case EOpInitialize:
5029 case EOpIndexDirect:
5030 case EOpIndexIndirect:
5031 case EOpIndexDirectStruct:
5032 case EOpIndexDirectInterfaceBlock:
5033 break;
5034 default:
5035 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5036 return false;
5037 }
5038 }
5039
Olli Etuaho244be012016-08-18 15:26:02 +03005040 if (left->getType().getStruct() || right->getType().getStruct())
5041 {
5042 switch (op)
5043 {
5044 case EOpIndexDirectStruct:
5045 ASSERT(left->getType().getStruct());
5046 break;
5047 case EOpEqual:
5048 case EOpNotEqual:
5049 case EOpAssign:
5050 case EOpInitialize:
5051 if (left->getType() != right->getType())
5052 {
5053 return false;
5054 }
5055 break;
5056 default:
5057 error(loc, "Invalid operation for structs", GetOperatorString(op));
5058 return false;
5059 }
5060 }
5061
Olli Etuaho94050052017-05-08 14:17:44 +03005062 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5063 {
5064 switch (op)
5065 {
5066 case EOpIndexDirectInterfaceBlock:
5067 ASSERT(left->getType().getInterfaceBlock());
5068 break;
5069 default:
5070 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5071 return false;
5072 }
5073 }
5074
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005075 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005076 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005077 error(loc, "array / non-array mismatch", GetOperatorString(op));
5078 return false;
5079 }
5080
5081 if (left->isArray())
5082 {
5083 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005084 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005085 {
5086 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5087 return false;
5088 }
5089
Olli Etuahoe79904c2015-03-18 16:56:42 +02005090 switch (op)
5091 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005092 case EOpEqual:
5093 case EOpNotEqual:
5094 case EOpAssign:
5095 case EOpInitialize:
5096 break;
5097 default:
5098 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5099 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005100 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005101 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005102 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005103 {
5104 error(loc, "array size mismatch", GetOperatorString(op));
5105 return false;
5106 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005107 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005108
5109 // Check ops which require integer / ivec parameters
5110 bool isBitShift = false;
5111 switch (op)
5112 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005113 case EOpBitShiftLeft:
5114 case EOpBitShiftRight:
5115 case EOpBitShiftLeftAssign:
5116 case EOpBitShiftRightAssign:
5117 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5118 // check that the basic type is an integer type.
5119 isBitShift = true;
5120 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5121 {
5122 return false;
5123 }
5124 break;
5125 case EOpBitwiseAnd:
5126 case EOpBitwiseXor:
5127 case EOpBitwiseOr:
5128 case EOpBitwiseAndAssign:
5129 case EOpBitwiseXorAssign:
5130 case EOpBitwiseOrAssign:
5131 // It is enough to check the type of only one operand, since later it
5132 // is checked that the operand types match.
5133 if (!IsInteger(left->getBasicType()))
5134 {
5135 return false;
5136 }
5137 break;
5138 default:
5139 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005140 }
5141
5142 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5143 // So the basic type should usually match.
5144 if (!isBitShift && left->getBasicType() != right->getBasicType())
5145 {
5146 return false;
5147 }
5148
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005149 // Check that:
5150 // 1. Type sizes match exactly on ops that require that.
5151 // 2. Restrictions for structs that contain arrays or samplers are respected.
5152 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005153 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005154 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005155 case EOpAssign:
5156 case EOpInitialize:
5157 case EOpEqual:
5158 case EOpNotEqual:
5159 // ESSL 1.00 sections 5.7, 5.8, 5.9
5160 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5161 {
5162 error(loc, "undefined operation for structs containing arrays",
5163 GetOperatorString(op));
5164 return false;
5165 }
5166 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5167 // we interpret the spec so that this extends to structs containing samplers,
5168 // similarly to ESSL 1.00 spec.
5169 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5170 left->getType().isStructureContainingSamplers())
5171 {
5172 error(loc, "undefined operation for structs containing samplers",
5173 GetOperatorString(op));
5174 return false;
5175 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005176
Olli Etuahoe1805592017-01-02 16:41:20 +00005177 if ((left->getNominalSize() != right->getNominalSize()) ||
5178 (left->getSecondarySize() != right->getSecondarySize()))
5179 {
5180 error(loc, "dimension mismatch", GetOperatorString(op));
5181 return false;
5182 }
5183 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005184 case EOpLessThan:
5185 case EOpGreaterThan:
5186 case EOpLessThanEqual:
5187 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005188 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005189 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005190 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005191 return false;
5192 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005193 break;
5194 case EOpAdd:
5195 case EOpSub:
5196 case EOpDiv:
5197 case EOpIMod:
5198 case EOpBitShiftLeft:
5199 case EOpBitShiftRight:
5200 case EOpBitwiseAnd:
5201 case EOpBitwiseXor:
5202 case EOpBitwiseOr:
5203 case EOpAddAssign:
5204 case EOpSubAssign:
5205 case EOpDivAssign:
5206 case EOpIModAssign:
5207 case EOpBitShiftLeftAssign:
5208 case EOpBitShiftRightAssign:
5209 case EOpBitwiseAndAssign:
5210 case EOpBitwiseXorAssign:
5211 case EOpBitwiseOrAssign:
5212 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5213 {
5214 return false;
5215 }
5216
5217 // Are the sizes compatible?
5218 if (left->getNominalSize() != right->getNominalSize() ||
5219 left->getSecondarySize() != right->getSecondarySize())
5220 {
5221 // If the nominal sizes of operands do not match:
5222 // One of them must be a scalar.
5223 if (!left->isScalar() && !right->isScalar())
5224 return false;
5225
5226 // In the case of compound assignment other than multiply-assign,
5227 // the right side needs to be a scalar. Otherwise a vector/matrix
5228 // would be assigned to a scalar. A scalar can't be shifted by a
5229 // vector either.
5230 if (!right->isScalar() &&
5231 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5232 return false;
5233 }
5234 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005235 default:
5236 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005237 }
5238
Olli Etuahod6b14282015-03-17 14:31:35 +02005239 return true;
5240}
5241
Olli Etuaho1dded802016-08-18 18:13:13 +03005242bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5243 const TType &left,
5244 const TType &right)
5245{
5246 switch (op)
5247 {
5248 case EOpMul:
5249 case EOpMulAssign:
5250 return left.getNominalSize() == right.getNominalSize() &&
5251 left.getSecondarySize() == right.getSecondarySize();
5252 case EOpVectorTimesScalar:
5253 return true;
5254 case EOpVectorTimesScalarAssign:
5255 ASSERT(!left.isMatrix() && !right.isMatrix());
5256 return left.isVector() && !right.isVector();
5257 case EOpVectorTimesMatrix:
5258 return left.getNominalSize() == right.getRows();
5259 case EOpVectorTimesMatrixAssign:
5260 ASSERT(!left.isMatrix() && right.isMatrix());
5261 return left.isVector() && left.getNominalSize() == right.getRows() &&
5262 left.getNominalSize() == right.getCols();
5263 case EOpMatrixTimesVector:
5264 return left.getCols() == right.getNominalSize();
5265 case EOpMatrixTimesScalar:
5266 return true;
5267 case EOpMatrixTimesScalarAssign:
5268 ASSERT(left.isMatrix() && !right.isMatrix());
5269 return !right.isVector();
5270 case EOpMatrixTimesMatrix:
5271 return left.getCols() == right.getRows();
5272 case EOpMatrixTimesMatrixAssign:
5273 ASSERT(left.isMatrix() && right.isMatrix());
5274 // We need to check two things:
5275 // 1. The matrix multiplication step is valid.
5276 // 2. The result will have the same number of columns as the lvalue.
5277 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5278
5279 default:
5280 UNREACHABLE();
5281 return false;
5282 }
5283}
5284
Jamie Madillb98c3a82015-07-23 14:26:04 -04005285TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5286 TIntermTyped *left,
5287 TIntermTyped *right,
5288 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005289{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005290 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005291 return nullptr;
5292
Olli Etuahofc1806e2015-03-17 13:03:11 +02005293 switch (op)
5294 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005295 case EOpEqual:
5296 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005297 case EOpLessThan:
5298 case EOpGreaterThan:
5299 case EOpLessThanEqual:
5300 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005301 break;
5302 case EOpLogicalOr:
5303 case EOpLogicalXor:
5304 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005305 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5306 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005307 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005308 {
5309 return nullptr;
5310 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005311 // Basic types matching should have been already checked.
5312 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005313 break;
5314 case EOpAdd:
5315 case EOpSub:
5316 case EOpDiv:
5317 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005318 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5319 !right->getType().getStruct());
5320 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005321 {
5322 return nullptr;
5323 }
5324 break;
5325 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005326 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5327 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005328 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005329 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005330 {
5331 return nullptr;
5332 }
5333 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005334 default:
5335 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005336 }
5337
Olli Etuaho1dded802016-08-18 18:13:13 +03005338 if (op == EOpMul)
5339 {
5340 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5341 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5342 {
5343 return nullptr;
5344 }
5345 }
5346
Olli Etuaho3fdec912016-08-18 15:08:06 +03005347 TIntermBinary *node = new TIntermBinary(op, left, right);
5348 node->setLine(loc);
5349
Olli Etuaho3fdec912016-08-18 15:08:06 +03005350 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005351 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005352}
5353
Jamie Madillb98c3a82015-07-23 14:26:04 -04005354TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5355 TIntermTyped *left,
5356 TIntermTyped *right,
5357 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005358{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005359 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005360 if (node == 0)
5361 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005362 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5363 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005364 return left;
5365 }
5366 return node;
5367}
5368
Jamie Madillb98c3a82015-07-23 14:26:04 -04005369TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5370 TIntermTyped *left,
5371 TIntermTyped *right,
5372 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005373{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005374 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005375 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005376 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005377 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5378 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005379 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005380 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005381 }
5382 return node;
5383}
5384
Olli Etuaho13389b62016-10-16 11:48:18 +01005385TIntermBinary *TParseContext::createAssign(TOperator op,
5386 TIntermTyped *left,
5387 TIntermTyped *right,
5388 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005389{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005390 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005391 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005392 if (op == EOpMulAssign)
5393 {
5394 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5395 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5396 {
5397 return nullptr;
5398 }
5399 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005400 TIntermBinary *node = new TIntermBinary(op, left, right);
5401 node->setLine(loc);
5402
Olli Etuaho3fdec912016-08-18 15:08:06 +03005403 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005404 }
5405 return nullptr;
5406}
5407
Jamie Madillb98c3a82015-07-23 14:26:04 -04005408TIntermTyped *TParseContext::addAssign(TOperator op,
5409 TIntermTyped *left,
5410 TIntermTyped *right,
5411 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005412{
Olli Etuahocce89652017-06-19 16:04:09 +03005413 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005414 TIntermTyped *node = createAssign(op, left, right, loc);
5415 if (node == nullptr)
5416 {
5417 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005418 return left;
5419 }
5420 return node;
5421}
5422
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005423TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5424 TIntermTyped *right,
5425 const TSourceLoc &loc)
5426{
Corentin Wallez0d959252016-07-12 17:26:32 -04005427 // WebGL2 section 5.26, the following results in an error:
5428 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005429 if (mShaderSpec == SH_WEBGL2_SPEC &&
5430 (left->isArray() || left->getBasicType() == EbtVoid ||
5431 left->getType().isStructureContainingArrays() || right->isArray() ||
5432 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005433 {
5434 error(loc,
5435 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5436 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005437 }
5438
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005439 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5440 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5441 commaNode->getTypePointer()->setQualifier(resultQualifier);
5442 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005443}
5444
Olli Etuaho49300862015-02-20 14:54:49 +02005445TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5446{
5447 switch (op)
5448 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005449 case EOpContinue:
5450 if (mLoopNestingLevel <= 0)
5451 {
5452 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005453 }
5454 break;
5455 case EOpBreak:
5456 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5457 {
5458 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005459 }
5460 break;
5461 case EOpReturn:
5462 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5463 {
5464 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005465 }
5466 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005467 case EOpKill:
5468 if (mShaderType != GL_FRAGMENT_SHADER)
5469 {
5470 error(loc, "discard supported in fragment shaders only", "discard");
5471 }
5472 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005473 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005474 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005475 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005476 }
Olli Etuahocce89652017-06-19 16:04:09 +03005477 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005478}
5479
Jamie Madillb98c3a82015-07-23 14:26:04 -04005480TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005481 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005482 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005483{
Olli Etuahocce89652017-06-19 16:04:09 +03005484 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005485 {
Olli Etuahocce89652017-06-19 16:04:09 +03005486 ASSERT(op == EOpReturn);
5487 mFunctionReturnsValue = true;
5488 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5489 {
5490 error(loc, "void function cannot return a value", "return");
5491 }
5492 else if (*mCurrentFunctionType != expression->getType())
5493 {
5494 error(loc, "function return is not matching type:", "return");
5495 }
Olli Etuaho49300862015-02-20 14:54:49 +02005496 }
Olli Etuahocce89652017-06-19 16:04:09 +03005497 TIntermBranch *node = new TIntermBranch(op, expression);
5498 node->setLine(loc);
5499 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005500}
5501
Martin Radev84aa2dc2017-09-11 15:51:02 +03005502void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5503{
5504 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5505 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5506 bool isTextureGather = (name == "textureGather");
5507 bool isTextureGatherOffset = (name == "textureGatherOffset");
5508 if (isTextureGather || isTextureGatherOffset)
5509 {
5510 TIntermNode *componentNode = nullptr;
5511 TIntermSequence *arguments = functionCall->getSequence();
5512 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5513 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5514 ASSERT(sampler != nullptr);
5515 switch (sampler->getBasicType())
5516 {
5517 case EbtSampler2D:
5518 case EbtISampler2D:
5519 case EbtUSampler2D:
5520 case EbtSampler2DArray:
5521 case EbtISampler2DArray:
5522 case EbtUSampler2DArray:
5523 if ((isTextureGather && arguments->size() == 3u) ||
5524 (isTextureGatherOffset && arguments->size() == 4u))
5525 {
5526 componentNode = arguments->back();
5527 }
5528 break;
5529 case EbtSamplerCube:
5530 case EbtISamplerCube:
5531 case EbtUSamplerCube:
5532 ASSERT(!isTextureGatherOffset);
5533 if (arguments->size() == 3u)
5534 {
5535 componentNode = arguments->back();
5536 }
5537 break;
5538 case EbtSampler2DShadow:
5539 case EbtSampler2DArrayShadow:
5540 case EbtSamplerCubeShadow:
5541 break;
5542 default:
5543 UNREACHABLE();
5544 break;
5545 }
5546 if (componentNode)
5547 {
5548 const TIntermConstantUnion *componentConstantUnion =
5549 componentNode->getAsConstantUnion();
5550 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5551 {
5552 error(functionCall->getLine(), "Texture component must be a constant expression",
5553 name.c_str());
5554 }
5555 else
5556 {
5557 int component = componentConstantUnion->getIConst(0);
5558 if (component < 0 || component > 3)
5559 {
5560 error(functionCall->getLine(), "Component must be in the range [0;3]",
5561 name.c_str());
5562 }
5563 }
5564 }
5565 }
5566}
5567
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005568void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5569{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005570 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005571 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005572 TIntermNode *offset = nullptr;
5573 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005574 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005575 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5576 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5577 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005578 {
5579 offset = arguments->back();
5580 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005581 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005582 {
5583 // A bias parameter might follow the offset parameter.
5584 ASSERT(arguments->size() >= 3);
5585 offset = (*arguments)[2];
5586 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005587 else if (name == "textureGatherOffset")
5588 {
5589 ASSERT(arguments->size() >= 3u);
5590 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5591 ASSERT(sampler != nullptr);
5592 switch (sampler->getBasicType())
5593 {
5594 case EbtSampler2D:
5595 case EbtISampler2D:
5596 case EbtUSampler2D:
5597 case EbtSampler2DArray:
5598 case EbtISampler2DArray:
5599 case EbtUSampler2DArray:
5600 offset = (*arguments)[2];
5601 break;
5602 case EbtSampler2DShadow:
5603 case EbtSampler2DArrayShadow:
5604 offset = (*arguments)[3];
5605 break;
5606 default:
5607 UNREACHABLE();
5608 break;
5609 }
5610 useTextureGatherOffsetConstraints = true;
5611 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005612 if (offset != nullptr)
5613 {
5614 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5615 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5616 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005617 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005618 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005619 }
5620 else
5621 {
5622 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5623 size_t size = offsetConstantUnion->getType().getObjectSize();
5624 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005625 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5626 : mMinProgramTexelOffset;
5627 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5628 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005629 for (size_t i = 0u; i < size; ++i)
5630 {
5631 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005632 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005633 {
5634 std::stringstream tokenStream;
5635 tokenStream << offsetValue;
5636 std::string token = tokenStream.str();
5637 error(offset->getLine(), "Texture offset value out of valid range",
5638 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005639 }
5640 }
5641 }
5642 }
5643}
5644
Jiajia Qina3106c52017-11-03 09:39:39 +08005645void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5646{
5647 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5648 if (IsAtomicBuiltin(name))
5649 {
5650 TIntermSequence *arguments = functionCall->getSequence();
5651 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5652
5653 if (IsBufferOrSharedVariable(memNode))
5654 {
5655 return;
5656 }
5657
5658 while (memNode->getAsBinaryNode())
5659 {
5660 memNode = memNode->getAsBinaryNode()->getLeft();
5661 if (IsBufferOrSharedVariable(memNode))
5662 {
5663 return;
5664 }
5665 }
5666
5667 error(memNode->getLine(),
5668 "The value passed to the mem argument of an atomic memory function does not "
5669 "correspond to a buffer or shared variable.",
5670 functionCall->getFunctionSymbolInfo()->getName().c_str());
5671 }
5672}
5673
Martin Radev2cc85b32016-08-05 16:22:53 +03005674// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5675void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5676{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005677 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005678 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5679
5680 if (name.compare(0, 5, "image") == 0)
5681 {
5682 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005683 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005684
Olli Etuaho485eefd2017-02-14 17:40:06 +00005685 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005686
5687 if (name.compare(5, 5, "Store") == 0)
5688 {
5689 if (memoryQualifier.readonly)
5690 {
5691 error(imageNode->getLine(),
5692 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005693 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005694 }
5695 }
5696 else if (name.compare(5, 4, "Load") == 0)
5697 {
5698 if (memoryQualifier.writeonly)
5699 {
5700 error(imageNode->getLine(),
5701 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005702 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005703 }
5704 }
5705 }
5706}
5707
5708// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5709void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5710 const TFunction *functionDefinition,
5711 const TIntermAggregate *functionCall)
5712{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005713 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005714
5715 const TIntermSequence &arguments = *functionCall->getSequence();
5716
5717 ASSERT(functionDefinition->getParamCount() == arguments.size());
5718
5719 for (size_t i = 0; i < arguments.size(); ++i)
5720 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005721 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5722 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005723 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5724 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5725
5726 if (IsImage(functionArgumentType.getBasicType()))
5727 {
5728 const TMemoryQualifier &functionArgumentMemoryQualifier =
5729 functionArgumentType.getMemoryQualifier();
5730 const TMemoryQualifier &functionParameterMemoryQualifier =
5731 functionParameterType.getMemoryQualifier();
5732 if (functionArgumentMemoryQualifier.readonly &&
5733 !functionParameterMemoryQualifier.readonly)
5734 {
5735 error(functionCall->getLine(),
5736 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005737 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005738 }
5739
5740 if (functionArgumentMemoryQualifier.writeonly &&
5741 !functionParameterMemoryQualifier.writeonly)
5742 {
5743 error(functionCall->getLine(),
5744 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005745 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005746 }
Martin Radev049edfa2016-11-11 14:35:37 +02005747
5748 if (functionArgumentMemoryQualifier.coherent &&
5749 !functionParameterMemoryQualifier.coherent)
5750 {
5751 error(functionCall->getLine(),
5752 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005753 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005754 }
5755
5756 if (functionArgumentMemoryQualifier.volatileQualifier &&
5757 !functionParameterMemoryQualifier.volatileQualifier)
5758 {
5759 error(functionCall->getLine(),
5760 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005761 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005762 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005763 }
5764 }
5765}
5766
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005767TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005768{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005769 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005770}
5771
5772TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005773 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005774 TIntermNode *thisNode,
5775 const TSourceLoc &loc)
5776{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005777 if (thisNode != nullptr)
5778 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005779 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005780 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005781
5782 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005783 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005784 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005785 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005786 }
5787 else
5788 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005789 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005790 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5791 }
5792}
5793
5794TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5795 TIntermSequence *arguments,
5796 TIntermNode *thisNode,
5797 const TSourceLoc &loc)
5798{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005799 TIntermTyped *typedThis = thisNode->getAsTyped();
5800 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5801 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5802 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5803 // So accessing fnCall->getName() below is safe.
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005804 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005805 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005806 error(loc, "invalid method", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005807 }
5808 else if (!arguments->empty())
5809 {
5810 error(loc, "method takes no parameters", "length");
5811 }
5812 else if (typedThis == nullptr || !typedThis->isArray())
5813 {
5814 error(loc, "length can only be called on arrays", "length");
5815 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005816 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5817 mGeometryShaderInputPrimitiveType == EptUndefined)
5818 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005819 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005820 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5821 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005822 else
5823 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005824 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5825 node->setLine(loc);
5826 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005827 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005828 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005829}
5830
5831TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5832 TIntermSequence *arguments,
5833 const TSourceLoc &loc)
5834{
5835 // First find by unmangled name to check whether the function name has been
5836 // hidden by a variable name or struct typename.
5837 // If a function is found, check for one with a matching argument list.
5838 bool builtIn;
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005839 const TSymbol *symbol = symbolTable.find(fnCall->name(), mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005840 if (symbol != nullptr && !symbol->isFunction())
5841 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005842 error(loc, "function name expected", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005843 }
5844 else
5845 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005846 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->name(), *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005847 mShaderVersion, &builtIn);
5848 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005849 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005850 error(loc, "no matching overloaded function found", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005851 }
5852 else
5853 {
5854 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005855 //
5856 // A declared function.
5857 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005858 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005859 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005860 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005861 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005862 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005863 if (builtIn && op != EOpNull)
5864 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005865 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005866 if (fnCandidate->getParamCount() == 1)
5867 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005868 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005869 TIntermNode *unaryParamNode = arguments->front();
5870 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005871 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005872 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005873 }
5874 else
5875 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005876 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005877 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005878 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005879
5880 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005881 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305882
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005883 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305884 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005885 // See if we can constant fold a built-in. Note that this may be possible
5886 // even if it is not const-qualified.
5887 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305888 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005889 else
5890 {
5891 return callNode;
5892 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005893 }
5894 }
5895 else
5896 {
5897 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005898 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005899
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005900 // If builtIn == false, the function is user defined - could be an overloaded
5901 // built-in as well.
5902 // if builtIn == true, it's a builtIn function with no op associated with it.
5903 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005904 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005905 {
Olli Etuahofe486322017-03-21 09:30:54 +00005906 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005907 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005908 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005909 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005910 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005911 }
5912 else
5913 {
Olli Etuahofe486322017-03-21 09:30:54 +00005914 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005915 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005916 }
5917
Jiajia Qinbc585152017-06-23 15:42:17 +08005918 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005919
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005920 callNode->setLine(loc);
5921
5922 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005923 }
5924 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005925 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005926
5927 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005928 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005929}
5930
Jamie Madillb98c3a82015-07-23 14:26:04 -04005931TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005932 TIntermTyped *trueExpression,
5933 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005934 const TSourceLoc &loc)
5935{
Olli Etuaho56229f12017-07-10 14:16:33 +03005936 if (!checkIsScalarBool(loc, cond))
5937 {
5938 return falseExpression;
5939 }
Olli Etuaho52901742015-04-15 13:42:45 +03005940
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005941 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005942 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005943 std::stringstream reasonStream;
5944 reasonStream << "mismatching ternary operator operand types '"
5945 << trueExpression->getCompleteString() << " and '"
5946 << falseExpression->getCompleteString() << "'";
5947 std::string reason = reasonStream.str();
5948 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005949 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005950 }
Olli Etuahode318b22016-10-25 16:18:25 +01005951 if (IsOpaqueType(trueExpression->getBasicType()))
5952 {
5953 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005954 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005955 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5956 // Note that structs containing opaque types don't need to be checked as structs are
5957 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005958 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005959 return falseExpression;
5960 }
5961
Jiajia Qinbc585152017-06-23 15:42:17 +08005962 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5963 falseExpression->getMemoryQualifier().writeonly)
5964 {
5965 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5966 return falseExpression;
5967 }
5968
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005969 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005970 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005971 // ESSL 3.00.6 section 5.7:
5972 // Ternary operator support is optional for arrays. No certainty that it works across all
5973 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5974 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005975 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005976 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005977 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005978 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005979 }
Olli Etuaho94050052017-05-08 14:17:44 +03005980 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5981 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005982 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005983 return falseExpression;
5984 }
5985
Corentin Wallez0d959252016-07-12 17:26:32 -04005986 // WebGL2 section 5.26, the following results in an error:
5987 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005988 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005989 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005990 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005991 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005992 }
5993
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005994 // Note that the node resulting from here can be a constant union without being qualified as
5995 // constant.
5996 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5997 node->setLine(loc);
5998
5999 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03006000}
Olli Etuaho49300862015-02-20 14:54:49 +02006001
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00006002//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006003// Parse an array of strings using yyparse.
6004//
6005// Returns 0 for success.
6006//
Jamie Madillb98c3a82015-07-23 14:26:04 -04006007int PaParseStrings(size_t count,
6008 const char *const string[],
6009 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05306010 TParseContext *context)
6011{
Yunchao He4f285442017-04-21 12:15:49 +08006012 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006013 return 1;
6014
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006015 if (glslang_initialize(context))
6016 return 1;
6017
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006018 int error = glslang_scan(count, string, length, context);
6019 if (!error)
6020 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006021
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006022 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006023
alokp@chromium.org6b495712012-06-29 00:06:58 +00006024 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006025}
Jamie Madill45bcc782016-11-07 13:58:48 -05006026
6027} // namespace sh