blob: 7149cde4799dc1a94a7b5205812da69704d50b76 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
jchen104cdac9e2017-05-08 11:01:20 +080012#include "common/mathutil.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
Olli Etuahod5f44c92017-11-29 17:15:40 +020014#include "compiler/translator/Declarator.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Kai Ninomiya614dd0f2017-11-22 14:04:48 -080016#include "compiler/translator/StaticType.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030017#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080018#include "compiler/translator/ValidateSwitch.h"
19#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030020#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021
Jamie Madill45bcc782016-11-07 13:58:48 -050022namespace sh
23{
24
alokp@chromium.org8b851c62012-06-15 16:25:11 +000025///////////////////////////////////////////////////////////////////////
26//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027// Sub- vector and matrix fields
28//
29////////////////////////////////////////////////////////////////////////
30
Martin Radev2cc85b32016-08-05 16:22:53 +030031namespace
32{
33
34const int kWebGLMaxStructNesting = 4;
35
Jiajia Qina3106c52017-11-03 09:39:39 +080036const std::array<const char *, 8> kAtomicBuiltin = {{"atomicAdd", "atomicMin", "atomicMax",
37 "atomicAnd", "atomicOr", "atomicXor",
38 "atomicExchange", "atomicCompSwap"}};
39
40bool IsAtomicBuiltin(const TString &name)
41{
42 for (size_t i = 0; i < kAtomicBuiltin.size(); ++i)
43 {
44 if (name.compare(kAtomicBuiltin[i]) == 0)
45 {
46 return true;
47 }
48 }
49 return false;
50}
51
Olli Etuaho0f684632017-07-13 12:42:15 +030052bool ContainsSampler(const TStructure *structType);
53
Martin Radev2cc85b32016-08-05 16:22:53 +030054bool ContainsSampler(const TType &type)
55{
56 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030057 {
Martin Radev2cc85b32016-08-05 16:22:53 +030058 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030059 }
jchen10cc2a10e2017-05-03 14:05:12 +080060 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030061 {
Olli Etuaho0f684632017-07-13 12:42:15 +030062 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030063 }
64
65 return false;
66}
67
Olli Etuaho0f684632017-07-13 12:42:15 +030068bool ContainsSampler(const TStructure *structType)
69{
70 for (const auto &field : structType->fields())
71 {
72 if (ContainsSampler(*field->type()))
73 return true;
74 }
75 return false;
76}
77
Olli Etuaho485eefd2017-02-14 17:40:06 +000078// Get a token from an image argument to use as an error message token.
79const char *GetImageArgumentToken(TIntermTyped *imageNode)
80{
81 ASSERT(IsImage(imageNode->getBasicType()));
82 while (imageNode->getAsBinaryNode() &&
83 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
84 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
85 {
86 imageNode = imageNode->getAsBinaryNode()->getLeft();
87 }
88 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
89 if (imageSymbol)
90 {
91 return imageSymbol->getSymbol().c_str();
92 }
93 return "image";
94}
95
Olli Etuahocce89652017-06-19 16:04:09 +030096bool CanSetDefaultPrecisionOnType(const TPublicType &type)
97{
98 if (!SupportsPrecision(type.getBasicType()))
99 {
100 return false;
101 }
102 if (type.getBasicType() == EbtUInt)
103 {
104 // ESSL 3.00.4 section 4.5.4
105 return false;
106 }
107 if (type.isAggregate())
108 {
109 // Not allowed to set for aggregate types
110 return false;
111 }
112 return true;
113}
114
Jiawei Shaod8105a02017-08-08 09:54:36 +0800115// Map input primitive types to input array sizes in a geometry shader.
116GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
117{
118 switch (primitiveType)
119 {
120 case EptPoints:
121 return 1u;
122 case EptLines:
123 return 2u;
124 case EptTriangles:
125 return 3u;
126 case EptLinesAdjacency:
127 return 4u;
128 case EptTrianglesAdjacency:
129 return 6u;
130 default:
131 UNREACHABLE();
132 return 0u;
133 }
134}
135
Jiajia Qina3106c52017-11-03 09:39:39 +0800136bool IsBufferOrSharedVariable(TIntermTyped *var)
137{
138 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
139 var->getQualifier() == EvqShared)
140 {
141 return true;
142 }
143 return false;
144}
145
Martin Radev2cc85b32016-08-05 16:22:53 +0300146} // namespace
147
jchen104cdac9e2017-05-08 11:01:20 +0800148// This tracks each binding point's current default offset for inheritance of subsequent
149// variables using the same binding, and keeps offsets unique and non overlapping.
150// See GLSL ES 3.1, section 4.4.6.
151class TParseContext::AtomicCounterBindingState
152{
153 public:
154 AtomicCounterBindingState() : mDefaultOffset(0) {}
155 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
156 // newly inserted span.
157 int insertSpan(int start, size_t length)
158 {
159 gl::RangeI newSpan(start, start + static_cast<int>(length));
160 for (const auto &span : mSpans)
161 {
162 if (newSpan.intersects(span))
163 {
164 return -1;
165 }
166 }
167 mSpans.push_back(newSpan);
168 mDefaultOffset = newSpan.high();
169 return start;
170 }
171 // Inserts a new span starting from the default offset.
172 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
173 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
174
175 private:
176 int mDefaultOffset;
177 std::vector<gl::RangeI> mSpans;
178};
179
Jamie Madillacb4b812016-11-07 13:50:29 -0500180TParseContext::TParseContext(TSymbolTable &symt,
181 TExtensionBehavior &ext,
182 sh::GLenum type,
183 ShShaderSpec spec,
184 ShCompileOptions options,
185 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000186 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500187 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300188 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300189 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500190 mShaderType(type),
191 mShaderSpec(spec),
192 mCompileOptions(options),
193 mShaderVersion(100),
194 mTreeRoot(nullptr),
195 mLoopNestingLevel(0),
196 mStructNestingLevel(0),
197 mSwitchNestingLevel(0),
198 mCurrentFunctionType(nullptr),
199 mFunctionReturnsValue(false),
200 mChecksPrecisionErrors(checksPrecErrors),
201 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800202 mDefaultUniformMatrixPacking(EmpColumnMajor),
203 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
204 mDefaultBufferMatrixPacking(EmpColumnMajor),
205 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000206 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500207 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000208 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500209 mShaderVersion,
210 mShaderType,
211 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000212 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500213 mScanner(nullptr),
214 mUsesFragData(false),
215 mUsesFragColor(false),
216 mUsesSecondaryOutputs(false),
217 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
218 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300219 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
220 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500221 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500222 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000223 mNumViews(-1),
224 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000225 mMaxImageUnits(resources.MaxImageUnits),
226 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000227 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800228 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800229 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800230 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800231 mDeclaringFunction(false),
232 mGeometryShaderInputPrimitiveType(EptUndefined),
233 mGeometryShaderOutputPrimitiveType(EptUndefined),
234 mGeometryShaderInvocations(0),
235 mGeometryShaderMaxVertices(-1),
236 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800237 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800238 mGeometryShaderInputArraySize(0u)
Jamie Madillacb4b812016-11-07 13:50:29 -0500239{
Jamie Madillacb4b812016-11-07 13:50:29 -0500240}
241
jchen104cdac9e2017-05-08 11:01:20 +0800242TParseContext::~TParseContext()
243{
244}
245
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300246bool TParseContext::parseVectorFields(const TSourceLoc &line,
247 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400248 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300249 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300251 ASSERT(fieldOffsets);
252 size_t fieldCount = compString.size();
253 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530254 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000255 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000256 return false;
257 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300258 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259
Jamie Madillb98c3a82015-07-23 14:26:04 -0400260 enum
261 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000262 exyzw,
263 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000264 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000265 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300267 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530268 {
269 switch (compString[i])
270 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400271 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300272 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400273 fieldSet[i] = exyzw;
274 break;
275 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300276 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 fieldSet[i] = ergba;
278 break;
279 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300280 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400281 fieldSet[i] = estpq;
282 break;
283 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300284 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400285 fieldSet[i] = exyzw;
286 break;
287 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300288 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400289 fieldSet[i] = ergba;
290 break;
291 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300292 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400293 fieldSet[i] = estpq;
294 break;
295 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300296 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400297 fieldSet[i] = exyzw;
298 break;
299 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300300 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400301 fieldSet[i] = ergba;
302 break;
303 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300304 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400305 fieldSet[i] = estpq;
306 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307
Jamie Madillb98c3a82015-07-23 14:26:04 -0400308 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300309 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400310 fieldSet[i] = exyzw;
311 break;
312 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300313 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400314 fieldSet[i] = ergba;
315 break;
316 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300317 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400318 fieldSet[i] = estpq;
319 break;
320 default:
321 error(line, "illegal vector field selection", compString.c_str());
322 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 }
324 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300326 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530327 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300328 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530329 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400330 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000331 return false;
332 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
Arun Patole7e7e68d2015-05-22 12:02:25 +0530334 if (i > 0)
335 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400336 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530337 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400338 error(line, "illegal - vector component fields not from the same set",
339 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000340 return false;
341 }
342 }
343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000345 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346}
347
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348///////////////////////////////////////////////////////////////////////
349//
350// Errors
351//
352////////////////////////////////////////////////////////////////////////
353
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354//
355// Used by flex/bison to output all syntax and parsing errors.
356//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000357void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000359 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
Olli Etuaho4de340a2016-12-16 09:32:03 +0000362void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530363{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000364 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000365}
366
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200367void TParseContext::outOfRangeError(bool isError,
368 const TSourceLoc &loc,
369 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000370 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200371{
372 if (isError)
373 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000374 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200375 }
376 else
377 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000378 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200379 }
380}
381
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382//
383// Same error message for all places assignments don't work.
384//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530385void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000387 std::stringstream reasonStream;
388 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
389 std::string reason = reasonStream.str();
390 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391}
392
393//
394// Same error message for all places unary operations don't work.
395//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530396void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000398 std::stringstream reasonStream;
399 reasonStream << "wrong operand type - no operation '" << op
400 << "' exists that takes an operand of type " << operand
401 << " (or there is no acceptable conversion)";
402 std::string reason = reasonStream.str();
403 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Same error message for all binary operations don't work.
408//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400409void TParseContext::binaryOpError(const TSourceLoc &line,
410 const char *op,
411 TString left,
412 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000414 std::stringstream reasonStream;
415 reasonStream << "wrong operand types - no operation '" << op
416 << "' exists that takes a left-hand operand of type '" << left
417 << "' and a right operand of type '" << right
418 << "' (or there is no acceptable conversion)";
419 std::string reason = reasonStream.str();
420 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
Olli Etuaho856c4972016-08-08 11:38:39 +0300423void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
424 TPrecision precision,
425 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530426{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400427 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300428 return;
Martin Radev70866b82016-07-22 15:27:42 +0300429
430 if (precision != EbpUndefined && !SupportsPrecision(type))
431 {
432 error(line, "illegal type for precision qualifier", getBasicString(type));
433 }
434
Olli Etuaho183d7e22015-11-20 15:59:09 +0200435 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530436 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200437 switch (type)
438 {
439 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400440 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300441 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200442 case EbtInt:
443 case EbtUInt:
444 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400445 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300446 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200447 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800448 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200449 {
jchen10cc2a10e2017-05-03 14:05:12 +0800450 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300451 return;
452 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200453 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000454 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000455}
456
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457// Both test and if necessary, spit out an error, to see if the node is really
458// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300459bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500461 TIntermSymbol *symNode = node->getAsSymbolNode();
462 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100463 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
464
465 if (swizzleNode)
466 {
467 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
468 if (ok && swizzleNode->hasDuplicateOffsets())
469 {
470 error(line, " l-value of swizzle cannot have duplicate components", op);
471 return false;
472 }
473 return ok;
474 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475
Arun Patole7e7e68d2015-05-22 12:02:25 +0530476 if (binaryNode)
477 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530479 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400480 case EOpIndexDirect:
481 case EOpIndexIndirect:
482 case EOpIndexDirectStruct:
483 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300484 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400485 default:
486 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000487 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000488 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300489 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000490 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491
jchen10cc2a10e2017-05-03 14:05:12 +0800492 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530493 switch (node->getQualifier())
494 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400495 case EvqConst:
496 message = "can't modify a const";
497 break;
498 case EvqConstReadOnly:
499 message = "can't modify a const";
500 break;
501 case EvqAttribute:
502 message = "can't modify an attribute";
503 break;
504 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400505 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800506 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800507 case EvqFlatIn:
508 case EvqSmoothIn:
509 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400510 message = "can't modify an input";
511 break;
512 case EvqUniform:
513 message = "can't modify a uniform";
514 break;
515 case EvqVaryingIn:
516 message = "can't modify a varying";
517 break;
518 case EvqFragCoord:
519 message = "can't modify gl_FragCoord";
520 break;
521 case EvqFrontFacing:
522 message = "can't modify gl_FrontFacing";
523 break;
524 case EvqPointCoord:
525 message = "can't modify gl_PointCoord";
526 break;
Martin Radevb0883602016-08-04 17:48:58 +0300527 case EvqNumWorkGroups:
528 message = "can't modify gl_NumWorkGroups";
529 break;
530 case EvqWorkGroupSize:
531 message = "can't modify gl_WorkGroupSize";
532 break;
533 case EvqWorkGroupID:
534 message = "can't modify gl_WorkGroupID";
535 break;
536 case EvqLocalInvocationID:
537 message = "can't modify gl_LocalInvocationID";
538 break;
539 case EvqGlobalInvocationID:
540 message = "can't modify gl_GlobalInvocationID";
541 break;
542 case EvqLocalInvocationIndex:
543 message = "can't modify gl_LocalInvocationIndex";
544 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300545 case EvqViewIDOVR:
546 message = "can't modify gl_ViewID_OVR";
547 break;
Martin Radev802abe02016-08-04 17:48:32 +0300548 case EvqComputeIn:
549 message = "can't modify work group size variable";
550 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800551 case EvqPerVertexIn:
552 message = "can't modify any member in gl_in";
553 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800554 case EvqPrimitiveIDIn:
555 message = "can't modify gl_PrimitiveIDIn";
556 break;
557 case EvqInvocationID:
558 message = "can't modify gl_InvocationID";
559 break;
560 case EvqPrimitiveID:
561 if (mShaderType == GL_FRAGMENT_SHADER)
562 {
563 message = "can't modify gl_PrimitiveID in a fragment shader";
564 }
565 break;
566 case EvqLayer:
567 if (mShaderType == GL_FRAGMENT_SHADER)
568 {
569 message = "can't modify gl_Layer in a fragment shader";
570 }
571 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400572 default:
573 //
574 // Type that can't be written to?
575 //
576 if (node->getBasicType() == EbtVoid)
577 {
578 message = "can't modify void";
579 }
jchen10cc2a10e2017-05-03 14:05:12 +0800580 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400581 {
jchen10cc2a10e2017-05-03 14:05:12 +0800582 message = "can't modify a variable with type ";
583 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300584 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800585 else if (node->getMemoryQualifier().readonly)
586 {
587 message = "can't modify a readonly variable";
588 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590
jchen10cc2a10e2017-05-03 14:05:12 +0800591 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530592 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000593 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594
Olli Etuaho8a176262016-08-16 14:23:01 +0300595 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 //
599 // Everything else is okay, no error.
600 //
jchen10cc2a10e2017-05-03 14:05:12 +0800601 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300602 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 //
605 // If we get here, we have an error and a message.
606 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530607 if (symNode)
608 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000609 const char *symbol = symNode->getSymbol().c_str();
610 std::stringstream reasonStream;
611 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
612 std::string reason = reasonStream.str();
613 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000614 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 else
616 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000617 std::stringstream reasonStream;
618 reasonStream << "l-value required (" << message << ")";
619 std::string reason = reasonStream.str();
620 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000621 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622
Olli Etuaho8a176262016-08-16 14:23:01 +0300623 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624}
625
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626// Both test, and if necessary spit out an error, to see if the node is really
627// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300628void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629{
Olli Etuaho383b7912016-08-05 11:22:59 +0300630 if (node->getQualifier() != EvqConst)
631 {
632 error(node->getLine(), "constant expression required", "");
633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634}
635
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636// Both test, and if necessary spit out an error, to see if the node is really
637// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300638void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639{
Olli Etuaho383b7912016-08-05 11:22:59 +0300640 if (!node->isScalarInt())
641 {
642 error(node->getLine(), "integer expression required", token);
643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644}
645
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646// Both test, and if necessary spit out an error, to see if we are currently
647// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800648bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649{
Olli Etuaho856c4972016-08-08 11:38:39 +0300650 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300651 {
652 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800653 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300654 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800655 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000656}
657
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300658// ESSL 3.00.5 sections 3.8 and 3.9.
659// If it starts "gl_" or contains two consecutive underscores, it's reserved.
660// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300661bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300664 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530665 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300666 error(line, reservedErrMsg, "gl_");
667 return false;
668 }
669 if (sh::IsWebGLBasedSpec(mShaderSpec))
670 {
671 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300673 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300674 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300676 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530677 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300678 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300679 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 }
681 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300682 if (identifier.find("__") != TString::npos)
683 {
684 error(line,
685 "identifiers containing two consecutive underscores (__) are reserved as "
686 "possible future keywords",
687 identifier.c_str());
688 return false;
689 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300690 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691}
692
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300693// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300694bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800695 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300696 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000697{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800698 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530699 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200700 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300701 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200703
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300704 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530705 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300706 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200707 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300708 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200709 {
jchen10cc2a10e2017-05-03 14:05:12 +0800710 std::string reason("cannot convert a variable with type ");
711 reason += getBasicString(argTyped->getBasicType());
712 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300713 return false;
714 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800715 else if (argTyped->getMemoryQualifier().writeonly)
716 {
717 error(line, "cannot convert a variable with writeonly", "constructor");
718 return false;
719 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200720 if (argTyped->getBasicType() == EbtVoid)
721 {
722 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300723 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200724 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000725 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726
Olli Etuaho856c4972016-08-08 11:38:39 +0300727 if (type.isArray())
728 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300729 // The size of an unsized constructor should already have been determined.
730 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300731 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300732 {
733 error(line, "array constructor needs one argument per array element", "constructor");
734 return false;
735 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300736 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
737 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800738 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300739 {
740 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300741 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500742 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300743 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500744 return false;
745 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300746 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300747 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000748 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300749 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300750 }
751 }
752 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300753 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300754 {
755 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300756 if (fields.size() != arguments->size())
757 {
758 error(line,
759 "Number of constructor parameters does not match the number of structure fields",
760 "constructor");
761 return false;
762 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300763
764 for (size_t i = 0; i < fields.size(); i++)
765 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800766 if (i >= arguments->size() ||
767 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300768 {
769 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000770 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300771 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300772 }
773 }
774 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300775 else
776 {
777 // We're constructing a scalar, vector, or matrix.
778
779 // Note: It's okay to have too many components available, but not okay to have unused
780 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
781 // there is an extra argument, so 'overFull' will become true.
782
783 size_t size = 0;
784 bool full = false;
785 bool overFull = false;
786 bool matrixArg = false;
787 for (TIntermNode *arg : *arguments)
788 {
789 const TIntermTyped *argTyped = arg->getAsTyped();
790 ASSERT(argTyped != nullptr);
791
Olli Etuaho487b63a2017-05-23 15:55:09 +0300792 if (argTyped->getBasicType() == EbtStruct)
793 {
794 error(line, "a struct cannot be used as a constructor argument for this type",
795 "constructor");
796 return false;
797 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300798 if (argTyped->getType().isArray())
799 {
800 error(line, "constructing from a non-dereferenced array", "constructor");
801 return false;
802 }
803 if (argTyped->getType().isMatrix())
804 {
805 matrixArg = true;
806 }
807
808 size += argTyped->getType().getObjectSize();
809 if (full)
810 {
811 overFull = true;
812 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300813 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300814 {
815 full = true;
816 }
817 }
818
819 if (type.isMatrix() && matrixArg)
820 {
821 if (arguments->size() != 1)
822 {
823 error(line, "constructing matrix from matrix can only take one argument",
824 "constructor");
825 return false;
826 }
827 }
828 else
829 {
830 if (size != 1 && size < type.getObjectSize())
831 {
832 error(line, "not enough data provided for construction", "constructor");
833 return false;
834 }
835 if (overFull)
836 {
837 error(line, "too many arguments", "constructor");
838 return false;
839 }
840 }
841 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300842
Olli Etuaho8a176262016-08-16 14:23:01 +0300843 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844}
845
Jamie Madillb98c3a82015-07-23 14:26:04 -0400846// This function checks to see if a void variable has been declared and raise an error message for
847// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848//
849// returns true in case of an error
850//
Olli Etuaho856c4972016-08-08 11:38:39 +0300851bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400852 const TString &identifier,
853 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300855 if (type == EbtVoid)
856 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000857 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300858 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300859 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860
Olli Etuaho8a176262016-08-16 14:23:01 +0300861 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
Jamie Madillb98c3a82015-07-23 14:26:04 -0400864// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300865// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300866bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300868 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530869 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000870 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300871 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530872 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300873 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874}
875
Jamie Madillb98c3a82015-07-23 14:26:04 -0400876// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300877// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300878void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879{
Martin Radev4a9cd802016-09-01 16:51:51 +0300880 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530881 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000882 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530883 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884}
885
jchen10cc2a10e2017-05-03 14:05:12 +0800886bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
887 const TTypeSpecifierNonArray &pType,
888 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530890 if (pType.type == EbtStruct)
891 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300892 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530893 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000894 std::stringstream reasonStream;
895 reasonStream << reason << " (structure contains a sampler)";
896 std::string reasonStr = reasonStream.str();
897 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300898 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000899 }
jchen10cc2a10e2017-05-03 14:05:12 +0800900 // only samplers need to be checked from structs, since other opaque types can't be struct
901 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300902 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530903 }
jchen10cc2a10e2017-05-03 14:05:12 +0800904 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530905 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000906 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300907 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000908 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909
Olli Etuaho8a176262016-08-16 14:23:01 +0300910 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911}
912
Olli Etuaho856c4972016-08-08 11:38:39 +0300913void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
914 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400915{
916 if (pType.layoutQualifier.location != -1)
917 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400918 error(line, "location must only be specified for a single input or output variable",
919 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400920 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400921}
922
Olli Etuaho856c4972016-08-08 11:38:39 +0300923void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
924 const TLayoutQualifier &layoutQualifier)
925{
926 if (layoutQualifier.location != -1)
927 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000928 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
929 if (mShaderVersion >= 310)
930 {
931 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800932 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000933 }
934 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300935 }
936}
937
Qin Jiajiaca68d982017-09-18 16:41:56 +0800938void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
939 const TLayoutBlockStorage &blockStorage,
940 const TQualifier &qualifier)
941{
942 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
943 {
944 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
945 }
946}
947
Martin Radev2cc85b32016-08-05 16:22:53 +0300948void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
949 TQualifier qualifier,
950 const TType &type)
951{
Martin Radev2cc85b32016-08-05 16:22:53 +0300952 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800953 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530954 {
jchen10cc2a10e2017-05-03 14:05:12 +0800955 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000956 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957}
958
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300960unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530962 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000963
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200964 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
965 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
966 // fold as array size.
967 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000968 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000969 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300970 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000971 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
Olli Etuaho856c4972016-08-08 11:38:39 +0300973 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400974
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000975 if (constant->getBasicType() == EbtUInt)
976 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300977 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000978 }
979 else
980 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300981 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000982
Olli Etuaho856c4972016-08-08 11:38:39 +0300983 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000984 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400985 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300986 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000987 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400988
Olli Etuaho856c4972016-08-08 11:38:39 +0300989 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400990 }
991
Olli Etuaho856c4972016-08-08 11:38:39 +0300992 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400993 {
994 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300995 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400996 }
997
998 // The size of arrays is restricted here to prevent issues further down the
999 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1000 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1001 const unsigned int sizeLimit = 65536;
1002
Olli Etuaho856c4972016-08-08 11:38:39 +03001003 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001004 {
1005 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001006 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001007 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001008
1009 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010}
1011
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001013bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1014 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015{
Olli Etuaho8a176262016-08-16 14:23:01 +03001016 if ((elementQualifier.qualifier == EvqAttribute) ||
1017 (elementQualifier.qualifier == EvqVertexIn) ||
1018 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001020 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001021 TType(elementQualifier).getQualifierString());
1022 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024
Olli Etuaho8a176262016-08-16 14:23:01 +03001025 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026}
1027
Olli Etuaho8a176262016-08-16 14:23:01 +03001028// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001029bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1030 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001032 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001033 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001034 error(line, "cannot declare arrays of arrays",
1035 TType(elementType).getCompleteString().c_str());
1036 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001037 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001038 return true;
1039}
1040
1041// Check if this qualified element type can be formed into an array. This is only called when array
1042// brackets are associated with an identifier in a declaration, like this:
1043// float a[2];
1044// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1045// are associated with the type, like this:
1046// float[2] a;
1047bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1048 const TPublicType &elementType)
1049{
1050 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1051 {
1052 return false;
1053 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001054 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1055 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1056 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001057 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001058 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001059 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001060 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001061 TType(elementType).getCompleteString().c_str());
1062 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001063 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001064 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065}
1066
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001068void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1069 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001070 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071{
Olli Etuaho3739d232015-04-08 12:23:44 +03001072 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001073 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001074 {
1075 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001076 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001077
1078 // Generate informative error messages for ESSL1.
1079 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001080 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001081 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301082 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001083 "structures containing arrays may not be declared constant since they cannot be "
1084 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301085 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001086 }
1087 else
1088 {
1089 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1090 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001092 // This will make the type sized if it isn't sized yet.
1093 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1094 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095}
1096
Olli Etuaho2935c582015-04-08 14:32:06 +03001097// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098// and update the symbol table.
1099//
Olli Etuaho2935c582015-04-08 14:32:06 +03001100// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001102bool TParseContext::declareVariable(const TSourceLoc &line,
1103 const TString &identifier,
1104 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001105 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106{
Olli Etuaho2935c582015-04-08 14:32:06 +03001107 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108
Olli Etuaho43364892017-02-13 16:00:12 +00001109 checkBindingIsValid(line, type);
1110
Olli Etuaho856c4972016-08-08 11:38:39 +03001111 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112
Olli Etuaho2935c582015-04-08 14:32:06 +03001113 // gl_LastFragData may be redeclared with a new precision qualifier
1114 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1115 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001116 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1117 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001118 if (type.isArrayOfArrays())
1119 {
1120 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1121 identifier.c_str());
1122 return false;
1123 }
1124 else if (static_cast<int>(type.getOutermostArraySize()) ==
1125 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001126 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001127 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001128 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001129 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001130 }
1131 }
1132 else
1133 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001134 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1135 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001136 return false;
1137 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001138 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001139
Olli Etuaho8a176262016-08-16 14:23:01 +03001140 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001141 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001142
Olli Etuaho0f684632017-07-13 12:42:15 +03001143 (*variable) = symbolTable.declareVariable(&identifier, type);
1144 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001145 {
1146 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001147 return false;
1148 }
1149
Olli Etuaho8a176262016-08-16 14:23:01 +03001150 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001151 return false;
1152
1153 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001154}
1155
Martin Radev70866b82016-07-22 15:27:42 +03001156void TParseContext::checkIsParameterQualifierValid(
1157 const TSourceLoc &line,
1158 const TTypeQualifierBuilder &typeQualifierBuilder,
1159 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301160{
Olli Etuahocce89652017-06-19 16:04:09 +03001161 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001162 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001163
1164 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301165 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001166 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1167 }
1168
1169 if (!IsImage(type->getBasicType()))
1170 {
Olli Etuaho43364892017-02-13 16:00:12 +00001171 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001172 }
1173 else
1174 {
1175 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001177
Martin Radev70866b82016-07-22 15:27:42 +03001178 type->setQualifier(typeQualifier.qualifier);
1179
1180 if (typeQualifier.precision != EbpUndefined)
1181 {
1182 type->setPrecision(typeQualifier.precision);
1183 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184}
1185
Olli Etuaho703671e2017-11-08 17:47:18 +02001186template <size_t size>
1187bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1188 const std::array<TExtension, size> &extensions)
1189{
1190 ASSERT(!extensions.empty());
1191 const TExtensionBehavior &extBehavior = extensionBehavior();
1192
1193 bool canUseWithWarning = false;
1194 bool canUseWithoutWarning = false;
1195
1196 const char *errorMsgString = "";
1197 TExtension errorMsgExtension = TExtension::UNDEFINED;
1198
1199 for (TExtension extension : extensions)
1200 {
1201 auto extIter = extBehavior.find(extension);
1202 if (canUseWithWarning)
1203 {
1204 // We already have an extension that we can use, but with a warning.
1205 // See if we can use the alternative extension without a warning.
1206 if (extIter == extBehavior.end())
1207 {
1208 continue;
1209 }
1210 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1211 {
1212 canUseWithoutWarning = true;
1213 break;
1214 }
1215 continue;
1216 }
1217 if (extIter == extBehavior.end())
1218 {
1219 errorMsgString = "extension is not supported";
1220 errorMsgExtension = extension;
1221 }
1222 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1223 {
1224 errorMsgString = "extension is disabled";
1225 errorMsgExtension = extension;
1226 }
1227 else if (extIter->second == EBhWarn)
1228 {
1229 errorMsgExtension = extension;
1230 canUseWithWarning = true;
1231 }
1232 else
1233 {
1234 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1235 canUseWithoutWarning = true;
1236 break;
1237 }
1238 }
1239
1240 if (canUseWithoutWarning)
1241 {
1242 return true;
1243 }
1244 if (canUseWithWarning)
1245 {
1246 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1247 return true;
1248 }
1249 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1250 return false;
1251}
1252
1253template bool TParseContext::checkCanUseOneOfExtensions(
1254 const TSourceLoc &line,
1255 const std::array<TExtension, 1> &extensions);
1256template bool TParseContext::checkCanUseOneOfExtensions(
1257 const TSourceLoc &line,
1258 const std::array<TExtension, 2> &extensions);
1259template bool TParseContext::checkCanUseOneOfExtensions(
1260 const TSourceLoc &line,
1261 const std::array<TExtension, 3> &extensions);
1262
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001263bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001264{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001265 ASSERT(extension != TExtension::UNDEFINED);
Jiawei Shao0e883132017-10-26 09:53:50 +08001266 ASSERT(extension != TExtension::EXT_geometry_shader);
Olli Etuaho703671e2017-11-08 17:47:18 +02001267 if (extension == TExtension::OES_geometry_shader)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301268 {
Olli Etuaho703671e2017-11-08 17:47:18 +02001269 // OES_geometry_shader and EXT_geometry_shader are always interchangeable.
1270 constexpr std::array<TExtension, 2u> extensions{
1271 {TExtension::EXT_geometry_shader, TExtension::OES_geometry_shader}};
1272 return checkCanUseOneOfExtensions(line, extensions);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001273 }
Corentin Wallez1d33c212017-11-13 10:21:39 -08001274 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001275}
1276
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001277// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1278// compile-time or link-time errors are the same whether or not the declaration is empty".
1279// This function implements all the checks that are done on qualifiers regardless of if the
1280// declaration is empty.
1281void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1282 const sh::TLayoutQualifier &layoutQualifier,
1283 const TSourceLoc &location)
1284{
1285 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1286 {
1287 error(location, "Shared memory declarations cannot have layout specified", "layout");
1288 }
1289
1290 if (layoutQualifier.matrixPacking != EmpUnspecified)
1291 {
1292 error(location, "layout qualifier only valid for interface blocks",
1293 getMatrixPackingString(layoutQualifier.matrixPacking));
1294 return;
1295 }
1296
1297 if (layoutQualifier.blockStorage != EbsUnspecified)
1298 {
1299 error(location, "layout qualifier only valid for interface blocks",
1300 getBlockStorageString(layoutQualifier.blockStorage));
1301 return;
1302 }
1303
1304 if (qualifier == EvqFragmentOut)
1305 {
1306 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1307 {
1308 error(location, "invalid layout qualifier combination", "yuv");
1309 return;
1310 }
1311 }
1312 else
1313 {
1314 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1315 }
1316
Olli Etuaho95468d12017-05-04 11:14:34 +03001317 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1318 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001319 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1320 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001321 {
1322 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1323 }
1324
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001325 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001326 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001327 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001328 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001329 // We're not checking whether the uniform location is in range here since that depends on
1330 // the type of the variable.
1331 // The type can only be fully determined for non-empty declarations.
1332 }
1333 if (!canHaveLocation)
1334 {
1335 checkLocationIsNotSpecified(location, layoutQualifier);
1336 }
1337}
1338
jchen104cdac9e2017-05-08 11:01:20 +08001339void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1340 const TSourceLoc &location)
1341{
1342 if (publicType.precision != EbpHigh)
1343 {
1344 error(location, "Can only be highp", "atomic counter");
1345 }
1346 // dEQP enforces compile error if location is specified. See uniform_location.test.
1347 if (publicType.layoutQualifier.location != -1)
1348 {
1349 error(location, "location must not be set for atomic_uint", "layout");
1350 }
1351 if (publicType.layoutQualifier.binding == -1)
1352 {
1353 error(location, "no binding specified", "atomic counter");
1354 }
1355}
1356
Olli Etuaho55bde912017-10-25 13:41:13 +03001357void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001358{
Olli Etuaho55bde912017-10-25 13:41:13 +03001359 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001360 {
1361 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1362 // error. It is assumed that this applies to empty declarations as well.
1363 error(location, "empty array declaration needs to specify a size", "");
1364 }
Martin Radevb8b01222016-11-20 23:25:53 +02001365}
1366
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001367// These checks are done for all declarations that are non-empty. They're done for non-empty
1368// declarations starting a declarator list, and declarators that follow an empty declaration.
1369void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1370 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001371{
Olli Etuahofa33d582015-04-09 14:33:12 +03001372 switch (publicType.qualifier)
1373 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001374 case EvqVaryingIn:
1375 case EvqVaryingOut:
1376 case EvqAttribute:
1377 case EvqVertexIn:
1378 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001379 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001380 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001381 {
1382 error(identifierLocation, "cannot be used with a structure",
1383 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001384 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001385 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001386 break;
1387 case EvqBuffer:
1388 if (publicType.getBasicType() != EbtInterfaceBlock)
1389 {
1390 error(identifierLocation,
1391 "cannot declare buffer variables at global scope(outside a block)",
1392 getQualifierString(publicType.qualifier));
1393 return;
1394 }
1395 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001396 default:
1397 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001398 }
jchen10cc2a10e2017-05-03 14:05:12 +08001399 std::string reason(getBasicString(publicType.getBasicType()));
1400 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001401 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001402 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001403 {
1404 return;
1405 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001406
Andrei Volykhina5527072017-03-22 16:46:30 +03001407 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1408 publicType.qualifier != EvqConst) &&
1409 publicType.getBasicType() == EbtYuvCscStandardEXT)
1410 {
1411 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1412 getQualifierString(publicType.qualifier));
1413 return;
1414 }
1415
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001416 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1417 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001418 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1419 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001420 TType type(publicType);
1421 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001422 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001423 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1424 publicType.layoutQualifier);
1425 }
1426 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001427
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001428 // check for layout qualifier issues
1429 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001430
Martin Radev2cc85b32016-08-05 16:22:53 +03001431 if (IsImage(publicType.getBasicType()))
1432 {
1433
1434 switch (layoutQualifier.imageInternalFormat)
1435 {
1436 case EiifRGBA32F:
1437 case EiifRGBA16F:
1438 case EiifR32F:
1439 case EiifRGBA8:
1440 case EiifRGBA8_SNORM:
1441 if (!IsFloatImage(publicType.getBasicType()))
1442 {
1443 error(identifierLocation,
1444 "internal image format requires a floating image type",
1445 getBasicString(publicType.getBasicType()));
1446 return;
1447 }
1448 break;
1449 case EiifRGBA32I:
1450 case EiifRGBA16I:
1451 case EiifRGBA8I:
1452 case EiifR32I:
1453 if (!IsIntegerImage(publicType.getBasicType()))
1454 {
1455 error(identifierLocation,
1456 "internal image format requires an integer image type",
1457 getBasicString(publicType.getBasicType()));
1458 return;
1459 }
1460 break;
1461 case EiifRGBA32UI:
1462 case EiifRGBA16UI:
1463 case EiifRGBA8UI:
1464 case EiifR32UI:
1465 if (!IsUnsignedImage(publicType.getBasicType()))
1466 {
1467 error(identifierLocation,
1468 "internal image format requires an unsigned image type",
1469 getBasicString(publicType.getBasicType()));
1470 return;
1471 }
1472 break;
1473 case EiifUnspecified:
1474 error(identifierLocation, "layout qualifier", "No image internal format specified");
1475 return;
1476 default:
1477 error(identifierLocation, "layout qualifier", "unrecognized token");
1478 return;
1479 }
1480
1481 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1482 switch (layoutQualifier.imageInternalFormat)
1483 {
1484 case EiifR32F:
1485 case EiifR32I:
1486 case EiifR32UI:
1487 break;
1488 default:
1489 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1490 {
1491 error(identifierLocation, "layout qualifier",
1492 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1493 "image variables must be qualified readonly and/or writeonly");
1494 return;
1495 }
1496 break;
1497 }
1498 }
1499 else
1500 {
Olli Etuaho43364892017-02-13 16:00:12 +00001501 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001502 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1503 }
jchen104cdac9e2017-05-08 11:01:20 +08001504
1505 if (IsAtomicCounter(publicType.getBasicType()))
1506 {
1507 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1508 }
1509 else
1510 {
1511 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1512 }
Olli Etuaho43364892017-02-13 16:00:12 +00001513}
Martin Radev2cc85b32016-08-05 16:22:53 +03001514
Olli Etuaho43364892017-02-13 16:00:12 +00001515void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1516{
1517 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001518 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1519 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1520 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1521 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1522 // when it comes to which shaders are accepted by the compiler.
1523 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001524 if (IsImage(type.getBasicType()))
1525 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001526 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1527 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001528 }
1529 else if (IsSampler(type.getBasicType()))
1530 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001531 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1532 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001533 }
jchen104cdac9e2017-05-08 11:01:20 +08001534 else if (IsAtomicCounter(type.getBasicType()))
1535 {
1536 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1537 }
Olli Etuaho43364892017-02-13 16:00:12 +00001538 else
1539 {
1540 ASSERT(!IsOpaqueType(type.getBasicType()));
1541 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001542 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001543}
1544
Olli Etuaho856c4972016-08-08 11:38:39 +03001545void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1546 const TString &layoutQualifierName,
1547 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001548{
1549
1550 if (mShaderVersion < versionRequired)
1551 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001552 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001553 }
1554}
1555
Olli Etuaho856c4972016-08-08 11:38:39 +03001556bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1557 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001558{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001559 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001560 for (size_t i = 0u; i < localSize.size(); ++i)
1561 {
1562 if (localSize[i] != -1)
1563 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001564 error(location,
1565 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1566 "global layout declaration",
1567 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001568 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001569 }
1570 }
1571
Olli Etuaho8a176262016-08-16 14:23:01 +03001572 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001573}
1574
Olli Etuaho43364892017-02-13 16:00:12 +00001575void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001576 TLayoutImageInternalFormat internalFormat)
1577{
1578 if (internalFormat != EiifUnspecified)
1579 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001580 error(location, "invalid layout qualifier: only valid when used with images",
1581 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001582 }
Olli Etuaho43364892017-02-13 16:00:12 +00001583}
1584
1585void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1586{
1587 if (binding != -1)
1588 {
1589 error(location,
1590 "invalid layout qualifier: only valid when used with opaque types or blocks",
1591 "binding");
1592 }
1593}
1594
jchen104cdac9e2017-05-08 11:01:20 +08001595void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1596{
1597 if (offset != -1)
1598 {
1599 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1600 "offset");
1601 }
1602}
1603
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001604void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1605 int binding,
1606 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001607{
1608 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001609 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001610 {
1611 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1612 }
1613}
1614
1615void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1616 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001617 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001618{
1619 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001620 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001621 {
1622 error(location, "sampler binding greater than maximum texture units", "binding");
1623 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001624}
1625
Jiajia Qinbc585152017-06-23 15:42:17 +08001626void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1627 const TQualifier &qualifier,
1628 int binding,
1629 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001630{
1631 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001632 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001633 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001634 if (binding + size > mMaxUniformBufferBindings)
1635 {
1636 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1637 "binding");
1638 }
1639 }
1640 else if (qualifier == EvqBuffer)
1641 {
1642 if (binding + size > mMaxShaderStorageBufferBindings)
1643 {
1644 error(location,
1645 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1646 "binding");
1647 }
jchen10af713a22017-04-19 09:10:56 +08001648 }
1649}
jchen104cdac9e2017-05-08 11:01:20 +08001650void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1651{
1652 if (binding >= mMaxAtomicCounterBindings)
1653 {
1654 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1655 "binding");
1656 }
1657}
jchen10af713a22017-04-19 09:10:56 +08001658
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001659void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1660 int objectLocationCount,
1661 const TLayoutQualifier &layoutQualifier)
1662{
1663 int loc = layoutQualifier.location;
1664 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1665 {
1666 error(location, "Uniform location out of range", "location");
1667 }
1668}
1669
Andrei Volykhina5527072017-03-22 16:46:30 +03001670void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1671{
1672 if (yuv != false)
1673 {
1674 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1675 }
1676}
1677
Jiajia Qinbc585152017-06-23 15:42:17 +08001678void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1679 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001680{
1681 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1682 {
1683 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001684 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1685 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1686 qual == EvqInOut || qual == EvqConstReadOnly))
1687 {
1688 if (argument->getMemoryQualifier().writeonly)
1689 {
1690 error(argument->getLine(),
1691 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1692 fnCall->getFunctionSymbolInfo()->getName().c_str());
1693 return;
1694 }
1695 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001696 if (qual == EvqOut || qual == EvqInOut)
1697 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001698 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001699 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001700 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001701 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001702 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001703 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001704 }
1705 }
1706 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001707}
1708
Martin Radev70866b82016-07-22 15:27:42 +03001709void TParseContext::checkInvariantVariableQualifier(bool invariant,
1710 const TQualifier qualifier,
1711 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001712{
Martin Radev70866b82016-07-22 15:27:42 +03001713 if (!invariant)
1714 return;
1715
1716 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001717 {
Martin Radev70866b82016-07-22 15:27:42 +03001718 // input variables in the fragment shader can be also qualified as invariant
1719 if (!sh::CanBeInvariantESSL1(qualifier))
1720 {
1721 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1722 }
1723 }
1724 else
1725 {
1726 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1727 {
1728 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1729 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001730 }
1731}
1732
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001733bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001734{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001735 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001736}
1737
Jamie Madillb98c3a82015-07-23 14:26:04 -04001738void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1739 const char *extName,
1740 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001741{
1742 pp::SourceLocation srcLoc;
1743 srcLoc.file = loc.first_file;
1744 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001745 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001746}
1747
Jamie Madillb98c3a82015-07-23 14:26:04 -04001748void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1749 const char *name,
1750 const char *value,
1751 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001752{
1753 pp::SourceLocation srcLoc;
1754 srcLoc.file = loc.first_file;
1755 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001756 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001757}
1758
Martin Radev4c4c8e72016-08-04 12:25:34 +03001759sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001760{
Jamie Madill2f294c92017-11-20 14:47:26 -05001761 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001762 for (size_t i = 0u; i < result.size(); ++i)
1763 {
1764 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1765 {
1766 result[i] = 1;
1767 }
1768 else
1769 {
1770 result[i] = mComputeShaderLocalSize[i];
1771 }
1772 }
1773 return result;
1774}
1775
Olli Etuaho56229f12017-07-10 14:16:33 +03001776TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1777 const TSourceLoc &line)
1778{
1779 TIntermConstantUnion *node = new TIntermConstantUnion(
1780 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1781 node->setLine(line);
1782 return node;
1783}
1784
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785/////////////////////////////////////////////////////////////////////////////////
1786//
1787// Non-Errors.
1788//
1789/////////////////////////////////////////////////////////////////////////////////
1790
Jamie Madill5c097022014-08-20 16:38:32 -04001791const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1792 const TString *name,
1793 const TSymbol *symbol)
1794{
Jamie Madill5c097022014-08-20 16:38:32 -04001795 if (!symbol)
1796 {
1797 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001798 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001799 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001800
1801 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001802 {
1803 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001804 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001805 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001806
1807 const TVariable *variable = static_cast<const TVariable *>(symbol);
1808
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001809 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001810 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001811 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001812 }
1813
Olli Etuaho0f684632017-07-13 12:42:15 +03001814 // Reject shaders using both gl_FragData and gl_FragColor
1815 TQualifier qualifier = variable->getType().getQualifier();
1816 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001817 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001818 mUsesFragData = true;
1819 }
1820 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1821 {
1822 mUsesFragColor = true;
1823 }
1824 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1825 {
1826 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001827 }
1828
Olli Etuaho0f684632017-07-13 12:42:15 +03001829 // This validation is not quite correct - it's only an error to write to
1830 // both FragData and FragColor. For simplicity, and because users shouldn't
1831 // be rewarded for reading from undefined varaibles, return an error
1832 // if they are both referenced, rather than assigned.
1833 if (mUsesFragData && mUsesFragColor)
1834 {
1835 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1836 if (mUsesSecondaryOutputs)
1837 {
1838 errorMessage =
1839 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1840 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1841 }
1842 error(location, errorMessage, name->c_str());
1843 }
1844
1845 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1846 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1847 qualifier == EvqWorkGroupSize)
1848 {
1849 error(location,
1850 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1851 "gl_WorkGroupSize");
1852 }
Jamie Madill5c097022014-08-20 16:38:32 -04001853 return variable;
1854}
1855
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001856TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1857 const TString *name,
1858 const TSymbol *symbol)
1859{
1860 const TVariable *variable = getNamedVariable(location, name, symbol);
1861
Olli Etuaho0f684632017-07-13 12:42:15 +03001862 if (!variable)
1863 {
1864 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1865 node->setLine(location);
1866 return node;
1867 }
1868
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001869 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001870 TIntermTyped *node = nullptr;
1871
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001872 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001873 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001874 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001875 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001876 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001877 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001878 {
1879 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1880 // needs to be added to the AST as a constant and not as a symbol.
1881 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1882 TConstantUnion *constArray = new TConstantUnion[3];
1883 for (size_t i = 0; i < 3; ++i)
1884 {
1885 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1886 }
1887
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001888 ASSERT(variableType.getBasicType() == EbtUInt);
1889 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001890
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001891 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001892 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001893 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001894 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001895 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1896 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001897 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001898 ASSERT(mGeometryShaderInputArraySize > 0u);
1899
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001900 node = new TIntermSymbol(variable->uniqueId(), variable->name(), variableType);
Olli Etuaho55bde912017-10-25 13:41:13 +03001901 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001902 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001903 else
1904 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001905 node = new TIntermSymbol(variable->uniqueId(), variable->name(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001906 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001907 ASSERT(node != nullptr);
1908 node->setLine(location);
1909 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001910}
1911
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912// Initializers show up in several places in the grammar. Have one set of
1913// code to handle them here.
1914//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001915// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001916bool TParseContext::executeInitializer(const TSourceLoc &line,
1917 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001918 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001919 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001920 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921{
Olli Etuaho13389b62016-10-16 11:48:18 +01001922 ASSERT(initNode != nullptr);
1923 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924
Olli Etuaho2935c582015-04-08 14:32:06 +03001925 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001926 if (type.isUnsizedArray())
1927 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001928 // In case initializer is not an array or type has more dimensions than initializer, this
1929 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1930 // actually is an array or not. Having a non-array initializer for an unsized array will
1931 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001932 auto *arraySizes = initializer->getType().getArraySizes();
1933 type.sizeUnsizedArrays(arraySizes);
Olli Etuaho376f1b52015-04-13 13:23:41 +03001934 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001935 if (!declareVariable(line, identifier, type, &variable))
1936 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001937 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939
Olli Etuahob0c645e2015-05-12 14:25:36 +03001940 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001941 if (symbolTable.atGlobalLevel() &&
1942 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001943 {
1944 // Error message does not completely match behavior with ESSL 1.00, but
1945 // we want to steer developers towards only using constant expressions.
1946 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001947 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001948 }
1949 if (globalInitWarning)
1950 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001951 warning(
1952 line,
1953 "global variable initializers should be constant expressions "
1954 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1955 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001956 }
1957
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001958 //
1959 // identifier must be of type constant, a global, or a temporary
1960 //
1961 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301962 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1963 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001964 error(line, " cannot initialize this type of qualifier ",
1965 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001966 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001967 }
1968 //
1969 // test for and propagate constant
1970 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971
Arun Patole7e7e68d2015-05-22 12:02:25 +05301972 if (qualifier == EvqConst)
1973 {
1974 if (qualifier != initializer->getType().getQualifier())
1975 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001976 std::stringstream reasonStream;
1977 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1978 << "'";
1979 std::string reason = reasonStream.str();
1980 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001981 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001982 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001983 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301984 if (type != initializer->getType())
1985 {
1986 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001987 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001988 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001989 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001990 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001991
1992 // Save the constant folded value to the variable if possible. For example array
1993 // initializers are not folded, since that way copying the array literal to multiple places
1994 // in the shader is avoided.
1995 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1996 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301997 if (initializer->getAsConstantUnion())
1998 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001999 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002000 ASSERT(*initNode == nullptr);
2001 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302002 }
2003 else if (initializer->getAsSymbolNode())
2004 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002005 const TSymbol *symbol =
2006 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
2007 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002008
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002009 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002010 if (constArray)
2011 {
2012 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002013 ASSERT(*initNode == nullptr);
2014 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002015 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002016 }
2017 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002018
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002019 TIntermSymbol *intermSymbol =
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002020 new TIntermSymbol(variable->uniqueId(), variable->name(), variable->getType());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002021 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002022 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2023 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002024 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002025 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002026 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002027 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028
Olli Etuaho914b79a2017-06-19 16:03:19 +03002029 return true;
2030}
2031
2032TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2033 const TString &identifier,
2034 TIntermTyped *initializer,
2035 const TSourceLoc &loc)
2036{
2037 checkIsScalarBool(loc, pType);
2038 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002039 TType type(pType);
2040 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002041 {
2042 // The initializer is valid. The init condition needs to have a node - either the
2043 // initializer node, or a constant node in case the initialized variable is const and won't
2044 // be recorded in the AST.
2045 if (initNode == nullptr)
2046 {
2047 return initializer;
2048 }
2049 else
2050 {
2051 TIntermDeclaration *declaration = new TIntermDeclaration();
2052 declaration->appendDeclarator(initNode);
2053 return declaration;
2054 }
2055 }
2056 return nullptr;
2057}
2058
2059TIntermNode *TParseContext::addLoop(TLoopType type,
2060 TIntermNode *init,
2061 TIntermNode *cond,
2062 TIntermTyped *expr,
2063 TIntermNode *body,
2064 const TSourceLoc &line)
2065{
2066 TIntermNode *node = nullptr;
2067 TIntermTyped *typedCond = nullptr;
2068 if (cond)
2069 {
2070 typedCond = cond->getAsTyped();
2071 }
2072 if (cond == nullptr || typedCond)
2073 {
Olli Etuahocce89652017-06-19 16:04:09 +03002074 if (type == ELoopDoWhile)
2075 {
2076 checkIsScalarBool(line, typedCond);
2077 }
2078 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2079 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2080 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2081 !typedCond->isVector()));
2082
Olli Etuaho3ec75682017-07-05 17:02:55 +03002083 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002084 node->setLine(line);
2085 return node;
2086 }
2087
Olli Etuahocce89652017-06-19 16:04:09 +03002088 ASSERT(type != ELoopDoWhile);
2089
Olli Etuaho914b79a2017-06-19 16:03:19 +03002090 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2091 ASSERT(declaration);
2092 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2093 ASSERT(declarator->getLeft()->getAsSymbolNode());
2094
2095 // The condition is a declaration. In the AST representation we don't support declarations as
2096 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2097 // the loop.
2098 TIntermBlock *block = new TIntermBlock();
2099
2100 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2101 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2102 block->appendStatement(declareCondition);
2103
2104 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2105 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002106 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002107 block->appendStatement(loop);
2108 loop->setLine(line);
2109 block->setLine(line);
2110 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111}
2112
Olli Etuahocce89652017-06-19 16:04:09 +03002113TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2114 TIntermNodePair code,
2115 const TSourceLoc &loc)
2116{
Olli Etuaho56229f12017-07-10 14:16:33 +03002117 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002118
2119 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002120 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002121 {
2122 if (cond->getAsConstantUnion()->getBConst(0) == true)
2123 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002124 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002125 }
2126 else
2127 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002128 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002129 }
2130 }
2131
Olli Etuaho3ec75682017-07-05 17:02:55 +03002132 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002133 node->setLine(loc);
2134
2135 return node;
2136}
2137
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002138void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2139{
2140 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2141 typeSpecifier->getBasicType());
2142
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002143 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002144 {
2145 error(typeSpecifier->getLine(), "not supported", "first-class array");
2146 typeSpecifier->clearArrayness();
2147 }
2148}
2149
Martin Radev70866b82016-07-22 15:27:42 +03002150TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302151 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002152{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002153 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002154
Martin Radev70866b82016-07-22 15:27:42 +03002155 TPublicType returnType = typeSpecifier;
2156 returnType.qualifier = typeQualifier.qualifier;
2157 returnType.invariant = typeQualifier.invariant;
2158 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002159 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002160 returnType.precision = typeSpecifier.precision;
2161
2162 if (typeQualifier.precision != EbpUndefined)
2163 {
2164 returnType.precision = typeQualifier.precision;
2165 }
2166
Martin Radev4a9cd802016-09-01 16:51:51 +03002167 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2168 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002169
Martin Radev4a9cd802016-09-01 16:51:51 +03002170 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2171 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002172
Martin Radev4a9cd802016-09-01 16:51:51 +03002173 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002174
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002175 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002176 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002177 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002178 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002179 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002180 returnType.clearArrayness();
2181 }
2182
Martin Radev70866b82016-07-22 15:27:42 +03002183 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002184 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002185 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002186 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002187 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002188 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002189
Martin Radev70866b82016-07-22 15:27:42 +03002190 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002191 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002192 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002193 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002194 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002195 }
2196 }
2197 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002198 {
Martin Radev70866b82016-07-22 15:27:42 +03002199 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002200 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002201 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002202 }
Martin Radev70866b82016-07-22 15:27:42 +03002203 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2204 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002205 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002206 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2207 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002208 }
Martin Radev70866b82016-07-22 15:27:42 +03002209 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002210 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002211 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002212 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002213 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002214 }
2215
2216 return returnType;
2217}
2218
Olli Etuaho856c4972016-08-08 11:38:39 +03002219void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2220 const TPublicType &type,
2221 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002222{
2223 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002224 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002225 {
2226 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002227 }
2228
2229 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2230 switch (qualifier)
2231 {
2232 case EvqVertexIn:
2233 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002234 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002235 {
2236 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002237 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002238 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002239 return;
2240 case EvqFragmentOut:
2241 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002242 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002243 {
2244 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002245 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002246 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002247 return;
2248 default:
2249 break;
2250 }
2251
2252 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2253 // restrictions.
2254 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002255 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2256 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002257 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2258 {
2259 error(qualifierLocation, "must use 'flat' interpolation here",
2260 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002261 }
2262
Martin Radev4a9cd802016-09-01 16:51:51 +03002263 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002264 {
2265 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2266 // These restrictions are only implied by the ESSL 3.00 spec, but
2267 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002268 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002269 {
2270 error(qualifierLocation, "cannot be an array of structures",
2271 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002272 }
2273 if (type.isStructureContainingArrays())
2274 {
2275 error(qualifierLocation, "cannot be a structure containing an array",
2276 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002277 }
2278 if (type.isStructureContainingType(EbtStruct))
2279 {
2280 error(qualifierLocation, "cannot be a structure containing a structure",
2281 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002282 }
2283 if (type.isStructureContainingType(EbtBool))
2284 {
2285 error(qualifierLocation, "cannot be a structure containing a bool",
2286 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002287 }
2288 }
2289}
2290
Martin Radev2cc85b32016-08-05 16:22:53 +03002291void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2292{
2293 if (qualifier.getType() == QtStorage)
2294 {
2295 const TStorageQualifierWrapper &storageQualifier =
2296 static_cast<const TStorageQualifierWrapper &>(qualifier);
2297 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2298 !symbolTable.atGlobalLevel())
2299 {
2300 error(storageQualifier.getLine(),
2301 "Local variables can only use the const storage qualifier.",
2302 storageQualifier.getQualifierString().c_str());
2303 }
2304 }
2305}
2306
Olli Etuaho43364892017-02-13 16:00:12 +00002307void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002308 const TSourceLoc &location)
2309{
Jiajia Qinbc585152017-06-23 15:42:17 +08002310 const std::string reason(
2311 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2312 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002313 if (memoryQualifier.readonly)
2314 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002315 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002316 }
2317 if (memoryQualifier.writeonly)
2318 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002319 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002320 }
Martin Radev049edfa2016-11-11 14:35:37 +02002321 if (memoryQualifier.coherent)
2322 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002323 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002324 }
2325 if (memoryQualifier.restrictQualifier)
2326 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002327 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002328 }
2329 if (memoryQualifier.volatileQualifier)
2330 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002331 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002332 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002333}
2334
jchen104cdac9e2017-05-08 11:01:20 +08002335// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2336// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002337void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2338 const TSourceLoc &loc,
2339 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002340{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002341 if (!IsAtomicCounter(type->getBasicType()))
2342 {
2343 return;
2344 }
2345
2346 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2347 : kAtomicCounterSize;
2348 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2349 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002350 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002351 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002352 {
2353 offset = bindingState.appendSpan(size);
2354 }
2355 else
2356 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002357 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002358 }
2359 if (offset == -1)
2360 {
2361 error(loc, "Offset overlapping", "atomic counter");
2362 return;
2363 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002364 layoutQualifier.offset = offset;
2365 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002366}
2367
Olli Etuaho454c34c2017-10-25 16:35:56 +03002368void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2369 const char *token,
2370 TType *type)
2371{
2372 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2373 {
2374 if (type->isArray() && type->getOutermostArraySize() == 0u)
2375 {
2376 // Set size for the unsized geometry shader inputs if they are declared after a valid
2377 // input primitive declaration.
2378 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2379 {
2380 ASSERT(mGeometryShaderInputArraySize > 0u);
2381 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2382 }
2383 else
2384 {
2385 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2386 // An input can be declared without an array size if there is a previous layout
2387 // which specifies the size.
2388 error(location,
2389 "Missing a valid input primitive declaration before declaring an unsized "
2390 "array input",
2391 token);
2392 }
2393 }
2394 else if (type->isArray())
2395 {
2396 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2397 }
2398 else
2399 {
2400 error(location, "Geometry shader input variable must be declared as an array", token);
2401 }
2402 }
2403}
2404
Olli Etuaho13389b62016-10-16 11:48:18 +01002405TIntermDeclaration *TParseContext::parseSingleDeclaration(
2406 TPublicType &publicType,
2407 const TSourceLoc &identifierOrTypeLocation,
2408 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002409{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002410 TType type(publicType);
2411 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2412 mDirectiveHandler.pragma().stdgl.invariantAll)
2413 {
2414 TQualifier qualifier = type.getQualifier();
2415
2416 // The directive handler has already taken care of rejecting invalid uses of this pragma
2417 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2418 // affected variable declarations:
2419 //
2420 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2421 // elsewhere, in TranslatorGLSL.)
2422 //
2423 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2424 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2425 // the way this is currently implemented we have to enable this compiler option before
2426 // parsing the shader and determining the shading language version it uses. If this were
2427 // implemented as a post-pass, the workaround could be more targeted.
2428 //
2429 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2430 // the specification, but there are desktop OpenGL drivers that expect that this is the
2431 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2432 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2433 {
2434 type.setInvariant(true);
2435 }
2436 }
2437
Olli Etuaho454c34c2017-10-25 16:35:56 +03002438 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002439
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002440 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2441 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002442
Olli Etuahobab4c082015-04-24 16:38:49 +03002443 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002444 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002445
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002446 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002447 if (emptyDeclaration)
2448 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002449 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002450 // In most cases we don't need to create a symbol node for an empty declaration.
2451 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2452 if (type.getBasicType() == EbtStruct)
2453 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002454 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002455 }
jchen104cdac9e2017-05-08 11:01:20 +08002456 else if (IsAtomicCounter(publicType.getBasicType()))
2457 {
2458 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2459 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002460 }
2461 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002462 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002463 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002464
Olli Etuaho55bde912017-10-25 13:41:13 +03002465 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002466
Olli Etuaho55bc9052017-10-25 17:33:06 +03002467 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002468
Olli Etuaho2935c582015-04-08 14:32:06 +03002469 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002470 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002471
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002472 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002473 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002474 symbol = new TIntermSymbol(variable->uniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002475 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002476 }
2477
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002478 TIntermDeclaration *declaration = new TIntermDeclaration();
2479 declaration->setLine(identifierOrTypeLocation);
2480 if (symbol)
2481 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002482 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002483 declaration->appendDeclarator(symbol);
2484 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002485 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002486}
2487
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002488TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2489 TPublicType &elementType,
2490 const TSourceLoc &identifierLocation,
2491 const TString &identifier,
2492 const TSourceLoc &indexLocation,
2493 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002494{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002495 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002496
Olli Etuaho55bde912017-10-25 13:41:13 +03002497 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002498 identifierLocation);
2499
Olli Etuaho55bde912017-10-25 13:41:13 +03002500 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002501
Olli Etuaho55bde912017-10-25 13:41:13 +03002502 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002503
Olli Etuaho55bde912017-10-25 13:41:13 +03002504 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002505 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002506
Olli Etuaho454c34c2017-10-25 16:35:56 +03002507 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002508
2509 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2510
Olli Etuaho55bc9052017-10-25 17:33:06 +03002511 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002512
Olli Etuaho2935c582015-04-08 14:32:06 +03002513 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002514 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002515
Olli Etuaho13389b62016-10-16 11:48:18 +01002516 TIntermDeclaration *declaration = new TIntermDeclaration();
2517 declaration->setLine(identifierLocation);
2518
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002519 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002520 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002521 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, arrayType);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002522 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002523 declaration->appendDeclarator(symbol);
2524 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002525
Olli Etuaho13389b62016-10-16 11:48:18 +01002526 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002527}
2528
Olli Etuaho13389b62016-10-16 11:48:18 +01002529TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2530 const TSourceLoc &identifierLocation,
2531 const TString &identifier,
2532 const TSourceLoc &initLocation,
2533 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002534{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002535 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002536
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002537 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2538 identifierLocation);
2539
2540 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002541
Olli Etuaho13389b62016-10-16 11:48:18 +01002542 TIntermDeclaration *declaration = new TIntermDeclaration();
2543 declaration->setLine(identifierLocation);
2544
2545 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002546 TType type(publicType);
2547 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002548 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002549 if (initNode)
2550 {
2551 declaration->appendDeclarator(initNode);
2552 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002553 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002554 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002555}
2556
Olli Etuaho13389b62016-10-16 11:48:18 +01002557TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002558 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002559 const TSourceLoc &identifierLocation,
2560 const TString &identifier,
2561 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002562 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002563 const TSourceLoc &initLocation,
2564 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002566 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002567
Olli Etuaho55bde912017-10-25 13:41:13 +03002568 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002569 identifierLocation);
2570
Olli Etuaho55bde912017-10-25 13:41:13 +03002571 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572
Olli Etuaho55bde912017-10-25 13:41:13 +03002573 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002574
Olli Etuaho55bde912017-10-25 13:41:13 +03002575 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002576 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002577
Olli Etuaho13389b62016-10-16 11:48:18 +01002578 TIntermDeclaration *declaration = new TIntermDeclaration();
2579 declaration->setLine(identifierLocation);
2580
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002581 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002582 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002583 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002584 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002585 if (initNode)
2586 {
2587 declaration->appendDeclarator(initNode);
2588 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002589 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002590
2591 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002592}
2593
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002594TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002595 const TTypeQualifierBuilder &typeQualifierBuilder,
2596 const TSourceLoc &identifierLoc,
2597 const TString *identifier,
2598 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002599{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002600 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002601
Martin Radev70866b82016-07-22 15:27:42 +03002602 if (!typeQualifier.invariant)
2603 {
2604 error(identifierLoc, "Expected invariant", identifier->c_str());
2605 return nullptr;
2606 }
2607 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2608 {
2609 return nullptr;
2610 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002611 if (!symbol)
2612 {
2613 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002614 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002615 }
Martin Radev70866b82016-07-22 15:27:42 +03002616 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002617 {
Martin Radev70866b82016-07-22 15:27:42 +03002618 error(identifierLoc, "invariant declaration specifies qualifier",
2619 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002620 }
Martin Radev70866b82016-07-22 15:27:42 +03002621 if (typeQualifier.precision != EbpUndefined)
2622 {
2623 error(identifierLoc, "invariant declaration specifies precision",
2624 getPrecisionString(typeQualifier.precision));
2625 }
2626 if (!typeQualifier.layoutQualifier.isEmpty())
2627 {
2628 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2629 }
2630
2631 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002632 if (!variable)
2633 {
2634 return nullptr;
2635 }
Martin Radev70866b82016-07-22 15:27:42 +03002636 const TType &type = variable->getType();
2637
2638 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2639 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002640 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002641
2642 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2643
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002644 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->uniqueId(), *identifier, type);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002645 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002646
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002647 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002648}
2649
Olli Etuaho13389b62016-10-16 11:48:18 +01002650void TParseContext::parseDeclarator(TPublicType &publicType,
2651 const TSourceLoc &identifierLocation,
2652 const TString &identifier,
2653 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002654{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002655 // If the declaration starting this declarator list was empty (example: int,), some checks were
2656 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002657 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002658 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002659 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2660 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002661 }
2662
Olli Etuaho856c4972016-08-08 11:38:39 +03002663 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002664
Olli Etuaho2935c582015-04-08 14:32:06 +03002665 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002666 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002667
2668 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2669
Olli Etuaho55bde912017-10-25 13:41:13 +03002670 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2671
Olli Etuaho55bc9052017-10-25 17:33:06 +03002672 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2673
Olli Etuaho43364892017-02-13 16:00:12 +00002674 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002675
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002676 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002677 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002678 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, type);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002679 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002680 declarationOut->appendDeclarator(symbol);
2681 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002682}
2683
Olli Etuaho55bde912017-10-25 13:41:13 +03002684void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002685 const TSourceLoc &identifierLocation,
2686 const TString &identifier,
2687 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002688 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002689 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002690{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002691 // If the declaration starting this declarator list was empty (example: int,), some checks were
2692 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002693 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002694 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002695 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002696 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002697 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002698
Olli Etuaho55bde912017-10-25 13:41:13 +03002699 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002700
Olli Etuaho55bde912017-10-25 13:41:13 +03002701 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002702 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002703 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002704 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002705
Olli Etuaho454c34c2017-10-25 16:35:56 +03002706 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2707
Olli Etuaho55bde912017-10-25 13:41:13 +03002708 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2709
Olli Etuaho55bc9052017-10-25 17:33:06 +03002710 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002711
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002712 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002713 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002714
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002715 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002716 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002717 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, arrayType);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002718 symbol->setLine(identifierLocation);
2719 declarationOut->appendDeclarator(symbol);
2720 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002721 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002722}
2723
Olli Etuaho13389b62016-10-16 11:48:18 +01002724void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2725 const TSourceLoc &identifierLocation,
2726 const TString &identifier,
2727 const TSourceLoc &initLocation,
2728 TIntermTyped *initializer,
2729 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002730{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002731 // If the declaration starting this declarator list was empty (example: int,), some checks were
2732 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002733 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002734 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002735 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2736 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002737 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002738
Olli Etuaho856c4972016-08-08 11:38:39 +03002739 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002740
Olli Etuaho13389b62016-10-16 11:48:18 +01002741 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002742 TType type(publicType);
2743 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002744 {
2745 //
2746 // build the intermediate representation
2747 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002748 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002749 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002750 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002751 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002752 }
2753}
2754
Olli Etuaho55bde912017-10-25 13:41:13 +03002755void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002756 const TSourceLoc &identifierLocation,
2757 const TString &identifier,
2758 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002759 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002760 const TSourceLoc &initLocation,
2761 TIntermTyped *initializer,
2762 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002763{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002764 // If the declaration starting this declarator list was empty (example: int,), some checks were
2765 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002766 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002767 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002768 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002769 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002770 }
2771
Olli Etuaho55bde912017-10-25 13:41:13 +03002772 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002773
Olli Etuaho55bde912017-10-25 13:41:13 +03002774 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002775
Olli Etuaho55bde912017-10-25 13:41:13 +03002776 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002777 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002778
2779 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002780 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002781 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002782 {
2783 if (initNode)
2784 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002785 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002786 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002787 }
2788}
2789
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002790TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2791{
2792 // It's simpler to parse an empty statement as a constant expression rather than having a
2793 // different type of node just for empty statements, that will be pruned from the AST anyway.
2794 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2795 node->setLine(location);
2796 return node;
2797}
2798
jchen104cdac9e2017-05-08 11:01:20 +08002799void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2800 const TSourceLoc &location)
2801{
2802 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2803 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2804 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2805 {
2806 error(location, "Requires both binding and offset", "layout");
2807 return;
2808 }
2809 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2810}
2811
Olli Etuahocce89652017-06-19 16:04:09 +03002812void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2813 const TPublicType &type,
2814 const TSourceLoc &loc)
2815{
2816 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2817 !getFragmentPrecisionHigh())
2818 {
2819 error(loc, "precision is not supported in fragment shader", "highp");
2820 }
2821
2822 if (!CanSetDefaultPrecisionOnType(type))
2823 {
2824 error(loc, "illegal type argument for default precision qualifier",
2825 getBasicString(type.getBasicType()));
2826 return;
2827 }
2828 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2829}
2830
Shaob5cc1192017-07-06 10:47:20 +08002831bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2832{
2833 switch (typeQualifier.layoutQualifier.primitiveType)
2834 {
2835 case EptLines:
2836 case EptLinesAdjacency:
2837 case EptTriangles:
2838 case EptTrianglesAdjacency:
2839 return typeQualifier.qualifier == EvqGeometryIn;
2840
2841 case EptLineStrip:
2842 case EptTriangleStrip:
2843 return typeQualifier.qualifier == EvqGeometryOut;
2844
2845 case EptPoints:
2846 return true;
2847
2848 default:
2849 UNREACHABLE();
2850 return false;
2851 }
2852}
2853
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002854void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2855 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002856{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002857 if (mGeometryShaderInputArraySize == 0u)
2858 {
2859 mGeometryShaderInputArraySize = inputArraySize;
2860 }
2861 else if (mGeometryShaderInputArraySize != inputArraySize)
2862 {
2863 error(line,
2864 "Array size or input primitive declaration doesn't match the size of earlier sized "
2865 "array inputs.",
2866 "layout");
2867 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002868}
2869
Shaob5cc1192017-07-06 10:47:20 +08002870bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2871{
2872 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2873
2874 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2875
2876 if (layoutQualifier.maxVertices != -1)
2877 {
2878 error(typeQualifier.line,
2879 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2880 return false;
2881 }
2882
2883 // Set mGeometryInputPrimitiveType if exists
2884 if (layoutQualifier.primitiveType != EptUndefined)
2885 {
2886 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2887 {
2888 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2889 return false;
2890 }
2891
2892 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2893 {
2894 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002895 setGeometryShaderInputArraySize(
2896 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2897 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002898 }
2899 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2900 {
2901 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2902 "layout");
2903 return false;
2904 }
2905 }
2906
2907 // Set mGeometryInvocations if exists
2908 if (layoutQualifier.invocations > 0)
2909 {
2910 if (mGeometryShaderInvocations == 0)
2911 {
2912 mGeometryShaderInvocations = layoutQualifier.invocations;
2913 }
2914 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2915 {
2916 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2917 "layout");
2918 return false;
2919 }
2920 }
2921
2922 return true;
2923}
2924
2925bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2926{
2927 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2928
2929 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2930
2931 if (layoutQualifier.invocations > 0)
2932 {
2933 error(typeQualifier.line,
2934 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2935 return false;
2936 }
2937
2938 // Set mGeometryOutputPrimitiveType if exists
2939 if (layoutQualifier.primitiveType != EptUndefined)
2940 {
2941 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2942 {
2943 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2944 return false;
2945 }
2946
2947 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2948 {
2949 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2950 }
2951 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2952 {
2953 error(typeQualifier.line,
2954 "primitive doesn't match earlier output primitive declaration", "layout");
2955 return false;
2956 }
2957 }
2958
2959 // Set mGeometryMaxVertices if exists
2960 if (layoutQualifier.maxVertices > -1)
2961 {
2962 if (mGeometryShaderMaxVertices == -1)
2963 {
2964 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2965 }
2966 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2967 {
2968 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2969 "layout");
2970 return false;
2971 }
2972 }
2973
2974 return true;
2975}
2976
Martin Radev70866b82016-07-22 15:27:42 +03002977void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002978{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002979 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002980 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002981
Martin Radev70866b82016-07-22 15:27:42 +03002982 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2983 typeQualifier.line);
2984
Jamie Madillc2128ff2016-07-04 10:26:17 -04002985 // It should never be the case, but some strange parser errors can send us here.
2986 if (layoutQualifier.isEmpty())
2987 {
2988 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002989 return;
2990 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002991
Martin Radev802abe02016-08-04 17:48:32 +03002992 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002993 {
Olli Etuaho43364892017-02-13 16:00:12 +00002994 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002995 return;
2996 }
2997
Olli Etuaho43364892017-02-13 16:00:12 +00002998 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2999
3000 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003001
3002 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3003
Andrei Volykhina5527072017-03-22 16:46:30 +03003004 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3005
jchen104cdac9e2017-05-08 11:01:20 +08003006 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3007
Qin Jiajiaca68d982017-09-18 16:41:56 +08003008 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3009 typeQualifier.qualifier);
3010
Martin Radev802abe02016-08-04 17:48:32 +03003011 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003012 {
Martin Radev802abe02016-08-04 17:48:32 +03003013 if (mComputeShaderLocalSizeDeclared &&
3014 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3015 {
3016 error(typeQualifier.line, "Work group size does not match the previous declaration",
3017 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003018 return;
3019 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003020
Martin Radev802abe02016-08-04 17:48:32 +03003021 if (mShaderVersion < 310)
3022 {
3023 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003024 return;
3025 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003026
Martin Radev4c4c8e72016-08-04 12:25:34 +03003027 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003028 {
3029 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003030 return;
3031 }
3032
3033 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3034 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3035
3036 const TConstantUnion *maxComputeWorkGroupSizeData =
3037 maxComputeWorkGroupSize->getConstPointer();
3038
3039 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3040 {
3041 if (layoutQualifier.localSize[i] != -1)
3042 {
3043 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3044 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3045 if (mComputeShaderLocalSize[i] < 1 ||
3046 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3047 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003048 std::stringstream reasonStream;
3049 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3050 << maxComputeWorkGroupSizeValue;
3051 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003052
Olli Etuaho4de340a2016-12-16 09:32:03 +00003053 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003054 return;
3055 }
3056 }
3057 }
3058
3059 mComputeShaderLocalSizeDeclared = true;
3060 }
Shaob5cc1192017-07-06 10:47:20 +08003061 else if (typeQualifier.qualifier == EvqGeometryIn)
3062 {
3063 if (mShaderVersion < 310)
3064 {
3065 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3066 return;
3067 }
3068
3069 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3070 {
3071 return;
3072 }
3073 }
3074 else if (typeQualifier.qualifier == EvqGeometryOut)
3075 {
3076 if (mShaderVersion < 310)
3077 {
3078 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3079 "layout");
3080 return;
3081 }
3082
3083 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3084 {
3085 return;
3086 }
3087 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003088 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3089 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003090 {
3091 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3092 // specification.
3093 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3094 {
3095 error(typeQualifier.line, "Number of views does not match the previous declaration",
3096 "layout");
3097 return;
3098 }
3099
3100 if (layoutQualifier.numViews == -1)
3101 {
3102 error(typeQualifier.line, "No num_views specified", "layout");
3103 return;
3104 }
3105
3106 if (layoutQualifier.numViews > mMaxNumViews)
3107 {
3108 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3109 "layout");
3110 return;
3111 }
3112
3113 mNumViews = layoutQualifier.numViews;
3114 }
Martin Radev802abe02016-08-04 17:48:32 +03003115 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003116 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003117 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003118 {
Martin Radev802abe02016-08-04 17:48:32 +03003119 return;
3120 }
3121
Jiajia Qinbc585152017-06-23 15:42:17 +08003122 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003123 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003124 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003125 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003126 return;
3127 }
3128
3129 if (mShaderVersion < 300)
3130 {
3131 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3132 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003133 return;
3134 }
3135
Olli Etuaho09b04a22016-12-15 13:30:26 +00003136 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003137
3138 if (layoutQualifier.matrixPacking != EmpUnspecified)
3139 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003140 if (typeQualifier.qualifier == EvqUniform)
3141 {
3142 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3143 }
3144 else if (typeQualifier.qualifier == EvqBuffer)
3145 {
3146 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3147 }
Martin Radev802abe02016-08-04 17:48:32 +03003148 }
3149
3150 if (layoutQualifier.blockStorage != EbsUnspecified)
3151 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003152 if (typeQualifier.qualifier == EvqUniform)
3153 {
3154 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3155 }
3156 else if (typeQualifier.qualifier == EvqBuffer)
3157 {
3158 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3159 }
Martin Radev802abe02016-08-04 17:48:32 +03003160 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003161 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003162}
3163
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003164TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3165 const TFunction &function,
3166 const TSourceLoc &location,
3167 bool insertParametersToSymbolTable)
3168{
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003169 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003170
Olli Etuahofe486322017-03-21 09:30:54 +00003171 TIntermFunctionPrototype *prototype =
3172 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003173 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3174 // point to the data that already exists in the symbol table.
3175 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3176 prototype->setLine(location);
3177
3178 for (size_t i = 0; i < function.getParamCount(); i++)
3179 {
3180 const TConstParameter &param = function.getParam(i);
3181
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003182 TIntermSymbol *symbol = nullptr;
3183
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003184 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3185 // be used for unused args).
3186 if (param.name != nullptr)
3187 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003188 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003189 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003190 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003191 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3192 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003193 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003194 symbol = new TIntermSymbol(variable->uniqueId(), variable->name(),
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003195 variable->getType());
3196 }
3197 else
3198 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003199 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003200 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003201 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003202 // Unsized type of a named parameter should have already been checked and sanitized.
3203 ASSERT(!param.type->isUnsizedArray());
3204 }
3205 else
3206 {
3207 if (param.type->isUnsizedArray())
3208 {
3209 error(location, "function parameter array must be sized at compile time", "[]");
3210 // We don't need to size the arrays since the parameter is unnamed and hence
3211 // inaccessible.
3212 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003213 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003214 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003215 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003216 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3217 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003218 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003219 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003220 symbol->setLine(location);
3221 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003222 }
3223 return prototype;
3224}
3225
Olli Etuaho16c745a2017-01-16 17:02:27 +00003226TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3227 const TFunction &parsedFunction,
3228 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003229{
Olli Etuaho476197f2016-10-11 13:59:08 +01003230 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3231 // first declaration. Either way the instance in the symbol table is used to track whether the
3232 // function is declared multiple times.
3233 TFunction *function = static_cast<TFunction *>(
3234 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3235 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003236 {
3237 // ESSL 1.00.17 section 4.2.7.
3238 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3239 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003240 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003241 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003242
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003243 TIntermFunctionPrototype *prototype =
3244 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003245
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003246 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003247
3248 if (!symbolTable.atGlobalLevel())
3249 {
3250 // ESSL 3.00.4 section 4.2.4.
3251 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003252 }
3253
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003254 return prototype;
3255}
3256
Olli Etuaho336b1472016-10-05 16:37:55 +01003257TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003258 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003259 TIntermBlock *functionBody,
3260 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003261{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003262 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003263 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3264 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003265 error(location, "function does not return a value:",
3266 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003267 }
3268
Olli Etuahof51fdd22016-10-03 10:03:40 +01003269 if (functionBody == nullptr)
3270 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003271 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003272 functionBody->setLine(location);
3273 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003274 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003275 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003276 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003277
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003278 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003279 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003280}
3281
Olli Etuaho476197f2016-10-11 13:59:08 +01003282void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3283 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003284 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003285{
Olli Etuaho476197f2016-10-11 13:59:08 +01003286 ASSERT(function);
3287 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003288 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003289 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003290
3291 if (builtIn)
3292 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003293 error(location, "built-in functions cannot be redefined", (*function)->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003294 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003295 else
Jamie Madill185fb402015-06-12 15:48:48 -04003296 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003297 TFunction *prevDec = static_cast<TFunction *>(
3298 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3299
3300 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3301 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3302 // occurance.
3303 if (*function != prevDec)
3304 {
3305 // Swap the parameters of the previous declaration to the parameters of the function
3306 // definition (parameter names may differ).
3307 prevDec->swapParameters(**function);
3308
3309 // The function definition will share the same symbol as any previous declaration.
3310 *function = prevDec;
3311 }
3312
3313 if ((*function)->isDefined())
3314 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003315 error(location, "function already has a body", (*function)->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003316 }
3317
3318 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003319 }
Jamie Madill185fb402015-06-12 15:48:48 -04003320
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003321 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003322 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003323 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003324
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003325 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003326 setLoopNestingLevel(0);
3327}
3328
Jamie Madillb98c3a82015-07-23 14:26:04 -04003329TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003330{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003331 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003332 // We don't know at this point whether this is a function definition or a prototype.
3333 // The definition production code will check for redefinitions.
3334 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003335 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003336 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3337 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003338 //
3339 TFunction *prevDec =
3340 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303341
Olli Etuahod80f2942017-11-06 12:44:45 +02003342 for (size_t i = 0u; i < function->getParamCount(); ++i)
3343 {
3344 auto &param = function->getParam(i);
3345 if (param.type->isStructSpecifier())
3346 {
3347 // ESSL 3.00.6 section 12.10.
3348 error(location, "Function parameter type cannot be a structure definition",
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003349 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003350 }
3351 }
3352
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003353 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
3354 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303355 {
Martin Radevda6254b2016-12-14 17:00:36 +02003356 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303357 // Therefore overloading or redefining builtin functions is an error.
3358 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003359 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303360 }
3361 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003362 {
3363 if (prevDec->getReturnType() != function->getReturnType())
3364 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003365 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003366 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003367 }
3368 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3369 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003370 if (prevDec->getParam(i).type->getQualifier() !=
3371 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003372 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003373 error(location,
3374 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003375 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003376 }
3377 }
3378 }
3379
3380 //
3381 // Check for previously declared variables using the same name.
3382 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003383 TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003384 if (prevSym)
3385 {
3386 if (!prevSym->isFunction())
3387 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003388 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003389 }
3390 }
3391 else
3392 {
3393 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003394 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003395 }
3396
3397 // We're at the inner scope level of the function's arguments and body statement.
3398 // Add the function prototype to the surrounding scope instead.
3399 symbolTable.getOuterLevel()->insert(function);
3400
Olli Etuaho78d13742017-01-18 13:06:10 +00003401 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003402 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003403 {
3404 if (function->getParamCount() > 0)
3405 {
3406 error(location, "function cannot take any parameter(s)", "main");
3407 }
3408 if (function->getReturnType().getBasicType() != EbtVoid)
3409 {
3410 error(location, "main function cannot return a value",
3411 function->getReturnType().getBasicString());
3412 }
3413 }
3414
Jamie Madill185fb402015-06-12 15:48:48 -04003415 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003416 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3417 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003418 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3419 //
3420 return function;
3421}
3422
Olli Etuaho9de84a52016-06-14 17:36:01 +03003423TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3424 const TString *name,
3425 const TSourceLoc &location)
3426{
3427 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3428 {
3429 error(location, "no qualifiers allowed for function return",
3430 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003431 }
3432 if (!type.layoutQualifier.isEmpty())
3433 {
3434 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003435 }
jchen10cc2a10e2017-05-03 14:05:12 +08003436 // make sure an opaque type is not involved as well...
3437 std::string reason(getBasicString(type.getBasicType()));
3438 reason += "s can't be function return values";
3439 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003440 if (mShaderVersion < 300)
3441 {
3442 // Array return values are forbidden, but there's also no valid syntax for declaring array
3443 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003444 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003445
3446 if (type.isStructureContainingArrays())
3447 {
3448 // ESSL 1.00.17 section 6.1 Function Definitions
3449 error(location, "structures containing arrays can't be function return values",
3450 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003451 }
3452 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003453
3454 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003455 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003456}
3457
Olli Etuahocce89652017-06-19 16:04:09 +03003458TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3459{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003460 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003461 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003462}
3463
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003464TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003465{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003466 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003467 {
3468 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3469 "[]");
3470 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003471 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003472 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003473 error(publicType.getLine(), "constructor can't be a structure definition",
3474 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003475 }
3476
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003477 TType *type = new TType(publicType);
3478 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003479 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003480 error(publicType.getLine(), "cannot construct this type",
3481 getBasicString(publicType.getBasicType()));
3482 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003483 }
3484
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003485 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003486}
3487
Olli Etuaho55bde912017-10-25 13:41:13 +03003488void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3489 const char *errorMessage,
3490 const char *token,
3491 TType *arrayType)
3492{
3493 if (arrayType->isUnsizedArray())
3494 {
3495 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003496 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003497 }
3498}
3499
3500TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003501 const TString *name,
3502 const TSourceLoc &nameLoc)
3503{
Olli Etuaho55bde912017-10-25 13:41:13 +03003504 ASSERT(type);
3505 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3506 type);
3507 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003508 {
3509 error(nameLoc, "illegal use of type 'void'", name->c_str());
3510 }
3511 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003512 TParameter param = {name, type};
3513 return param;
3514}
3515
Olli Etuaho55bde912017-10-25 13:41:13 +03003516TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3517 const TString *name,
3518 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003519{
Olli Etuaho55bde912017-10-25 13:41:13 +03003520 TType *type = new TType(publicType);
3521 return parseParameterDeclarator(type, name, nameLoc);
3522}
3523
3524TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3525 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003526 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003527 const TSourceLoc &arrayLoc,
3528 TPublicType *elementType)
3529{
3530 checkArrayElementIsNotArray(arrayLoc, *elementType);
3531 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003532 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003533 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003534}
3535
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003536bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3537 TType type,
3538 const TSourceLoc &line)
3539{
3540 if (arguments->empty())
3541 {
3542 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3543 return false;
3544 }
3545 for (TIntermNode *arg : *arguments)
3546 {
3547 TIntermTyped *element = arg->getAsTyped();
3548 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003549 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3550 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003551 {
3552 error(line, "constructing from a non-dereferenced array", "constructor");
3553 return false;
3554 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003555 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003556 {
3557 if (dimensionalityFromElement == 1u)
3558 {
3559 error(line, "implicitly sized array of arrays constructor argument is not an array",
3560 "constructor");
3561 }
3562 else
3563 {
3564 error(line,
3565 "implicitly sized array of arrays constructor argument dimensionality is too "
3566 "low",
3567 "constructor");
3568 }
3569 return false;
3570 }
3571 }
3572 return true;
3573}
3574
Jamie Madillb98c3a82015-07-23 14:26:04 -04003575// This function is used to test for the correctness of the parameters passed to various constructor
3576// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003577//
Olli Etuaho856c4972016-08-08 11:38:39 +03003578// 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 +00003579//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003580TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003581 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303582 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003583{
Olli Etuaho856c4972016-08-08 11:38:39 +03003584 if (type.isUnsizedArray())
3585 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003586 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003587 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003588 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003589 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003590 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003591 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3592 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003593 if (type.getOutermostArraySize() == 0u)
3594 {
3595 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3596 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003597 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003598 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003599 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003600 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003601 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003602 }
3603 }
3604 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003605 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003606
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003607 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003608 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003609 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003610 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003611
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003612 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003613 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003614
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003615 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3616 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003617 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003618 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003619 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003620 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003621}
3622
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003623//
3624// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003625// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003626//
Olli Etuaho13389b62016-10-16 11:48:18 +01003627TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003628 const TTypeQualifierBuilder &typeQualifierBuilder,
3629 const TSourceLoc &nameLine,
3630 const TString &blockName,
3631 TFieldList *fieldList,
3632 const TString *instanceName,
3633 const TSourceLoc &instanceLine,
3634 TIntermTyped *arrayIndex,
3635 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003636{
Olli Etuaho856c4972016-08-08 11:38:39 +03003637 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003638
Olli Etuaho77ba4082016-12-16 12:01:18 +00003639 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003640
Jiajia Qinbc585152017-06-23 15:42:17 +08003641 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003642 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003643 error(typeQualifier.line,
3644 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3645 "3.10",
3646 getQualifierString(typeQualifier.qualifier));
3647 }
3648 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3649 {
3650 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003651 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003652 }
3653
Martin Radev70866b82016-07-22 15:27:42 +03003654 if (typeQualifier.invariant)
3655 {
3656 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3657 }
3658
Jiajia Qinbc585152017-06-23 15:42:17 +08003659 if (typeQualifier.qualifier != EvqBuffer)
3660 {
3661 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3662 }
Olli Etuaho43364892017-02-13 16:00:12 +00003663
jchen10af713a22017-04-19 09:10:56 +08003664 // add array index
3665 unsigned int arraySize = 0;
3666 if (arrayIndex != nullptr)
3667 {
3668 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3669 }
3670
3671 if (mShaderVersion < 310)
3672 {
3673 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3674 }
3675 else
3676 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003677 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3678 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003679 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003680
Andrei Volykhina5527072017-03-22 16:46:30 +03003681 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3682
Jamie Madill099c0f32013-06-20 11:55:52 -04003683 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003684 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003685 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3686 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003687
Jamie Madill099c0f32013-06-20 11:55:52 -04003688 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3689 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003690 if (typeQualifier.qualifier == EvqUniform)
3691 {
3692 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3693 }
3694 else if (typeQualifier.qualifier == EvqBuffer)
3695 {
3696 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3697 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003698 }
3699
Jamie Madill1566ef72013-06-20 11:55:54 -04003700 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3701 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003702 if (typeQualifier.qualifier == EvqUniform)
3703 {
3704 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3705 }
3706 else if (typeQualifier.qualifier == EvqBuffer)
3707 {
3708 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3709 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003710 }
3711
Olli Etuaho856c4972016-08-08 11:38:39 +03003712 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003713
Martin Radev2cc85b32016-08-05 16:22:53 +03003714 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3715
Olli Etuaho0f684632017-07-13 12:42:15 +03003716 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303717 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003718 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003719 }
3720
Jamie Madill98493dd2013-07-08 14:39:03 -04003721 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303722 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3723 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003724 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303725 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003726 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303727 {
jchen10cc2a10e2017-05-03 14:05:12 +08003728 std::string reason("unsupported type - ");
3729 reason += fieldType->getBasicString();
3730 reason += " types are not allowed in interface blocks";
3731 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003732 }
3733
Jamie Madill98493dd2013-07-08 14:39:03 -04003734 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003735 switch (qualifier)
3736 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003737 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003738 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003739 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003740 if (typeQualifier.qualifier == EvqBuffer)
3741 {
3742 error(field->line(), "invalid qualifier on shader storage block member",
3743 getQualifierString(qualifier));
3744 }
3745 break;
3746 case EvqBuffer:
3747 if (typeQualifier.qualifier == EvqUniform)
3748 {
3749 error(field->line(), "invalid qualifier on uniform block member",
3750 getQualifierString(qualifier));
3751 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003752 break;
3753 default:
3754 error(field->line(), "invalid qualifier on interface block member",
3755 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003756 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003757 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003758
Martin Radev70866b82016-07-22 15:27:42 +03003759 if (fieldType->isInvariant())
3760 {
3761 error(field->line(), "invalid qualifier on interface block member", "invariant");
3762 }
3763
Jamie Madilla5efff92013-06-06 11:56:47 -04003764 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003765 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003766 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003767 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003768
Jamie Madill98493dd2013-07-08 14:39:03 -04003769 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003770 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003771 error(field->line(), "invalid layout qualifier: cannot be used here",
3772 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003773 }
3774
Jamie Madill98493dd2013-07-08 14:39:03 -04003775 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003776 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003777 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003778 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003779 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003780 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003781 warning(field->line(),
3782 "extraneous layout qualifier: only has an effect on matrix types",
3783 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003784 }
3785
Jamie Madill98493dd2013-07-08 14:39:03 -04003786 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003787
Olli Etuahoebee5b32017-11-23 12:56:32 +02003788 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3789 typeQualifier.qualifier != EvqBuffer)
3790 {
3791 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3792 checkIsNotUnsizedArray(field->line(),
3793 "array members of interface blocks must specify a size",
3794 field->name().c_str(), field->type());
3795 }
3796
Jiajia Qinbc585152017-06-23 15:42:17 +08003797 if (typeQualifier.qualifier == EvqBuffer)
3798 {
3799 // set memory qualifiers
3800 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3801 // qualified with a memory qualifier, it is as if all of its members were declared with
3802 // the same memory qualifier.
3803 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3804 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3805 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3806 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3807 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3808 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3809 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3810 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3811 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3812 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3813 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003814 }
3815
Jamie Madillb98c3a82015-07-23 14:26:04 -04003816 TInterfaceBlock *interfaceBlock =
Olli Etuaho12a18ad2017-12-01 16:59:47 +02003817 new TInterfaceBlock(&blockName, fieldList, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003818 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3819 if (arrayIndex != nullptr)
3820 {
3821 interfaceBlockType.makeArray(arraySize);
3822 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003823
3824 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003825 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003826
Jamie Madill98493dd2013-07-08 14:39:03 -04003827 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003828 {
3829 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003830 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3831 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003832 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303833 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003834
3835 // set parent pointer of the field variable
3836 fieldType->setInterfaceBlock(interfaceBlock);
3837
Olli Etuaho0f684632017-07-13 12:42:15 +03003838 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003839
Olli Etuaho0f684632017-07-13 12:42:15 +03003840 if (fieldVariable)
3841 {
3842 fieldVariable->setQualifier(typeQualifier.qualifier);
3843 }
3844 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303845 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003846 error(field->line(), "redefinition of an interface block member name",
3847 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003848 }
3849 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003850 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003851 }
3852 else
3853 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003854 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003855
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003856 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003857 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3858 if (instanceTypeDef)
3859 {
3860 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003861 symbolId = &instanceTypeDef->uniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003862 }
3863 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303864 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003865 error(instanceLine, "redefinition of an interface block instance name",
3866 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003867 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003868 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003869 }
3870
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003871 TIntermDeclaration *declaration = nullptr;
3872
3873 if (symbolId)
3874 {
3875 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3876 blockSymbol->setLine(typeQualifier.line);
3877 declaration = new TIntermDeclaration();
3878 declaration->appendDeclarator(blockSymbol);
3879 declaration->setLine(nameLine);
3880 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003881
3882 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003883 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003884}
3885
Olli Etuaho383b7912016-08-05 11:22:59 +03003886void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003887{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003888 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003889
3890 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003891 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303892 if (mStructNestingLevel > 1)
3893 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003894 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003895 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003896}
3897
3898void TParseContext::exitStructDeclaration()
3899{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003900 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003901}
3902
Olli Etuaho8a176262016-08-16 14:23:01 +03003903void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003904{
Jamie Madillacb4b812016-11-07 13:50:29 -05003905 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303906 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003907 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003908 }
3909
Arun Patole7e7e68d2015-05-22 12:02:25 +05303910 if (field.type()->getBasicType() != EbtStruct)
3911 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003912 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003913 }
3914
3915 // We're already inside a structure definition at this point, so add
3916 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303917 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3918 {
Jamie Madill41a49272014-03-18 16:10:13 -04003919 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003920 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3921 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003922 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003923 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003924 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003925 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003926}
3927
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003928//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003929// Parse an array index expression
3930//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003931TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3932 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303933 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003934{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003935 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3936 {
3937 if (baseExpression->getAsSymbolNode())
3938 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303939 error(location, " left of '[' is not of type array, matrix, or vector ",
3940 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003941 }
3942 else
3943 {
3944 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3945 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003946
Olli Etuaho3ec75682017-07-05 17:02:55 +03003947 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003948 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003949
Jiawei Shaod8105a02017-08-08 09:54:36 +08003950 if (baseExpression->getQualifier() == EvqPerVertexIn)
3951 {
3952 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3953 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3954 {
3955 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3956 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3957 }
3958 }
3959
Jamie Madill21c1e452014-12-29 11:33:41 -05003960 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3961
Olli Etuaho36b05142015-11-12 13:10:42 +02003962 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3963 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3964 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3965 // index is a constant expression.
3966 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3967 {
3968 if (baseExpression->isInterfaceBlock())
3969 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003970 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3971 switch (baseExpression->getQualifier())
3972 {
3973 case EvqPerVertexIn:
3974 break;
3975 case EvqUniform:
3976 case EvqBuffer:
3977 error(location,
3978 "array indexes for uniform block arrays and shader storage block arrays "
3979 "must be constant integral expressions",
3980 "[");
3981 break;
3982 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003983 // We can reach here only in error cases.
3984 ASSERT(mDiagnostics->numErrors() > 0);
3985 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003986 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003987 }
3988 else if (baseExpression->getQualifier() == EvqFragmentOut)
3989 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003990 error(location,
3991 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003992 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003993 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3994 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003995 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003996 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003997 }
3998
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003999 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04004000 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004001 // If an out-of-range index is not qualified as constant, the behavior in the spec is
4002 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
4003 // constant fold expressions that are not constant expressions). The most compatible way to
4004 // handle this case is to report a warning instead of an error and force the index to be in
4005 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004006 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03004007 int index = 0;
4008 if (indexConstantUnion->getBasicType() == EbtInt)
4009 {
4010 index = indexConstantUnion->getIConst(0);
4011 }
4012 else if (indexConstantUnion->getBasicType() == EbtUInt)
4013 {
4014 index = static_cast<int>(indexConstantUnion->getUConst(0));
4015 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004016
4017 int safeIndex = -1;
4018
Olli Etuahoebee5b32017-11-23 12:56:32 +02004019 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004020 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004021 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4022 safeIndex = 0;
4023 }
4024
4025 if (!baseExpression->getType().isUnsizedArray())
4026 {
4027 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004028 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004029 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004030 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004031 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4032 {
4033 outOfRangeError(outOfRangeIndexIsError, location,
4034 "array index for gl_FragData must be zero when "
4035 "GL_EXT_draw_buffers is disabled",
4036 "[]");
4037 safeIndex = 0;
4038 }
4039 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004040 }
4041 // Only do generic out-of-range check if similar error hasn't already been reported.
4042 if (safeIndex < 0)
4043 {
4044 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004045 {
4046 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4047 baseExpression->getOutermostArraySize(),
4048 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004049 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004050 else if (baseExpression->isMatrix())
4051 {
4052 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4053 baseExpression->getType().getCols(),
4054 "matrix field selection out of range");
4055 }
4056 else
4057 {
4058 ASSERT(baseExpression->isVector());
4059 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4060 baseExpression->getType().getNominalSize(),
4061 "vector field selection out of range");
4062 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004063 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004064
Olli Etuahoebee5b32017-11-23 12:56:32 +02004065 ASSERT(safeIndex >= 0);
4066 // Data of constant unions can't be changed, because it may be shared with other
4067 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4068 // sanitized object.
4069 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4070 {
4071 TConstantUnion *safeConstantUnion = new TConstantUnion();
4072 safeConstantUnion->setIConst(safeIndex);
4073 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4074 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4075 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004076
Olli Etuahoebee5b32017-11-23 12:56:32 +02004077 TIntermBinary *node =
4078 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4079 node->setLine(location);
4080 return node->fold(mDiagnostics);
4081 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004082 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004083
4084 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4085 node->setLine(location);
4086 // Indirect indexing can never be constant folded.
4087 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004088}
4089
Olli Etuahoebee5b32017-11-23 12:56:32 +02004090int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4091 const TSourceLoc &location,
4092 int index,
4093 int arraySize,
4094 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004095{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004096 // Should not reach here with an unsized / runtime-sized array.
4097 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004098 // A negative index should already have been checked.
4099 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004100 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004101 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004102 std::stringstream reasonStream;
4103 reasonStream << reason << " '" << index << "'";
4104 std::string token = reasonStream.str();
4105 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004106 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004107 }
4108 return index;
4109}
4110
Jamie Madillb98c3a82015-07-23 14:26:04 -04004111TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4112 const TSourceLoc &dotLocation,
4113 const TString &fieldString,
4114 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004115{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004116 if (baseExpression->isArray())
4117 {
4118 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004119 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004120 }
4121
4122 if (baseExpression->isVector())
4123 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004124 TVector<int> fieldOffsets;
4125 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4126 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004127 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004128 fieldOffsets.resize(1);
4129 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004130 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004131 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4132 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004133
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004134 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004135 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004136 else if (baseExpression->getBasicType() == EbtStruct)
4137 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304138 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004139 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004140 {
4141 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004142 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004143 }
4144 else
4145 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004146 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004147 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004148 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004149 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004150 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004151 {
4152 fieldFound = true;
4153 break;
4154 }
4155 }
4156 if (fieldFound)
4157 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004158 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004159 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004160 TIntermBinary *node =
4161 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4162 node->setLine(dotLocation);
4163 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004164 }
4165 else
4166 {
4167 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004168 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004169 }
4170 }
4171 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004172 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004173 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304174 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004175 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004176 {
4177 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004178 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004179 }
4180 else
4181 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004182 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004183 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004184 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004185 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004186 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004187 {
4188 fieldFound = true;
4189 break;
4190 }
4191 }
4192 if (fieldFound)
4193 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004194 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004195 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004196 TIntermBinary *node =
4197 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4198 node->setLine(dotLocation);
4199 // Indexing interface blocks can never be constant folded.
4200 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004201 }
4202 else
4203 {
4204 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004205 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004206 }
4207 }
4208 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004209 else
4210 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004211 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004212 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004213 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304214 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004215 }
4216 else
4217 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304218 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004219 " field selection requires structure, vector, or interface block on left hand "
4220 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304221 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004222 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004223 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004224 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004225}
4226
Jamie Madillb98c3a82015-07-23 14:26:04 -04004227TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4228 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004229{
Jamie Madill2f294c92017-11-20 14:47:26 -05004230 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004231
4232 if (qualifierType == "shared")
4233 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004234 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004235 {
4236 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4237 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004238 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004239 }
4240 else if (qualifierType == "packed")
4241 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004242 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004243 {
4244 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4245 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004246 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004247 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004248 else if (qualifierType == "std430")
4249 {
4250 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4251 qualifier.blockStorage = EbsStd430;
4252 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004253 else if (qualifierType == "std140")
4254 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004255 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004256 }
4257 else if (qualifierType == "row_major")
4258 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004259 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004260 }
4261 else if (qualifierType == "column_major")
4262 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004263 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004264 }
4265 else if (qualifierType == "location")
4266 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004267 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4268 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004269 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004270 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004271 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004272 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4273 {
4274 qualifier.yuv = true;
4275 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004276 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004277 else if (qualifierType == "rgba32f")
4278 {
4279 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4280 qualifier.imageInternalFormat = EiifRGBA32F;
4281 }
4282 else if (qualifierType == "rgba16f")
4283 {
4284 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4285 qualifier.imageInternalFormat = EiifRGBA16F;
4286 }
4287 else if (qualifierType == "r32f")
4288 {
4289 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4290 qualifier.imageInternalFormat = EiifR32F;
4291 }
4292 else if (qualifierType == "rgba8")
4293 {
4294 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4295 qualifier.imageInternalFormat = EiifRGBA8;
4296 }
4297 else if (qualifierType == "rgba8_snorm")
4298 {
4299 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4300 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4301 }
4302 else if (qualifierType == "rgba32i")
4303 {
4304 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4305 qualifier.imageInternalFormat = EiifRGBA32I;
4306 }
4307 else if (qualifierType == "rgba16i")
4308 {
4309 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4310 qualifier.imageInternalFormat = EiifRGBA16I;
4311 }
4312 else if (qualifierType == "rgba8i")
4313 {
4314 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4315 qualifier.imageInternalFormat = EiifRGBA8I;
4316 }
4317 else if (qualifierType == "r32i")
4318 {
4319 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4320 qualifier.imageInternalFormat = EiifR32I;
4321 }
4322 else if (qualifierType == "rgba32ui")
4323 {
4324 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4325 qualifier.imageInternalFormat = EiifRGBA32UI;
4326 }
4327 else if (qualifierType == "rgba16ui")
4328 {
4329 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4330 qualifier.imageInternalFormat = EiifRGBA16UI;
4331 }
4332 else if (qualifierType == "rgba8ui")
4333 {
4334 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4335 qualifier.imageInternalFormat = EiifRGBA8UI;
4336 }
4337 else if (qualifierType == "r32ui")
4338 {
4339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4340 qualifier.imageInternalFormat = EiifR32UI;
4341 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004342 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4343 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004344 {
4345 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4346 qualifier.primitiveType = EptPoints;
4347 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004348 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4349 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004350 {
4351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4352 qualifier.primitiveType = EptLines;
4353 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004354 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4355 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004356 {
4357 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4358 qualifier.primitiveType = EptLinesAdjacency;
4359 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004360 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4361 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004362 {
4363 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4364 qualifier.primitiveType = EptTriangles;
4365 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004366 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4367 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004368 {
4369 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4370 qualifier.primitiveType = EptTrianglesAdjacency;
4371 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004372 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4373 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004374 {
4375 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4376 qualifier.primitiveType = EptLineStrip;
4377 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004378 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4379 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004380 {
4381 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4382 qualifier.primitiveType = EptTriangleStrip;
4383 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004384
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004385 else
4386 {
4387 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004388 }
4389
Jamie Madilla5efff92013-06-06 11:56:47 -04004390 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004391}
4392
Martin Radev802abe02016-08-04 17:48:32 +03004393void TParseContext::parseLocalSize(const TString &qualifierType,
4394 const TSourceLoc &qualifierTypeLine,
4395 int intValue,
4396 const TSourceLoc &intValueLine,
4397 const std::string &intValueString,
4398 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004399 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004400{
Olli Etuaho856c4972016-08-08 11:38:39 +03004401 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004402 if (intValue < 1)
4403 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004404 std::stringstream reasonStream;
4405 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4406 std::string reason = reasonStream.str();
4407 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004408 }
4409 (*localSize)[index] = intValue;
4410}
4411
Olli Etuaho09b04a22016-12-15 13:30:26 +00004412void TParseContext::parseNumViews(int intValue,
4413 const TSourceLoc &intValueLine,
4414 const std::string &intValueString,
4415 int *numViews)
4416{
4417 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4418 // specification.
4419 if (intValue < 1)
4420 {
4421 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4422 }
4423 *numViews = intValue;
4424}
4425
Shaob5cc1192017-07-06 10:47:20 +08004426void TParseContext::parseInvocations(int intValue,
4427 const TSourceLoc &intValueLine,
4428 const std::string &intValueString,
4429 int *numInvocations)
4430{
4431 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4432 // it doesn't make sense to accept invocations <= 0.
4433 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4434 {
4435 error(intValueLine,
4436 "out of range: invocations must be in the range of [1, "
4437 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4438 intValueString.c_str());
4439 }
4440 else
4441 {
4442 *numInvocations = intValue;
4443 }
4444}
4445
4446void TParseContext::parseMaxVertices(int intValue,
4447 const TSourceLoc &intValueLine,
4448 const std::string &intValueString,
4449 int *maxVertices)
4450{
4451 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4452 // it doesn't make sense to accept max_vertices < 0.
4453 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4454 {
4455 error(
4456 intValueLine,
4457 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4458 intValueString.c_str());
4459 }
4460 else
4461 {
4462 *maxVertices = intValue;
4463 }
4464}
4465
Jamie Madillb98c3a82015-07-23 14:26:04 -04004466TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4467 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004468 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304469 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004470{
Jamie Madill2f294c92017-11-20 14:47:26 -05004471 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004472
Martin Radev802abe02016-08-04 17:48:32 +03004473 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004474
Martin Radev802abe02016-08-04 17:48:32 +03004475 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004476 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004477 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004478 if (intValue < 0)
4479 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004480 error(intValueLine, "out of range: location must be non-negative",
4481 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004482 }
4483 else
4484 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004485 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004486 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004487 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004488 }
Olli Etuaho43364892017-02-13 16:00:12 +00004489 else if (qualifierType == "binding")
4490 {
4491 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4492 if (intValue < 0)
4493 {
4494 error(intValueLine, "out of range: binding must be non-negative",
4495 intValueString.c_str());
4496 }
4497 else
4498 {
4499 qualifier.binding = intValue;
4500 }
4501 }
jchen104cdac9e2017-05-08 11:01:20 +08004502 else if (qualifierType == "offset")
4503 {
4504 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4505 if (intValue < 0)
4506 {
4507 error(intValueLine, "out of range: offset must be non-negative",
4508 intValueString.c_str());
4509 }
4510 else
4511 {
4512 qualifier.offset = intValue;
4513 }
4514 }
Martin Radev802abe02016-08-04 17:48:32 +03004515 else if (qualifierType == "local_size_x")
4516 {
4517 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4518 &qualifier.localSize);
4519 }
4520 else if (qualifierType == "local_size_y")
4521 {
4522 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4523 &qualifier.localSize);
4524 }
4525 else if (qualifierType == "local_size_z")
4526 {
4527 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4528 &qualifier.localSize);
4529 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004530 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004531 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004532 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4533 {
4534 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4535 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004536 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004537 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4538 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004539 {
4540 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4541 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004542 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4543 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004544 {
4545 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4546 }
4547
Martin Radev802abe02016-08-04 17:48:32 +03004548 else
4549 {
4550 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004551 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004552
Jamie Madilla5efff92013-06-06 11:56:47 -04004553 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004554}
4555
Olli Etuaho613b9592016-09-05 12:05:53 +03004556TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4557{
4558 return new TTypeQualifierBuilder(
4559 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4560 mShaderVersion);
4561}
4562
Olli Etuahocce89652017-06-19 16:04:09 +03004563TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4564 const TSourceLoc &loc)
4565{
4566 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4567 return new TStorageQualifierWrapper(qualifier, loc);
4568}
4569
4570TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4571{
4572 if (getShaderType() == GL_VERTEX_SHADER)
4573 {
4574 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4575 }
4576 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4577}
4578
4579TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4580{
4581 if (declaringFunction())
4582 {
4583 return new TStorageQualifierWrapper(EvqIn, loc);
4584 }
Shaob5cc1192017-07-06 10:47:20 +08004585
4586 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004587 {
Shaob5cc1192017-07-06 10:47:20 +08004588 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004589 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004590 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004591 {
4592 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4593 }
4594 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004595 }
Shaob5cc1192017-07-06 10:47:20 +08004596 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004597 {
Shaob5cc1192017-07-06 10:47:20 +08004598 if (mShaderVersion < 300)
4599 {
4600 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4601 }
4602 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004603 }
Shaob5cc1192017-07-06 10:47:20 +08004604 case GL_COMPUTE_SHADER:
4605 {
4606 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4607 }
4608 case GL_GEOMETRY_SHADER_OES:
4609 {
4610 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4611 }
4612 default:
4613 {
4614 UNREACHABLE();
4615 return new TStorageQualifierWrapper(EvqLast, loc);
4616 }
Olli Etuahocce89652017-06-19 16:04:09 +03004617 }
Olli Etuahocce89652017-06-19 16:04:09 +03004618}
4619
4620TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4621{
4622 if (declaringFunction())
4623 {
4624 return new TStorageQualifierWrapper(EvqOut, loc);
4625 }
Shaob5cc1192017-07-06 10:47:20 +08004626 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004627 {
Shaob5cc1192017-07-06 10:47:20 +08004628 case GL_VERTEX_SHADER:
4629 {
4630 if (mShaderVersion < 300)
4631 {
4632 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4633 }
4634 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4635 }
4636 case GL_FRAGMENT_SHADER:
4637 {
4638 if (mShaderVersion < 300)
4639 {
4640 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4641 }
4642 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4643 }
4644 case GL_COMPUTE_SHADER:
4645 {
4646 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4647 return new TStorageQualifierWrapper(EvqLast, loc);
4648 }
4649 case GL_GEOMETRY_SHADER_OES:
4650 {
4651 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4652 }
4653 default:
4654 {
4655 UNREACHABLE();
4656 return new TStorageQualifierWrapper(EvqLast, loc);
4657 }
Olli Etuahocce89652017-06-19 16:04:09 +03004658 }
Olli Etuahocce89652017-06-19 16:04:09 +03004659}
4660
4661TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4662{
4663 if (!declaringFunction())
4664 {
4665 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4666 }
4667 return new TStorageQualifierWrapper(EvqInOut, loc);
4668}
4669
Jamie Madillb98c3a82015-07-23 14:26:04 -04004670TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004671 TLayoutQualifier rightQualifier,
4672 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004673{
Martin Radevc28888b2016-07-22 15:27:42 +03004674 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004675 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004676}
4677
Olli Etuahod5f44c92017-11-29 17:15:40 +02004678TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004679{
4680 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004681 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004682}
4683
Olli Etuahod5f44c92017-11-29 17:15:40 +02004684TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4685 const TSourceLoc &loc,
4686 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004687{
4688 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004689 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004690}
4691
Olli Etuaho722bfb52017-10-26 17:00:11 +03004692void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4693 const TFieldList::const_iterator end,
4694 const TString &name,
4695 const TSourceLoc &location)
4696{
4697 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4698 {
4699 if ((*fieldIter)->name() == name)
4700 {
4701 error(location, "duplicate field name in structure", name.c_str());
4702 }
4703 }
4704}
4705
4706TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4707{
4708 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4709 ++fieldIter)
4710 {
4711 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4712 location);
4713 }
4714 return fields;
4715}
4716
Olli Etuaho4de340a2016-12-16 09:32:03 +00004717TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4718 const TFieldList *newlyAddedFields,
4719 const TSourceLoc &location)
4720{
4721 for (TField *field : *newlyAddedFields)
4722 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004723 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4724 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004725 processedFields->push_back(field);
4726 }
4727 return processedFields;
4728}
4729
Martin Radev70866b82016-07-22 15:27:42 +03004730TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4731 const TTypeQualifierBuilder &typeQualifierBuilder,
4732 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004733 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004734{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004735 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004736
Martin Radev70866b82016-07-22 15:27:42 +03004737 typeSpecifier->qualifier = typeQualifier.qualifier;
4738 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004739 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004740 typeSpecifier->invariant = typeQualifier.invariant;
4741 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304742 {
Martin Radev70866b82016-07-22 15:27:42 +03004743 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004744 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004745 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004746}
4747
Jamie Madillb98c3a82015-07-23 14:26:04 -04004748TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004749 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004750{
Martin Radev4a9cd802016-09-01 16:51:51 +03004751 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4752 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004753
Olli Etuahod5f44c92017-11-29 17:15:40 +02004754 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004755 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004756
Martin Radev4a9cd802016-09-01 16:51:51 +03004757 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004758
Olli Etuahod5f44c92017-11-29 17:15:40 +02004759 TFieldList *fieldList = new TFieldList();
4760
4761 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304762 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004763 TType *type = new TType(typeSpecifier);
4764 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304765 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004766 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004767 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004768 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004769 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004770
Olli Etuahod5f44c92017-11-29 17:15:40 +02004771 TField *field = new TField(type, declarator->name(), declarator->line());
4772 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4773 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004774 }
4775
Olli Etuahod5f44c92017-11-29 17:15:40 +02004776 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004777}
4778
Martin Radev4a9cd802016-09-01 16:51:51 +03004779TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4780 const TSourceLoc &nameLine,
4781 const TString *structName,
4782 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004783{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004784 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004785
Jamie Madill9b820842015-02-12 10:40:10 -05004786 // Store a bool in the struct if we're at global scope, to allow us to
4787 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004788 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004789
Jamie Madill98493dd2013-07-08 14:39:03 -04004790 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004791 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004792 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004793 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304794 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004795 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004796 }
4797 }
4798
4799 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004800 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004801 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004802 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004803 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004804 switch (qualifier)
4805 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004806 case EvqGlobal:
4807 case EvqTemporary:
4808 break;
4809 default:
4810 error(field.line(), "invalid qualifier on struct member",
4811 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004812 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004813 }
Martin Radev70866b82016-07-22 15:27:42 +03004814 if (field.type()->isInvariant())
4815 {
4816 error(field.line(), "invalid qualifier on struct member", "invariant");
4817 }
jchen104cdac9e2017-05-08 11:01:20 +08004818 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4819 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004820 {
4821 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4822 }
4823
Olli Etuahoebee5b32017-11-23 12:56:32 +02004824 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4825 field.name().c_str(), field.type());
4826
Olli Etuaho43364892017-02-13 16:00:12 +00004827 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4828
4829 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004830
4831 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004832 }
4833
Martin Radev4a9cd802016-09-01 16:51:51 +03004834 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004835 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004836 exitStructDeclaration();
4837
Martin Radev4a9cd802016-09-01 16:51:51 +03004838 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004839}
4840
Jamie Madillb98c3a82015-07-23 14:26:04 -04004841TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004842 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004843 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004844{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004845 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004846 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004847 init->isVector())
4848 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004849 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4850 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004851 return nullptr;
4852 }
4853
Olli Etuaho923ecef2017-10-11 12:01:38 +03004854 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004855 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004856 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004857 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004858 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004859 }
4860
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004861 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4862 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004863 return node;
4864}
4865
4866TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4867{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004868 if (mSwitchNestingLevel == 0)
4869 {
4870 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004871 return nullptr;
4872 }
4873 if (condition == nullptr)
4874 {
4875 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004876 return nullptr;
4877 }
4878 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004879 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004880 {
4881 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004882 }
4883 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004884 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4885 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4886 // fold in case labels.
4887 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004888 {
4889 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004890 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004891 TIntermCase *node = new TIntermCase(condition);
4892 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004893 return node;
4894}
4895
4896TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4897{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004898 if (mSwitchNestingLevel == 0)
4899 {
4900 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004901 return nullptr;
4902 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004903 TIntermCase *node = new TIntermCase(nullptr);
4904 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004905 return node;
4906}
4907
Jamie Madillb98c3a82015-07-23 14:26:04 -04004908TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4909 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004910 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004911{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004912 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004913
4914 switch (op)
4915 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004916 case EOpLogicalNot:
4917 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4918 child->isVector())
4919 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004920 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004921 return nullptr;
4922 }
4923 break;
4924 case EOpBitwiseNot:
4925 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4926 child->isMatrix() || child->isArray())
4927 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004928 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004929 return nullptr;
4930 }
4931 break;
4932 case EOpPostIncrement:
4933 case EOpPreIncrement:
4934 case EOpPostDecrement:
4935 case EOpPreDecrement:
4936 case EOpNegative:
4937 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004938 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4939 child->getBasicType() == EbtBool || child->isArray() ||
4940 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004941 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004942 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004943 return nullptr;
4944 }
4945 // Operators for built-ins are already type checked against their prototype.
4946 default:
4947 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004948 }
4949
Jiajia Qinbc585152017-06-23 15:42:17 +08004950 if (child->getMemoryQualifier().writeonly)
4951 {
4952 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4953 return nullptr;
4954 }
4955
Olli Etuahof119a262016-08-19 15:54:22 +03004956 TIntermUnary *node = new TIntermUnary(op, child);
4957 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004958
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004959 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004960}
4961
Olli Etuaho09b22472015-02-11 11:47:26 +02004962TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4963{
Olli Etuahocce89652017-06-19 16:04:09 +03004964 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004965 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004966 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004967 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004968 return child;
4969 }
4970 return node;
4971}
4972
Jamie Madillb98c3a82015-07-23 14:26:04 -04004973TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4974 TIntermTyped *child,
4975 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004976{
Olli Etuaho856c4972016-08-08 11:38:39 +03004977 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004978 return addUnaryMath(op, child, loc);
4979}
4980
Jamie Madillb98c3a82015-07-23 14:26:04 -04004981bool TParseContext::binaryOpCommonCheck(TOperator op,
4982 TIntermTyped *left,
4983 TIntermTyped *right,
4984 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004985{
jchen10b4cf5652017-05-05 18:51:17 +08004986 // Check opaque types are not allowed to be operands in expressions other than array indexing
4987 // and structure member selection.
4988 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4989 {
4990 switch (op)
4991 {
4992 case EOpIndexDirect:
4993 case EOpIndexIndirect:
4994 break;
4995 case EOpIndexDirectStruct:
4996 UNREACHABLE();
4997
4998 default:
4999 error(loc, "Invalid operation for variables with an opaque type",
5000 GetOperatorString(op));
5001 return false;
5002 }
5003 }
jchen10cc2a10e2017-05-03 14:05:12 +08005004
Jiajia Qinbc585152017-06-23 15:42:17 +08005005 if (right->getMemoryQualifier().writeonly)
5006 {
5007 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5008 return false;
5009 }
5010
5011 if (left->getMemoryQualifier().writeonly)
5012 {
5013 switch (op)
5014 {
5015 case EOpAssign:
5016 case EOpInitialize:
5017 case EOpIndexDirect:
5018 case EOpIndexIndirect:
5019 case EOpIndexDirectStruct:
5020 case EOpIndexDirectInterfaceBlock:
5021 break;
5022 default:
5023 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5024 return false;
5025 }
5026 }
5027
Olli Etuaho244be012016-08-18 15:26:02 +03005028 if (left->getType().getStruct() || right->getType().getStruct())
5029 {
5030 switch (op)
5031 {
5032 case EOpIndexDirectStruct:
5033 ASSERT(left->getType().getStruct());
5034 break;
5035 case EOpEqual:
5036 case EOpNotEqual:
5037 case EOpAssign:
5038 case EOpInitialize:
5039 if (left->getType() != right->getType())
5040 {
5041 return false;
5042 }
5043 break;
5044 default:
5045 error(loc, "Invalid operation for structs", GetOperatorString(op));
5046 return false;
5047 }
5048 }
5049
Olli Etuaho94050052017-05-08 14:17:44 +03005050 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5051 {
5052 switch (op)
5053 {
5054 case EOpIndexDirectInterfaceBlock:
5055 ASSERT(left->getType().getInterfaceBlock());
5056 break;
5057 default:
5058 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5059 return false;
5060 }
5061 }
5062
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005063 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005064 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005065 error(loc, "array / non-array mismatch", GetOperatorString(op));
5066 return false;
5067 }
5068
5069 if (left->isArray())
5070 {
5071 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005072 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005073 {
5074 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5075 return false;
5076 }
5077
Olli Etuahoe79904c2015-03-18 16:56:42 +02005078 switch (op)
5079 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005080 case EOpEqual:
5081 case EOpNotEqual:
5082 case EOpAssign:
5083 case EOpInitialize:
5084 break;
5085 default:
5086 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5087 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005088 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005089 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005090 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005091 {
5092 error(loc, "array size mismatch", GetOperatorString(op));
5093 return false;
5094 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005095 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005096
5097 // Check ops which require integer / ivec parameters
5098 bool isBitShift = false;
5099 switch (op)
5100 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005101 case EOpBitShiftLeft:
5102 case EOpBitShiftRight:
5103 case EOpBitShiftLeftAssign:
5104 case EOpBitShiftRightAssign:
5105 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5106 // check that the basic type is an integer type.
5107 isBitShift = true;
5108 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5109 {
5110 return false;
5111 }
5112 break;
5113 case EOpBitwiseAnd:
5114 case EOpBitwiseXor:
5115 case EOpBitwiseOr:
5116 case EOpBitwiseAndAssign:
5117 case EOpBitwiseXorAssign:
5118 case EOpBitwiseOrAssign:
5119 // It is enough to check the type of only one operand, since later it
5120 // is checked that the operand types match.
5121 if (!IsInteger(left->getBasicType()))
5122 {
5123 return false;
5124 }
5125 break;
5126 default:
5127 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005128 }
5129
5130 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5131 // So the basic type should usually match.
5132 if (!isBitShift && left->getBasicType() != right->getBasicType())
5133 {
5134 return false;
5135 }
5136
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005137 // Check that:
5138 // 1. Type sizes match exactly on ops that require that.
5139 // 2. Restrictions for structs that contain arrays or samplers are respected.
5140 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005141 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005142 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005143 case EOpAssign:
5144 case EOpInitialize:
5145 case EOpEqual:
5146 case EOpNotEqual:
5147 // ESSL 1.00 sections 5.7, 5.8, 5.9
5148 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5149 {
5150 error(loc, "undefined operation for structs containing arrays",
5151 GetOperatorString(op));
5152 return false;
5153 }
5154 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5155 // we interpret the spec so that this extends to structs containing samplers,
5156 // similarly to ESSL 1.00 spec.
5157 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5158 left->getType().isStructureContainingSamplers())
5159 {
5160 error(loc, "undefined operation for structs containing samplers",
5161 GetOperatorString(op));
5162 return false;
5163 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005164
Olli Etuahoe1805592017-01-02 16:41:20 +00005165 if ((left->getNominalSize() != right->getNominalSize()) ||
5166 (left->getSecondarySize() != right->getSecondarySize()))
5167 {
5168 error(loc, "dimension mismatch", GetOperatorString(op));
5169 return false;
5170 }
5171 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005172 case EOpLessThan:
5173 case EOpGreaterThan:
5174 case EOpLessThanEqual:
5175 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005176 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005177 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005178 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005179 return false;
5180 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005181 break;
5182 case EOpAdd:
5183 case EOpSub:
5184 case EOpDiv:
5185 case EOpIMod:
5186 case EOpBitShiftLeft:
5187 case EOpBitShiftRight:
5188 case EOpBitwiseAnd:
5189 case EOpBitwiseXor:
5190 case EOpBitwiseOr:
5191 case EOpAddAssign:
5192 case EOpSubAssign:
5193 case EOpDivAssign:
5194 case EOpIModAssign:
5195 case EOpBitShiftLeftAssign:
5196 case EOpBitShiftRightAssign:
5197 case EOpBitwiseAndAssign:
5198 case EOpBitwiseXorAssign:
5199 case EOpBitwiseOrAssign:
5200 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5201 {
5202 return false;
5203 }
5204
5205 // Are the sizes compatible?
5206 if (left->getNominalSize() != right->getNominalSize() ||
5207 left->getSecondarySize() != right->getSecondarySize())
5208 {
5209 // If the nominal sizes of operands do not match:
5210 // One of them must be a scalar.
5211 if (!left->isScalar() && !right->isScalar())
5212 return false;
5213
5214 // In the case of compound assignment other than multiply-assign,
5215 // the right side needs to be a scalar. Otherwise a vector/matrix
5216 // would be assigned to a scalar. A scalar can't be shifted by a
5217 // vector either.
5218 if (!right->isScalar() &&
5219 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5220 return false;
5221 }
5222 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005223 default:
5224 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005225 }
5226
Olli Etuahod6b14282015-03-17 14:31:35 +02005227 return true;
5228}
5229
Olli Etuaho1dded802016-08-18 18:13:13 +03005230bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5231 const TType &left,
5232 const TType &right)
5233{
5234 switch (op)
5235 {
5236 case EOpMul:
5237 case EOpMulAssign:
5238 return left.getNominalSize() == right.getNominalSize() &&
5239 left.getSecondarySize() == right.getSecondarySize();
5240 case EOpVectorTimesScalar:
5241 return true;
5242 case EOpVectorTimesScalarAssign:
5243 ASSERT(!left.isMatrix() && !right.isMatrix());
5244 return left.isVector() && !right.isVector();
5245 case EOpVectorTimesMatrix:
5246 return left.getNominalSize() == right.getRows();
5247 case EOpVectorTimesMatrixAssign:
5248 ASSERT(!left.isMatrix() && right.isMatrix());
5249 return left.isVector() && left.getNominalSize() == right.getRows() &&
5250 left.getNominalSize() == right.getCols();
5251 case EOpMatrixTimesVector:
5252 return left.getCols() == right.getNominalSize();
5253 case EOpMatrixTimesScalar:
5254 return true;
5255 case EOpMatrixTimesScalarAssign:
5256 ASSERT(left.isMatrix() && !right.isMatrix());
5257 return !right.isVector();
5258 case EOpMatrixTimesMatrix:
5259 return left.getCols() == right.getRows();
5260 case EOpMatrixTimesMatrixAssign:
5261 ASSERT(left.isMatrix() && right.isMatrix());
5262 // We need to check two things:
5263 // 1. The matrix multiplication step is valid.
5264 // 2. The result will have the same number of columns as the lvalue.
5265 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5266
5267 default:
5268 UNREACHABLE();
5269 return false;
5270 }
5271}
5272
Jamie Madillb98c3a82015-07-23 14:26:04 -04005273TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5274 TIntermTyped *left,
5275 TIntermTyped *right,
5276 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005277{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005278 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005279 return nullptr;
5280
Olli Etuahofc1806e2015-03-17 13:03:11 +02005281 switch (op)
5282 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005283 case EOpEqual:
5284 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005285 case EOpLessThan:
5286 case EOpGreaterThan:
5287 case EOpLessThanEqual:
5288 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005289 break;
5290 case EOpLogicalOr:
5291 case EOpLogicalXor:
5292 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005293 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5294 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005295 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005296 {
5297 return nullptr;
5298 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005299 // Basic types matching should have been already checked.
5300 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005301 break;
5302 case EOpAdd:
5303 case EOpSub:
5304 case EOpDiv:
5305 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005306 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5307 !right->getType().getStruct());
5308 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005309 {
5310 return nullptr;
5311 }
5312 break;
5313 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005314 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5315 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005316 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005317 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005318 {
5319 return nullptr;
5320 }
5321 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005322 default:
5323 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005324 }
5325
Olli Etuaho1dded802016-08-18 18:13:13 +03005326 if (op == EOpMul)
5327 {
5328 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5329 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5330 {
5331 return nullptr;
5332 }
5333 }
5334
Olli Etuaho3fdec912016-08-18 15:08:06 +03005335 TIntermBinary *node = new TIntermBinary(op, left, right);
5336 node->setLine(loc);
5337
Olli Etuaho3fdec912016-08-18 15:08:06 +03005338 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005339 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005340}
5341
Jamie Madillb98c3a82015-07-23 14:26:04 -04005342TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5343 TIntermTyped *left,
5344 TIntermTyped *right,
5345 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005346{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005347 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005348 if (node == 0)
5349 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005350 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5351 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005352 return left;
5353 }
5354 return node;
5355}
5356
Jamie Madillb98c3a82015-07-23 14:26:04 -04005357TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5358 TIntermTyped *left,
5359 TIntermTyped *right,
5360 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005361{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005362 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005363 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005364 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005365 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5366 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005367 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005368 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005369 }
5370 return node;
5371}
5372
Olli Etuaho13389b62016-10-16 11:48:18 +01005373TIntermBinary *TParseContext::createAssign(TOperator op,
5374 TIntermTyped *left,
5375 TIntermTyped *right,
5376 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005377{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005378 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005379 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005380 if (op == EOpMulAssign)
5381 {
5382 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5383 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5384 {
5385 return nullptr;
5386 }
5387 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005388 TIntermBinary *node = new TIntermBinary(op, left, right);
5389 node->setLine(loc);
5390
Olli Etuaho3fdec912016-08-18 15:08:06 +03005391 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005392 }
5393 return nullptr;
5394}
5395
Jamie Madillb98c3a82015-07-23 14:26:04 -04005396TIntermTyped *TParseContext::addAssign(TOperator op,
5397 TIntermTyped *left,
5398 TIntermTyped *right,
5399 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005400{
Olli Etuahocce89652017-06-19 16:04:09 +03005401 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005402 TIntermTyped *node = createAssign(op, left, right, loc);
5403 if (node == nullptr)
5404 {
5405 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005406 return left;
5407 }
5408 return node;
5409}
5410
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005411TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5412 TIntermTyped *right,
5413 const TSourceLoc &loc)
5414{
Corentin Wallez0d959252016-07-12 17:26:32 -04005415 // WebGL2 section 5.26, the following results in an error:
5416 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005417 if (mShaderSpec == SH_WEBGL2_SPEC &&
5418 (left->isArray() || left->getBasicType() == EbtVoid ||
5419 left->getType().isStructureContainingArrays() || right->isArray() ||
5420 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005421 {
5422 error(loc,
5423 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5424 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005425 }
5426
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005427 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5428 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5429 commaNode->getTypePointer()->setQualifier(resultQualifier);
5430 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005431}
5432
Olli Etuaho49300862015-02-20 14:54:49 +02005433TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5434{
5435 switch (op)
5436 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005437 case EOpContinue:
5438 if (mLoopNestingLevel <= 0)
5439 {
5440 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005441 }
5442 break;
5443 case EOpBreak:
5444 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5445 {
5446 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005447 }
5448 break;
5449 case EOpReturn:
5450 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5451 {
5452 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005453 }
5454 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005455 case EOpKill:
5456 if (mShaderType != GL_FRAGMENT_SHADER)
5457 {
5458 error(loc, "discard supported in fragment shaders only", "discard");
5459 }
5460 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005461 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005462 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005463 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005464 }
Olli Etuahocce89652017-06-19 16:04:09 +03005465 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005466}
5467
Jamie Madillb98c3a82015-07-23 14:26:04 -04005468TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005469 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005470 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005471{
Olli Etuahocce89652017-06-19 16:04:09 +03005472 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005473 {
Olli Etuahocce89652017-06-19 16:04:09 +03005474 ASSERT(op == EOpReturn);
5475 mFunctionReturnsValue = true;
5476 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5477 {
5478 error(loc, "void function cannot return a value", "return");
5479 }
5480 else if (*mCurrentFunctionType != expression->getType())
5481 {
5482 error(loc, "function return is not matching type:", "return");
5483 }
Olli Etuaho49300862015-02-20 14:54:49 +02005484 }
Olli Etuahocce89652017-06-19 16:04:09 +03005485 TIntermBranch *node = new TIntermBranch(op, expression);
5486 node->setLine(loc);
5487 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005488}
5489
Martin Radev84aa2dc2017-09-11 15:51:02 +03005490void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5491{
5492 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5493 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5494 bool isTextureGather = (name == "textureGather");
5495 bool isTextureGatherOffset = (name == "textureGatherOffset");
5496 if (isTextureGather || isTextureGatherOffset)
5497 {
5498 TIntermNode *componentNode = nullptr;
5499 TIntermSequence *arguments = functionCall->getSequence();
5500 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5501 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5502 ASSERT(sampler != nullptr);
5503 switch (sampler->getBasicType())
5504 {
5505 case EbtSampler2D:
5506 case EbtISampler2D:
5507 case EbtUSampler2D:
5508 case EbtSampler2DArray:
5509 case EbtISampler2DArray:
5510 case EbtUSampler2DArray:
5511 if ((isTextureGather && arguments->size() == 3u) ||
5512 (isTextureGatherOffset && arguments->size() == 4u))
5513 {
5514 componentNode = arguments->back();
5515 }
5516 break;
5517 case EbtSamplerCube:
5518 case EbtISamplerCube:
5519 case EbtUSamplerCube:
5520 ASSERT(!isTextureGatherOffset);
5521 if (arguments->size() == 3u)
5522 {
5523 componentNode = arguments->back();
5524 }
5525 break;
5526 case EbtSampler2DShadow:
5527 case EbtSampler2DArrayShadow:
5528 case EbtSamplerCubeShadow:
5529 break;
5530 default:
5531 UNREACHABLE();
5532 break;
5533 }
5534 if (componentNode)
5535 {
5536 const TIntermConstantUnion *componentConstantUnion =
5537 componentNode->getAsConstantUnion();
5538 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5539 {
5540 error(functionCall->getLine(), "Texture component must be a constant expression",
5541 name.c_str());
5542 }
5543 else
5544 {
5545 int component = componentConstantUnion->getIConst(0);
5546 if (component < 0 || component > 3)
5547 {
5548 error(functionCall->getLine(), "Component must be in the range [0;3]",
5549 name.c_str());
5550 }
5551 }
5552 }
5553 }
5554}
5555
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005556void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5557{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005558 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005559 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005560 TIntermNode *offset = nullptr;
5561 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005562 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005563 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5564 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5565 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005566 {
5567 offset = arguments->back();
5568 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005569 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005570 {
5571 // A bias parameter might follow the offset parameter.
5572 ASSERT(arguments->size() >= 3);
5573 offset = (*arguments)[2];
5574 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005575 else if (name == "textureGatherOffset")
5576 {
5577 ASSERT(arguments->size() >= 3u);
5578 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5579 ASSERT(sampler != nullptr);
5580 switch (sampler->getBasicType())
5581 {
5582 case EbtSampler2D:
5583 case EbtISampler2D:
5584 case EbtUSampler2D:
5585 case EbtSampler2DArray:
5586 case EbtISampler2DArray:
5587 case EbtUSampler2DArray:
5588 offset = (*arguments)[2];
5589 break;
5590 case EbtSampler2DShadow:
5591 case EbtSampler2DArrayShadow:
5592 offset = (*arguments)[3];
5593 break;
5594 default:
5595 UNREACHABLE();
5596 break;
5597 }
5598 useTextureGatherOffsetConstraints = true;
5599 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005600 if (offset != nullptr)
5601 {
5602 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5603 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5604 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005605 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005606 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005607 }
5608 else
5609 {
5610 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5611 size_t size = offsetConstantUnion->getType().getObjectSize();
5612 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005613 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5614 : mMinProgramTexelOffset;
5615 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5616 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005617 for (size_t i = 0u; i < size; ++i)
5618 {
5619 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005620 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005621 {
5622 std::stringstream tokenStream;
5623 tokenStream << offsetValue;
5624 std::string token = tokenStream.str();
5625 error(offset->getLine(), "Texture offset value out of valid range",
5626 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005627 }
5628 }
5629 }
5630 }
5631}
5632
Jiajia Qina3106c52017-11-03 09:39:39 +08005633void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5634{
5635 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5636 if (IsAtomicBuiltin(name))
5637 {
5638 TIntermSequence *arguments = functionCall->getSequence();
5639 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5640
5641 if (IsBufferOrSharedVariable(memNode))
5642 {
5643 return;
5644 }
5645
5646 while (memNode->getAsBinaryNode())
5647 {
5648 memNode = memNode->getAsBinaryNode()->getLeft();
5649 if (IsBufferOrSharedVariable(memNode))
5650 {
5651 return;
5652 }
5653 }
5654
5655 error(memNode->getLine(),
5656 "The value passed to the mem argument of an atomic memory function does not "
5657 "correspond to a buffer or shared variable.",
5658 functionCall->getFunctionSymbolInfo()->getName().c_str());
5659 }
5660}
5661
Martin Radev2cc85b32016-08-05 16:22:53 +03005662// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5663void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5664{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005665 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005666 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5667
5668 if (name.compare(0, 5, "image") == 0)
5669 {
5670 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005671 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005672
Olli Etuaho485eefd2017-02-14 17:40:06 +00005673 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005674
5675 if (name.compare(5, 5, "Store") == 0)
5676 {
5677 if (memoryQualifier.readonly)
5678 {
5679 error(imageNode->getLine(),
5680 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005681 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005682 }
5683 }
5684 else if (name.compare(5, 4, "Load") == 0)
5685 {
5686 if (memoryQualifier.writeonly)
5687 {
5688 error(imageNode->getLine(),
5689 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005690 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005691 }
5692 }
5693 }
5694}
5695
5696// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5697void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5698 const TFunction *functionDefinition,
5699 const TIntermAggregate *functionCall)
5700{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005701 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005702
5703 const TIntermSequence &arguments = *functionCall->getSequence();
5704
5705 ASSERT(functionDefinition->getParamCount() == arguments.size());
5706
5707 for (size_t i = 0; i < arguments.size(); ++i)
5708 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005709 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5710 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005711 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5712 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5713
5714 if (IsImage(functionArgumentType.getBasicType()))
5715 {
5716 const TMemoryQualifier &functionArgumentMemoryQualifier =
5717 functionArgumentType.getMemoryQualifier();
5718 const TMemoryQualifier &functionParameterMemoryQualifier =
5719 functionParameterType.getMemoryQualifier();
5720 if (functionArgumentMemoryQualifier.readonly &&
5721 !functionParameterMemoryQualifier.readonly)
5722 {
5723 error(functionCall->getLine(),
5724 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005725 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005726 }
5727
5728 if (functionArgumentMemoryQualifier.writeonly &&
5729 !functionParameterMemoryQualifier.writeonly)
5730 {
5731 error(functionCall->getLine(),
5732 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005733 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005734 }
Martin Radev049edfa2016-11-11 14:35:37 +02005735
5736 if (functionArgumentMemoryQualifier.coherent &&
5737 !functionParameterMemoryQualifier.coherent)
5738 {
5739 error(functionCall->getLine(),
5740 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005741 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005742 }
5743
5744 if (functionArgumentMemoryQualifier.volatileQualifier &&
5745 !functionParameterMemoryQualifier.volatileQualifier)
5746 {
5747 error(functionCall->getLine(),
5748 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005749 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005750 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005751 }
5752 }
5753}
5754
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005755TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005756{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005757 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005758}
5759
5760TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005761 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005762 TIntermNode *thisNode,
5763 const TSourceLoc &loc)
5764{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005765 if (thisNode != nullptr)
5766 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005767 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005768 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005769
5770 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005771 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005772 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005773 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005774 }
5775 else
5776 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005777 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005778 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5779 }
5780}
5781
5782TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5783 TIntermSequence *arguments,
5784 TIntermNode *thisNode,
5785 const TSourceLoc &loc)
5786{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005787 TIntermTyped *typedThis = thisNode->getAsTyped();
5788 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5789 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5790 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5791 // So accessing fnCall->getName() below is safe.
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005792 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005793 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005794 error(loc, "invalid method", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005795 }
5796 else if (!arguments->empty())
5797 {
5798 error(loc, "method takes no parameters", "length");
5799 }
5800 else if (typedThis == nullptr || !typedThis->isArray())
5801 {
5802 error(loc, "length can only be called on arrays", "length");
5803 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005804 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5805 mGeometryShaderInputPrimitiveType == EptUndefined)
5806 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005807 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005808 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5809 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005810 else
5811 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005812 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5813 node->setLine(loc);
5814 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005815 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005816 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005817}
5818
5819TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5820 TIntermSequence *arguments,
5821 const TSourceLoc &loc)
5822{
5823 // First find by unmangled name to check whether the function name has been
5824 // hidden by a variable name or struct typename.
5825 // If a function is found, check for one with a matching argument list.
5826 bool builtIn;
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005827 const TSymbol *symbol = symbolTable.find(fnCall->name(), mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005828 if (symbol != nullptr && !symbol->isFunction())
5829 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005830 error(loc, "function name expected", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005831 }
5832 else
5833 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005834 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->name(), *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005835 mShaderVersion, &builtIn);
5836 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005837 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005838 error(loc, "no matching overloaded function found", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005839 }
5840 else
5841 {
5842 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005843 //
5844 // A declared function.
5845 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005846 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005847 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005848 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005849 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005850 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005851 if (builtIn && op != EOpNull)
5852 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005853 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005854 if (fnCandidate->getParamCount() == 1)
5855 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005856 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005857 TIntermNode *unaryParamNode = arguments->front();
5858 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005859 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005860 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005861 }
5862 else
5863 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005864 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005865 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005866 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005867
5868 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005869 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305870
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005871 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305872 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005873 // See if we can constant fold a built-in. Note that this may be possible
5874 // even if it is not const-qualified.
5875 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305876 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005877 else
5878 {
5879 return callNode;
5880 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005881 }
5882 }
5883 else
5884 {
5885 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005886 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005887
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005888 // If builtIn == false, the function is user defined - could be an overloaded
5889 // built-in as well.
5890 // if builtIn == true, it's a builtIn function with no op associated with it.
5891 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005892 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005893 {
Olli Etuahofe486322017-03-21 09:30:54 +00005894 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005895 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005896 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005897 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005898 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005899 }
5900 else
5901 {
Olli Etuahofe486322017-03-21 09:30:54 +00005902 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005903 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005904 }
5905
Jiajia Qinbc585152017-06-23 15:42:17 +08005906 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005907
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005908 callNode->setLine(loc);
5909
5910 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005911 }
5912 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005913 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005914
5915 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005916 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005917}
5918
Jamie Madillb98c3a82015-07-23 14:26:04 -04005919TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005920 TIntermTyped *trueExpression,
5921 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005922 const TSourceLoc &loc)
5923{
Olli Etuaho56229f12017-07-10 14:16:33 +03005924 if (!checkIsScalarBool(loc, cond))
5925 {
5926 return falseExpression;
5927 }
Olli Etuaho52901742015-04-15 13:42:45 +03005928
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005929 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005930 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005931 std::stringstream reasonStream;
5932 reasonStream << "mismatching ternary operator operand types '"
5933 << trueExpression->getCompleteString() << " and '"
5934 << falseExpression->getCompleteString() << "'";
5935 std::string reason = reasonStream.str();
5936 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005937 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005938 }
Olli Etuahode318b22016-10-25 16:18:25 +01005939 if (IsOpaqueType(trueExpression->getBasicType()))
5940 {
5941 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005942 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005943 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5944 // Note that structs containing opaque types don't need to be checked as structs are
5945 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005946 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005947 return falseExpression;
5948 }
5949
Jiajia Qinbc585152017-06-23 15:42:17 +08005950 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5951 falseExpression->getMemoryQualifier().writeonly)
5952 {
5953 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5954 return falseExpression;
5955 }
5956
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005957 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005958 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005959 // ESSL 3.00.6 section 5.7:
5960 // Ternary operator support is optional for arrays. No certainty that it works across all
5961 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5962 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005963 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005964 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005965 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005966 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005967 }
Olli Etuaho94050052017-05-08 14:17:44 +03005968 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5969 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005970 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005971 return falseExpression;
5972 }
5973
Corentin Wallez0d959252016-07-12 17:26:32 -04005974 // WebGL2 section 5.26, the following results in an error:
5975 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005976 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005977 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005978 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005979 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005980 }
5981
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005982 // Note that the node resulting from here can be a constant union without being qualified as
5983 // constant.
5984 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5985 node->setLine(loc);
5986
5987 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005988}
Olli Etuaho49300862015-02-20 14:54:49 +02005989
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005990//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005991// Parse an array of strings using yyparse.
5992//
5993// Returns 0 for success.
5994//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005995int PaParseStrings(size_t count,
5996 const char *const string[],
5997 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305998 TParseContext *context)
5999{
Yunchao He4f285442017-04-21 12:15:49 +08006000 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006001 return 1;
6002
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006003 if (glslang_initialize(context))
6004 return 1;
6005
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006006 int error = glslang_scan(count, string, length, context);
6007 if (!error)
6008 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006009
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006010 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006011
alokp@chromium.org6b495712012-06-29 00:06:58 +00006012 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006013}
Jamie Madill45bcc782016-11-07 13:58:48 -05006014
6015} // namespace sh