blob: 5aa6882c29b4cce3e9003477fa90301abd7c4000 [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 Etuaho195be942017-12-04 23:40:14 +02001109 (*variable) = new TVariable(&symbolTable, &identifier, type, SymbolType::UserDefined);
1110
Olli Etuaho43364892017-02-13 16:00:12 +00001111 checkBindingIsValid(line, type);
1112
Olli Etuaho856c4972016-08-08 11:38:39 +03001113 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114
Olli Etuaho2935c582015-04-08 14:32:06 +03001115 // gl_LastFragData may be redeclared with a new precision qualifier
1116 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1117 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001118 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1119 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001120 if (type.isArrayOfArrays())
1121 {
1122 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1123 identifier.c_str());
1124 return false;
1125 }
1126 else if (static_cast<int>(type.getOutermostArraySize()) ==
1127 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001128 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001129 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001130 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001131 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001132 }
1133 }
1134 else
1135 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001136 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1137 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001138 return false;
1139 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141
Olli Etuaho8a176262016-08-16 14:23:01 +03001142 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001143 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001144
Olli Etuaho195be942017-12-04 23:40:14 +02001145 if (!symbolTable.declareVariable(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001146 {
1147 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001148 return false;
1149 }
1150
Olli Etuaho8a176262016-08-16 14:23:01 +03001151 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001152 return false;
1153
1154 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001155}
1156
Martin Radev70866b82016-07-22 15:27:42 +03001157void TParseContext::checkIsParameterQualifierValid(
1158 const TSourceLoc &line,
1159 const TTypeQualifierBuilder &typeQualifierBuilder,
1160 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301161{
Olli Etuahocce89652017-06-19 16:04:09 +03001162 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001163 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001164
1165 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301166 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001167 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1168 }
1169
1170 if (!IsImage(type->getBasicType()))
1171 {
Olli Etuaho43364892017-02-13 16:00:12 +00001172 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001173 }
1174 else
1175 {
1176 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001177 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001178
Martin Radev70866b82016-07-22 15:27:42 +03001179 type->setQualifier(typeQualifier.qualifier);
1180
1181 if (typeQualifier.precision != EbpUndefined)
1182 {
1183 type->setPrecision(typeQualifier.precision);
1184 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185}
1186
Olli Etuaho703671e2017-11-08 17:47:18 +02001187template <size_t size>
1188bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1189 const std::array<TExtension, size> &extensions)
1190{
1191 ASSERT(!extensions.empty());
1192 const TExtensionBehavior &extBehavior = extensionBehavior();
1193
1194 bool canUseWithWarning = false;
1195 bool canUseWithoutWarning = false;
1196
1197 const char *errorMsgString = "";
1198 TExtension errorMsgExtension = TExtension::UNDEFINED;
1199
1200 for (TExtension extension : extensions)
1201 {
1202 auto extIter = extBehavior.find(extension);
1203 if (canUseWithWarning)
1204 {
1205 // We already have an extension that we can use, but with a warning.
1206 // See if we can use the alternative extension without a warning.
1207 if (extIter == extBehavior.end())
1208 {
1209 continue;
1210 }
1211 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1212 {
1213 canUseWithoutWarning = true;
1214 break;
1215 }
1216 continue;
1217 }
1218 if (extIter == extBehavior.end())
1219 {
1220 errorMsgString = "extension is not supported";
1221 errorMsgExtension = extension;
1222 }
1223 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1224 {
1225 errorMsgString = "extension is disabled";
1226 errorMsgExtension = extension;
1227 }
1228 else if (extIter->second == EBhWarn)
1229 {
1230 errorMsgExtension = extension;
1231 canUseWithWarning = true;
1232 }
1233 else
1234 {
1235 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1236 canUseWithoutWarning = true;
1237 break;
1238 }
1239 }
1240
1241 if (canUseWithoutWarning)
1242 {
1243 return true;
1244 }
1245 if (canUseWithWarning)
1246 {
1247 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1248 return true;
1249 }
1250 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1251 return false;
1252}
1253
1254template bool TParseContext::checkCanUseOneOfExtensions(
1255 const TSourceLoc &line,
1256 const std::array<TExtension, 1> &extensions);
1257template bool TParseContext::checkCanUseOneOfExtensions(
1258 const TSourceLoc &line,
1259 const std::array<TExtension, 2> &extensions);
1260template bool TParseContext::checkCanUseOneOfExtensions(
1261 const TSourceLoc &line,
1262 const std::array<TExtension, 3> &extensions);
1263
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001264bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001265{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001266 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001267 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268}
1269
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001270// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1271// compile-time or link-time errors are the same whether or not the declaration is empty".
1272// This function implements all the checks that are done on qualifiers regardless of if the
1273// declaration is empty.
1274void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1275 const sh::TLayoutQualifier &layoutQualifier,
1276 const TSourceLoc &location)
1277{
1278 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1279 {
1280 error(location, "Shared memory declarations cannot have layout specified", "layout");
1281 }
1282
1283 if (layoutQualifier.matrixPacking != EmpUnspecified)
1284 {
1285 error(location, "layout qualifier only valid for interface blocks",
1286 getMatrixPackingString(layoutQualifier.matrixPacking));
1287 return;
1288 }
1289
1290 if (layoutQualifier.blockStorage != EbsUnspecified)
1291 {
1292 error(location, "layout qualifier only valid for interface blocks",
1293 getBlockStorageString(layoutQualifier.blockStorage));
1294 return;
1295 }
1296
1297 if (qualifier == EvqFragmentOut)
1298 {
1299 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1300 {
1301 error(location, "invalid layout qualifier combination", "yuv");
1302 return;
1303 }
1304 }
1305 else
1306 {
1307 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1308 }
1309
Olli Etuaho95468d12017-05-04 11:14:34 +03001310 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1311 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001312 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1313 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001314 {
1315 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1316 }
1317
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001318 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001319 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001320 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001321 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001322 // We're not checking whether the uniform location is in range here since that depends on
1323 // the type of the variable.
1324 // The type can only be fully determined for non-empty declarations.
1325 }
1326 if (!canHaveLocation)
1327 {
1328 checkLocationIsNotSpecified(location, layoutQualifier);
1329 }
1330}
1331
jchen104cdac9e2017-05-08 11:01:20 +08001332void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1333 const TSourceLoc &location)
1334{
1335 if (publicType.precision != EbpHigh)
1336 {
1337 error(location, "Can only be highp", "atomic counter");
1338 }
1339 // dEQP enforces compile error if location is specified. See uniform_location.test.
1340 if (publicType.layoutQualifier.location != -1)
1341 {
1342 error(location, "location must not be set for atomic_uint", "layout");
1343 }
1344 if (publicType.layoutQualifier.binding == -1)
1345 {
1346 error(location, "no binding specified", "atomic counter");
1347 }
1348}
1349
Olli Etuaho55bde912017-10-25 13:41:13 +03001350void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001351{
Olli Etuaho55bde912017-10-25 13:41:13 +03001352 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001353 {
1354 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1355 // error. It is assumed that this applies to empty declarations as well.
1356 error(location, "empty array declaration needs to specify a size", "");
1357 }
Martin Radevb8b01222016-11-20 23:25:53 +02001358}
1359
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001360// These checks are done for all declarations that are non-empty. They're done for non-empty
1361// declarations starting a declarator list, and declarators that follow an empty declaration.
1362void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1363 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001364{
Olli Etuahofa33d582015-04-09 14:33:12 +03001365 switch (publicType.qualifier)
1366 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001367 case EvqVaryingIn:
1368 case EvqVaryingOut:
1369 case EvqAttribute:
1370 case EvqVertexIn:
1371 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001372 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001373 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001374 {
1375 error(identifierLocation, "cannot be used with a structure",
1376 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001377 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001378 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001379 break;
1380 case EvqBuffer:
1381 if (publicType.getBasicType() != EbtInterfaceBlock)
1382 {
1383 error(identifierLocation,
1384 "cannot declare buffer variables at global scope(outside a block)",
1385 getQualifierString(publicType.qualifier));
1386 return;
1387 }
1388 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001389 default:
1390 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001391 }
jchen10cc2a10e2017-05-03 14:05:12 +08001392 std::string reason(getBasicString(publicType.getBasicType()));
1393 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001394 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001395 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001396 {
1397 return;
1398 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001399
Andrei Volykhina5527072017-03-22 16:46:30 +03001400 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1401 publicType.qualifier != EvqConst) &&
1402 publicType.getBasicType() == EbtYuvCscStandardEXT)
1403 {
1404 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1405 getQualifierString(publicType.qualifier));
1406 return;
1407 }
1408
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001409 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1410 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001411 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1412 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001413 TType type(publicType);
1414 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001415 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001416 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1417 publicType.layoutQualifier);
1418 }
1419 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001420
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001421 // check for layout qualifier issues
1422 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001423
Martin Radev2cc85b32016-08-05 16:22:53 +03001424 if (IsImage(publicType.getBasicType()))
1425 {
1426
1427 switch (layoutQualifier.imageInternalFormat)
1428 {
1429 case EiifRGBA32F:
1430 case EiifRGBA16F:
1431 case EiifR32F:
1432 case EiifRGBA8:
1433 case EiifRGBA8_SNORM:
1434 if (!IsFloatImage(publicType.getBasicType()))
1435 {
1436 error(identifierLocation,
1437 "internal image format requires a floating image type",
1438 getBasicString(publicType.getBasicType()));
1439 return;
1440 }
1441 break;
1442 case EiifRGBA32I:
1443 case EiifRGBA16I:
1444 case EiifRGBA8I:
1445 case EiifR32I:
1446 if (!IsIntegerImage(publicType.getBasicType()))
1447 {
1448 error(identifierLocation,
1449 "internal image format requires an integer image type",
1450 getBasicString(publicType.getBasicType()));
1451 return;
1452 }
1453 break;
1454 case EiifRGBA32UI:
1455 case EiifRGBA16UI:
1456 case EiifRGBA8UI:
1457 case EiifR32UI:
1458 if (!IsUnsignedImage(publicType.getBasicType()))
1459 {
1460 error(identifierLocation,
1461 "internal image format requires an unsigned image type",
1462 getBasicString(publicType.getBasicType()));
1463 return;
1464 }
1465 break;
1466 case EiifUnspecified:
1467 error(identifierLocation, "layout qualifier", "No image internal format specified");
1468 return;
1469 default:
1470 error(identifierLocation, "layout qualifier", "unrecognized token");
1471 return;
1472 }
1473
1474 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1475 switch (layoutQualifier.imageInternalFormat)
1476 {
1477 case EiifR32F:
1478 case EiifR32I:
1479 case EiifR32UI:
1480 break;
1481 default:
1482 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1483 {
1484 error(identifierLocation, "layout qualifier",
1485 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1486 "image variables must be qualified readonly and/or writeonly");
1487 return;
1488 }
1489 break;
1490 }
1491 }
1492 else
1493 {
Olli Etuaho43364892017-02-13 16:00:12 +00001494 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001495 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1496 }
jchen104cdac9e2017-05-08 11:01:20 +08001497
1498 if (IsAtomicCounter(publicType.getBasicType()))
1499 {
1500 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1501 }
1502 else
1503 {
1504 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1505 }
Olli Etuaho43364892017-02-13 16:00:12 +00001506}
Martin Radev2cc85b32016-08-05 16:22:53 +03001507
Olli Etuaho43364892017-02-13 16:00:12 +00001508void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1509{
1510 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001511 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1512 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1513 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1514 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1515 // when it comes to which shaders are accepted by the compiler.
1516 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001517 if (IsImage(type.getBasicType()))
1518 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001519 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1520 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001521 }
1522 else if (IsSampler(type.getBasicType()))
1523 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001524 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1525 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001526 }
jchen104cdac9e2017-05-08 11:01:20 +08001527 else if (IsAtomicCounter(type.getBasicType()))
1528 {
1529 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1530 }
Olli Etuaho43364892017-02-13 16:00:12 +00001531 else
1532 {
1533 ASSERT(!IsOpaqueType(type.getBasicType()));
1534 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001535 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001536}
1537
Olli Etuaho856c4972016-08-08 11:38:39 +03001538void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1539 const TString &layoutQualifierName,
1540 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001541{
1542
1543 if (mShaderVersion < versionRequired)
1544 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001545 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001546 }
1547}
1548
Olli Etuaho856c4972016-08-08 11:38:39 +03001549bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1550 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001551{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001552 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001553 for (size_t i = 0u; i < localSize.size(); ++i)
1554 {
1555 if (localSize[i] != -1)
1556 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001557 error(location,
1558 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1559 "global layout declaration",
1560 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001561 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001562 }
1563 }
1564
Olli Etuaho8a176262016-08-16 14:23:01 +03001565 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001566}
1567
Olli Etuaho43364892017-02-13 16:00:12 +00001568void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001569 TLayoutImageInternalFormat internalFormat)
1570{
1571 if (internalFormat != EiifUnspecified)
1572 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001573 error(location, "invalid layout qualifier: only valid when used with images",
1574 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001575 }
Olli Etuaho43364892017-02-13 16:00:12 +00001576}
1577
1578void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1579{
1580 if (binding != -1)
1581 {
1582 error(location,
1583 "invalid layout qualifier: only valid when used with opaque types or blocks",
1584 "binding");
1585 }
1586}
1587
jchen104cdac9e2017-05-08 11:01:20 +08001588void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1589{
1590 if (offset != -1)
1591 {
1592 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1593 "offset");
1594 }
1595}
1596
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001597void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1598 int binding,
1599 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001600{
1601 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001602 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001603 {
1604 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1605 }
1606}
1607
1608void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1609 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001610 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001611{
1612 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001613 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001614 {
1615 error(location, "sampler binding greater than maximum texture units", "binding");
1616 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001617}
1618
Jiajia Qinbc585152017-06-23 15:42:17 +08001619void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1620 const TQualifier &qualifier,
1621 int binding,
1622 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001623{
1624 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001625 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001626 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001627 if (binding + size > mMaxUniformBufferBindings)
1628 {
1629 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1630 "binding");
1631 }
1632 }
1633 else if (qualifier == EvqBuffer)
1634 {
1635 if (binding + size > mMaxShaderStorageBufferBindings)
1636 {
1637 error(location,
1638 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1639 "binding");
1640 }
jchen10af713a22017-04-19 09:10:56 +08001641 }
1642}
jchen104cdac9e2017-05-08 11:01:20 +08001643void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1644{
1645 if (binding >= mMaxAtomicCounterBindings)
1646 {
1647 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1648 "binding");
1649 }
1650}
jchen10af713a22017-04-19 09:10:56 +08001651
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001652void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1653 int objectLocationCount,
1654 const TLayoutQualifier &layoutQualifier)
1655{
1656 int loc = layoutQualifier.location;
1657 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1658 {
1659 error(location, "Uniform location out of range", "location");
1660 }
1661}
1662
Andrei Volykhina5527072017-03-22 16:46:30 +03001663void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1664{
1665 if (yuv != false)
1666 {
1667 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1668 }
1669}
1670
Jiajia Qinbc585152017-06-23 15:42:17 +08001671void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1672 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001673{
1674 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1675 {
1676 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001677 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1678 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1679 qual == EvqInOut || qual == EvqConstReadOnly))
1680 {
1681 if (argument->getMemoryQualifier().writeonly)
1682 {
1683 error(argument->getLine(),
1684 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001685 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001686 return;
1687 }
1688 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001689 if (qual == EvqOut || qual == EvqInOut)
1690 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001691 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001692 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001693 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001694 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001695 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001696 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001697 }
1698 }
1699 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001700}
1701
Martin Radev70866b82016-07-22 15:27:42 +03001702void TParseContext::checkInvariantVariableQualifier(bool invariant,
1703 const TQualifier qualifier,
1704 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001705{
Martin Radev70866b82016-07-22 15:27:42 +03001706 if (!invariant)
1707 return;
1708
1709 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001710 {
Martin Radev70866b82016-07-22 15:27:42 +03001711 // input variables in the fragment shader can be also qualified as invariant
1712 if (!sh::CanBeInvariantESSL1(qualifier))
1713 {
1714 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1715 }
1716 }
1717 else
1718 {
1719 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1720 {
1721 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1722 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001723 }
1724}
1725
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001726bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001727{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001728 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001729}
1730
Jamie Madillb98c3a82015-07-23 14:26:04 -04001731void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1732 const char *extName,
1733 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001734{
1735 pp::SourceLocation srcLoc;
1736 srcLoc.file = loc.first_file;
1737 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001738 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001739}
1740
Jamie Madillb98c3a82015-07-23 14:26:04 -04001741void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1742 const char *name,
1743 const char *value,
1744 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001745{
1746 pp::SourceLocation srcLoc;
1747 srcLoc.file = loc.first_file;
1748 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001749 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001750}
1751
Martin Radev4c4c8e72016-08-04 12:25:34 +03001752sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001753{
Jamie Madill2f294c92017-11-20 14:47:26 -05001754 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001755 for (size_t i = 0u; i < result.size(); ++i)
1756 {
1757 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1758 {
1759 result[i] = 1;
1760 }
1761 else
1762 {
1763 result[i] = mComputeShaderLocalSize[i];
1764 }
1765 }
1766 return result;
1767}
1768
Olli Etuaho56229f12017-07-10 14:16:33 +03001769TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1770 const TSourceLoc &line)
1771{
1772 TIntermConstantUnion *node = new TIntermConstantUnion(
1773 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1774 node->setLine(line);
1775 return node;
1776}
1777
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001778/////////////////////////////////////////////////////////////////////////////////
1779//
1780// Non-Errors.
1781//
1782/////////////////////////////////////////////////////////////////////////////////
1783
Jamie Madill5c097022014-08-20 16:38:32 -04001784const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1785 const TString *name,
1786 const TSymbol *symbol)
1787{
Jamie Madill5c097022014-08-20 16:38:32 -04001788 if (!symbol)
1789 {
1790 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001791 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001792 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001793
1794 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001795 {
1796 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001797 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001798 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001799
1800 const TVariable *variable = static_cast<const TVariable *>(symbol);
1801
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001802 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001803 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001804 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001805 }
1806
Olli Etuaho0f684632017-07-13 12:42:15 +03001807 // Reject shaders using both gl_FragData and gl_FragColor
1808 TQualifier qualifier = variable->getType().getQualifier();
1809 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001810 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001811 mUsesFragData = true;
1812 }
1813 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1814 {
1815 mUsesFragColor = true;
1816 }
1817 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1818 {
1819 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001820 }
1821
Olli Etuaho0f684632017-07-13 12:42:15 +03001822 // This validation is not quite correct - it's only an error to write to
1823 // both FragData and FragColor. For simplicity, and because users shouldn't
1824 // be rewarded for reading from undefined varaibles, return an error
1825 // if they are both referenced, rather than assigned.
1826 if (mUsesFragData && mUsesFragColor)
1827 {
1828 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1829 if (mUsesSecondaryOutputs)
1830 {
1831 errorMessage =
1832 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1833 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1834 }
1835 error(location, errorMessage, name->c_str());
1836 }
1837
1838 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1839 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1840 qualifier == EvqWorkGroupSize)
1841 {
1842 error(location,
1843 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1844 "gl_WorkGroupSize");
1845 }
Jamie Madill5c097022014-08-20 16:38:32 -04001846 return variable;
1847}
1848
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001849TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1850 const TString *name,
1851 const TSymbol *symbol)
1852{
1853 const TVariable *variable = getNamedVariable(location, name, symbol);
1854
Olli Etuaho0f684632017-07-13 12:42:15 +03001855 if (!variable)
1856 {
1857 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1858 node->setLine(location);
1859 return node;
1860 }
1861
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001862 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001863 TIntermTyped *node = nullptr;
1864
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001865 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001866 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001867 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001868 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001869 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001870 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001871 {
1872 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1873 // needs to be added to the AST as a constant and not as a symbol.
1874 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1875 TConstantUnion *constArray = new TConstantUnion[3];
1876 for (size_t i = 0; i < 3; ++i)
1877 {
1878 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1879 }
1880
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001881 ASSERT(variableType.getBasicType() == EbtUInt);
1882 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001883
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001884 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001885 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001886 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001887 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001888 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1889 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001890 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001891 ASSERT(mGeometryShaderInputArraySize > 0u);
1892
Olli Etuaho195be942017-12-04 23:40:14 +02001893 node = new TIntermSymbol(variable);
Olli Etuaho55bde912017-10-25 13:41:13 +03001894 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001895 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001896 else
1897 {
Olli Etuaho195be942017-12-04 23:40:14 +02001898 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001899 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001900 ASSERT(node != nullptr);
1901 node->setLine(location);
1902 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001903}
1904
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905// Initializers show up in several places in the grammar. Have one set of
1906// code to handle them here.
1907//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001908// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001909bool TParseContext::executeInitializer(const TSourceLoc &line,
1910 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001911 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001912 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001913 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914{
Olli Etuaho13389b62016-10-16 11:48:18 +01001915 ASSERT(initNode != nullptr);
1916 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917
Olli Etuaho376f1b52015-04-13 13:23:41 +03001918 if (type.isUnsizedArray())
1919 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001920 // In case initializer is not an array or type has more dimensions than initializer, this
1921 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1922 // actually is an array or not. Having a non-array initializer for an unsized array will
1923 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001924 auto *arraySizes = initializer->getType().getArraySizes();
1925 type.sizeUnsizedArrays(arraySizes);
Olli Etuaho376f1b52015-04-13 13:23:41 +03001926 }
Olli Etuaho195be942017-12-04 23:40:14 +02001927
1928 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001929 if (!declareVariable(line, identifier, type, &variable))
1930 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001931 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001932 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933
Olli Etuahob0c645e2015-05-12 14:25:36 +03001934 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001935 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001936 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001937 {
1938 // Error message does not completely match behavior with ESSL 1.00, but
1939 // we want to steer developers towards only using constant expressions.
1940 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001941 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001942 }
1943 if (globalInitWarning)
1944 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001945 warning(
1946 line,
1947 "global variable initializers should be constant expressions "
1948 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1949 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001950 }
1951
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001952 //
1953 // identifier must be of type constant, a global, or a temporary
1954 //
1955 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301956 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1957 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001958 error(line, " cannot initialize this type of qualifier ",
1959 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001960 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001961 }
1962 //
1963 // test for and propagate constant
1964 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965
Arun Patole7e7e68d2015-05-22 12:02:25 +05301966 if (qualifier == EvqConst)
1967 {
1968 if (qualifier != initializer->getType().getQualifier())
1969 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001970 std::stringstream reasonStream;
1971 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1972 << "'";
1973 std::string reason = reasonStream.str();
1974 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001975 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001976 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001977 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301978 if (type != initializer->getType())
1979 {
1980 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001981 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001982 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001983 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001984 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001985
1986 // Save the constant folded value to the variable if possible. For example array
1987 // initializers are not folded, since that way copying the array literal to multiple places
1988 // in the shader is avoided.
1989 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1990 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301991 if (initializer->getAsConstantUnion())
1992 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001993 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001994 ASSERT(*initNode == nullptr);
1995 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301996 }
1997 else if (initializer->getAsSymbolNode())
1998 {
Olli Etuahob6af22b2017-12-15 14:05:44 +02001999 const TVariable &var = initializer->getAsSymbolNode()->variable();
2000 const TConstantUnion *constArray = var.getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002001 if (constArray)
2002 {
2003 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002004 ASSERT(*initNode == nullptr);
2005 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002006 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002007 }
2008 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002009
Olli Etuaho195be942017-12-04 23:40:14 +02002010 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002011 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002012 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2013 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002014 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002015 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002016 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002017 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018
Olli Etuaho914b79a2017-06-19 16:03:19 +03002019 return true;
2020}
2021
2022TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2023 const TString &identifier,
2024 TIntermTyped *initializer,
2025 const TSourceLoc &loc)
2026{
2027 checkIsScalarBool(loc, pType);
2028 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002029 TType type(pType);
2030 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002031 {
2032 // The initializer is valid. The init condition needs to have a node - either the
2033 // initializer node, or a constant node in case the initialized variable is const and won't
2034 // be recorded in the AST.
2035 if (initNode == nullptr)
2036 {
2037 return initializer;
2038 }
2039 else
2040 {
2041 TIntermDeclaration *declaration = new TIntermDeclaration();
2042 declaration->appendDeclarator(initNode);
2043 return declaration;
2044 }
2045 }
2046 return nullptr;
2047}
2048
2049TIntermNode *TParseContext::addLoop(TLoopType type,
2050 TIntermNode *init,
2051 TIntermNode *cond,
2052 TIntermTyped *expr,
2053 TIntermNode *body,
2054 const TSourceLoc &line)
2055{
2056 TIntermNode *node = nullptr;
2057 TIntermTyped *typedCond = nullptr;
2058 if (cond)
2059 {
2060 typedCond = cond->getAsTyped();
2061 }
2062 if (cond == nullptr || typedCond)
2063 {
Olli Etuahocce89652017-06-19 16:04:09 +03002064 if (type == ELoopDoWhile)
2065 {
2066 checkIsScalarBool(line, typedCond);
2067 }
2068 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2069 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2070 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2071 !typedCond->isVector()));
2072
Olli Etuaho3ec75682017-07-05 17:02:55 +03002073 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002074 node->setLine(line);
2075 return node;
2076 }
2077
Olli Etuahocce89652017-06-19 16:04:09 +03002078 ASSERT(type != ELoopDoWhile);
2079
Olli Etuaho914b79a2017-06-19 16:03:19 +03002080 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2081 ASSERT(declaration);
2082 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2083 ASSERT(declarator->getLeft()->getAsSymbolNode());
2084
2085 // The condition is a declaration. In the AST representation we don't support declarations as
2086 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2087 // the loop.
2088 TIntermBlock *block = new TIntermBlock();
2089
2090 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2091 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2092 block->appendStatement(declareCondition);
2093
2094 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2095 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002096 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002097 block->appendStatement(loop);
2098 loop->setLine(line);
2099 block->setLine(line);
2100 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101}
2102
Olli Etuahocce89652017-06-19 16:04:09 +03002103TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2104 TIntermNodePair code,
2105 const TSourceLoc &loc)
2106{
Olli Etuaho56229f12017-07-10 14:16:33 +03002107 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002108
2109 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002110 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002111 {
2112 if (cond->getAsConstantUnion()->getBConst(0) == true)
2113 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002114 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002115 }
2116 else
2117 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002118 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002119 }
2120 }
2121
Olli Etuaho3ec75682017-07-05 17:02:55 +03002122 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002123 node->setLine(loc);
2124
2125 return node;
2126}
2127
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002128void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2129{
2130 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2131 typeSpecifier->getBasicType());
2132
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002133 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002134 {
2135 error(typeSpecifier->getLine(), "not supported", "first-class array");
2136 typeSpecifier->clearArrayness();
2137 }
2138}
2139
Martin Radev70866b82016-07-22 15:27:42 +03002140TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302141 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002142{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002143 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002144
Martin Radev70866b82016-07-22 15:27:42 +03002145 TPublicType returnType = typeSpecifier;
2146 returnType.qualifier = typeQualifier.qualifier;
2147 returnType.invariant = typeQualifier.invariant;
2148 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002149 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002150 returnType.precision = typeSpecifier.precision;
2151
2152 if (typeQualifier.precision != EbpUndefined)
2153 {
2154 returnType.precision = typeQualifier.precision;
2155 }
2156
Martin Radev4a9cd802016-09-01 16:51:51 +03002157 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2158 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002159
Martin Radev4a9cd802016-09-01 16:51:51 +03002160 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2161 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002162
Martin Radev4a9cd802016-09-01 16:51:51 +03002163 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002164
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002165 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002166 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002167 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002168 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002169 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002170 returnType.clearArrayness();
2171 }
2172
Martin Radev70866b82016-07-22 15:27:42 +03002173 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002174 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002175 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002176 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002177 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002178 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002179
Martin Radev70866b82016-07-22 15:27:42 +03002180 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002181 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002182 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002183 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002184 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002185 }
2186 }
2187 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002188 {
Martin Radev70866b82016-07-22 15:27:42 +03002189 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002190 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002191 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002192 }
Martin Radev70866b82016-07-22 15:27:42 +03002193 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2194 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002195 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002196 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2197 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002198 }
Martin Radev70866b82016-07-22 15:27:42 +03002199 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002200 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002201 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002202 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002203 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002204 }
2205
2206 return returnType;
2207}
2208
Olli Etuaho856c4972016-08-08 11:38:39 +03002209void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2210 const TPublicType &type,
2211 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002212{
2213 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002214 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002215 {
2216 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002217 }
2218
2219 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2220 switch (qualifier)
2221 {
2222 case EvqVertexIn:
2223 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002224 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002225 {
2226 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002227 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002228 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002229 return;
2230 case EvqFragmentOut:
2231 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002232 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002233 {
2234 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002235 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002236 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002237 return;
2238 default:
2239 break;
2240 }
2241
2242 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2243 // restrictions.
2244 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002245 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2246 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002247 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2248 {
2249 error(qualifierLocation, "must use 'flat' interpolation here",
2250 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002251 }
2252
Martin Radev4a9cd802016-09-01 16:51:51 +03002253 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002254 {
2255 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2256 // These restrictions are only implied by the ESSL 3.00 spec, but
2257 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002258 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002259 {
2260 error(qualifierLocation, "cannot be an array of structures",
2261 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002262 }
2263 if (type.isStructureContainingArrays())
2264 {
2265 error(qualifierLocation, "cannot be a structure containing an array",
2266 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 }
2268 if (type.isStructureContainingType(EbtStruct))
2269 {
2270 error(qualifierLocation, "cannot be a structure containing a structure",
2271 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002272 }
2273 if (type.isStructureContainingType(EbtBool))
2274 {
2275 error(qualifierLocation, "cannot be a structure containing a bool",
2276 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002277 }
2278 }
2279}
2280
Martin Radev2cc85b32016-08-05 16:22:53 +03002281void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2282{
2283 if (qualifier.getType() == QtStorage)
2284 {
2285 const TStorageQualifierWrapper &storageQualifier =
2286 static_cast<const TStorageQualifierWrapper &>(qualifier);
2287 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2288 !symbolTable.atGlobalLevel())
2289 {
2290 error(storageQualifier.getLine(),
2291 "Local variables can only use the const storage qualifier.",
2292 storageQualifier.getQualifierString().c_str());
2293 }
2294 }
2295}
2296
Olli Etuaho43364892017-02-13 16:00:12 +00002297void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002298 const TSourceLoc &location)
2299{
Jiajia Qinbc585152017-06-23 15:42:17 +08002300 const std::string reason(
2301 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2302 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002303 if (memoryQualifier.readonly)
2304 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002305 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002306 }
2307 if (memoryQualifier.writeonly)
2308 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002309 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002310 }
Martin Radev049edfa2016-11-11 14:35:37 +02002311 if (memoryQualifier.coherent)
2312 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002313 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002314 }
2315 if (memoryQualifier.restrictQualifier)
2316 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002317 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002318 }
2319 if (memoryQualifier.volatileQualifier)
2320 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002321 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002322 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002323}
2324
jchen104cdac9e2017-05-08 11:01:20 +08002325// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2326// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002327void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2328 const TSourceLoc &loc,
2329 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002330{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002331 if (!IsAtomicCounter(type->getBasicType()))
2332 {
2333 return;
2334 }
2335
2336 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2337 : kAtomicCounterSize;
2338 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2339 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002340 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002341 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002342 {
2343 offset = bindingState.appendSpan(size);
2344 }
2345 else
2346 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002347 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002348 }
2349 if (offset == -1)
2350 {
2351 error(loc, "Offset overlapping", "atomic counter");
2352 return;
2353 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002354 layoutQualifier.offset = offset;
2355 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002356}
2357
Olli Etuaho454c34c2017-10-25 16:35:56 +03002358void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2359 const char *token,
2360 TType *type)
2361{
2362 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2363 {
2364 if (type->isArray() && type->getOutermostArraySize() == 0u)
2365 {
2366 // Set size for the unsized geometry shader inputs if they are declared after a valid
2367 // input primitive declaration.
2368 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2369 {
2370 ASSERT(mGeometryShaderInputArraySize > 0u);
2371 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2372 }
2373 else
2374 {
2375 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2376 // An input can be declared without an array size if there is a previous layout
2377 // which specifies the size.
2378 error(location,
2379 "Missing a valid input primitive declaration before declaring an unsized "
2380 "array input",
2381 token);
2382 }
2383 }
2384 else if (type->isArray())
2385 {
2386 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2387 }
2388 else
2389 {
2390 error(location, "Geometry shader input variable must be declared as an array", token);
2391 }
2392 }
2393}
2394
Olli Etuaho13389b62016-10-16 11:48:18 +01002395TIntermDeclaration *TParseContext::parseSingleDeclaration(
2396 TPublicType &publicType,
2397 const TSourceLoc &identifierOrTypeLocation,
2398 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002399{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002400 TType type(publicType);
2401 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2402 mDirectiveHandler.pragma().stdgl.invariantAll)
2403 {
2404 TQualifier qualifier = type.getQualifier();
2405
2406 // The directive handler has already taken care of rejecting invalid uses of this pragma
2407 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2408 // affected variable declarations:
2409 //
2410 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2411 // elsewhere, in TranslatorGLSL.)
2412 //
2413 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2414 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2415 // the way this is currently implemented we have to enable this compiler option before
2416 // parsing the shader and determining the shading language version it uses. If this were
2417 // implemented as a post-pass, the workaround could be more targeted.
2418 //
2419 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2420 // the specification, but there are desktop OpenGL drivers that expect that this is the
2421 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2422 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2423 {
2424 type.setInvariant(true);
2425 }
2426 }
2427
Olli Etuaho454c34c2017-10-25 16:35:56 +03002428 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002429
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002430 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2431 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002432
Olli Etuahobab4c082015-04-24 16:38:49 +03002433 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002434 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002435
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002436 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002437 if (emptyDeclaration)
2438 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002439 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002440 // In most cases we don't need to create a symbol node for an empty declaration.
2441 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2442 if (type.getBasicType() == EbtStruct)
2443 {
Olli Etuaho195be942017-12-04 23:40:14 +02002444 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01002445 new TVariable(&symbolTable, nullptr, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002446 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002447 }
jchen104cdac9e2017-05-08 11:01:20 +08002448 else if (IsAtomicCounter(publicType.getBasicType()))
2449 {
2450 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2451 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002452 }
2453 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002454 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002455 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002456
Olli Etuaho55bde912017-10-25 13:41:13 +03002457 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002458
Olli Etuaho55bc9052017-10-25 17:33:06 +03002459 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002460
Olli Etuaho2935c582015-04-08 14:32:06 +03002461 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002462 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002463 {
Olli Etuaho195be942017-12-04 23:40:14 +02002464 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002465 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002466 }
2467
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002468 TIntermDeclaration *declaration = new TIntermDeclaration();
2469 declaration->setLine(identifierOrTypeLocation);
2470 if (symbol)
2471 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002472 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002473 declaration->appendDeclarator(symbol);
2474 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002475 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002476}
2477
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002478TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2479 TPublicType &elementType,
2480 const TSourceLoc &identifierLocation,
2481 const TString &identifier,
2482 const TSourceLoc &indexLocation,
2483 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002484{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002485 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002486
Olli Etuaho55bde912017-10-25 13:41:13 +03002487 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002488 identifierLocation);
2489
Olli Etuaho55bde912017-10-25 13:41:13 +03002490 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002491
Olli Etuaho55bde912017-10-25 13:41:13 +03002492 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002493
Olli Etuaho55bde912017-10-25 13:41:13 +03002494 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002495 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002496
Olli Etuaho454c34c2017-10-25 16:35:56 +03002497 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002498
2499 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2500
Olli Etuaho55bc9052017-10-25 17:33:06 +03002501 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002502
Olli Etuaho13389b62016-10-16 11:48:18 +01002503 TIntermDeclaration *declaration = new TIntermDeclaration();
2504 declaration->setLine(identifierLocation);
2505
Olli Etuaho195be942017-12-04 23:40:14 +02002506 TVariable *variable = nullptr;
2507 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002508 {
Olli Etuaho195be942017-12-04 23:40:14 +02002509 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002510 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002511 declaration->appendDeclarator(symbol);
2512 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002513
Olli Etuaho13389b62016-10-16 11:48:18 +01002514 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002515}
2516
Olli Etuaho13389b62016-10-16 11:48:18 +01002517TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2518 const TSourceLoc &identifierLocation,
2519 const TString &identifier,
2520 const TSourceLoc &initLocation,
2521 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002522{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002523 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002524
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002525 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2526 identifierLocation);
2527
2528 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002529
Olli Etuaho13389b62016-10-16 11:48:18 +01002530 TIntermDeclaration *declaration = new TIntermDeclaration();
2531 declaration->setLine(identifierLocation);
2532
2533 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002534 TType type(publicType);
2535 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002536 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002537 if (initNode)
2538 {
2539 declaration->appendDeclarator(initNode);
2540 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002541 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002542 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002543}
2544
Olli Etuaho13389b62016-10-16 11:48:18 +01002545TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002546 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002547 const TSourceLoc &identifierLocation,
2548 const TString &identifier,
2549 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002550 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002551 const TSourceLoc &initLocation,
2552 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002553{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002554 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002555
Olli Etuaho55bde912017-10-25 13:41:13 +03002556 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002557 identifierLocation);
2558
Olli Etuaho55bde912017-10-25 13:41:13 +03002559 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002560
Olli Etuaho55bde912017-10-25 13:41:13 +03002561 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002562
Olli Etuaho55bde912017-10-25 13:41:13 +03002563 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002564 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565
Olli Etuaho13389b62016-10-16 11:48:18 +01002566 TIntermDeclaration *declaration = new TIntermDeclaration();
2567 declaration->setLine(identifierLocation);
2568
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002569 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002570 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002571 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002573 if (initNode)
2574 {
2575 declaration->appendDeclarator(initNode);
2576 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002577 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002578
2579 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002580}
2581
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002582TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002583 const TTypeQualifierBuilder &typeQualifierBuilder,
2584 const TSourceLoc &identifierLoc,
2585 const TString *identifier,
2586 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002587{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002588 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002589
Martin Radev70866b82016-07-22 15:27:42 +03002590 if (!typeQualifier.invariant)
2591 {
2592 error(identifierLoc, "Expected invariant", identifier->c_str());
2593 return nullptr;
2594 }
2595 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2596 {
2597 return nullptr;
2598 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002599 if (!symbol)
2600 {
2601 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002602 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002603 }
Martin Radev70866b82016-07-22 15:27:42 +03002604 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002605 {
Martin Radev70866b82016-07-22 15:27:42 +03002606 error(identifierLoc, "invariant declaration specifies qualifier",
2607 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002608 }
Martin Radev70866b82016-07-22 15:27:42 +03002609 if (typeQualifier.precision != EbpUndefined)
2610 {
2611 error(identifierLoc, "invariant declaration specifies precision",
2612 getPrecisionString(typeQualifier.precision));
2613 }
2614 if (!typeQualifier.layoutQualifier.isEmpty())
2615 {
2616 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2617 }
2618
2619 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002620 if (!variable)
2621 {
2622 return nullptr;
2623 }
Martin Radev70866b82016-07-22 15:27:42 +03002624 const TType &type = variable->getType();
2625
2626 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2627 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002628 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002629
2630 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2631
Olli Etuaho195be942017-12-04 23:40:14 +02002632 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002633 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002634
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002635 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002636}
2637
Olli Etuaho13389b62016-10-16 11:48:18 +01002638void TParseContext::parseDeclarator(TPublicType &publicType,
2639 const TSourceLoc &identifierLocation,
2640 const TString &identifier,
2641 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002642{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002643 // If the declaration starting this declarator list was empty (example: int,), some checks were
2644 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002645 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002646 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002647 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2648 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002649 }
2650
Olli Etuaho856c4972016-08-08 11:38:39 +03002651 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002652
Olli Etuaho43364892017-02-13 16:00:12 +00002653 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002654
2655 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2656
Olli Etuaho55bde912017-10-25 13:41:13 +03002657 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2658
Olli Etuaho55bc9052017-10-25 17:33:06 +03002659 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2660
Olli Etuaho195be942017-12-04 23:40:14 +02002661 TVariable *variable = nullptr;
2662 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002663 {
Olli Etuaho195be942017-12-04 23:40:14 +02002664 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002665 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002666 declarationOut->appendDeclarator(symbol);
2667 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002668}
2669
Olli Etuaho55bde912017-10-25 13:41:13 +03002670void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002671 const TSourceLoc &identifierLocation,
2672 const TString &identifier,
2673 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002674 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002675 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002676{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002677 // If the declaration starting this declarator list was empty (example: int,), some checks were
2678 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002679 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002680 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002681 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002682 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002683 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002684
Olli Etuaho55bde912017-10-25 13:41:13 +03002685 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002686
Olli Etuaho55bde912017-10-25 13:41:13 +03002687 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002688 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002689 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002690 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002691
Olli Etuaho454c34c2017-10-25 16:35:56 +03002692 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2693
Olli Etuaho55bde912017-10-25 13:41:13 +03002694 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2695
Olli Etuaho55bc9052017-10-25 17:33:06 +03002696 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002697
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002698 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002699 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002700 {
Olli Etuaho195be942017-12-04 23:40:14 +02002701 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002702 symbol->setLine(identifierLocation);
2703 declarationOut->appendDeclarator(symbol);
2704 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002705 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002706}
2707
Olli Etuaho13389b62016-10-16 11:48:18 +01002708void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2709 const TSourceLoc &identifierLocation,
2710 const TString &identifier,
2711 const TSourceLoc &initLocation,
2712 TIntermTyped *initializer,
2713 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002714{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002715 // If the declaration starting this declarator list was empty (example: int,), some checks were
2716 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002717 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002718 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002719 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2720 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002721 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002722
Olli Etuaho856c4972016-08-08 11:38:39 +03002723 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002724
Olli Etuaho13389b62016-10-16 11:48:18 +01002725 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002726 TType type(publicType);
2727 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002728 {
2729 //
2730 // build the intermediate representation
2731 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002732 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002733 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002734 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002735 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002736 }
2737}
2738
Olli Etuaho55bde912017-10-25 13:41:13 +03002739void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002740 const TSourceLoc &identifierLocation,
2741 const TString &identifier,
2742 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002743 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002744 const TSourceLoc &initLocation,
2745 TIntermTyped *initializer,
2746 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002747{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002748 // If the declaration starting this declarator list was empty (example: int,), some checks were
2749 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002750 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002751 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002752 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002753 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002754 }
2755
Olli Etuaho55bde912017-10-25 13:41:13 +03002756 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002757
Olli Etuaho55bde912017-10-25 13:41:13 +03002758 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002759
Olli Etuaho55bde912017-10-25 13:41:13 +03002760 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002761 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762
2763 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002764 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002765 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002766 {
2767 if (initNode)
2768 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002769 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002770 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002771 }
2772}
2773
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002774TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2775{
2776 // It's simpler to parse an empty statement as a constant expression rather than having a
2777 // different type of node just for empty statements, that will be pruned from the AST anyway.
2778 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2779 node->setLine(location);
2780 return node;
2781}
2782
jchen104cdac9e2017-05-08 11:01:20 +08002783void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2784 const TSourceLoc &location)
2785{
2786 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2787 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2788 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2789 {
2790 error(location, "Requires both binding and offset", "layout");
2791 return;
2792 }
2793 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2794}
2795
Olli Etuahocce89652017-06-19 16:04:09 +03002796void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2797 const TPublicType &type,
2798 const TSourceLoc &loc)
2799{
2800 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2801 !getFragmentPrecisionHigh())
2802 {
2803 error(loc, "precision is not supported in fragment shader", "highp");
2804 }
2805
2806 if (!CanSetDefaultPrecisionOnType(type))
2807 {
2808 error(loc, "illegal type argument for default precision qualifier",
2809 getBasicString(type.getBasicType()));
2810 return;
2811 }
2812 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2813}
2814
Shaob5cc1192017-07-06 10:47:20 +08002815bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2816{
2817 switch (typeQualifier.layoutQualifier.primitiveType)
2818 {
2819 case EptLines:
2820 case EptLinesAdjacency:
2821 case EptTriangles:
2822 case EptTrianglesAdjacency:
2823 return typeQualifier.qualifier == EvqGeometryIn;
2824
2825 case EptLineStrip:
2826 case EptTriangleStrip:
2827 return typeQualifier.qualifier == EvqGeometryOut;
2828
2829 case EptPoints:
2830 return true;
2831
2832 default:
2833 UNREACHABLE();
2834 return false;
2835 }
2836}
2837
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002838void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2839 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002840{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002841 if (mGeometryShaderInputArraySize == 0u)
2842 {
2843 mGeometryShaderInputArraySize = inputArraySize;
2844 }
2845 else if (mGeometryShaderInputArraySize != inputArraySize)
2846 {
2847 error(line,
2848 "Array size or input primitive declaration doesn't match the size of earlier sized "
2849 "array inputs.",
2850 "layout");
2851 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002852}
2853
Shaob5cc1192017-07-06 10:47:20 +08002854bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2855{
2856 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2857
2858 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2859
2860 if (layoutQualifier.maxVertices != -1)
2861 {
2862 error(typeQualifier.line,
2863 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2864 return false;
2865 }
2866
2867 // Set mGeometryInputPrimitiveType if exists
2868 if (layoutQualifier.primitiveType != EptUndefined)
2869 {
2870 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2871 {
2872 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2873 return false;
2874 }
2875
2876 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2877 {
2878 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002879 setGeometryShaderInputArraySize(
2880 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2881 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002882 }
2883 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2884 {
2885 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2886 "layout");
2887 return false;
2888 }
2889 }
2890
2891 // Set mGeometryInvocations if exists
2892 if (layoutQualifier.invocations > 0)
2893 {
2894 if (mGeometryShaderInvocations == 0)
2895 {
2896 mGeometryShaderInvocations = layoutQualifier.invocations;
2897 }
2898 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2899 {
2900 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2901 "layout");
2902 return false;
2903 }
2904 }
2905
2906 return true;
2907}
2908
2909bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2910{
2911 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2912
2913 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2914
2915 if (layoutQualifier.invocations > 0)
2916 {
2917 error(typeQualifier.line,
2918 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2919 return false;
2920 }
2921
2922 // Set mGeometryOutputPrimitiveType if exists
2923 if (layoutQualifier.primitiveType != EptUndefined)
2924 {
2925 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2926 {
2927 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2928 return false;
2929 }
2930
2931 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2932 {
2933 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2934 }
2935 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2936 {
2937 error(typeQualifier.line,
2938 "primitive doesn't match earlier output primitive declaration", "layout");
2939 return false;
2940 }
2941 }
2942
2943 // Set mGeometryMaxVertices if exists
2944 if (layoutQualifier.maxVertices > -1)
2945 {
2946 if (mGeometryShaderMaxVertices == -1)
2947 {
2948 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2949 }
2950 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2951 {
2952 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2953 "layout");
2954 return false;
2955 }
2956 }
2957
2958 return true;
2959}
2960
Martin Radev70866b82016-07-22 15:27:42 +03002961void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002962{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002963 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002964 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002965
Martin Radev70866b82016-07-22 15:27:42 +03002966 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2967 typeQualifier.line);
2968
Jamie Madillc2128ff2016-07-04 10:26:17 -04002969 // It should never be the case, but some strange parser errors can send us here.
2970 if (layoutQualifier.isEmpty())
2971 {
2972 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002973 return;
2974 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002975
Martin Radev802abe02016-08-04 17:48:32 +03002976 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002977 {
Olli Etuaho43364892017-02-13 16:00:12 +00002978 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002979 return;
2980 }
2981
Olli Etuaho43364892017-02-13 16:00:12 +00002982 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2983
2984 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002985
2986 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2987
Andrei Volykhina5527072017-03-22 16:46:30 +03002988 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2989
jchen104cdac9e2017-05-08 11:01:20 +08002990 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2991
Qin Jiajiaca68d982017-09-18 16:41:56 +08002992 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2993 typeQualifier.qualifier);
2994
Martin Radev802abe02016-08-04 17:48:32 +03002995 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002996 {
Martin Radev802abe02016-08-04 17:48:32 +03002997 if (mComputeShaderLocalSizeDeclared &&
2998 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2999 {
3000 error(typeQualifier.line, "Work group size does not match the previous declaration",
3001 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003002 return;
3003 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003004
Martin Radev802abe02016-08-04 17:48:32 +03003005 if (mShaderVersion < 310)
3006 {
3007 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003008 return;
3009 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003010
Martin Radev4c4c8e72016-08-04 12:25:34 +03003011 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003012 {
3013 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003014 return;
3015 }
3016
3017 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3018 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3019
3020 const TConstantUnion *maxComputeWorkGroupSizeData =
3021 maxComputeWorkGroupSize->getConstPointer();
3022
3023 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3024 {
3025 if (layoutQualifier.localSize[i] != -1)
3026 {
3027 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3028 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3029 if (mComputeShaderLocalSize[i] < 1 ||
3030 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3031 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003032 std::stringstream reasonStream;
3033 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3034 << maxComputeWorkGroupSizeValue;
3035 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003036
Olli Etuaho4de340a2016-12-16 09:32:03 +00003037 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003038 return;
3039 }
3040 }
3041 }
3042
3043 mComputeShaderLocalSizeDeclared = true;
3044 }
Shaob5cc1192017-07-06 10:47:20 +08003045 else if (typeQualifier.qualifier == EvqGeometryIn)
3046 {
3047 if (mShaderVersion < 310)
3048 {
3049 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3050 return;
3051 }
3052
3053 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3054 {
3055 return;
3056 }
3057 }
3058 else if (typeQualifier.qualifier == EvqGeometryOut)
3059 {
3060 if (mShaderVersion < 310)
3061 {
3062 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3063 "layout");
3064 return;
3065 }
3066
3067 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3068 {
3069 return;
3070 }
3071 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003072 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3073 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003074 {
3075 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3076 // specification.
3077 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3078 {
3079 error(typeQualifier.line, "Number of views does not match the previous declaration",
3080 "layout");
3081 return;
3082 }
3083
3084 if (layoutQualifier.numViews == -1)
3085 {
3086 error(typeQualifier.line, "No num_views specified", "layout");
3087 return;
3088 }
3089
3090 if (layoutQualifier.numViews > mMaxNumViews)
3091 {
3092 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3093 "layout");
3094 return;
3095 }
3096
3097 mNumViews = layoutQualifier.numViews;
3098 }
Martin Radev802abe02016-08-04 17:48:32 +03003099 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003100 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003101 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003102 {
Martin Radev802abe02016-08-04 17:48:32 +03003103 return;
3104 }
3105
Jiajia Qinbc585152017-06-23 15:42:17 +08003106 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003107 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003108 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003109 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003110 return;
3111 }
3112
3113 if (mShaderVersion < 300)
3114 {
3115 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3116 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003117 return;
3118 }
3119
Olli Etuaho09b04a22016-12-15 13:30:26 +00003120 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003121
3122 if (layoutQualifier.matrixPacking != EmpUnspecified)
3123 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003124 if (typeQualifier.qualifier == EvqUniform)
3125 {
3126 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3127 }
3128 else if (typeQualifier.qualifier == EvqBuffer)
3129 {
3130 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3131 }
Martin Radev802abe02016-08-04 17:48:32 +03003132 }
3133
3134 if (layoutQualifier.blockStorage != EbsUnspecified)
3135 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003136 if (typeQualifier.qualifier == EvqUniform)
3137 {
3138 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3139 }
3140 else if (typeQualifier.qualifier == EvqBuffer)
3141 {
3142 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3143 }
Martin Radev802abe02016-08-04 17:48:32 +03003144 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003145 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003146}
3147
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003148TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3149 const TFunction &function,
3150 const TSourceLoc &location,
3151 bool insertParametersToSymbolTable)
3152{
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003153 ASSERT(function.name());
3154 checkIsNotReserved(location, *function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003155
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003156 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003157 prototype->setLine(location);
3158
3159 for (size_t i = 0; i < function.getParamCount(); i++)
3160 {
3161 const TConstParameter &param = function.getParam(i);
3162
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003163 TIntermSymbol *symbol = nullptr;
3164
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003165 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3166 // be used for unused args).
3167 if (param.name != nullptr)
3168 {
Olli Etuaho195be942017-12-04 23:40:14 +02003169 TVariable *variable =
3170 new TVariable(&symbolTable, param.name, *param.type, SymbolType::UserDefined);
3171 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003172 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003173 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003174 {
Olli Etuaho195be942017-12-04 23:40:14 +02003175 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003176 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003177 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003178 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003179 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003180 // Unsized type of a named parameter should have already been checked and sanitized.
3181 ASSERT(!param.type->isUnsizedArray());
3182 }
3183 else
3184 {
3185 if (param.type->isUnsizedArray())
3186 {
3187 error(location, "function parameter array must be sized at compile time", "[]");
3188 // We don't need to size the arrays since the parameter is unnamed and hence
3189 // inaccessible.
3190 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003191 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003192 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003193 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003194 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3195 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003196 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003197 new TVariable(&symbolTable, nullptr, *param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003198 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003199 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003200 symbol->setLine(location);
3201 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003202 }
3203 return prototype;
3204}
3205
Olli Etuaho16c745a2017-01-16 17:02:27 +00003206TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3207 const TFunction &parsedFunction,
3208 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003209{
Olli Etuaho476197f2016-10-11 13:59:08 +01003210 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3211 // first declaration. Either way the instance in the symbol table is used to track whether the
3212 // function is declared multiple times.
3213 TFunction *function = static_cast<TFunction *>(
3214 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3215 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003216 {
3217 // ESSL 1.00.17 section 4.2.7.
3218 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3219 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003220 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003221 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003222
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003223 TIntermFunctionPrototype *prototype =
3224 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003225
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003226 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003227
3228 if (!symbolTable.atGlobalLevel())
3229 {
3230 // ESSL 3.00.4 section 4.2.4.
3231 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003232 }
3233
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003234 return prototype;
3235}
3236
Olli Etuaho336b1472016-10-05 16:37:55 +01003237TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003238 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003239 TIntermBlock *functionBody,
3240 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003241{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003242 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003243 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3244 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003245 error(location, "function does not return a value:",
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003246 functionPrototype->getFunction()->name()->c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003247 }
3248
Olli Etuahof51fdd22016-10-03 10:03:40 +01003249 if (functionBody == nullptr)
3250 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003251 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003252 functionBody->setLine(location);
3253 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003254 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003255 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003256 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003257
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003258 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003259 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003260}
3261
Olli Etuaho476197f2016-10-11 13:59:08 +01003262void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3263 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003264 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003265{
Olli Etuaho476197f2016-10-11 13:59:08 +01003266 ASSERT(function);
3267 ASSERT(*function);
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003268 ASSERT((*function)->name());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003269 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003270 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003271
3272 if (builtIn)
3273 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003274 error(location, "built-in functions cannot be redefined", (*function)->name()->c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003275 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003276 else
Jamie Madill185fb402015-06-12 15:48:48 -04003277 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003278 TFunction *prevDec = static_cast<TFunction *>(
3279 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3280
3281 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3282 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3283 // occurance.
3284 if (*function != prevDec)
3285 {
3286 // Swap the parameters of the previous declaration to the parameters of the function
3287 // definition (parameter names may differ).
3288 prevDec->swapParameters(**function);
3289
3290 // The function definition will share the same symbol as any previous declaration.
3291 *function = prevDec;
3292 }
3293
3294 if ((*function)->isDefined())
3295 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003296 error(location, "function already has a body", (*function)->name()->c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003297 }
3298
3299 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003300 }
Jamie Madill185fb402015-06-12 15:48:48 -04003301
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003302 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003303 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003304 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003305
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003306 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003307 setLoopNestingLevel(0);
3308}
3309
Jamie Madillb98c3a82015-07-23 14:26:04 -04003310TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003311{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003312 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003313 // We don't know at this point whether this is a function definition or a prototype.
3314 // The definition production code will check for redefinitions.
3315 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003316 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003317 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3318 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003319 //
3320 TFunction *prevDec =
3321 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303322
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003323 ASSERT(function->name() != nullptr);
3324
Olli Etuahod80f2942017-11-06 12:44:45 +02003325 for (size_t i = 0u; i < function->getParamCount(); ++i)
3326 {
3327 auto &param = function->getParam(i);
3328 if (param.type->isStructSpecifier())
3329 {
3330 // ESSL 3.00.6 section 12.10.
3331 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003332 function->name()->c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003333 }
3334 }
3335
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003336 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003337 function->name()->c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303338 {
Martin Radevda6254b2016-12-14 17:00:36 +02003339 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303340 // Therefore overloading or redefining builtin functions is an error.
3341 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003342 function->name()->c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303343 }
3344 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003345 {
3346 if (prevDec->getReturnType() != function->getReturnType())
3347 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003348 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003349 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003350 }
3351 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3352 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003353 if (prevDec->getParam(i).type->getQualifier() !=
3354 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003355 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003356 error(location,
3357 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003358 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003359 }
3360 }
3361 }
3362
3363 //
3364 // Check for previously declared variables using the same name.
3365 //
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003366 TSymbol *prevSym = symbolTable.find(*function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003367 if (prevSym)
3368 {
3369 if (!prevSym->isFunction())
3370 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003371 error(location, "redefinition of a function", function->name()->c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003372 }
3373 }
3374 else
3375 {
3376 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003377 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003378 }
3379
3380 // We're at the inner scope level of the function's arguments and body statement.
3381 // Add the function prototype to the surrounding scope instead.
3382 symbolTable.getOuterLevel()->insert(function);
3383
Olli Etuaho78d13742017-01-18 13:06:10 +00003384 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003385 if (*function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003386 {
3387 if (function->getParamCount() > 0)
3388 {
3389 error(location, "function cannot take any parameter(s)", "main");
3390 }
3391 if (function->getReturnType().getBasicType() != EbtVoid)
3392 {
3393 error(location, "main function cannot return a value",
3394 function->getReturnType().getBasicString());
3395 }
3396 }
3397
Jamie Madill185fb402015-06-12 15:48:48 -04003398 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003399 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3400 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003401 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3402 //
3403 return function;
3404}
3405
Olli Etuaho9de84a52016-06-14 17:36:01 +03003406TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3407 const TString *name,
3408 const TSourceLoc &location)
3409{
3410 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3411 {
3412 error(location, "no qualifiers allowed for function return",
3413 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003414 }
3415 if (!type.layoutQualifier.isEmpty())
3416 {
3417 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003418 }
jchen10cc2a10e2017-05-03 14:05:12 +08003419 // make sure an opaque type is not involved as well...
3420 std::string reason(getBasicString(type.getBasicType()));
3421 reason += "s can't be function return values";
3422 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003423 if (mShaderVersion < 300)
3424 {
3425 // Array return values are forbidden, but there's also no valid syntax for declaring array
3426 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003427 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003428
3429 if (type.isStructureContainingArrays())
3430 {
3431 // ESSL 1.00.17 section 6.1 Function Definitions
3432 error(location, "structures containing arrays can't be function return values",
3433 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003434 }
3435 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003436
3437 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003438 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003439}
3440
Olli Etuahocce89652017-06-19 16:04:09 +03003441TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3442{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003443 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003444 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3445 // really only interested in the mangled name of the function to look up the actual function
3446 // from the symbol table. If we just had the name string and the types of the parameters that
3447 // would be enough, but TFunction carries a lot of extra information in addition to that.
3448 // Besides function calls we do have to store constructor calls in the same data structure, for
3449 // them we need to store a TType.
Olli Etuaho0c371002017-12-13 17:00:25 +04003450 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved, false);
Olli Etuahocce89652017-06-19 16:04:09 +03003451}
3452
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003453TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003454{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003455 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003456 {
3457 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3458 "[]");
3459 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003460 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003461 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003462 error(publicType.getLine(), "constructor can't be a structure definition",
3463 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003464 }
3465
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003466 TType *type = new TType(publicType);
3467 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003468 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003469 error(publicType.getLine(), "cannot construct this type",
3470 getBasicString(publicType.getBasicType()));
3471 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003472 }
3473
Olli Etuaho0c371002017-12-13 17:00:25 +04003474 return new TFunction(&symbolTable, nullptr, type, SymbolType::NotResolved, true, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003475}
3476
Olli Etuaho55bde912017-10-25 13:41:13 +03003477void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3478 const char *errorMessage,
3479 const char *token,
3480 TType *arrayType)
3481{
3482 if (arrayType->isUnsizedArray())
3483 {
3484 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003485 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003486 }
3487}
3488
3489TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003490 const TString *name,
3491 const TSourceLoc &nameLoc)
3492{
Olli Etuaho55bde912017-10-25 13:41:13 +03003493 ASSERT(type);
3494 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3495 type);
3496 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003497 {
3498 error(nameLoc, "illegal use of type 'void'", name->c_str());
3499 }
3500 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003501 TParameter param = {name, type};
3502 return param;
3503}
3504
Olli Etuaho55bde912017-10-25 13:41:13 +03003505TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3506 const TString *name,
3507 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003508{
Olli Etuaho55bde912017-10-25 13:41:13 +03003509 TType *type = new TType(publicType);
3510 return parseParameterDeclarator(type, name, nameLoc);
3511}
3512
3513TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3514 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003515 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003516 const TSourceLoc &arrayLoc,
3517 TPublicType *elementType)
3518{
3519 checkArrayElementIsNotArray(arrayLoc, *elementType);
3520 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003521 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003522 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003523}
3524
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003525bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3526 TType type,
3527 const TSourceLoc &line)
3528{
3529 if (arguments->empty())
3530 {
3531 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3532 return false;
3533 }
3534 for (TIntermNode *arg : *arguments)
3535 {
3536 TIntermTyped *element = arg->getAsTyped();
3537 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003538 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3539 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003540 {
3541 error(line, "constructing from a non-dereferenced array", "constructor");
3542 return false;
3543 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003544 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003545 {
3546 if (dimensionalityFromElement == 1u)
3547 {
3548 error(line, "implicitly sized array of arrays constructor argument is not an array",
3549 "constructor");
3550 }
3551 else
3552 {
3553 error(line,
3554 "implicitly sized array of arrays constructor argument dimensionality is too "
3555 "low",
3556 "constructor");
3557 }
3558 return false;
3559 }
3560 }
3561 return true;
3562}
3563
Jamie Madillb98c3a82015-07-23 14:26:04 -04003564// This function is used to test for the correctness of the parameters passed to various constructor
3565// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003566//
Olli Etuaho856c4972016-08-08 11:38:39 +03003567// 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 +00003568//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003569TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003570 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303571 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003572{
Olli Etuaho856c4972016-08-08 11:38:39 +03003573 if (type.isUnsizedArray())
3574 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003575 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003576 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003577 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003578 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003579 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003580 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3581 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003582 if (type.getOutermostArraySize() == 0u)
3583 {
3584 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3585 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003586 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003587 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003588 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003589 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003590 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003591 }
3592 }
3593 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003594 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003595
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003596 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003597 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003598 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003599 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003600
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003601 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003602 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003603
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003604 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3605 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003606 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003607 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003608 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003609 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003610}
3611
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003612//
3613// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003614// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003615//
Olli Etuaho13389b62016-10-16 11:48:18 +01003616TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003617 const TTypeQualifierBuilder &typeQualifierBuilder,
3618 const TSourceLoc &nameLine,
3619 const TString &blockName,
3620 TFieldList *fieldList,
3621 const TString *instanceName,
3622 const TSourceLoc &instanceLine,
3623 TIntermTyped *arrayIndex,
3624 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003625{
Olli Etuaho856c4972016-08-08 11:38:39 +03003626 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003627
Olli Etuaho77ba4082016-12-16 12:01:18 +00003628 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003629
Jiajia Qinbc585152017-06-23 15:42:17 +08003630 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003631 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003632 error(typeQualifier.line,
3633 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3634 "3.10",
3635 getQualifierString(typeQualifier.qualifier));
3636 }
3637 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3638 {
3639 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003640 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003641 }
3642
Martin Radev70866b82016-07-22 15:27:42 +03003643 if (typeQualifier.invariant)
3644 {
3645 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3646 }
3647
Jiajia Qinbc585152017-06-23 15:42:17 +08003648 if (typeQualifier.qualifier != EvqBuffer)
3649 {
3650 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3651 }
Olli Etuaho43364892017-02-13 16:00:12 +00003652
jchen10af713a22017-04-19 09:10:56 +08003653 // add array index
3654 unsigned int arraySize = 0;
3655 if (arrayIndex != nullptr)
3656 {
3657 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3658 }
3659
3660 if (mShaderVersion < 310)
3661 {
3662 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3663 }
3664 else
3665 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003666 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3667 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003668 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003669
Andrei Volykhina5527072017-03-22 16:46:30 +03003670 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3671
Jamie Madill099c0f32013-06-20 11:55:52 -04003672 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003673 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003674 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3675 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003676
Jamie Madill099c0f32013-06-20 11:55:52 -04003677 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3678 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003679 if (typeQualifier.qualifier == EvqUniform)
3680 {
3681 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3682 }
3683 else if (typeQualifier.qualifier == EvqBuffer)
3684 {
3685 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3686 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003687 }
3688
Jamie Madill1566ef72013-06-20 11:55:54 -04003689 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3690 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003691 if (typeQualifier.qualifier == EvqUniform)
3692 {
3693 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3694 }
3695 else if (typeQualifier.qualifier == EvqBuffer)
3696 {
3697 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3698 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003699 }
3700
Olli Etuaho856c4972016-08-08 11:38:39 +03003701 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003702
Martin Radev2cc85b32016-08-05 16:22:53 +03003703 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3704
Jamie Madill98493dd2013-07-08 14:39:03 -04003705 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303706 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3707 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003708 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303709 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003710 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303711 {
jchen10cc2a10e2017-05-03 14:05:12 +08003712 std::string reason("unsupported type - ");
3713 reason += fieldType->getBasicString();
3714 reason += " types are not allowed in interface blocks";
3715 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003716 }
3717
Jamie Madill98493dd2013-07-08 14:39:03 -04003718 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003719 switch (qualifier)
3720 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003721 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003722 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003723 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003724 if (typeQualifier.qualifier == EvqBuffer)
3725 {
3726 error(field->line(), "invalid qualifier on shader storage block member",
3727 getQualifierString(qualifier));
3728 }
3729 break;
3730 case EvqBuffer:
3731 if (typeQualifier.qualifier == EvqUniform)
3732 {
3733 error(field->line(), "invalid qualifier on uniform block member",
3734 getQualifierString(qualifier));
3735 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003736 break;
3737 default:
3738 error(field->line(), "invalid qualifier on interface block member",
3739 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003740 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003741 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003742
Martin Radev70866b82016-07-22 15:27:42 +03003743 if (fieldType->isInvariant())
3744 {
3745 error(field->line(), "invalid qualifier on interface block member", "invariant");
3746 }
3747
Jamie Madilla5efff92013-06-06 11:56:47 -04003748 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003749 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003750 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003751 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003752
Jamie Madill98493dd2013-07-08 14:39:03 -04003753 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003754 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003755 error(field->line(), "invalid layout qualifier: cannot be used here",
3756 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003757 }
3758
Jamie Madill98493dd2013-07-08 14:39:03 -04003759 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003760 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003761 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003762 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003763 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003764 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003765 warning(field->line(),
3766 "extraneous layout qualifier: only has an effect on matrix types",
3767 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003768 }
3769
Jamie Madill98493dd2013-07-08 14:39:03 -04003770 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003771
Olli Etuahoebee5b32017-11-23 12:56:32 +02003772 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3773 typeQualifier.qualifier != EvqBuffer)
3774 {
3775 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3776 checkIsNotUnsizedArray(field->line(),
3777 "array members of interface blocks must specify a size",
3778 field->name().c_str(), field->type());
3779 }
3780
Jiajia Qinbc585152017-06-23 15:42:17 +08003781 if (typeQualifier.qualifier == EvqBuffer)
3782 {
3783 // set memory qualifiers
3784 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3785 // qualified with a memory qualifier, it is as if all of its members were declared with
3786 // the same memory qualifier.
3787 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3788 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3789 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3790 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3791 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3792 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3793 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3794 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3795 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3796 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3797 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003798 }
3799
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003800 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3801 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003802 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3803 {
3804 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3805 }
3806
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003807 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3808 if (arrayIndex != nullptr)
3809 {
3810 interfaceBlockType.makeArray(arraySize);
3811 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003812
Olli Etuaho195be942017-12-04 23:40:14 +02003813 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003814 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3815 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003816 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003817 new TVariable(&symbolTable, instanceName, interfaceBlockType,
3818 instanceName ? SymbolType::UserDefined : SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003819
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003820 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003821 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003822 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003823 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3824 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003825 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303826 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003827
3828 // set parent pointer of the field variable
3829 fieldType->setInterfaceBlock(interfaceBlock);
3830
Olli Etuaho195be942017-12-04 23:40:14 +02003831 TVariable *fieldVariable =
3832 new TVariable(&symbolTable, &field->name(), *fieldType, SymbolType::UserDefined);
3833 if (symbolTable.declareVariable(fieldVariable))
Olli Etuaho0f684632017-07-13 12:42:15 +03003834 {
3835 fieldVariable->setQualifier(typeQualifier.qualifier);
3836 }
3837 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303838 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003839 error(field->line(), "redefinition of an interface block member name",
3840 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003841 }
3842 }
3843 }
3844 else
3845 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003846 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003847
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003848 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003849 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303850 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003851 error(instanceLine, "redefinition of an interface block instance name",
3852 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003853 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003854 }
3855
Olli Etuaho195be942017-12-04 23:40:14 +02003856 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3857 blockSymbol->setLine(typeQualifier.line);
3858 TIntermDeclaration *declaration = new TIntermDeclaration();
3859 declaration->appendDeclarator(blockSymbol);
3860 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003861
3862 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003863 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003864}
3865
Olli Etuaho383b7912016-08-05 11:22:59 +03003866void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003867{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003868 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003869
3870 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003871 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303872 if (mStructNestingLevel > 1)
3873 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003874 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003875 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003876}
3877
3878void TParseContext::exitStructDeclaration()
3879{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003880 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003881}
3882
Olli Etuaho8a176262016-08-16 14:23:01 +03003883void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003884{
Jamie Madillacb4b812016-11-07 13:50:29 -05003885 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303886 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003887 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003888 }
3889
Arun Patole7e7e68d2015-05-22 12:02:25 +05303890 if (field.type()->getBasicType() != EbtStruct)
3891 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003892 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003893 }
3894
3895 // We're already inside a structure definition at this point, so add
3896 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303897 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3898 {
Jamie Madill41a49272014-03-18 16:10:13 -04003899 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003900 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3901 {
3902 // This may happen in case there are nested struct definitions. While they are also
3903 // invalid GLSL, they don't cause a syntax error.
3904 reasonStream << "Struct nesting";
3905 }
3906 else
3907 {
3908 reasonStream << "Reference of struct type "
3909 << field.type()->getStruct()->name()->c_str();
3910 }
3911 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003912 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003913 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003914 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003915 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003916}
3917
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003918//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003919// Parse an array index expression
3920//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003921TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3922 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303923 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003924{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003925 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3926 {
3927 if (baseExpression->getAsSymbolNode())
3928 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303929 error(location, " left of '[' is not of type array, matrix, or vector ",
3930 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003931 }
3932 else
3933 {
3934 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3935 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003936
Olli Etuaho3ec75682017-07-05 17:02:55 +03003937 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003938 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003939
Jiawei Shaod8105a02017-08-08 09:54:36 +08003940 if (baseExpression->getQualifier() == EvqPerVertexIn)
3941 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003942 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003943 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3944 {
3945 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3946 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3947 }
3948 }
3949
Jamie Madill21c1e452014-12-29 11:33:41 -05003950 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3951
Olli Etuaho36b05142015-11-12 13:10:42 +02003952 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3953 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3954 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3955 // index is a constant expression.
3956 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3957 {
3958 if (baseExpression->isInterfaceBlock())
3959 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003960 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003961 switch (baseExpression->getQualifier())
3962 {
3963 case EvqPerVertexIn:
3964 break;
3965 case EvqUniform:
3966 case EvqBuffer:
3967 error(location,
3968 "array indexes for uniform block arrays and shader storage block arrays "
3969 "must be constant integral expressions",
3970 "[");
3971 break;
3972 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003973 // We can reach here only in error cases.
3974 ASSERT(mDiagnostics->numErrors() > 0);
3975 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003976 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003977 }
3978 else if (baseExpression->getQualifier() == EvqFragmentOut)
3979 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003980 error(location,
3981 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003982 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003983 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3984 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003985 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003986 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003987 }
3988
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003989 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003990 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003991 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3992 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3993 // constant fold expressions that are not constant expressions). The most compatible way to
3994 // handle this case is to report a warning instead of an error and force the index to be in
3995 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003996 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003997 int index = 0;
3998 if (indexConstantUnion->getBasicType() == EbtInt)
3999 {
4000 index = indexConstantUnion->getIConst(0);
4001 }
4002 else if (indexConstantUnion->getBasicType() == EbtUInt)
4003 {
4004 index = static_cast<int>(indexConstantUnion->getUConst(0));
4005 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004006
4007 int safeIndex = -1;
4008
Olli Etuahoebee5b32017-11-23 12:56:32 +02004009 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004010 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004011 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4012 safeIndex = 0;
4013 }
4014
4015 if (!baseExpression->getType().isUnsizedArray())
4016 {
4017 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004018 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004019 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004020 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004021 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4022 {
4023 outOfRangeError(outOfRangeIndexIsError, location,
4024 "array index for gl_FragData must be zero when "
4025 "GL_EXT_draw_buffers is disabled",
4026 "[]");
4027 safeIndex = 0;
4028 }
4029 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004030 }
4031 // Only do generic out-of-range check if similar error hasn't already been reported.
4032 if (safeIndex < 0)
4033 {
4034 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004035 {
4036 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4037 baseExpression->getOutermostArraySize(),
4038 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004039 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004040 else if (baseExpression->isMatrix())
4041 {
4042 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4043 baseExpression->getType().getCols(),
4044 "matrix field selection out of range");
4045 }
4046 else
4047 {
4048 ASSERT(baseExpression->isVector());
4049 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4050 baseExpression->getType().getNominalSize(),
4051 "vector field selection out of range");
4052 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004053 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004054
Olli Etuahoebee5b32017-11-23 12:56:32 +02004055 ASSERT(safeIndex >= 0);
4056 // Data of constant unions can't be changed, because it may be shared with other
4057 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4058 // sanitized object.
4059 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4060 {
4061 TConstantUnion *safeConstantUnion = new TConstantUnion();
4062 safeConstantUnion->setIConst(safeIndex);
4063 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4064 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4065 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004066
Olli Etuahoebee5b32017-11-23 12:56:32 +02004067 TIntermBinary *node =
4068 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4069 node->setLine(location);
4070 return node->fold(mDiagnostics);
4071 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004072 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004073
4074 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4075 node->setLine(location);
4076 // Indirect indexing can never be constant folded.
4077 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004078}
4079
Olli Etuahoebee5b32017-11-23 12:56:32 +02004080int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4081 const TSourceLoc &location,
4082 int index,
4083 int arraySize,
4084 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004085{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004086 // Should not reach here with an unsized / runtime-sized array.
4087 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004088 // A negative index should already have been checked.
4089 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004090 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004091 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004092 std::stringstream reasonStream;
4093 reasonStream << reason << " '" << index << "'";
4094 std::string token = reasonStream.str();
4095 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004096 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004097 }
4098 return index;
4099}
4100
Jamie Madillb98c3a82015-07-23 14:26:04 -04004101TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4102 const TSourceLoc &dotLocation,
4103 const TString &fieldString,
4104 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004105{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004106 if (baseExpression->isArray())
4107 {
4108 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004109 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004110 }
4111
4112 if (baseExpression->isVector())
4113 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004114 TVector<int> fieldOffsets;
4115 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4116 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004117 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004118 fieldOffsets.resize(1);
4119 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004120 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004121 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4122 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004123
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004124 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004125 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004126 else if (baseExpression->getBasicType() == EbtStruct)
4127 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304128 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004129 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004130 {
4131 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004132 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004133 }
4134 else
4135 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004136 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004137 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004138 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004139 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004140 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004141 {
4142 fieldFound = true;
4143 break;
4144 }
4145 }
4146 if (fieldFound)
4147 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004148 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004149 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004150 TIntermBinary *node =
4151 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4152 node->setLine(dotLocation);
4153 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004154 }
4155 else
4156 {
4157 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004158 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004159 }
4160 }
4161 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004162 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004163 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304164 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004165 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004166 {
4167 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004168 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004169 }
4170 else
4171 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004172 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004173 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004174 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004175 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004176 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004177 {
4178 fieldFound = true;
4179 break;
4180 }
4181 }
4182 if (fieldFound)
4183 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004184 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004185 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004186 TIntermBinary *node =
4187 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4188 node->setLine(dotLocation);
4189 // Indexing interface blocks can never be constant folded.
4190 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004191 }
4192 else
4193 {
4194 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004195 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004196 }
4197 }
4198 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004199 else
4200 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004201 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004202 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004203 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304204 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004205 }
4206 else
4207 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304208 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004209 " field selection requires structure, vector, or interface block on left hand "
4210 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304211 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004212 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004213 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004214 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004215}
4216
Jamie Madillb98c3a82015-07-23 14:26:04 -04004217TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4218 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004219{
Jamie Madill2f294c92017-11-20 14:47:26 -05004220 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004221
4222 if (qualifierType == "shared")
4223 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004224 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004225 {
4226 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4227 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004228 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004229 }
4230 else if (qualifierType == "packed")
4231 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004232 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004233 {
4234 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4235 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004236 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004237 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004238 else if (qualifierType == "std430")
4239 {
4240 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4241 qualifier.blockStorage = EbsStd430;
4242 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004243 else if (qualifierType == "std140")
4244 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004245 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004246 }
4247 else if (qualifierType == "row_major")
4248 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004249 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004250 }
4251 else if (qualifierType == "column_major")
4252 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004253 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004254 }
4255 else if (qualifierType == "location")
4256 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004257 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4258 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004259 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004260 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004261 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004262 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4263 {
4264 qualifier.yuv = true;
4265 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004266 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004267 else if (qualifierType == "rgba32f")
4268 {
4269 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4270 qualifier.imageInternalFormat = EiifRGBA32F;
4271 }
4272 else if (qualifierType == "rgba16f")
4273 {
4274 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4275 qualifier.imageInternalFormat = EiifRGBA16F;
4276 }
4277 else if (qualifierType == "r32f")
4278 {
4279 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4280 qualifier.imageInternalFormat = EiifR32F;
4281 }
4282 else if (qualifierType == "rgba8")
4283 {
4284 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4285 qualifier.imageInternalFormat = EiifRGBA8;
4286 }
4287 else if (qualifierType == "rgba8_snorm")
4288 {
4289 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4290 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4291 }
4292 else if (qualifierType == "rgba32i")
4293 {
4294 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4295 qualifier.imageInternalFormat = EiifRGBA32I;
4296 }
4297 else if (qualifierType == "rgba16i")
4298 {
4299 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4300 qualifier.imageInternalFormat = EiifRGBA16I;
4301 }
4302 else if (qualifierType == "rgba8i")
4303 {
4304 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4305 qualifier.imageInternalFormat = EiifRGBA8I;
4306 }
4307 else if (qualifierType == "r32i")
4308 {
4309 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4310 qualifier.imageInternalFormat = EiifR32I;
4311 }
4312 else if (qualifierType == "rgba32ui")
4313 {
4314 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4315 qualifier.imageInternalFormat = EiifRGBA32UI;
4316 }
4317 else if (qualifierType == "rgba16ui")
4318 {
4319 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4320 qualifier.imageInternalFormat = EiifRGBA16UI;
4321 }
4322 else if (qualifierType == "rgba8ui")
4323 {
4324 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4325 qualifier.imageInternalFormat = EiifRGBA8UI;
4326 }
4327 else if (qualifierType == "r32ui")
4328 {
4329 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4330 qualifier.imageInternalFormat = EiifR32UI;
4331 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004332 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4333 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004334 {
4335 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4336 qualifier.primitiveType = EptPoints;
4337 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004338 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4339 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004340 {
4341 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4342 qualifier.primitiveType = EptLines;
4343 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004344 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4345 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004346 {
4347 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4348 qualifier.primitiveType = EptLinesAdjacency;
4349 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004350 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4351 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004352 {
4353 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4354 qualifier.primitiveType = EptTriangles;
4355 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004356 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4357 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004358 {
4359 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4360 qualifier.primitiveType = EptTrianglesAdjacency;
4361 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004362 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4363 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004364 {
4365 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4366 qualifier.primitiveType = EptLineStrip;
4367 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004368 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4369 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004370 {
4371 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4372 qualifier.primitiveType = EptTriangleStrip;
4373 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004374
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004375 else
4376 {
4377 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004378 }
4379
Jamie Madilla5efff92013-06-06 11:56:47 -04004380 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004381}
4382
Martin Radev802abe02016-08-04 17:48:32 +03004383void TParseContext::parseLocalSize(const TString &qualifierType,
4384 const TSourceLoc &qualifierTypeLine,
4385 int intValue,
4386 const TSourceLoc &intValueLine,
4387 const std::string &intValueString,
4388 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004389 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004390{
Olli Etuaho856c4972016-08-08 11:38:39 +03004391 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004392 if (intValue < 1)
4393 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004394 std::stringstream reasonStream;
4395 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4396 std::string reason = reasonStream.str();
4397 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004398 }
4399 (*localSize)[index] = intValue;
4400}
4401
Olli Etuaho09b04a22016-12-15 13:30:26 +00004402void TParseContext::parseNumViews(int intValue,
4403 const TSourceLoc &intValueLine,
4404 const std::string &intValueString,
4405 int *numViews)
4406{
4407 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4408 // specification.
4409 if (intValue < 1)
4410 {
4411 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4412 }
4413 *numViews = intValue;
4414}
4415
Shaob5cc1192017-07-06 10:47:20 +08004416void TParseContext::parseInvocations(int intValue,
4417 const TSourceLoc &intValueLine,
4418 const std::string &intValueString,
4419 int *numInvocations)
4420{
4421 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4422 // it doesn't make sense to accept invocations <= 0.
4423 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4424 {
4425 error(intValueLine,
4426 "out of range: invocations must be in the range of [1, "
4427 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4428 intValueString.c_str());
4429 }
4430 else
4431 {
4432 *numInvocations = intValue;
4433 }
4434}
4435
4436void TParseContext::parseMaxVertices(int intValue,
4437 const TSourceLoc &intValueLine,
4438 const std::string &intValueString,
4439 int *maxVertices)
4440{
4441 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4442 // it doesn't make sense to accept max_vertices < 0.
4443 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4444 {
4445 error(
4446 intValueLine,
4447 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4448 intValueString.c_str());
4449 }
4450 else
4451 {
4452 *maxVertices = intValue;
4453 }
4454}
4455
Jamie Madillb98c3a82015-07-23 14:26:04 -04004456TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4457 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004458 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304459 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004460{
Jamie Madill2f294c92017-11-20 14:47:26 -05004461 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004462
Martin Radev802abe02016-08-04 17:48:32 +03004463 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004464
Martin Radev802abe02016-08-04 17:48:32 +03004465 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004466 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004467 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004468 if (intValue < 0)
4469 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004470 error(intValueLine, "out of range: location must be non-negative",
4471 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004472 }
4473 else
4474 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004475 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004476 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004477 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004478 }
Olli Etuaho43364892017-02-13 16:00:12 +00004479 else if (qualifierType == "binding")
4480 {
4481 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4482 if (intValue < 0)
4483 {
4484 error(intValueLine, "out of range: binding must be non-negative",
4485 intValueString.c_str());
4486 }
4487 else
4488 {
4489 qualifier.binding = intValue;
4490 }
4491 }
jchen104cdac9e2017-05-08 11:01:20 +08004492 else if (qualifierType == "offset")
4493 {
4494 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4495 if (intValue < 0)
4496 {
4497 error(intValueLine, "out of range: offset must be non-negative",
4498 intValueString.c_str());
4499 }
4500 else
4501 {
4502 qualifier.offset = intValue;
4503 }
4504 }
Martin Radev802abe02016-08-04 17:48:32 +03004505 else if (qualifierType == "local_size_x")
4506 {
4507 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4508 &qualifier.localSize);
4509 }
4510 else if (qualifierType == "local_size_y")
4511 {
4512 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4513 &qualifier.localSize);
4514 }
4515 else if (qualifierType == "local_size_z")
4516 {
4517 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4518 &qualifier.localSize);
4519 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004520 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004521 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004522 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4523 {
4524 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4525 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004526 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004527 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4528 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004529 {
4530 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4531 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004532 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4533 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004534 {
4535 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4536 }
4537
Martin Radev802abe02016-08-04 17:48:32 +03004538 else
4539 {
4540 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004541 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004542
Jamie Madilla5efff92013-06-06 11:56:47 -04004543 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004544}
4545
Olli Etuaho613b9592016-09-05 12:05:53 +03004546TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4547{
4548 return new TTypeQualifierBuilder(
4549 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4550 mShaderVersion);
4551}
4552
Olli Etuahocce89652017-06-19 16:04:09 +03004553TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4554 const TSourceLoc &loc)
4555{
4556 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4557 return new TStorageQualifierWrapper(qualifier, loc);
4558}
4559
4560TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4561{
4562 if (getShaderType() == GL_VERTEX_SHADER)
4563 {
4564 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4565 }
4566 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4567}
4568
4569TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4570{
4571 if (declaringFunction())
4572 {
4573 return new TStorageQualifierWrapper(EvqIn, loc);
4574 }
Shaob5cc1192017-07-06 10:47:20 +08004575
4576 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004577 {
Shaob5cc1192017-07-06 10:47:20 +08004578 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004579 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004580 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004581 {
4582 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4583 }
4584 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004585 }
Shaob5cc1192017-07-06 10:47:20 +08004586 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004587 {
Shaob5cc1192017-07-06 10:47:20 +08004588 if (mShaderVersion < 300)
4589 {
4590 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4591 }
4592 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004593 }
Shaob5cc1192017-07-06 10:47:20 +08004594 case GL_COMPUTE_SHADER:
4595 {
4596 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4597 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004598 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004599 {
4600 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4601 }
4602 default:
4603 {
4604 UNREACHABLE();
4605 return new TStorageQualifierWrapper(EvqLast, loc);
4606 }
Olli Etuahocce89652017-06-19 16:04:09 +03004607 }
Olli Etuahocce89652017-06-19 16:04:09 +03004608}
4609
4610TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4611{
4612 if (declaringFunction())
4613 {
4614 return new TStorageQualifierWrapper(EvqOut, loc);
4615 }
Shaob5cc1192017-07-06 10:47:20 +08004616 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004617 {
Shaob5cc1192017-07-06 10:47:20 +08004618 case GL_VERTEX_SHADER:
4619 {
4620 if (mShaderVersion < 300)
4621 {
4622 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4623 }
4624 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4625 }
4626 case GL_FRAGMENT_SHADER:
4627 {
4628 if (mShaderVersion < 300)
4629 {
4630 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4631 }
4632 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4633 }
4634 case GL_COMPUTE_SHADER:
4635 {
4636 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4637 return new TStorageQualifierWrapper(EvqLast, loc);
4638 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004639 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004640 {
4641 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4642 }
4643 default:
4644 {
4645 UNREACHABLE();
4646 return new TStorageQualifierWrapper(EvqLast, loc);
4647 }
Olli Etuahocce89652017-06-19 16:04:09 +03004648 }
Olli Etuahocce89652017-06-19 16:04:09 +03004649}
4650
4651TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4652{
4653 if (!declaringFunction())
4654 {
4655 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4656 }
4657 return new TStorageQualifierWrapper(EvqInOut, loc);
4658}
4659
Jamie Madillb98c3a82015-07-23 14:26:04 -04004660TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004661 TLayoutQualifier rightQualifier,
4662 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004663{
Martin Radevc28888b2016-07-22 15:27:42 +03004664 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004665 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004666}
4667
Olli Etuahod5f44c92017-11-29 17:15:40 +02004668TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004669{
4670 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004671 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004672}
4673
Olli Etuahod5f44c92017-11-29 17:15:40 +02004674TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4675 const TSourceLoc &loc,
4676 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004677{
4678 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004679 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004680}
4681
Olli Etuaho722bfb52017-10-26 17:00:11 +03004682void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4683 const TFieldList::const_iterator end,
4684 const TString &name,
4685 const TSourceLoc &location)
4686{
4687 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4688 {
4689 if ((*fieldIter)->name() == name)
4690 {
4691 error(location, "duplicate field name in structure", name.c_str());
4692 }
4693 }
4694}
4695
4696TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4697{
4698 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4699 ++fieldIter)
4700 {
4701 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4702 location);
4703 }
4704 return fields;
4705}
4706
Olli Etuaho4de340a2016-12-16 09:32:03 +00004707TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4708 const TFieldList *newlyAddedFields,
4709 const TSourceLoc &location)
4710{
4711 for (TField *field : *newlyAddedFields)
4712 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004713 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4714 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004715 processedFields->push_back(field);
4716 }
4717 return processedFields;
4718}
4719
Martin Radev70866b82016-07-22 15:27:42 +03004720TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4721 const TTypeQualifierBuilder &typeQualifierBuilder,
4722 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004723 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004724{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004725 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004726
Martin Radev70866b82016-07-22 15:27:42 +03004727 typeSpecifier->qualifier = typeQualifier.qualifier;
4728 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004729 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004730 typeSpecifier->invariant = typeQualifier.invariant;
4731 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304732 {
Martin Radev70866b82016-07-22 15:27:42 +03004733 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004734 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004735 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004736}
4737
Jamie Madillb98c3a82015-07-23 14:26:04 -04004738TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004739 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004740{
Martin Radev4a9cd802016-09-01 16:51:51 +03004741 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4742 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004743
Olli Etuahod5f44c92017-11-29 17:15:40 +02004744 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004745 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004746
Martin Radev4a9cd802016-09-01 16:51:51 +03004747 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004748
Olli Etuahod5f44c92017-11-29 17:15:40 +02004749 TFieldList *fieldList = new TFieldList();
4750
4751 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304752 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004753 TType *type = new TType(typeSpecifier);
4754 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304755 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004756 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004757 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004758 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004759 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004760
Olli Etuahod5f44c92017-11-29 17:15:40 +02004761 TField *field = new TField(type, declarator->name(), declarator->line());
4762 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4763 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004764 }
4765
Olli Etuahod5f44c92017-11-29 17:15:40 +02004766 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004767}
4768
Martin Radev4a9cd802016-09-01 16:51:51 +03004769TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4770 const TSourceLoc &nameLine,
4771 const TString *structName,
4772 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004773{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004774 SymbolType structSymbolType = SymbolType::UserDefined;
4775 if (structName == nullptr)
4776 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004777 structSymbolType = SymbolType::Empty;
4778 }
4779 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004780
Jamie Madill9b820842015-02-12 10:40:10 -05004781 // Store a bool in the struct if we're at global scope, to allow us to
4782 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004783 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004784
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004785 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004786 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004787 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004788 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304789 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004790 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004791 }
4792 }
4793
4794 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004795 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004796 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004797 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004798 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004799 switch (qualifier)
4800 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004801 case EvqGlobal:
4802 case EvqTemporary:
4803 break;
4804 default:
4805 error(field.line(), "invalid qualifier on struct member",
4806 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004807 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004808 }
Martin Radev70866b82016-07-22 15:27:42 +03004809 if (field.type()->isInvariant())
4810 {
4811 error(field.line(), "invalid qualifier on struct member", "invariant");
4812 }
jchen104cdac9e2017-05-08 11:01:20 +08004813 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4814 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004815 {
4816 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4817 }
4818
Olli Etuahoebee5b32017-11-23 12:56:32 +02004819 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4820 field.name().c_str(), field.type());
4821
Olli Etuaho43364892017-02-13 16:00:12 +00004822 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4823
4824 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004825
4826 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004827 }
4828
Martin Radev4a9cd802016-09-01 16:51:51 +03004829 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004830 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004831 exitStructDeclaration();
4832
Martin Radev4a9cd802016-09-01 16:51:51 +03004833 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004834}
4835
Jamie Madillb98c3a82015-07-23 14:26:04 -04004836TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004837 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004838 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004839{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004840 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004841 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004842 init->isVector())
4843 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004844 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4845 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004846 return nullptr;
4847 }
4848
Olli Etuaho923ecef2017-10-11 12:01:38 +03004849 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004850 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004851 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004852 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004853 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004854 }
4855
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004856 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4857 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004858 return node;
4859}
4860
4861TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4862{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004863 if (mSwitchNestingLevel == 0)
4864 {
4865 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004866 return nullptr;
4867 }
4868 if (condition == nullptr)
4869 {
4870 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004871 return nullptr;
4872 }
4873 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004874 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004875 {
4876 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004877 }
4878 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004879 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4880 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4881 // fold in case labels.
4882 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004883 {
4884 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004885 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004886 TIntermCase *node = new TIntermCase(condition);
4887 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004888 return node;
4889}
4890
4891TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4892{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004893 if (mSwitchNestingLevel == 0)
4894 {
4895 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004896 return nullptr;
4897 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004898 TIntermCase *node = new TIntermCase(nullptr);
4899 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004900 return node;
4901}
4902
Jamie Madillb98c3a82015-07-23 14:26:04 -04004903TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4904 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004905 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004906{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004907 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004908
4909 switch (op)
4910 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004911 case EOpLogicalNot:
4912 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4913 child->isVector())
4914 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004915 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004916 return nullptr;
4917 }
4918 break;
4919 case EOpBitwiseNot:
4920 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4921 child->isMatrix() || child->isArray())
4922 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004923 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004924 return nullptr;
4925 }
4926 break;
4927 case EOpPostIncrement:
4928 case EOpPreIncrement:
4929 case EOpPostDecrement:
4930 case EOpPreDecrement:
4931 case EOpNegative:
4932 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004933 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4934 child->getBasicType() == EbtBool || child->isArray() ||
4935 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004936 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004937 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004938 return nullptr;
4939 }
4940 // Operators for built-ins are already type checked against their prototype.
4941 default:
4942 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004943 }
4944
Jiajia Qinbc585152017-06-23 15:42:17 +08004945 if (child->getMemoryQualifier().writeonly)
4946 {
4947 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4948 return nullptr;
4949 }
4950
Olli Etuahof119a262016-08-19 15:54:22 +03004951 TIntermUnary *node = new TIntermUnary(op, child);
4952 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004953
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004954 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004955}
4956
Olli Etuaho09b22472015-02-11 11:47:26 +02004957TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4958{
Olli Etuahocce89652017-06-19 16:04:09 +03004959 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004960 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004961 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004962 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004963 return child;
4964 }
4965 return node;
4966}
4967
Jamie Madillb98c3a82015-07-23 14:26:04 -04004968TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4969 TIntermTyped *child,
4970 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004971{
Olli Etuaho856c4972016-08-08 11:38:39 +03004972 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004973 return addUnaryMath(op, child, loc);
4974}
4975
Jamie Madillb98c3a82015-07-23 14:26:04 -04004976bool TParseContext::binaryOpCommonCheck(TOperator op,
4977 TIntermTyped *left,
4978 TIntermTyped *right,
4979 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004980{
jchen10b4cf5652017-05-05 18:51:17 +08004981 // Check opaque types are not allowed to be operands in expressions other than array indexing
4982 // and structure member selection.
4983 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4984 {
4985 switch (op)
4986 {
4987 case EOpIndexDirect:
4988 case EOpIndexIndirect:
4989 break;
4990 case EOpIndexDirectStruct:
4991 UNREACHABLE();
4992
4993 default:
4994 error(loc, "Invalid operation for variables with an opaque type",
4995 GetOperatorString(op));
4996 return false;
4997 }
4998 }
jchen10cc2a10e2017-05-03 14:05:12 +08004999
Jiajia Qinbc585152017-06-23 15:42:17 +08005000 if (right->getMemoryQualifier().writeonly)
5001 {
5002 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5003 return false;
5004 }
5005
5006 if (left->getMemoryQualifier().writeonly)
5007 {
5008 switch (op)
5009 {
5010 case EOpAssign:
5011 case EOpInitialize:
5012 case EOpIndexDirect:
5013 case EOpIndexIndirect:
5014 case EOpIndexDirectStruct:
5015 case EOpIndexDirectInterfaceBlock:
5016 break;
5017 default:
5018 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5019 return false;
5020 }
5021 }
5022
Olli Etuaho244be012016-08-18 15:26:02 +03005023 if (left->getType().getStruct() || right->getType().getStruct())
5024 {
5025 switch (op)
5026 {
5027 case EOpIndexDirectStruct:
5028 ASSERT(left->getType().getStruct());
5029 break;
5030 case EOpEqual:
5031 case EOpNotEqual:
5032 case EOpAssign:
5033 case EOpInitialize:
5034 if (left->getType() != right->getType())
5035 {
5036 return false;
5037 }
5038 break;
5039 default:
5040 error(loc, "Invalid operation for structs", GetOperatorString(op));
5041 return false;
5042 }
5043 }
5044
Olli Etuaho94050052017-05-08 14:17:44 +03005045 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5046 {
5047 switch (op)
5048 {
5049 case EOpIndexDirectInterfaceBlock:
5050 ASSERT(left->getType().getInterfaceBlock());
5051 break;
5052 default:
5053 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5054 return false;
5055 }
5056 }
5057
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005058 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005059 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005060 error(loc, "array / non-array mismatch", GetOperatorString(op));
5061 return false;
5062 }
5063
5064 if (left->isArray())
5065 {
5066 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005067 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005068 {
5069 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5070 return false;
5071 }
5072
Olli Etuahoe79904c2015-03-18 16:56:42 +02005073 switch (op)
5074 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005075 case EOpEqual:
5076 case EOpNotEqual:
5077 case EOpAssign:
5078 case EOpInitialize:
5079 break;
5080 default:
5081 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5082 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005083 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005084 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005085 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005086 {
5087 error(loc, "array size mismatch", GetOperatorString(op));
5088 return false;
5089 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005090 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005091
5092 // Check ops which require integer / ivec parameters
5093 bool isBitShift = false;
5094 switch (op)
5095 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005096 case EOpBitShiftLeft:
5097 case EOpBitShiftRight:
5098 case EOpBitShiftLeftAssign:
5099 case EOpBitShiftRightAssign:
5100 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5101 // check that the basic type is an integer type.
5102 isBitShift = true;
5103 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5104 {
5105 return false;
5106 }
5107 break;
5108 case EOpBitwiseAnd:
5109 case EOpBitwiseXor:
5110 case EOpBitwiseOr:
5111 case EOpBitwiseAndAssign:
5112 case EOpBitwiseXorAssign:
5113 case EOpBitwiseOrAssign:
5114 // It is enough to check the type of only one operand, since later it
5115 // is checked that the operand types match.
5116 if (!IsInteger(left->getBasicType()))
5117 {
5118 return false;
5119 }
5120 break;
5121 default:
5122 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005123 }
5124
5125 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5126 // So the basic type should usually match.
5127 if (!isBitShift && left->getBasicType() != right->getBasicType())
5128 {
5129 return false;
5130 }
5131
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005132 // Check that:
5133 // 1. Type sizes match exactly on ops that require that.
5134 // 2. Restrictions for structs that contain arrays or samplers are respected.
5135 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005136 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005137 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005138 case EOpAssign:
5139 case EOpInitialize:
5140 case EOpEqual:
5141 case EOpNotEqual:
5142 // ESSL 1.00 sections 5.7, 5.8, 5.9
5143 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5144 {
5145 error(loc, "undefined operation for structs containing arrays",
5146 GetOperatorString(op));
5147 return false;
5148 }
5149 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5150 // we interpret the spec so that this extends to structs containing samplers,
5151 // similarly to ESSL 1.00 spec.
5152 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5153 left->getType().isStructureContainingSamplers())
5154 {
5155 error(loc, "undefined operation for structs containing samplers",
5156 GetOperatorString(op));
5157 return false;
5158 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005159
Olli Etuahoe1805592017-01-02 16:41:20 +00005160 if ((left->getNominalSize() != right->getNominalSize()) ||
5161 (left->getSecondarySize() != right->getSecondarySize()))
5162 {
5163 error(loc, "dimension mismatch", GetOperatorString(op));
5164 return false;
5165 }
5166 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005167 case EOpLessThan:
5168 case EOpGreaterThan:
5169 case EOpLessThanEqual:
5170 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005171 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005172 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005173 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005174 return false;
5175 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005176 break;
5177 case EOpAdd:
5178 case EOpSub:
5179 case EOpDiv:
5180 case EOpIMod:
5181 case EOpBitShiftLeft:
5182 case EOpBitShiftRight:
5183 case EOpBitwiseAnd:
5184 case EOpBitwiseXor:
5185 case EOpBitwiseOr:
5186 case EOpAddAssign:
5187 case EOpSubAssign:
5188 case EOpDivAssign:
5189 case EOpIModAssign:
5190 case EOpBitShiftLeftAssign:
5191 case EOpBitShiftRightAssign:
5192 case EOpBitwiseAndAssign:
5193 case EOpBitwiseXorAssign:
5194 case EOpBitwiseOrAssign:
5195 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5196 {
5197 return false;
5198 }
5199
5200 // Are the sizes compatible?
5201 if (left->getNominalSize() != right->getNominalSize() ||
5202 left->getSecondarySize() != right->getSecondarySize())
5203 {
5204 // If the nominal sizes of operands do not match:
5205 // One of them must be a scalar.
5206 if (!left->isScalar() && !right->isScalar())
5207 return false;
5208
5209 // In the case of compound assignment other than multiply-assign,
5210 // the right side needs to be a scalar. Otherwise a vector/matrix
5211 // would be assigned to a scalar. A scalar can't be shifted by a
5212 // vector either.
5213 if (!right->isScalar() &&
5214 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5215 return false;
5216 }
5217 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005218 default:
5219 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005220 }
5221
Olli Etuahod6b14282015-03-17 14:31:35 +02005222 return true;
5223}
5224
Olli Etuaho1dded802016-08-18 18:13:13 +03005225bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5226 const TType &left,
5227 const TType &right)
5228{
5229 switch (op)
5230 {
5231 case EOpMul:
5232 case EOpMulAssign:
5233 return left.getNominalSize() == right.getNominalSize() &&
5234 left.getSecondarySize() == right.getSecondarySize();
5235 case EOpVectorTimesScalar:
5236 return true;
5237 case EOpVectorTimesScalarAssign:
5238 ASSERT(!left.isMatrix() && !right.isMatrix());
5239 return left.isVector() && !right.isVector();
5240 case EOpVectorTimesMatrix:
5241 return left.getNominalSize() == right.getRows();
5242 case EOpVectorTimesMatrixAssign:
5243 ASSERT(!left.isMatrix() && right.isMatrix());
5244 return left.isVector() && left.getNominalSize() == right.getRows() &&
5245 left.getNominalSize() == right.getCols();
5246 case EOpMatrixTimesVector:
5247 return left.getCols() == right.getNominalSize();
5248 case EOpMatrixTimesScalar:
5249 return true;
5250 case EOpMatrixTimesScalarAssign:
5251 ASSERT(left.isMatrix() && !right.isMatrix());
5252 return !right.isVector();
5253 case EOpMatrixTimesMatrix:
5254 return left.getCols() == right.getRows();
5255 case EOpMatrixTimesMatrixAssign:
5256 ASSERT(left.isMatrix() && right.isMatrix());
5257 // We need to check two things:
5258 // 1. The matrix multiplication step is valid.
5259 // 2. The result will have the same number of columns as the lvalue.
5260 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5261
5262 default:
5263 UNREACHABLE();
5264 return false;
5265 }
5266}
5267
Jamie Madillb98c3a82015-07-23 14:26:04 -04005268TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5269 TIntermTyped *left,
5270 TIntermTyped *right,
5271 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005272{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005273 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005274 return nullptr;
5275
Olli Etuahofc1806e2015-03-17 13:03:11 +02005276 switch (op)
5277 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005278 case EOpEqual:
5279 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005280 case EOpLessThan:
5281 case EOpGreaterThan:
5282 case EOpLessThanEqual:
5283 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005284 break;
5285 case EOpLogicalOr:
5286 case EOpLogicalXor:
5287 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005288 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5289 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005290 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005291 {
5292 return nullptr;
5293 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005294 // Basic types matching should have been already checked.
5295 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005296 break;
5297 case EOpAdd:
5298 case EOpSub:
5299 case EOpDiv:
5300 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005301 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5302 !right->getType().getStruct());
5303 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005304 {
5305 return nullptr;
5306 }
5307 break;
5308 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005309 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5310 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005311 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005312 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005313 {
5314 return nullptr;
5315 }
5316 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005317 default:
5318 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005319 }
5320
Olli Etuaho1dded802016-08-18 18:13:13 +03005321 if (op == EOpMul)
5322 {
5323 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5324 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5325 {
5326 return nullptr;
5327 }
5328 }
5329
Olli Etuaho3fdec912016-08-18 15:08:06 +03005330 TIntermBinary *node = new TIntermBinary(op, left, right);
5331 node->setLine(loc);
5332
Olli Etuaho3fdec912016-08-18 15:08:06 +03005333 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005334 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005335}
5336
Jamie Madillb98c3a82015-07-23 14:26:04 -04005337TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5338 TIntermTyped *left,
5339 TIntermTyped *right,
5340 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005341{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005342 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005343 if (node == 0)
5344 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005345 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5346 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005347 return left;
5348 }
5349 return node;
5350}
5351
Jamie Madillb98c3a82015-07-23 14:26:04 -04005352TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5353 TIntermTyped *left,
5354 TIntermTyped *right,
5355 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005356{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005357 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005358 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005359 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005360 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5361 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005362 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005363 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005364 }
5365 return node;
5366}
5367
Olli Etuaho13389b62016-10-16 11:48:18 +01005368TIntermBinary *TParseContext::createAssign(TOperator op,
5369 TIntermTyped *left,
5370 TIntermTyped *right,
5371 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005372{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005373 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005374 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005375 if (op == EOpMulAssign)
5376 {
5377 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5378 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5379 {
5380 return nullptr;
5381 }
5382 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005383 TIntermBinary *node = new TIntermBinary(op, left, right);
5384 node->setLine(loc);
5385
Olli Etuaho3fdec912016-08-18 15:08:06 +03005386 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005387 }
5388 return nullptr;
5389}
5390
Jamie Madillb98c3a82015-07-23 14:26:04 -04005391TIntermTyped *TParseContext::addAssign(TOperator op,
5392 TIntermTyped *left,
5393 TIntermTyped *right,
5394 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005395{
Olli Etuahocce89652017-06-19 16:04:09 +03005396 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005397 TIntermTyped *node = createAssign(op, left, right, loc);
5398 if (node == nullptr)
5399 {
5400 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005401 return left;
5402 }
5403 return node;
5404}
5405
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005406TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5407 TIntermTyped *right,
5408 const TSourceLoc &loc)
5409{
Corentin Wallez0d959252016-07-12 17:26:32 -04005410 // WebGL2 section 5.26, the following results in an error:
5411 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005412 if (mShaderSpec == SH_WEBGL2_SPEC &&
5413 (left->isArray() || left->getBasicType() == EbtVoid ||
5414 left->getType().isStructureContainingArrays() || right->isArray() ||
5415 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005416 {
5417 error(loc,
5418 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5419 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005420 }
5421
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005422 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5423 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5424 commaNode->getTypePointer()->setQualifier(resultQualifier);
5425 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005426}
5427
Olli Etuaho49300862015-02-20 14:54:49 +02005428TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5429{
5430 switch (op)
5431 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005432 case EOpContinue:
5433 if (mLoopNestingLevel <= 0)
5434 {
5435 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005436 }
5437 break;
5438 case EOpBreak:
5439 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5440 {
5441 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005442 }
5443 break;
5444 case EOpReturn:
5445 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5446 {
5447 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005448 }
5449 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005450 case EOpKill:
5451 if (mShaderType != GL_FRAGMENT_SHADER)
5452 {
5453 error(loc, "discard supported in fragment shaders only", "discard");
5454 }
5455 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005456 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005457 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005458 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005459 }
Olli Etuahocce89652017-06-19 16:04:09 +03005460 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005461}
5462
Jamie Madillb98c3a82015-07-23 14:26:04 -04005463TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005464 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005465 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005466{
Olli Etuahocce89652017-06-19 16:04:09 +03005467 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005468 {
Olli Etuahocce89652017-06-19 16:04:09 +03005469 ASSERT(op == EOpReturn);
5470 mFunctionReturnsValue = true;
5471 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5472 {
5473 error(loc, "void function cannot return a value", "return");
5474 }
5475 else if (*mCurrentFunctionType != expression->getType())
5476 {
5477 error(loc, "function return is not matching type:", "return");
5478 }
Olli Etuaho49300862015-02-20 14:54:49 +02005479 }
Olli Etuahocce89652017-06-19 16:04:09 +03005480 TIntermBranch *node = new TIntermBranch(op, expression);
5481 node->setLine(loc);
5482 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005483}
5484
Martin Radev84aa2dc2017-09-11 15:51:02 +03005485void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5486{
5487 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho1bb85282017-12-14 13:39:53 +02005488 const TString &name = *functionCall->getFunction()->name();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005489 bool isTextureGather = (name == "textureGather");
5490 bool isTextureGatherOffset = (name == "textureGatherOffset");
5491 if (isTextureGather || isTextureGatherOffset)
5492 {
5493 TIntermNode *componentNode = nullptr;
5494 TIntermSequence *arguments = functionCall->getSequence();
5495 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5496 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5497 ASSERT(sampler != nullptr);
5498 switch (sampler->getBasicType())
5499 {
5500 case EbtSampler2D:
5501 case EbtISampler2D:
5502 case EbtUSampler2D:
5503 case EbtSampler2DArray:
5504 case EbtISampler2DArray:
5505 case EbtUSampler2DArray:
5506 if ((isTextureGather && arguments->size() == 3u) ||
5507 (isTextureGatherOffset && arguments->size() == 4u))
5508 {
5509 componentNode = arguments->back();
5510 }
5511 break;
5512 case EbtSamplerCube:
5513 case EbtISamplerCube:
5514 case EbtUSamplerCube:
5515 ASSERT(!isTextureGatherOffset);
5516 if (arguments->size() == 3u)
5517 {
5518 componentNode = arguments->back();
5519 }
5520 break;
5521 case EbtSampler2DShadow:
5522 case EbtSampler2DArrayShadow:
5523 case EbtSamplerCubeShadow:
5524 break;
5525 default:
5526 UNREACHABLE();
5527 break;
5528 }
5529 if (componentNode)
5530 {
5531 const TIntermConstantUnion *componentConstantUnion =
5532 componentNode->getAsConstantUnion();
5533 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5534 {
5535 error(functionCall->getLine(), "Texture component must be a constant expression",
5536 name.c_str());
5537 }
5538 else
5539 {
5540 int component = componentConstantUnion->getIConst(0);
5541 if (component < 0 || component > 3)
5542 {
5543 error(functionCall->getLine(), "Component must be in the range [0;3]",
5544 name.c_str());
5545 }
5546 }
5547 }
5548 }
5549}
5550
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005551void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5552{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005553 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho1bb85282017-12-14 13:39:53 +02005554 const TString &name = *functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005555 TIntermNode *offset = nullptr;
5556 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005557 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005558 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5559 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5560 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005561 {
5562 offset = arguments->back();
5563 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005564 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005565 {
5566 // A bias parameter might follow the offset parameter.
5567 ASSERT(arguments->size() >= 3);
5568 offset = (*arguments)[2];
5569 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005570 else if (name == "textureGatherOffset")
5571 {
5572 ASSERT(arguments->size() >= 3u);
5573 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5574 ASSERT(sampler != nullptr);
5575 switch (sampler->getBasicType())
5576 {
5577 case EbtSampler2D:
5578 case EbtISampler2D:
5579 case EbtUSampler2D:
5580 case EbtSampler2DArray:
5581 case EbtISampler2DArray:
5582 case EbtUSampler2DArray:
5583 offset = (*arguments)[2];
5584 break;
5585 case EbtSampler2DShadow:
5586 case EbtSampler2DArrayShadow:
5587 offset = (*arguments)[3];
5588 break;
5589 default:
5590 UNREACHABLE();
5591 break;
5592 }
5593 useTextureGatherOffsetConstraints = true;
5594 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005595 if (offset != nullptr)
5596 {
5597 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5598 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5599 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005600 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005601 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005602 }
5603 else
5604 {
5605 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5606 size_t size = offsetConstantUnion->getType().getObjectSize();
5607 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005608 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5609 : mMinProgramTexelOffset;
5610 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5611 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005612 for (size_t i = 0u; i < size; ++i)
5613 {
5614 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005615 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005616 {
5617 std::stringstream tokenStream;
5618 tokenStream << offsetValue;
5619 std::string token = tokenStream.str();
5620 error(offset->getLine(), "Texture offset value out of valid range",
5621 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005622 }
5623 }
5624 }
5625 }
5626}
5627
Jiajia Qina3106c52017-11-03 09:39:39 +08005628void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5629{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005630 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5631 const TString &name = *functionCall->getFunction()->name();
Jiajia Qina3106c52017-11-03 09:39:39 +08005632 if (IsAtomicBuiltin(name))
5633 {
5634 TIntermSequence *arguments = functionCall->getSequence();
5635 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5636
5637 if (IsBufferOrSharedVariable(memNode))
5638 {
5639 return;
5640 }
5641
5642 while (memNode->getAsBinaryNode())
5643 {
5644 memNode = memNode->getAsBinaryNode()->getLeft();
5645 if (IsBufferOrSharedVariable(memNode))
5646 {
5647 return;
5648 }
5649 }
5650
5651 error(memNode->getLine(),
5652 "The value passed to the mem argument of an atomic memory function does not "
5653 "correspond to a buffer or shared variable.",
Olli Etuaho1bb85282017-12-14 13:39:53 +02005654 functionCall->getFunction()->name()->c_str());
Jiajia Qina3106c52017-11-03 09:39:39 +08005655 }
5656}
5657
Martin Radev2cc85b32016-08-05 16:22:53 +03005658// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5659void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5660{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005661 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho1bb85282017-12-14 13:39:53 +02005662 const TString &name = *functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005663
5664 if (name.compare(0, 5, "image") == 0)
5665 {
5666 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005667 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005668
Olli Etuaho485eefd2017-02-14 17:40:06 +00005669 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005670
5671 if (name.compare(5, 5, "Store") == 0)
5672 {
5673 if (memoryQualifier.readonly)
5674 {
5675 error(imageNode->getLine(),
5676 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005677 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005678 }
5679 }
5680 else if (name.compare(5, 4, "Load") == 0)
5681 {
5682 if (memoryQualifier.writeonly)
5683 {
5684 error(imageNode->getLine(),
5685 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005686 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005687 }
5688 }
5689 }
5690}
5691
5692// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5693void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5694 const TFunction *functionDefinition,
5695 const TIntermAggregate *functionCall)
5696{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005697 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005698
5699 const TIntermSequence &arguments = *functionCall->getSequence();
5700
5701 ASSERT(functionDefinition->getParamCount() == arguments.size());
5702
5703 for (size_t i = 0; i < arguments.size(); ++i)
5704 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005705 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5706 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005707 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5708 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5709
5710 if (IsImage(functionArgumentType.getBasicType()))
5711 {
5712 const TMemoryQualifier &functionArgumentMemoryQualifier =
5713 functionArgumentType.getMemoryQualifier();
5714 const TMemoryQualifier &functionParameterMemoryQualifier =
5715 functionParameterType.getMemoryQualifier();
5716 if (functionArgumentMemoryQualifier.readonly &&
5717 !functionParameterMemoryQualifier.readonly)
5718 {
5719 error(functionCall->getLine(),
5720 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005721 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005722 }
5723
5724 if (functionArgumentMemoryQualifier.writeonly &&
5725 !functionParameterMemoryQualifier.writeonly)
5726 {
5727 error(functionCall->getLine(),
5728 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005729 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005730 }
Martin Radev049edfa2016-11-11 14:35:37 +02005731
5732 if (functionArgumentMemoryQualifier.coherent &&
5733 !functionParameterMemoryQualifier.coherent)
5734 {
5735 error(functionCall->getLine(),
5736 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005737 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005738 }
5739
5740 if (functionArgumentMemoryQualifier.volatileQualifier &&
5741 !functionParameterMemoryQualifier.volatileQualifier)
5742 {
5743 error(functionCall->getLine(),
5744 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005745 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005746 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005747 }
5748 }
5749}
5750
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005751TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005752{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005753 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005754}
5755
5756TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005757 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005758 TIntermNode *thisNode,
5759 const TSourceLoc &loc)
5760{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005761 if (thisNode != nullptr)
5762 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005763 return addMethod(fnCall->name(), arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005764 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005765
5766 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005767 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005768 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005769 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005770 }
5771 else
5772 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005773 ASSERT(op == EOpNull);
Olli Etuaho0c371002017-12-13 17:00:25 +04005774 return addNonConstructorFunctionCall(fnCall->name(), arguments, loc);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005775 }
5776}
5777
Olli Etuaho0c371002017-12-13 17:00:25 +04005778TIntermTyped *TParseContext::addMethod(const TString *name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005779 TIntermSequence *arguments,
5780 TIntermNode *thisNode,
5781 const TSourceLoc &loc)
5782{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005783 TIntermTyped *typedThis = thisNode->getAsTyped();
5784 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5785 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5786 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005787 // So accessing fnCall->name() below is safe.
Olli Etuaho0c371002017-12-13 17:00:25 +04005788 if (*name != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005789 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005790 error(loc, "invalid method", name->c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005791 }
5792 else if (!arguments->empty())
5793 {
5794 error(loc, "method takes no parameters", "length");
5795 }
5796 else if (typedThis == nullptr || !typedThis->isArray())
5797 {
5798 error(loc, "length can only be called on arrays", "length");
5799 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005800 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5801 mGeometryShaderInputPrimitiveType == EptUndefined)
5802 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005803 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005804 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5805 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005806 else
5807 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005808 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5809 node->setLine(loc);
5810 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005811 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005812 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005813}
5814
Olli Etuaho0c371002017-12-13 17:00:25 +04005815TIntermTyped *TParseContext::addNonConstructorFunctionCall(const TString *name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005816 TIntermSequence *arguments,
5817 const TSourceLoc &loc)
5818{
Olli Etuaho0c371002017-12-13 17:00:25 +04005819 ASSERT(name);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005820 // First find by unmangled name to check whether the function name has been
5821 // hidden by a variable name or struct typename.
5822 // If a function is found, check for one with a matching argument list.
5823 bool builtIn;
Olli Etuaho0c371002017-12-13 17:00:25 +04005824 const TSymbol *symbol = symbolTable.find(*name, mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005825 if (symbol != nullptr && !symbol->isFunction())
5826 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005827 error(loc, "function name expected", name->c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005828 }
5829 else
5830 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005831 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(*name, *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005832 mShaderVersion, &builtIn);
5833 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005834 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005835 error(loc, "no matching overloaded function found", name->c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005836 }
5837 else
5838 {
5839 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005840 //
5841 // A declared function.
5842 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005843 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005844 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005845 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005846 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005847 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005848 if (builtIn && op != EOpNull)
5849 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005850 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005851 if (fnCandidate->getParamCount() == 1)
5852 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005853 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005854 TIntermNode *unaryParamNode = arguments->front();
5855 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005856 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005857 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005858 }
5859 else
5860 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005861 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005862 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005863 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005864
5865 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005866 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305867
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005868 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305869 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005870 // See if we can constant fold a built-in. Note that this may be possible
5871 // even if it is not const-qualified.
5872 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305873 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005874 else
5875 {
5876 return callNode;
5877 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005878 }
5879 }
5880 else
5881 {
5882 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005883 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005884
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005885 // If builtIn == false, the function is user defined - could be an overloaded
5886 // built-in as well.
5887 // if builtIn == true, it's a builtIn function with no op associated with it.
5888 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005889 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005890 {
Olli Etuahofe486322017-03-21 09:30:54 +00005891 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005892 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005893 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005894 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005895 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005896 }
5897 else
5898 {
Olli Etuahofe486322017-03-21 09:30:54 +00005899 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005900 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005901 }
5902
Jiajia Qinbc585152017-06-23 15:42:17 +08005903 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005904
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005905 callNode->setLine(loc);
5906
5907 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005908 }
5909 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005910 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005911
5912 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005913 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005914}
5915
Jamie Madillb98c3a82015-07-23 14:26:04 -04005916TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005917 TIntermTyped *trueExpression,
5918 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005919 const TSourceLoc &loc)
5920{
Olli Etuaho56229f12017-07-10 14:16:33 +03005921 if (!checkIsScalarBool(loc, cond))
5922 {
5923 return falseExpression;
5924 }
Olli Etuaho52901742015-04-15 13:42:45 +03005925
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005926 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005927 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005928 std::stringstream reasonStream;
5929 reasonStream << "mismatching ternary operator operand types '"
5930 << trueExpression->getCompleteString() << " and '"
5931 << falseExpression->getCompleteString() << "'";
5932 std::string reason = reasonStream.str();
5933 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005934 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005935 }
Olli Etuahode318b22016-10-25 16:18:25 +01005936 if (IsOpaqueType(trueExpression->getBasicType()))
5937 {
5938 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005939 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005940 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5941 // Note that structs containing opaque types don't need to be checked as structs are
5942 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005943 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005944 return falseExpression;
5945 }
5946
Jiajia Qinbc585152017-06-23 15:42:17 +08005947 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5948 falseExpression->getMemoryQualifier().writeonly)
5949 {
5950 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5951 return falseExpression;
5952 }
5953
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005954 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005955 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005956 // ESSL 3.00.6 section 5.7:
5957 // Ternary operator support is optional for arrays. No certainty that it works across all
5958 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5959 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005960 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005961 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005962 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005963 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005964 }
Olli Etuaho94050052017-05-08 14:17:44 +03005965 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5966 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005967 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005968 return falseExpression;
5969 }
5970
Corentin Wallez0d959252016-07-12 17:26:32 -04005971 // WebGL2 section 5.26, the following results in an error:
5972 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005973 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005974 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005975 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005976 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005977 }
5978
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005979 // Note that the node resulting from here can be a constant union without being qualified as
5980 // constant.
5981 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5982 node->setLine(loc);
5983
5984 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005985}
Olli Etuaho49300862015-02-20 14:54:49 +02005986
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005987//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005988// Parse an array of strings using yyparse.
5989//
5990// Returns 0 for success.
5991//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005992int PaParseStrings(size_t count,
5993 const char *const string[],
5994 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305995 TParseContext *context)
5996{
Yunchao He4f285442017-04-21 12:15:49 +08005997 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005998 return 1;
5999
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006000 if (glslang_initialize(context))
6001 return 1;
6002
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006003 int error = glslang_scan(count, string, length, context);
6004 if (!error)
6005 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006006
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006007 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006008
alokp@chromium.org6b495712012-06-29 00:06:58 +00006009 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006010}
Jamie Madill45bcc782016-11-07 13:58:48 -05006011
6012} // namespace sh