blob: 6274cf79e3645ac48151131bc4d8fc61ea4c65f8 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
jchen104cdac9e2017-05-08 11:01:20 +080012#include "common/mathutil.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
Olli Etuahod5f44c92017-11-29 17:15:40 +020014#include "compiler/translator/Declarator.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Kai Ninomiya614dd0f2017-11-22 14:04:48 -080016#include "compiler/translator/StaticType.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030017#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080018#include "compiler/translator/ValidateSwitch.h"
19#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030020#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021
Jamie Madill45bcc782016-11-07 13:58:48 -050022namespace sh
23{
24
alokp@chromium.org8b851c62012-06-15 16:25:11 +000025///////////////////////////////////////////////////////////////////////
26//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027// Sub- vector and matrix fields
28//
29////////////////////////////////////////////////////////////////////////
30
Martin Radev2cc85b32016-08-05 16:22:53 +030031namespace
32{
33
34const int kWebGLMaxStructNesting = 4;
35
Jiajia Qina3106c52017-11-03 09:39:39 +080036const std::array<const char *, 8> kAtomicBuiltin = {{"atomicAdd", "atomicMin", "atomicMax",
37 "atomicAnd", "atomicOr", "atomicXor",
38 "atomicExchange", "atomicCompSwap"}};
39
40bool IsAtomicBuiltin(const TString &name)
41{
42 for (size_t i = 0; i < kAtomicBuiltin.size(); ++i)
43 {
44 if (name.compare(kAtomicBuiltin[i]) == 0)
45 {
46 return true;
47 }
48 }
49 return false;
50}
51
Olli Etuaho0f684632017-07-13 12:42:15 +030052bool ContainsSampler(const TStructure *structType);
53
Martin Radev2cc85b32016-08-05 16:22:53 +030054bool ContainsSampler(const TType &type)
55{
56 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030057 {
Martin Radev2cc85b32016-08-05 16:22:53 +030058 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030059 }
jchen10cc2a10e2017-05-03 14:05:12 +080060 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030061 {
Olli Etuaho0f684632017-07-13 12:42:15 +030062 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030063 }
64
65 return false;
66}
67
Olli Etuaho0f684632017-07-13 12:42:15 +030068bool ContainsSampler(const TStructure *structType)
69{
70 for (const auto &field : structType->fields())
71 {
72 if (ContainsSampler(*field->type()))
73 return true;
74 }
75 return false;
76}
77
Olli Etuaho485eefd2017-02-14 17:40:06 +000078// Get a token from an image argument to use as an error message token.
79const char *GetImageArgumentToken(TIntermTyped *imageNode)
80{
81 ASSERT(IsImage(imageNode->getBasicType()));
82 while (imageNode->getAsBinaryNode() &&
83 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
84 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
85 {
86 imageNode = imageNode->getAsBinaryNode()->getLeft();
87 }
88 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
89 if (imageSymbol)
90 {
91 return imageSymbol->getSymbol().c_str();
92 }
93 return "image";
94}
95
Olli Etuahocce89652017-06-19 16:04:09 +030096bool CanSetDefaultPrecisionOnType(const TPublicType &type)
97{
98 if (!SupportsPrecision(type.getBasicType()))
99 {
100 return false;
101 }
102 if (type.getBasicType() == EbtUInt)
103 {
104 // ESSL 3.00.4 section 4.5.4
105 return false;
106 }
107 if (type.isAggregate())
108 {
109 // Not allowed to set for aggregate types
110 return false;
111 }
112 return true;
113}
114
Jiawei Shaod8105a02017-08-08 09:54:36 +0800115// Map input primitive types to input array sizes in a geometry shader.
116GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
117{
118 switch (primitiveType)
119 {
120 case EptPoints:
121 return 1u;
122 case EptLines:
123 return 2u;
124 case EptTriangles:
125 return 3u;
126 case EptLinesAdjacency:
127 return 4u;
128 case EptTrianglesAdjacency:
129 return 6u;
130 default:
131 UNREACHABLE();
132 return 0u;
133 }
134}
135
Jiajia Qina3106c52017-11-03 09:39:39 +0800136bool IsBufferOrSharedVariable(TIntermTyped *var)
137{
138 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
139 var->getQualifier() == EvqShared)
140 {
141 return true;
142 }
143 return false;
144}
145
Martin Radev2cc85b32016-08-05 16:22:53 +0300146} // namespace
147
jchen104cdac9e2017-05-08 11:01:20 +0800148// This tracks each binding point's current default offset for inheritance of subsequent
149// variables using the same binding, and keeps offsets unique and non overlapping.
150// See GLSL ES 3.1, section 4.4.6.
151class TParseContext::AtomicCounterBindingState
152{
153 public:
154 AtomicCounterBindingState() : mDefaultOffset(0) {}
155 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
156 // newly inserted span.
157 int insertSpan(int start, size_t length)
158 {
159 gl::RangeI newSpan(start, start + static_cast<int>(length));
160 for (const auto &span : mSpans)
161 {
162 if (newSpan.intersects(span))
163 {
164 return -1;
165 }
166 }
167 mSpans.push_back(newSpan);
168 mDefaultOffset = newSpan.high();
169 return start;
170 }
171 // Inserts a new span starting from the default offset.
172 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
173 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
174
175 private:
176 int mDefaultOffset;
177 std::vector<gl::RangeI> mSpans;
178};
179
Jamie Madillacb4b812016-11-07 13:50:29 -0500180TParseContext::TParseContext(TSymbolTable &symt,
181 TExtensionBehavior &ext,
182 sh::GLenum type,
183 ShShaderSpec spec,
184 ShCompileOptions options,
185 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000186 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500187 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300188 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300189 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500190 mShaderType(type),
191 mShaderSpec(spec),
192 mCompileOptions(options),
193 mShaderVersion(100),
194 mTreeRoot(nullptr),
195 mLoopNestingLevel(0),
196 mStructNestingLevel(0),
197 mSwitchNestingLevel(0),
198 mCurrentFunctionType(nullptr),
199 mFunctionReturnsValue(false),
200 mChecksPrecisionErrors(checksPrecErrors),
201 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800202 mDefaultUniformMatrixPacking(EmpColumnMajor),
203 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
204 mDefaultBufferMatrixPacking(EmpColumnMajor),
205 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000206 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500207 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000208 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500209 mShaderVersion,
210 mShaderType,
211 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000212 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500213 mScanner(nullptr),
214 mUsesFragData(false),
215 mUsesFragColor(false),
216 mUsesSecondaryOutputs(false),
217 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
218 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300219 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
220 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500221 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500222 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000223 mNumViews(-1),
224 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000225 mMaxImageUnits(resources.MaxImageUnits),
226 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000227 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800228 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800229 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800230 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800231 mDeclaringFunction(false),
232 mGeometryShaderInputPrimitiveType(EptUndefined),
233 mGeometryShaderOutputPrimitiveType(EptUndefined),
234 mGeometryShaderInvocations(0),
235 mGeometryShaderMaxVertices(-1),
236 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800237 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800238 mGeometryShaderInputArraySize(0u)
Jamie Madillacb4b812016-11-07 13:50:29 -0500239{
Jamie Madillacb4b812016-11-07 13:50:29 -0500240}
241
jchen104cdac9e2017-05-08 11:01:20 +0800242TParseContext::~TParseContext()
243{
244}
245
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300246bool TParseContext::parseVectorFields(const TSourceLoc &line,
247 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400248 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300249 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300251 ASSERT(fieldOffsets);
252 size_t fieldCount = compString.size();
253 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530254 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000255 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000256 return false;
257 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300258 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259
Jamie Madillb98c3a82015-07-23 14:26:04 -0400260 enum
261 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000262 exyzw,
263 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000264 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000265 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300267 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530268 {
269 switch (compString[i])
270 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400271 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300272 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400273 fieldSet[i] = exyzw;
274 break;
275 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300276 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 fieldSet[i] = ergba;
278 break;
279 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300280 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400281 fieldSet[i] = estpq;
282 break;
283 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300284 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400285 fieldSet[i] = exyzw;
286 break;
287 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300288 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400289 fieldSet[i] = ergba;
290 break;
291 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300292 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400293 fieldSet[i] = estpq;
294 break;
295 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300296 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400297 fieldSet[i] = exyzw;
298 break;
299 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300300 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400301 fieldSet[i] = ergba;
302 break;
303 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300304 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400305 fieldSet[i] = estpq;
306 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307
Jamie Madillb98c3a82015-07-23 14:26:04 -0400308 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300309 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400310 fieldSet[i] = exyzw;
311 break;
312 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300313 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400314 fieldSet[i] = ergba;
315 break;
316 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300317 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400318 fieldSet[i] = estpq;
319 break;
320 default:
321 error(line, "illegal vector field selection", compString.c_str());
322 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 }
324 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300326 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530327 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300328 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530329 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400330 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000331 return false;
332 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
Arun Patole7e7e68d2015-05-22 12:02:25 +0530334 if (i > 0)
335 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400336 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530337 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400338 error(line, "illegal - vector component fields not from the same set",
339 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000340 return false;
341 }
342 }
343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000345 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346}
347
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348///////////////////////////////////////////////////////////////////////
349//
350// Errors
351//
352////////////////////////////////////////////////////////////////////////
353
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354//
355// Used by flex/bison to output all syntax and parsing errors.
356//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000357void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000359 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
Olli Etuaho4de340a2016-12-16 09:32:03 +0000362void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530363{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000364 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000365}
366
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200367void TParseContext::outOfRangeError(bool isError,
368 const TSourceLoc &loc,
369 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000370 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200371{
372 if (isError)
373 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000374 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200375 }
376 else
377 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000378 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200379 }
380}
381
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382//
383// Same error message for all places assignments don't work.
384//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530385void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000387 std::stringstream reasonStream;
388 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
389 std::string reason = reasonStream.str();
390 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391}
392
393//
394// Same error message for all places unary operations don't work.
395//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530396void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000398 std::stringstream reasonStream;
399 reasonStream << "wrong operand type - no operation '" << op
400 << "' exists that takes an operand of type " << operand
401 << " (or there is no acceptable conversion)";
402 std::string reason = reasonStream.str();
403 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Same error message for all binary operations don't work.
408//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400409void TParseContext::binaryOpError(const TSourceLoc &line,
410 const char *op,
411 TString left,
412 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000414 std::stringstream reasonStream;
415 reasonStream << "wrong operand types - no operation '" << op
416 << "' exists that takes a left-hand operand of type '" << left
417 << "' and a right operand of type '" << right
418 << "' (or there is no acceptable conversion)";
419 std::string reason = reasonStream.str();
420 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
Olli Etuaho856c4972016-08-08 11:38:39 +0300423void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
424 TPrecision precision,
425 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530426{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400427 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300428 return;
Martin Radev70866b82016-07-22 15:27:42 +0300429
430 if (precision != EbpUndefined && !SupportsPrecision(type))
431 {
432 error(line, "illegal type for precision qualifier", getBasicString(type));
433 }
434
Olli Etuaho183d7e22015-11-20 15:59:09 +0200435 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530436 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200437 switch (type)
438 {
439 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400440 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300441 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200442 case EbtInt:
443 case EbtUInt:
444 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400445 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300446 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200447 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800448 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200449 {
jchen10cc2a10e2017-05-03 14:05:12 +0800450 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300451 return;
452 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200453 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000454 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000455}
456
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457// Both test and if necessary, spit out an error, to see if the node is really
458// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300459bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500461 TIntermSymbol *symNode = node->getAsSymbolNode();
462 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100463 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
464
465 if (swizzleNode)
466 {
467 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
468 if (ok && swizzleNode->hasDuplicateOffsets())
469 {
470 error(line, " l-value of swizzle cannot have duplicate components", op);
471 return false;
472 }
473 return ok;
474 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475
Arun Patole7e7e68d2015-05-22 12:02:25 +0530476 if (binaryNode)
477 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530479 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400480 case EOpIndexDirect:
481 case EOpIndexIndirect:
482 case EOpIndexDirectStruct:
483 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300484 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400485 default:
486 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000487 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000488 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300489 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000490 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491
jchen10cc2a10e2017-05-03 14:05:12 +0800492 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530493 switch (node->getQualifier())
494 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400495 case EvqConst:
496 message = "can't modify a const";
497 break;
498 case EvqConstReadOnly:
499 message = "can't modify a const";
500 break;
501 case EvqAttribute:
502 message = "can't modify an attribute";
503 break;
504 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400505 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800506 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800507 case EvqFlatIn:
508 case EvqSmoothIn:
509 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400510 message = "can't modify an input";
511 break;
512 case EvqUniform:
513 message = "can't modify a uniform";
514 break;
515 case EvqVaryingIn:
516 message = "can't modify a varying";
517 break;
518 case EvqFragCoord:
519 message = "can't modify gl_FragCoord";
520 break;
521 case EvqFrontFacing:
522 message = "can't modify gl_FrontFacing";
523 break;
524 case EvqPointCoord:
525 message = "can't modify gl_PointCoord";
526 break;
Martin Radevb0883602016-08-04 17:48:58 +0300527 case EvqNumWorkGroups:
528 message = "can't modify gl_NumWorkGroups";
529 break;
530 case EvqWorkGroupSize:
531 message = "can't modify gl_WorkGroupSize";
532 break;
533 case EvqWorkGroupID:
534 message = "can't modify gl_WorkGroupID";
535 break;
536 case EvqLocalInvocationID:
537 message = "can't modify gl_LocalInvocationID";
538 break;
539 case EvqGlobalInvocationID:
540 message = "can't modify gl_GlobalInvocationID";
541 break;
542 case EvqLocalInvocationIndex:
543 message = "can't modify gl_LocalInvocationIndex";
544 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300545 case EvqViewIDOVR:
546 message = "can't modify gl_ViewID_OVR";
547 break;
Martin Radev802abe02016-08-04 17:48:32 +0300548 case EvqComputeIn:
549 message = "can't modify work group size variable";
550 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800551 case EvqPerVertexIn:
552 message = "can't modify any member in gl_in";
553 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800554 case EvqPrimitiveIDIn:
555 message = "can't modify gl_PrimitiveIDIn";
556 break;
557 case EvqInvocationID:
558 message = "can't modify gl_InvocationID";
559 break;
560 case EvqPrimitiveID:
561 if (mShaderType == GL_FRAGMENT_SHADER)
562 {
563 message = "can't modify gl_PrimitiveID in a fragment shader";
564 }
565 break;
566 case EvqLayer:
567 if (mShaderType == GL_FRAGMENT_SHADER)
568 {
569 message = "can't modify gl_Layer in a fragment shader";
570 }
571 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400572 default:
573 //
574 // Type that can't be written to?
575 //
576 if (node->getBasicType() == EbtVoid)
577 {
578 message = "can't modify void";
579 }
jchen10cc2a10e2017-05-03 14:05:12 +0800580 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400581 {
jchen10cc2a10e2017-05-03 14:05:12 +0800582 message = "can't modify a variable with type ";
583 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300584 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800585 else if (node->getMemoryQualifier().readonly)
586 {
587 message = "can't modify a readonly variable";
588 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590
jchen10cc2a10e2017-05-03 14:05:12 +0800591 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530592 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000593 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594
Olli Etuaho8a176262016-08-16 14:23:01 +0300595 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 //
599 // Everything else is okay, no error.
600 //
jchen10cc2a10e2017-05-03 14:05:12 +0800601 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300602 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 //
605 // If we get here, we have an error and a message.
606 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530607 if (symNode)
608 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000609 const char *symbol = symNode->getSymbol().c_str();
610 std::stringstream reasonStream;
611 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
612 std::string reason = reasonStream.str();
613 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000614 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 else
616 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000617 std::stringstream reasonStream;
618 reasonStream << "l-value required (" << message << ")";
619 std::string reason = reasonStream.str();
620 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000621 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622
Olli Etuaho8a176262016-08-16 14:23:01 +0300623 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624}
625
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626// Both test, and if necessary spit out an error, to see if the node is really
627// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300628void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629{
Olli Etuaho383b7912016-08-05 11:22:59 +0300630 if (node->getQualifier() != EvqConst)
631 {
632 error(node->getLine(), "constant expression required", "");
633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634}
635
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636// Both test, and if necessary spit out an error, to see if the node is really
637// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300638void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639{
Olli Etuaho383b7912016-08-05 11:22:59 +0300640 if (!node->isScalarInt())
641 {
642 error(node->getLine(), "integer expression required", token);
643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644}
645
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646// Both test, and if necessary spit out an error, to see if we are currently
647// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800648bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649{
Olli Etuaho856c4972016-08-08 11:38:39 +0300650 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300651 {
652 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800653 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300654 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800655 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000656}
657
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300658// ESSL 3.00.5 sections 3.8 and 3.9.
659// If it starts "gl_" or contains two consecutive underscores, it's reserved.
660// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300661bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300664 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530665 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300666 error(line, reservedErrMsg, "gl_");
667 return false;
668 }
669 if (sh::IsWebGLBasedSpec(mShaderSpec))
670 {
671 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300673 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300674 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300676 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530677 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300678 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300679 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 }
681 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300682 if (identifier.find("__") != TString::npos)
683 {
684 error(line,
685 "identifiers containing two consecutive underscores (__) are reserved as "
686 "possible future keywords",
687 identifier.c_str());
688 return false;
689 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300690 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691}
692
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300693// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300694bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800695 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300696 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000697{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800698 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530699 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200700 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300701 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200703
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300704 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530705 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300706 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200707 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300708 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200709 {
jchen10cc2a10e2017-05-03 14:05:12 +0800710 std::string reason("cannot convert a variable with type ");
711 reason += getBasicString(argTyped->getBasicType());
712 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300713 return false;
714 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800715 else if (argTyped->getMemoryQualifier().writeonly)
716 {
717 error(line, "cannot convert a variable with writeonly", "constructor");
718 return false;
719 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200720 if (argTyped->getBasicType() == EbtVoid)
721 {
722 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300723 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200724 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000725 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726
Olli Etuaho856c4972016-08-08 11:38:39 +0300727 if (type.isArray())
728 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300729 // The size of an unsized constructor should already have been determined.
730 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300731 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300732 {
733 error(line, "array constructor needs one argument per array element", "constructor");
734 return false;
735 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300736 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
737 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800738 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300739 {
740 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300741 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500742 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300743 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500744 return false;
745 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300746 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300747 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000748 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300749 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300750 }
751 }
752 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300753 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300754 {
755 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300756 if (fields.size() != arguments->size())
757 {
758 error(line,
759 "Number of constructor parameters does not match the number of structure fields",
760 "constructor");
761 return false;
762 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300763
764 for (size_t i = 0; i < fields.size(); i++)
765 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800766 if (i >= arguments->size() ||
767 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300768 {
769 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000770 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300771 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300772 }
773 }
774 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300775 else
776 {
777 // We're constructing a scalar, vector, or matrix.
778
779 // Note: It's okay to have too many components available, but not okay to have unused
780 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
781 // there is an extra argument, so 'overFull' will become true.
782
783 size_t size = 0;
784 bool full = false;
785 bool overFull = false;
786 bool matrixArg = false;
787 for (TIntermNode *arg : *arguments)
788 {
789 const TIntermTyped *argTyped = arg->getAsTyped();
790 ASSERT(argTyped != nullptr);
791
Olli Etuaho487b63a2017-05-23 15:55:09 +0300792 if (argTyped->getBasicType() == EbtStruct)
793 {
794 error(line, "a struct cannot be used as a constructor argument for this type",
795 "constructor");
796 return false;
797 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300798 if (argTyped->getType().isArray())
799 {
800 error(line, "constructing from a non-dereferenced array", "constructor");
801 return false;
802 }
803 if (argTyped->getType().isMatrix())
804 {
805 matrixArg = true;
806 }
807
808 size += argTyped->getType().getObjectSize();
809 if (full)
810 {
811 overFull = true;
812 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300813 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300814 {
815 full = true;
816 }
817 }
818
819 if (type.isMatrix() && matrixArg)
820 {
821 if (arguments->size() != 1)
822 {
823 error(line, "constructing matrix from matrix can only take one argument",
824 "constructor");
825 return false;
826 }
827 }
828 else
829 {
830 if (size != 1 && size < type.getObjectSize())
831 {
832 error(line, "not enough data provided for construction", "constructor");
833 return false;
834 }
835 if (overFull)
836 {
837 error(line, "too many arguments", "constructor");
838 return false;
839 }
840 }
841 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300842
Olli Etuaho8a176262016-08-16 14:23:01 +0300843 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844}
845
Jamie Madillb98c3a82015-07-23 14:26:04 -0400846// This function checks to see if a void variable has been declared and raise an error message for
847// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848//
849// returns true in case of an error
850//
Olli Etuaho856c4972016-08-08 11:38:39 +0300851bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400852 const TString &identifier,
853 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300855 if (type == EbtVoid)
856 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000857 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300858 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300859 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860
Olli Etuaho8a176262016-08-16 14:23:01 +0300861 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
Jamie Madillb98c3a82015-07-23 14:26:04 -0400864// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300865// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300866bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300868 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530869 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000870 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300871 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530872 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300873 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874}
875
Jamie Madillb98c3a82015-07-23 14:26:04 -0400876// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300877// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300878void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879{
Martin Radev4a9cd802016-09-01 16:51:51 +0300880 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530881 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000882 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530883 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884}
885
jchen10cc2a10e2017-05-03 14:05:12 +0800886bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
887 const TTypeSpecifierNonArray &pType,
888 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530890 if (pType.type == EbtStruct)
891 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300892 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530893 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000894 std::stringstream reasonStream;
895 reasonStream << reason << " (structure contains a sampler)";
896 std::string reasonStr = reasonStream.str();
897 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300898 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000899 }
jchen10cc2a10e2017-05-03 14:05:12 +0800900 // only samplers need to be checked from structs, since other opaque types can't be struct
901 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300902 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530903 }
jchen10cc2a10e2017-05-03 14:05:12 +0800904 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530905 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000906 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300907 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000908 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909
Olli Etuaho8a176262016-08-16 14:23:01 +0300910 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911}
912
Olli Etuaho856c4972016-08-08 11:38:39 +0300913void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
914 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400915{
916 if (pType.layoutQualifier.location != -1)
917 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400918 error(line, "location must only be specified for a single input or output variable",
919 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400920 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400921}
922
Olli Etuaho856c4972016-08-08 11:38:39 +0300923void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
924 const TLayoutQualifier &layoutQualifier)
925{
926 if (layoutQualifier.location != -1)
927 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000928 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
929 if (mShaderVersion >= 310)
930 {
931 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800932 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000933 }
934 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300935 }
936}
937
Qin Jiajiaca68d982017-09-18 16:41:56 +0800938void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
939 const TLayoutBlockStorage &blockStorage,
940 const TQualifier &qualifier)
941{
942 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
943 {
944 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
945 }
946}
947
Martin Radev2cc85b32016-08-05 16:22:53 +0300948void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
949 TQualifier qualifier,
950 const TType &type)
951{
Martin Radev2cc85b32016-08-05 16:22:53 +0300952 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800953 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530954 {
jchen10cc2a10e2017-05-03 14:05:12 +0800955 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000956 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957}
958
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300960unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530962 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000963
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200964 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
965 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
966 // fold as array size.
967 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000968 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000969 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300970 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000971 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
Olli Etuaho856c4972016-08-08 11:38:39 +0300973 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400974
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000975 if (constant->getBasicType() == EbtUInt)
976 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300977 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000978 }
979 else
980 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300981 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000982
Olli Etuaho856c4972016-08-08 11:38:39 +0300983 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000984 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400985 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300986 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000987 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400988
Olli Etuaho856c4972016-08-08 11:38:39 +0300989 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400990 }
991
Olli Etuaho856c4972016-08-08 11:38:39 +0300992 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400993 {
994 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300995 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400996 }
997
998 // The size of arrays is restricted here to prevent issues further down the
999 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1000 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1001 const unsigned int sizeLimit = 65536;
1002
Olli Etuaho856c4972016-08-08 11:38:39 +03001003 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001004 {
1005 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001006 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001007 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001008
1009 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010}
1011
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001013bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1014 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015{
Olli Etuaho8a176262016-08-16 14:23:01 +03001016 if ((elementQualifier.qualifier == EvqAttribute) ||
1017 (elementQualifier.qualifier == EvqVertexIn) ||
1018 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001020 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001021 TType(elementQualifier).getQualifierString());
1022 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024
Olli Etuaho8a176262016-08-16 14:23:01 +03001025 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026}
1027
Olli Etuaho8a176262016-08-16 14:23:01 +03001028// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001029bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1030 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001032 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001033 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001034 error(line, "cannot declare arrays of arrays",
1035 TType(elementType).getCompleteString().c_str());
1036 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001037 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001038 return true;
1039}
1040
1041// Check if this qualified element type can be formed into an array. This is only called when array
1042// brackets are associated with an identifier in a declaration, like this:
1043// float a[2];
1044// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1045// are associated with the type, like this:
1046// float[2] a;
1047bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1048 const TPublicType &elementType)
1049{
1050 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1051 {
1052 return false;
1053 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001054 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1055 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1056 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001057 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001058 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001059 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001060 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001061 TType(elementType).getCompleteString().c_str());
1062 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001063 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001064 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065}
1066
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001068void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1069 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001070 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071{
Olli Etuaho3739d232015-04-08 12:23:44 +03001072 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001073 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001074 {
1075 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001076 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001077
1078 // Generate informative error messages for ESSL1.
1079 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001080 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001081 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301082 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001083 "structures containing arrays may not be declared constant since they cannot be "
1084 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301085 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001086 }
1087 else
1088 {
1089 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1090 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001092 // This will make the type sized if it isn't sized yet.
1093 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1094 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095}
1096
Olli Etuaho2935c582015-04-08 14:32:06 +03001097// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098// and update the symbol table.
1099//
Olli Etuaho2935c582015-04-08 14:32:06 +03001100// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001102bool TParseContext::declareVariable(const TSourceLoc &line,
1103 const TString &identifier,
1104 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001105 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106{
Olli Etuaho2935c582015-04-08 14:32:06 +03001107 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108
Olli Etuaho195be942017-12-04 23:40:14 +02001109 (*variable) = new TVariable(&symbolTable, &identifier, type, SymbolType::UserDefined);
1110
Olli Etuaho43364892017-02-13 16:00:12 +00001111 checkBindingIsValid(line, type);
1112
Olli Etuaho856c4972016-08-08 11:38:39 +03001113 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114
Olli Etuaho2935c582015-04-08 14:32:06 +03001115 // gl_LastFragData may be redeclared with a new precision qualifier
1116 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1117 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001118 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1119 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001120 if (type.isArrayOfArrays())
1121 {
1122 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1123 identifier.c_str());
1124 return false;
1125 }
1126 else if (static_cast<int>(type.getOutermostArraySize()) ==
1127 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001128 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001129 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001130 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001131 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001132 }
1133 }
1134 else
1135 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001136 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1137 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001138 return false;
1139 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141
Olli Etuaho8a176262016-08-16 14:23:01 +03001142 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001143 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001144
Olli Etuaho195be942017-12-04 23:40:14 +02001145 if (!symbolTable.declareVariable(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001146 {
1147 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001148 return false;
1149 }
1150
Olli Etuaho8a176262016-08-16 14:23:01 +03001151 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001152 return false;
1153
1154 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001155}
1156
Martin Radev70866b82016-07-22 15:27:42 +03001157void TParseContext::checkIsParameterQualifierValid(
1158 const TSourceLoc &line,
1159 const TTypeQualifierBuilder &typeQualifierBuilder,
1160 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301161{
Olli Etuahocce89652017-06-19 16:04:09 +03001162 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001163 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001164
1165 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301166 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001167 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1168 }
1169
1170 if (!IsImage(type->getBasicType()))
1171 {
Olli Etuaho43364892017-02-13 16:00:12 +00001172 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001173 }
1174 else
1175 {
1176 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001177 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001178
Martin Radev70866b82016-07-22 15:27:42 +03001179 type->setQualifier(typeQualifier.qualifier);
1180
1181 if (typeQualifier.precision != EbpUndefined)
1182 {
1183 type->setPrecision(typeQualifier.precision);
1184 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185}
1186
Olli Etuaho703671e2017-11-08 17:47:18 +02001187template <size_t size>
1188bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1189 const std::array<TExtension, size> &extensions)
1190{
1191 ASSERT(!extensions.empty());
1192 const TExtensionBehavior &extBehavior = extensionBehavior();
1193
1194 bool canUseWithWarning = false;
1195 bool canUseWithoutWarning = false;
1196
1197 const char *errorMsgString = "";
1198 TExtension errorMsgExtension = TExtension::UNDEFINED;
1199
1200 for (TExtension extension : extensions)
1201 {
1202 auto extIter = extBehavior.find(extension);
1203 if (canUseWithWarning)
1204 {
1205 // We already have an extension that we can use, but with a warning.
1206 // See if we can use the alternative extension without a warning.
1207 if (extIter == extBehavior.end())
1208 {
1209 continue;
1210 }
1211 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1212 {
1213 canUseWithoutWarning = true;
1214 break;
1215 }
1216 continue;
1217 }
1218 if (extIter == extBehavior.end())
1219 {
1220 errorMsgString = "extension is not supported";
1221 errorMsgExtension = extension;
1222 }
1223 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1224 {
1225 errorMsgString = "extension is disabled";
1226 errorMsgExtension = extension;
1227 }
1228 else if (extIter->second == EBhWarn)
1229 {
1230 errorMsgExtension = extension;
1231 canUseWithWarning = true;
1232 }
1233 else
1234 {
1235 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1236 canUseWithoutWarning = true;
1237 break;
1238 }
1239 }
1240
1241 if (canUseWithoutWarning)
1242 {
1243 return true;
1244 }
1245 if (canUseWithWarning)
1246 {
1247 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1248 return true;
1249 }
1250 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1251 return false;
1252}
1253
1254template bool TParseContext::checkCanUseOneOfExtensions(
1255 const TSourceLoc &line,
1256 const std::array<TExtension, 1> &extensions);
1257template bool TParseContext::checkCanUseOneOfExtensions(
1258 const TSourceLoc &line,
1259 const std::array<TExtension, 2> &extensions);
1260template bool TParseContext::checkCanUseOneOfExtensions(
1261 const TSourceLoc &line,
1262 const std::array<TExtension, 3> &extensions);
1263
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001264bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001265{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001266 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001267 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268}
1269
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001270// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1271// compile-time or link-time errors are the same whether or not the declaration is empty".
1272// This function implements all the checks that are done on qualifiers regardless of if the
1273// declaration is empty.
1274void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1275 const sh::TLayoutQualifier &layoutQualifier,
1276 const TSourceLoc &location)
1277{
1278 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1279 {
1280 error(location, "Shared memory declarations cannot have layout specified", "layout");
1281 }
1282
1283 if (layoutQualifier.matrixPacking != EmpUnspecified)
1284 {
1285 error(location, "layout qualifier only valid for interface blocks",
1286 getMatrixPackingString(layoutQualifier.matrixPacking));
1287 return;
1288 }
1289
1290 if (layoutQualifier.blockStorage != EbsUnspecified)
1291 {
1292 error(location, "layout qualifier only valid for interface blocks",
1293 getBlockStorageString(layoutQualifier.blockStorage));
1294 return;
1295 }
1296
1297 if (qualifier == EvqFragmentOut)
1298 {
1299 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1300 {
1301 error(location, "invalid layout qualifier combination", "yuv");
1302 return;
1303 }
1304 }
1305 else
1306 {
1307 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1308 }
1309
Olli Etuaho95468d12017-05-04 11:14:34 +03001310 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1311 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001312 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1313 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001314 {
1315 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1316 }
1317
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001318 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001319 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001320 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001321 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001322 // We're not checking whether the uniform location is in range here since that depends on
1323 // the type of the variable.
1324 // The type can only be fully determined for non-empty declarations.
1325 }
1326 if (!canHaveLocation)
1327 {
1328 checkLocationIsNotSpecified(location, layoutQualifier);
1329 }
1330}
1331
jchen104cdac9e2017-05-08 11:01:20 +08001332void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1333 const TSourceLoc &location)
1334{
1335 if (publicType.precision != EbpHigh)
1336 {
1337 error(location, "Can only be highp", "atomic counter");
1338 }
1339 // dEQP enforces compile error if location is specified. See uniform_location.test.
1340 if (publicType.layoutQualifier.location != -1)
1341 {
1342 error(location, "location must not be set for atomic_uint", "layout");
1343 }
1344 if (publicType.layoutQualifier.binding == -1)
1345 {
1346 error(location, "no binding specified", "atomic counter");
1347 }
1348}
1349
Olli Etuaho55bde912017-10-25 13:41:13 +03001350void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001351{
Olli Etuaho55bde912017-10-25 13:41:13 +03001352 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001353 {
1354 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1355 // error. It is assumed that this applies to empty declarations as well.
1356 error(location, "empty array declaration needs to specify a size", "");
1357 }
Martin Radevb8b01222016-11-20 23:25:53 +02001358}
1359
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001360// These checks are done for all declarations that are non-empty. They're done for non-empty
1361// declarations starting a declarator list, and declarators that follow an empty declaration.
1362void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1363 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001364{
Olli Etuahofa33d582015-04-09 14:33:12 +03001365 switch (publicType.qualifier)
1366 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001367 case EvqVaryingIn:
1368 case EvqVaryingOut:
1369 case EvqAttribute:
1370 case EvqVertexIn:
1371 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001372 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001373 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001374 {
1375 error(identifierLocation, "cannot be used with a structure",
1376 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001377 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001378 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001379 break;
1380 case EvqBuffer:
1381 if (publicType.getBasicType() != EbtInterfaceBlock)
1382 {
1383 error(identifierLocation,
1384 "cannot declare buffer variables at global scope(outside a block)",
1385 getQualifierString(publicType.qualifier));
1386 return;
1387 }
1388 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001389 default:
1390 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001391 }
jchen10cc2a10e2017-05-03 14:05:12 +08001392 std::string reason(getBasicString(publicType.getBasicType()));
1393 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001394 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001395 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001396 {
1397 return;
1398 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001399
Andrei Volykhina5527072017-03-22 16:46:30 +03001400 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1401 publicType.qualifier != EvqConst) &&
1402 publicType.getBasicType() == EbtYuvCscStandardEXT)
1403 {
1404 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1405 getQualifierString(publicType.qualifier));
1406 return;
1407 }
1408
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001409 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1410 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001411 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1412 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001413 TType type(publicType);
1414 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001415 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001416 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1417 publicType.layoutQualifier);
1418 }
1419 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001420
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001421 // check for layout qualifier issues
1422 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001423
Martin Radev2cc85b32016-08-05 16:22:53 +03001424 if (IsImage(publicType.getBasicType()))
1425 {
1426
1427 switch (layoutQualifier.imageInternalFormat)
1428 {
1429 case EiifRGBA32F:
1430 case EiifRGBA16F:
1431 case EiifR32F:
1432 case EiifRGBA8:
1433 case EiifRGBA8_SNORM:
1434 if (!IsFloatImage(publicType.getBasicType()))
1435 {
1436 error(identifierLocation,
1437 "internal image format requires a floating image type",
1438 getBasicString(publicType.getBasicType()));
1439 return;
1440 }
1441 break;
1442 case EiifRGBA32I:
1443 case EiifRGBA16I:
1444 case EiifRGBA8I:
1445 case EiifR32I:
1446 if (!IsIntegerImage(publicType.getBasicType()))
1447 {
1448 error(identifierLocation,
1449 "internal image format requires an integer image type",
1450 getBasicString(publicType.getBasicType()));
1451 return;
1452 }
1453 break;
1454 case EiifRGBA32UI:
1455 case EiifRGBA16UI:
1456 case EiifRGBA8UI:
1457 case EiifR32UI:
1458 if (!IsUnsignedImage(publicType.getBasicType()))
1459 {
1460 error(identifierLocation,
1461 "internal image format requires an unsigned image type",
1462 getBasicString(publicType.getBasicType()));
1463 return;
1464 }
1465 break;
1466 case EiifUnspecified:
1467 error(identifierLocation, "layout qualifier", "No image internal format specified");
1468 return;
1469 default:
1470 error(identifierLocation, "layout qualifier", "unrecognized token");
1471 return;
1472 }
1473
1474 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1475 switch (layoutQualifier.imageInternalFormat)
1476 {
1477 case EiifR32F:
1478 case EiifR32I:
1479 case EiifR32UI:
1480 break;
1481 default:
1482 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1483 {
1484 error(identifierLocation, "layout qualifier",
1485 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1486 "image variables must be qualified readonly and/or writeonly");
1487 return;
1488 }
1489 break;
1490 }
1491 }
1492 else
1493 {
Olli Etuaho43364892017-02-13 16:00:12 +00001494 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001495 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1496 }
jchen104cdac9e2017-05-08 11:01:20 +08001497
1498 if (IsAtomicCounter(publicType.getBasicType()))
1499 {
1500 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1501 }
1502 else
1503 {
1504 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1505 }
Olli Etuaho43364892017-02-13 16:00:12 +00001506}
Martin Radev2cc85b32016-08-05 16:22:53 +03001507
Olli Etuaho43364892017-02-13 16:00:12 +00001508void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1509{
1510 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001511 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1512 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1513 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1514 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1515 // when it comes to which shaders are accepted by the compiler.
1516 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001517 if (IsImage(type.getBasicType()))
1518 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001519 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1520 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001521 }
1522 else if (IsSampler(type.getBasicType()))
1523 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001524 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1525 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001526 }
jchen104cdac9e2017-05-08 11:01:20 +08001527 else if (IsAtomicCounter(type.getBasicType()))
1528 {
1529 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1530 }
Olli Etuaho43364892017-02-13 16:00:12 +00001531 else
1532 {
1533 ASSERT(!IsOpaqueType(type.getBasicType()));
1534 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001535 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001536}
1537
Olli Etuaho856c4972016-08-08 11:38:39 +03001538void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1539 const TString &layoutQualifierName,
1540 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001541{
1542
1543 if (mShaderVersion < versionRequired)
1544 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001545 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001546 }
1547}
1548
Olli Etuaho856c4972016-08-08 11:38:39 +03001549bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1550 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001551{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001552 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001553 for (size_t i = 0u; i < localSize.size(); ++i)
1554 {
1555 if (localSize[i] != -1)
1556 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001557 error(location,
1558 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1559 "global layout declaration",
1560 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001561 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001562 }
1563 }
1564
Olli Etuaho8a176262016-08-16 14:23:01 +03001565 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001566}
1567
Olli Etuaho43364892017-02-13 16:00:12 +00001568void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001569 TLayoutImageInternalFormat internalFormat)
1570{
1571 if (internalFormat != EiifUnspecified)
1572 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001573 error(location, "invalid layout qualifier: only valid when used with images",
1574 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001575 }
Olli Etuaho43364892017-02-13 16:00:12 +00001576}
1577
1578void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1579{
1580 if (binding != -1)
1581 {
1582 error(location,
1583 "invalid layout qualifier: only valid when used with opaque types or blocks",
1584 "binding");
1585 }
1586}
1587
jchen104cdac9e2017-05-08 11:01:20 +08001588void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1589{
1590 if (offset != -1)
1591 {
1592 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1593 "offset");
1594 }
1595}
1596
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001597void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1598 int binding,
1599 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001600{
1601 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001602 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001603 {
1604 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1605 }
1606}
1607
1608void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1609 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001610 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001611{
1612 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001613 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001614 {
1615 error(location, "sampler binding greater than maximum texture units", "binding");
1616 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001617}
1618
Jiajia Qinbc585152017-06-23 15:42:17 +08001619void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1620 const TQualifier &qualifier,
1621 int binding,
1622 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001623{
1624 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001625 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001626 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001627 if (binding + size > mMaxUniformBufferBindings)
1628 {
1629 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1630 "binding");
1631 }
1632 }
1633 else if (qualifier == EvqBuffer)
1634 {
1635 if (binding + size > mMaxShaderStorageBufferBindings)
1636 {
1637 error(location,
1638 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1639 "binding");
1640 }
jchen10af713a22017-04-19 09:10:56 +08001641 }
1642}
jchen104cdac9e2017-05-08 11:01:20 +08001643void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1644{
1645 if (binding >= mMaxAtomicCounterBindings)
1646 {
1647 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1648 "binding");
1649 }
1650}
jchen10af713a22017-04-19 09:10:56 +08001651
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001652void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1653 int objectLocationCount,
1654 const TLayoutQualifier &layoutQualifier)
1655{
1656 int loc = layoutQualifier.location;
1657 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1658 {
1659 error(location, "Uniform location out of range", "location");
1660 }
1661}
1662
Andrei Volykhina5527072017-03-22 16:46:30 +03001663void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1664{
1665 if (yuv != false)
1666 {
1667 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1668 }
1669}
1670
Jiajia Qinbc585152017-06-23 15:42:17 +08001671void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1672 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001673{
1674 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1675 {
1676 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001677 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1678 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1679 qual == EvqInOut || qual == EvqConstReadOnly))
1680 {
1681 if (argument->getMemoryQualifier().writeonly)
1682 {
1683 error(argument->getLine(),
1684 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001685 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001686 return;
1687 }
1688 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001689 if (qual == EvqOut || qual == EvqInOut)
1690 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001691 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001692 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001693 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001694 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001695 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001696 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001697 }
1698 }
1699 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001700}
1701
Martin Radev70866b82016-07-22 15:27:42 +03001702void TParseContext::checkInvariantVariableQualifier(bool invariant,
1703 const TQualifier qualifier,
1704 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001705{
Martin Radev70866b82016-07-22 15:27:42 +03001706 if (!invariant)
1707 return;
1708
1709 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001710 {
Martin Radev70866b82016-07-22 15:27:42 +03001711 // input variables in the fragment shader can be also qualified as invariant
1712 if (!sh::CanBeInvariantESSL1(qualifier))
1713 {
1714 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1715 }
1716 }
1717 else
1718 {
1719 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1720 {
1721 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1722 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001723 }
1724}
1725
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001726bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001727{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001728 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001729}
1730
Jamie Madillb98c3a82015-07-23 14:26:04 -04001731void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1732 const char *extName,
1733 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001734{
1735 pp::SourceLocation srcLoc;
1736 srcLoc.file = loc.first_file;
1737 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001738 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001739}
1740
Jamie Madillb98c3a82015-07-23 14:26:04 -04001741void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1742 const char *name,
1743 const char *value,
1744 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001745{
1746 pp::SourceLocation srcLoc;
1747 srcLoc.file = loc.first_file;
1748 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001749 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001750}
1751
Martin Radev4c4c8e72016-08-04 12:25:34 +03001752sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001753{
Jamie Madill2f294c92017-11-20 14:47:26 -05001754 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001755 for (size_t i = 0u; i < result.size(); ++i)
1756 {
1757 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1758 {
1759 result[i] = 1;
1760 }
1761 else
1762 {
1763 result[i] = mComputeShaderLocalSize[i];
1764 }
1765 }
1766 return result;
1767}
1768
Olli Etuaho56229f12017-07-10 14:16:33 +03001769TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1770 const TSourceLoc &line)
1771{
1772 TIntermConstantUnion *node = new TIntermConstantUnion(
1773 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1774 node->setLine(line);
1775 return node;
1776}
1777
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001778/////////////////////////////////////////////////////////////////////////////////
1779//
1780// Non-Errors.
1781//
1782/////////////////////////////////////////////////////////////////////////////////
1783
Jamie Madill5c097022014-08-20 16:38:32 -04001784const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1785 const TString *name,
1786 const TSymbol *symbol)
1787{
Jamie Madill5c097022014-08-20 16:38:32 -04001788 if (!symbol)
1789 {
1790 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001791 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001792 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001793
1794 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001795 {
1796 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001797 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001798 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001799
1800 const TVariable *variable = static_cast<const TVariable *>(symbol);
1801
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001802 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001803 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001804 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001805 }
1806
Olli Etuaho0f684632017-07-13 12:42:15 +03001807 // Reject shaders using both gl_FragData and gl_FragColor
1808 TQualifier qualifier = variable->getType().getQualifier();
1809 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001810 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001811 mUsesFragData = true;
1812 }
1813 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1814 {
1815 mUsesFragColor = true;
1816 }
1817 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1818 {
1819 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001820 }
1821
Olli Etuaho0f684632017-07-13 12:42:15 +03001822 // This validation is not quite correct - it's only an error to write to
1823 // both FragData and FragColor. For simplicity, and because users shouldn't
1824 // be rewarded for reading from undefined varaibles, return an error
1825 // if they are both referenced, rather than assigned.
1826 if (mUsesFragData && mUsesFragColor)
1827 {
1828 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1829 if (mUsesSecondaryOutputs)
1830 {
1831 errorMessage =
1832 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1833 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1834 }
1835 error(location, errorMessage, name->c_str());
1836 }
1837
1838 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1839 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1840 qualifier == EvqWorkGroupSize)
1841 {
1842 error(location,
1843 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1844 "gl_WorkGroupSize");
1845 }
Jamie Madill5c097022014-08-20 16:38:32 -04001846 return variable;
1847}
1848
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001849TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1850 const TString *name,
1851 const TSymbol *symbol)
1852{
1853 const TVariable *variable = getNamedVariable(location, name, symbol);
1854
Olli Etuaho0f684632017-07-13 12:42:15 +03001855 if (!variable)
1856 {
1857 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1858 node->setLine(location);
1859 return node;
1860 }
1861
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001862 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001863 TIntermTyped *node = nullptr;
1864
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001865 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001866 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001867 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001868 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001869 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001870 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001871 {
1872 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1873 // needs to be added to the AST as a constant and not as a symbol.
1874 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1875 TConstantUnion *constArray = new TConstantUnion[3];
1876 for (size_t i = 0; i < 3; ++i)
1877 {
1878 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1879 }
1880
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001881 ASSERT(variableType.getBasicType() == EbtUInt);
1882 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001883
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001884 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001885 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001886 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001887 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001888 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1889 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001890 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001891 ASSERT(mGeometryShaderInputArraySize > 0u);
1892
Olli Etuaho195be942017-12-04 23:40:14 +02001893 node = new TIntermSymbol(variable);
Olli Etuaho55bde912017-10-25 13:41:13 +03001894 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001895 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001896 else
1897 {
Olli Etuaho195be942017-12-04 23:40:14 +02001898 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001899 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001900 ASSERT(node != nullptr);
1901 node->setLine(location);
1902 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001903}
1904
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905// Initializers show up in several places in the grammar. Have one set of
1906// code to handle them here.
1907//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001908// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001909bool TParseContext::executeInitializer(const TSourceLoc &line,
1910 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001911 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001912 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001913 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914{
Olli Etuaho13389b62016-10-16 11:48:18 +01001915 ASSERT(initNode != nullptr);
1916 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917
Olli Etuaho376f1b52015-04-13 13:23:41 +03001918 if (type.isUnsizedArray())
1919 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001920 // In case initializer is not an array or type has more dimensions than initializer, this
1921 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1922 // actually is an array or not. Having a non-array initializer for an unsized array will
1923 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001924 auto *arraySizes = initializer->getType().getArraySizes();
1925 type.sizeUnsizedArrays(arraySizes);
Olli Etuaho376f1b52015-04-13 13:23:41 +03001926 }
Olli Etuaho195be942017-12-04 23:40:14 +02001927
1928 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001929 if (!declareVariable(line, identifier, type, &variable))
1930 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001931 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001932 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933
Olli Etuahob0c645e2015-05-12 14:25:36 +03001934 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001935 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001936 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001937 {
1938 // Error message does not completely match behavior with ESSL 1.00, but
1939 // we want to steer developers towards only using constant expressions.
1940 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001941 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001942 }
1943 if (globalInitWarning)
1944 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001945 warning(
1946 line,
1947 "global variable initializers should be constant expressions "
1948 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1949 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001950 }
1951
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001952 //
1953 // identifier must be of type constant, a global, or a temporary
1954 //
1955 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301956 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1957 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001958 error(line, " cannot initialize this type of qualifier ",
1959 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001960 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001961 }
1962 //
1963 // test for and propagate constant
1964 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965
Arun Patole7e7e68d2015-05-22 12:02:25 +05301966 if (qualifier == EvqConst)
1967 {
1968 if (qualifier != initializer->getType().getQualifier())
1969 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001970 std::stringstream reasonStream;
1971 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1972 << "'";
1973 std::string reason = reasonStream.str();
1974 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001975 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001976 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001977 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301978 if (type != initializer->getType())
1979 {
1980 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001981 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001982 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001983 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001984 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001985
1986 // Save the constant folded value to the variable if possible. For example array
1987 // initializers are not folded, since that way copying the array literal to multiple places
1988 // in the shader is avoided.
1989 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1990 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301991 if (initializer->getAsConstantUnion())
1992 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001993 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001994 ASSERT(*initNode == nullptr);
1995 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301996 }
1997 else if (initializer->getAsSymbolNode())
1998 {
Olli Etuahob6af22b2017-12-15 14:05:44 +02001999 const TVariable &var = initializer->getAsSymbolNode()->variable();
2000 const TConstantUnion *constArray = var.getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002001 if (constArray)
2002 {
2003 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002004 ASSERT(*initNode == nullptr);
2005 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002006 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002007 }
2008 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002009
Olli Etuaho195be942017-12-04 23:40:14 +02002010 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002011 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002012 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2013 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002014 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002015 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002016 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002017 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018
Olli Etuaho914b79a2017-06-19 16:03:19 +03002019 return true;
2020}
2021
2022TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2023 const TString &identifier,
2024 TIntermTyped *initializer,
2025 const TSourceLoc &loc)
2026{
2027 checkIsScalarBool(loc, pType);
2028 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002029 TType type(pType);
2030 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002031 {
2032 // The initializer is valid. The init condition needs to have a node - either the
2033 // initializer node, or a constant node in case the initialized variable is const and won't
2034 // be recorded in the AST.
2035 if (initNode == nullptr)
2036 {
2037 return initializer;
2038 }
2039 else
2040 {
2041 TIntermDeclaration *declaration = new TIntermDeclaration();
2042 declaration->appendDeclarator(initNode);
2043 return declaration;
2044 }
2045 }
2046 return nullptr;
2047}
2048
2049TIntermNode *TParseContext::addLoop(TLoopType type,
2050 TIntermNode *init,
2051 TIntermNode *cond,
2052 TIntermTyped *expr,
2053 TIntermNode *body,
2054 const TSourceLoc &line)
2055{
2056 TIntermNode *node = nullptr;
2057 TIntermTyped *typedCond = nullptr;
2058 if (cond)
2059 {
2060 typedCond = cond->getAsTyped();
2061 }
2062 if (cond == nullptr || typedCond)
2063 {
Olli Etuahocce89652017-06-19 16:04:09 +03002064 if (type == ELoopDoWhile)
2065 {
2066 checkIsScalarBool(line, typedCond);
2067 }
2068 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2069 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2070 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2071 !typedCond->isVector()));
2072
Olli Etuaho3ec75682017-07-05 17:02:55 +03002073 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002074 node->setLine(line);
2075 return node;
2076 }
2077
Olli Etuahocce89652017-06-19 16:04:09 +03002078 ASSERT(type != ELoopDoWhile);
2079
Olli Etuaho914b79a2017-06-19 16:03:19 +03002080 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2081 ASSERT(declaration);
2082 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2083 ASSERT(declarator->getLeft()->getAsSymbolNode());
2084
2085 // The condition is a declaration. In the AST representation we don't support declarations as
2086 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2087 // the loop.
2088 TIntermBlock *block = new TIntermBlock();
2089
2090 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2091 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2092 block->appendStatement(declareCondition);
2093
2094 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2095 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002096 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002097 block->appendStatement(loop);
2098 loop->setLine(line);
2099 block->setLine(line);
2100 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101}
2102
Olli Etuahocce89652017-06-19 16:04:09 +03002103TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2104 TIntermNodePair code,
2105 const TSourceLoc &loc)
2106{
Olli Etuaho56229f12017-07-10 14:16:33 +03002107 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002108
2109 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002110 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002111 {
2112 if (cond->getAsConstantUnion()->getBConst(0) == true)
2113 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002114 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002115 }
2116 else
2117 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002118 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002119 }
2120 }
2121
Olli Etuaho3ec75682017-07-05 17:02:55 +03002122 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002123 node->setLine(loc);
2124
2125 return node;
2126}
2127
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002128void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2129{
2130 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2131 typeSpecifier->getBasicType());
2132
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002133 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002134 {
2135 error(typeSpecifier->getLine(), "not supported", "first-class array");
2136 typeSpecifier->clearArrayness();
2137 }
2138}
2139
Martin Radev70866b82016-07-22 15:27:42 +03002140TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302141 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002142{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002143 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002144
Martin Radev70866b82016-07-22 15:27:42 +03002145 TPublicType returnType = typeSpecifier;
2146 returnType.qualifier = typeQualifier.qualifier;
2147 returnType.invariant = typeQualifier.invariant;
2148 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002149 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002150 returnType.precision = typeSpecifier.precision;
2151
2152 if (typeQualifier.precision != EbpUndefined)
2153 {
2154 returnType.precision = typeQualifier.precision;
2155 }
2156
Martin Radev4a9cd802016-09-01 16:51:51 +03002157 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2158 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002159
Martin Radev4a9cd802016-09-01 16:51:51 +03002160 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2161 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002162
Martin Radev4a9cd802016-09-01 16:51:51 +03002163 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002164
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002165 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002166 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002167 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002168 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002169 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002170 returnType.clearArrayness();
2171 }
2172
Martin Radev70866b82016-07-22 15:27:42 +03002173 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002174 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002175 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002176 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002177 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002178 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002179
Martin Radev70866b82016-07-22 15:27:42 +03002180 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002181 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002182 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002183 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002184 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002185 }
2186 }
2187 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002188 {
Martin Radev70866b82016-07-22 15:27:42 +03002189 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002190 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002191 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002192 }
Martin Radev70866b82016-07-22 15:27:42 +03002193 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2194 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002195 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002196 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2197 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002198 }
Martin Radev70866b82016-07-22 15:27:42 +03002199 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002200 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002201 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002202 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002203 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002204 }
2205
2206 return returnType;
2207}
2208
Olli Etuaho856c4972016-08-08 11:38:39 +03002209void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2210 const TPublicType &type,
2211 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002212{
2213 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002214 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002215 {
2216 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002217 }
2218
2219 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2220 switch (qualifier)
2221 {
2222 case EvqVertexIn:
2223 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002224 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002225 {
2226 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002227 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002228 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002229 return;
2230 case EvqFragmentOut:
2231 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002232 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002233 {
2234 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002235 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002236 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002237 return;
2238 default:
2239 break;
2240 }
2241
2242 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2243 // restrictions.
2244 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002245 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2246 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002247 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2248 {
2249 error(qualifierLocation, "must use 'flat' interpolation here",
2250 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002251 }
2252
Martin Radev4a9cd802016-09-01 16:51:51 +03002253 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002254 {
2255 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2256 // These restrictions are only implied by the ESSL 3.00 spec, but
2257 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002258 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002259 {
2260 error(qualifierLocation, "cannot be an array of structures",
2261 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002262 }
2263 if (type.isStructureContainingArrays())
2264 {
2265 error(qualifierLocation, "cannot be a structure containing an array",
2266 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 }
2268 if (type.isStructureContainingType(EbtStruct))
2269 {
2270 error(qualifierLocation, "cannot be a structure containing a structure",
2271 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002272 }
2273 if (type.isStructureContainingType(EbtBool))
2274 {
2275 error(qualifierLocation, "cannot be a structure containing a bool",
2276 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002277 }
2278 }
2279}
2280
Martin Radev2cc85b32016-08-05 16:22:53 +03002281void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2282{
2283 if (qualifier.getType() == QtStorage)
2284 {
2285 const TStorageQualifierWrapper &storageQualifier =
2286 static_cast<const TStorageQualifierWrapper &>(qualifier);
2287 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2288 !symbolTable.atGlobalLevel())
2289 {
2290 error(storageQualifier.getLine(),
2291 "Local variables can only use the const storage qualifier.",
2292 storageQualifier.getQualifierString().c_str());
2293 }
2294 }
2295}
2296
Olli Etuaho43364892017-02-13 16:00:12 +00002297void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002298 const TSourceLoc &location)
2299{
Jiajia Qinbc585152017-06-23 15:42:17 +08002300 const std::string reason(
2301 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2302 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002303 if (memoryQualifier.readonly)
2304 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002305 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002306 }
2307 if (memoryQualifier.writeonly)
2308 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002309 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002310 }
Martin Radev049edfa2016-11-11 14:35:37 +02002311 if (memoryQualifier.coherent)
2312 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002313 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002314 }
2315 if (memoryQualifier.restrictQualifier)
2316 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002317 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002318 }
2319 if (memoryQualifier.volatileQualifier)
2320 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002321 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002322 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002323}
2324
jchen104cdac9e2017-05-08 11:01:20 +08002325// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2326// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002327void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2328 const TSourceLoc &loc,
2329 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002330{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002331 if (!IsAtomicCounter(type->getBasicType()))
2332 {
2333 return;
2334 }
2335
2336 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2337 : kAtomicCounterSize;
2338 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2339 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002340 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002341 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002342 {
2343 offset = bindingState.appendSpan(size);
2344 }
2345 else
2346 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002347 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002348 }
2349 if (offset == -1)
2350 {
2351 error(loc, "Offset overlapping", "atomic counter");
2352 return;
2353 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002354 layoutQualifier.offset = offset;
2355 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002356}
2357
Olli Etuaho454c34c2017-10-25 16:35:56 +03002358void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2359 const char *token,
2360 TType *type)
2361{
2362 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2363 {
2364 if (type->isArray() && type->getOutermostArraySize() == 0u)
2365 {
2366 // Set size for the unsized geometry shader inputs if they are declared after a valid
2367 // input primitive declaration.
2368 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2369 {
2370 ASSERT(mGeometryShaderInputArraySize > 0u);
2371 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2372 }
2373 else
2374 {
2375 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2376 // An input can be declared without an array size if there is a previous layout
2377 // which specifies the size.
2378 error(location,
2379 "Missing a valid input primitive declaration before declaring an unsized "
2380 "array input",
2381 token);
2382 }
2383 }
2384 else if (type->isArray())
2385 {
2386 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2387 }
2388 else
2389 {
2390 error(location, "Geometry shader input variable must be declared as an array", token);
2391 }
2392 }
2393}
2394
Olli Etuaho13389b62016-10-16 11:48:18 +01002395TIntermDeclaration *TParseContext::parseSingleDeclaration(
2396 TPublicType &publicType,
2397 const TSourceLoc &identifierOrTypeLocation,
2398 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002399{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002400 TType type(publicType);
2401 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2402 mDirectiveHandler.pragma().stdgl.invariantAll)
2403 {
2404 TQualifier qualifier = type.getQualifier();
2405
2406 // The directive handler has already taken care of rejecting invalid uses of this pragma
2407 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2408 // affected variable declarations:
2409 //
2410 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2411 // elsewhere, in TranslatorGLSL.)
2412 //
2413 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2414 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2415 // the way this is currently implemented we have to enable this compiler option before
2416 // parsing the shader and determining the shading language version it uses. If this were
2417 // implemented as a post-pass, the workaround could be more targeted.
2418 //
2419 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2420 // the specification, but there are desktop OpenGL drivers that expect that this is the
2421 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2422 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2423 {
2424 type.setInvariant(true);
2425 }
2426 }
2427
Olli Etuaho454c34c2017-10-25 16:35:56 +03002428 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002429
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002430 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2431 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002432
Olli Etuahobab4c082015-04-24 16:38:49 +03002433 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002434 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002435
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002436 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002437 if (emptyDeclaration)
2438 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002439 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002440 // In most cases we don't need to create a symbol node for an empty declaration.
2441 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2442 if (type.getBasicType() == EbtStruct)
2443 {
Olli Etuaho195be942017-12-04 23:40:14 +02002444 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01002445 new TVariable(&symbolTable, nullptr, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002446 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002447 }
jchen104cdac9e2017-05-08 11:01:20 +08002448 else if (IsAtomicCounter(publicType.getBasicType()))
2449 {
2450 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2451 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002452 }
2453 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002454 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002455 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002456
Olli Etuaho55bde912017-10-25 13:41:13 +03002457 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002458
Olli Etuaho55bc9052017-10-25 17:33:06 +03002459 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002460
Olli Etuaho2935c582015-04-08 14:32:06 +03002461 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002462 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002463 {
Olli Etuaho195be942017-12-04 23:40:14 +02002464 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002465 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002466 }
2467
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002468 TIntermDeclaration *declaration = new TIntermDeclaration();
2469 declaration->setLine(identifierOrTypeLocation);
2470 if (symbol)
2471 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002472 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002473 declaration->appendDeclarator(symbol);
2474 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002475 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002476}
2477
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002478TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2479 TPublicType &elementType,
2480 const TSourceLoc &identifierLocation,
2481 const TString &identifier,
2482 const TSourceLoc &indexLocation,
2483 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002484{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002485 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002486
Olli Etuaho55bde912017-10-25 13:41:13 +03002487 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002488 identifierLocation);
2489
Olli Etuaho55bde912017-10-25 13:41:13 +03002490 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002491
Olli Etuaho55bde912017-10-25 13:41:13 +03002492 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002493
Olli Etuaho55bde912017-10-25 13:41:13 +03002494 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002495 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002496
Olli Etuaho454c34c2017-10-25 16:35:56 +03002497 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002498
2499 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2500
Olli Etuaho55bc9052017-10-25 17:33:06 +03002501 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002502
Olli Etuaho13389b62016-10-16 11:48:18 +01002503 TIntermDeclaration *declaration = new TIntermDeclaration();
2504 declaration->setLine(identifierLocation);
2505
Olli Etuaho195be942017-12-04 23:40:14 +02002506 TVariable *variable = nullptr;
2507 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002508 {
Olli Etuaho195be942017-12-04 23:40:14 +02002509 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002510 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002511 declaration->appendDeclarator(symbol);
2512 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002513
Olli Etuaho13389b62016-10-16 11:48:18 +01002514 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002515}
2516
Olli Etuaho13389b62016-10-16 11:48:18 +01002517TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2518 const TSourceLoc &identifierLocation,
2519 const TString &identifier,
2520 const TSourceLoc &initLocation,
2521 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002522{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002523 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002524
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002525 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2526 identifierLocation);
2527
2528 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002529
Olli Etuaho13389b62016-10-16 11:48:18 +01002530 TIntermDeclaration *declaration = new TIntermDeclaration();
2531 declaration->setLine(identifierLocation);
2532
2533 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002534 TType type(publicType);
2535 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002536 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002537 if (initNode)
2538 {
2539 declaration->appendDeclarator(initNode);
2540 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002541 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002542 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002543}
2544
Olli Etuaho13389b62016-10-16 11:48:18 +01002545TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002546 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002547 const TSourceLoc &identifierLocation,
2548 const TString &identifier,
2549 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002550 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002551 const TSourceLoc &initLocation,
2552 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002553{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002554 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002555
Olli Etuaho55bde912017-10-25 13:41:13 +03002556 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002557 identifierLocation);
2558
Olli Etuaho55bde912017-10-25 13:41:13 +03002559 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002560
Olli Etuaho55bde912017-10-25 13:41:13 +03002561 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002562
Olli Etuaho55bde912017-10-25 13:41:13 +03002563 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002564 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565
Olli Etuaho13389b62016-10-16 11:48:18 +01002566 TIntermDeclaration *declaration = new TIntermDeclaration();
2567 declaration->setLine(identifierLocation);
2568
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002569 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002570 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002571 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002573 if (initNode)
2574 {
2575 declaration->appendDeclarator(initNode);
2576 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002577 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002578
2579 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002580}
2581
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002582TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002583 const TTypeQualifierBuilder &typeQualifierBuilder,
2584 const TSourceLoc &identifierLoc,
2585 const TString *identifier,
2586 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002587{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002588 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002589
Martin Radev70866b82016-07-22 15:27:42 +03002590 if (!typeQualifier.invariant)
2591 {
2592 error(identifierLoc, "Expected invariant", identifier->c_str());
2593 return nullptr;
2594 }
2595 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2596 {
2597 return nullptr;
2598 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002599 if (!symbol)
2600 {
2601 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002602 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002603 }
Martin Radev70866b82016-07-22 15:27:42 +03002604 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002605 {
Martin Radev70866b82016-07-22 15:27:42 +03002606 error(identifierLoc, "invariant declaration specifies qualifier",
2607 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002608 }
Martin Radev70866b82016-07-22 15:27:42 +03002609 if (typeQualifier.precision != EbpUndefined)
2610 {
2611 error(identifierLoc, "invariant declaration specifies precision",
2612 getPrecisionString(typeQualifier.precision));
2613 }
2614 if (!typeQualifier.layoutQualifier.isEmpty())
2615 {
2616 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2617 }
2618
2619 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002620 if (!variable)
2621 {
2622 return nullptr;
2623 }
Martin Radev70866b82016-07-22 15:27:42 +03002624 const TType &type = variable->getType();
2625
2626 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2627 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002628 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002629
2630 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2631
Olli Etuaho195be942017-12-04 23:40:14 +02002632 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002633 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002634
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002635 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002636}
2637
Olli Etuaho13389b62016-10-16 11:48:18 +01002638void TParseContext::parseDeclarator(TPublicType &publicType,
2639 const TSourceLoc &identifierLocation,
2640 const TString &identifier,
2641 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002642{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002643 // If the declaration starting this declarator list was empty (example: int,), some checks were
2644 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002645 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002646 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002647 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2648 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002649 }
2650
Olli Etuaho856c4972016-08-08 11:38:39 +03002651 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002652
Olli Etuaho43364892017-02-13 16:00:12 +00002653 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002654
2655 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2656
Olli Etuaho55bde912017-10-25 13:41:13 +03002657 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2658
Olli Etuaho55bc9052017-10-25 17:33:06 +03002659 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2660
Olli Etuaho195be942017-12-04 23:40:14 +02002661 TVariable *variable = nullptr;
2662 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002663 {
Olli Etuaho195be942017-12-04 23:40:14 +02002664 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002665 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002666 declarationOut->appendDeclarator(symbol);
2667 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002668}
2669
Olli Etuaho55bde912017-10-25 13:41:13 +03002670void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002671 const TSourceLoc &identifierLocation,
2672 const TString &identifier,
2673 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002674 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002675 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002676{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002677 // If the declaration starting this declarator list was empty (example: int,), some checks were
2678 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002679 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002680 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002681 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002682 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002683 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002684
Olli Etuaho55bde912017-10-25 13:41:13 +03002685 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002686
Olli Etuaho55bde912017-10-25 13:41:13 +03002687 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002688 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002689 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002690 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002691
Olli Etuaho454c34c2017-10-25 16:35:56 +03002692 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2693
Olli Etuaho55bde912017-10-25 13:41:13 +03002694 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2695
Olli Etuaho55bc9052017-10-25 17:33:06 +03002696 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002697
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002698 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002699 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002700 {
Olli Etuaho195be942017-12-04 23:40:14 +02002701 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002702 symbol->setLine(identifierLocation);
2703 declarationOut->appendDeclarator(symbol);
2704 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002705 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002706}
2707
Olli Etuaho13389b62016-10-16 11:48:18 +01002708void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2709 const TSourceLoc &identifierLocation,
2710 const TString &identifier,
2711 const TSourceLoc &initLocation,
2712 TIntermTyped *initializer,
2713 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002714{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002715 // If the declaration starting this declarator list was empty (example: int,), some checks were
2716 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002717 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002718 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002719 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2720 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002721 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002722
Olli Etuaho856c4972016-08-08 11:38:39 +03002723 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002724
Olli Etuaho13389b62016-10-16 11:48:18 +01002725 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002726 TType type(publicType);
2727 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002728 {
2729 //
2730 // build the intermediate representation
2731 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002732 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002733 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002734 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002735 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002736 }
2737}
2738
Olli Etuaho55bde912017-10-25 13:41:13 +03002739void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002740 const TSourceLoc &identifierLocation,
2741 const TString &identifier,
2742 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002743 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002744 const TSourceLoc &initLocation,
2745 TIntermTyped *initializer,
2746 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002747{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002748 // If the declaration starting this declarator list was empty (example: int,), some checks were
2749 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002750 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002751 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002752 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002753 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002754 }
2755
Olli Etuaho55bde912017-10-25 13:41:13 +03002756 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002757
Olli Etuaho55bde912017-10-25 13:41:13 +03002758 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002759
Olli Etuaho55bde912017-10-25 13:41:13 +03002760 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002761 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762
2763 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002764 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002765 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002766 {
2767 if (initNode)
2768 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002769 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002770 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002771 }
2772}
2773
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002774TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2775{
2776 // It's simpler to parse an empty statement as a constant expression rather than having a
2777 // different type of node just for empty statements, that will be pruned from the AST anyway.
2778 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2779 node->setLine(location);
2780 return node;
2781}
2782
jchen104cdac9e2017-05-08 11:01:20 +08002783void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2784 const TSourceLoc &location)
2785{
2786 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2787 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2788 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2789 {
2790 error(location, "Requires both binding and offset", "layout");
2791 return;
2792 }
2793 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2794}
2795
Olli Etuahocce89652017-06-19 16:04:09 +03002796void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2797 const TPublicType &type,
2798 const TSourceLoc &loc)
2799{
2800 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2801 !getFragmentPrecisionHigh())
2802 {
2803 error(loc, "precision is not supported in fragment shader", "highp");
2804 }
2805
2806 if (!CanSetDefaultPrecisionOnType(type))
2807 {
2808 error(loc, "illegal type argument for default precision qualifier",
2809 getBasicString(type.getBasicType()));
2810 return;
2811 }
2812 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2813}
2814
Shaob5cc1192017-07-06 10:47:20 +08002815bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2816{
2817 switch (typeQualifier.layoutQualifier.primitiveType)
2818 {
2819 case EptLines:
2820 case EptLinesAdjacency:
2821 case EptTriangles:
2822 case EptTrianglesAdjacency:
2823 return typeQualifier.qualifier == EvqGeometryIn;
2824
2825 case EptLineStrip:
2826 case EptTriangleStrip:
2827 return typeQualifier.qualifier == EvqGeometryOut;
2828
2829 case EptPoints:
2830 return true;
2831
2832 default:
2833 UNREACHABLE();
2834 return false;
2835 }
2836}
2837
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002838void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2839 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002840{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002841 if (mGeometryShaderInputArraySize == 0u)
2842 {
2843 mGeometryShaderInputArraySize = inputArraySize;
2844 }
2845 else if (mGeometryShaderInputArraySize != inputArraySize)
2846 {
2847 error(line,
2848 "Array size or input primitive declaration doesn't match the size of earlier sized "
2849 "array inputs.",
2850 "layout");
2851 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002852}
2853
Shaob5cc1192017-07-06 10:47:20 +08002854bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2855{
2856 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2857
2858 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2859
2860 if (layoutQualifier.maxVertices != -1)
2861 {
2862 error(typeQualifier.line,
2863 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2864 return false;
2865 }
2866
2867 // Set mGeometryInputPrimitiveType if exists
2868 if (layoutQualifier.primitiveType != EptUndefined)
2869 {
2870 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2871 {
2872 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2873 return false;
2874 }
2875
2876 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2877 {
2878 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002879 setGeometryShaderInputArraySize(
2880 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2881 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002882 }
2883 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2884 {
2885 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2886 "layout");
2887 return false;
2888 }
2889 }
2890
2891 // Set mGeometryInvocations if exists
2892 if (layoutQualifier.invocations > 0)
2893 {
2894 if (mGeometryShaderInvocations == 0)
2895 {
2896 mGeometryShaderInvocations = layoutQualifier.invocations;
2897 }
2898 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2899 {
2900 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2901 "layout");
2902 return false;
2903 }
2904 }
2905
2906 return true;
2907}
2908
2909bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2910{
2911 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2912
2913 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2914
2915 if (layoutQualifier.invocations > 0)
2916 {
2917 error(typeQualifier.line,
2918 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2919 return false;
2920 }
2921
2922 // Set mGeometryOutputPrimitiveType if exists
2923 if (layoutQualifier.primitiveType != EptUndefined)
2924 {
2925 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2926 {
2927 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2928 return false;
2929 }
2930
2931 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2932 {
2933 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2934 }
2935 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2936 {
2937 error(typeQualifier.line,
2938 "primitive doesn't match earlier output primitive declaration", "layout");
2939 return false;
2940 }
2941 }
2942
2943 // Set mGeometryMaxVertices if exists
2944 if (layoutQualifier.maxVertices > -1)
2945 {
2946 if (mGeometryShaderMaxVertices == -1)
2947 {
2948 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2949 }
2950 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2951 {
2952 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2953 "layout");
2954 return false;
2955 }
2956 }
2957
2958 return true;
2959}
2960
Martin Radev70866b82016-07-22 15:27:42 +03002961void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002962{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002963 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002964 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002965
Martin Radev70866b82016-07-22 15:27:42 +03002966 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2967 typeQualifier.line);
2968
Jamie Madillc2128ff2016-07-04 10:26:17 -04002969 // It should never be the case, but some strange parser errors can send us here.
2970 if (layoutQualifier.isEmpty())
2971 {
2972 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002973 return;
2974 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002975
Martin Radev802abe02016-08-04 17:48:32 +03002976 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002977 {
Olli Etuaho43364892017-02-13 16:00:12 +00002978 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002979 return;
2980 }
2981
Olli Etuaho43364892017-02-13 16:00:12 +00002982 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2983
2984 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002985
2986 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2987
Andrei Volykhina5527072017-03-22 16:46:30 +03002988 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2989
jchen104cdac9e2017-05-08 11:01:20 +08002990 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2991
Qin Jiajiaca68d982017-09-18 16:41:56 +08002992 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2993 typeQualifier.qualifier);
2994
Martin Radev802abe02016-08-04 17:48:32 +03002995 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002996 {
Martin Radev802abe02016-08-04 17:48:32 +03002997 if (mComputeShaderLocalSizeDeclared &&
2998 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2999 {
3000 error(typeQualifier.line, "Work group size does not match the previous declaration",
3001 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003002 return;
3003 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003004
Martin Radev802abe02016-08-04 17:48:32 +03003005 if (mShaderVersion < 310)
3006 {
3007 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003008 return;
3009 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003010
Martin Radev4c4c8e72016-08-04 12:25:34 +03003011 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003012 {
3013 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003014 return;
3015 }
3016
3017 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3018 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3019
3020 const TConstantUnion *maxComputeWorkGroupSizeData =
3021 maxComputeWorkGroupSize->getConstPointer();
3022
3023 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3024 {
3025 if (layoutQualifier.localSize[i] != -1)
3026 {
3027 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3028 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3029 if (mComputeShaderLocalSize[i] < 1 ||
3030 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3031 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003032 std::stringstream reasonStream;
3033 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3034 << maxComputeWorkGroupSizeValue;
3035 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003036
Olli Etuaho4de340a2016-12-16 09:32:03 +00003037 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003038 return;
3039 }
3040 }
3041 }
3042
3043 mComputeShaderLocalSizeDeclared = true;
3044 }
Shaob5cc1192017-07-06 10:47:20 +08003045 else if (typeQualifier.qualifier == EvqGeometryIn)
3046 {
3047 if (mShaderVersion < 310)
3048 {
3049 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3050 return;
3051 }
3052
3053 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3054 {
3055 return;
3056 }
3057 }
3058 else if (typeQualifier.qualifier == EvqGeometryOut)
3059 {
3060 if (mShaderVersion < 310)
3061 {
3062 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3063 "layout");
3064 return;
3065 }
3066
3067 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3068 {
3069 return;
3070 }
3071 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003072 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3073 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003074 {
3075 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3076 // specification.
3077 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3078 {
3079 error(typeQualifier.line, "Number of views does not match the previous declaration",
3080 "layout");
3081 return;
3082 }
3083
3084 if (layoutQualifier.numViews == -1)
3085 {
3086 error(typeQualifier.line, "No num_views specified", "layout");
3087 return;
3088 }
3089
3090 if (layoutQualifier.numViews > mMaxNumViews)
3091 {
3092 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3093 "layout");
3094 return;
3095 }
3096
3097 mNumViews = layoutQualifier.numViews;
3098 }
Martin Radev802abe02016-08-04 17:48:32 +03003099 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003100 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003101 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003102 {
Martin Radev802abe02016-08-04 17:48:32 +03003103 return;
3104 }
3105
Jiajia Qinbc585152017-06-23 15:42:17 +08003106 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003107 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003108 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003109 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003110 return;
3111 }
3112
3113 if (mShaderVersion < 300)
3114 {
3115 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3116 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003117 return;
3118 }
3119
Olli Etuaho09b04a22016-12-15 13:30:26 +00003120 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003121
3122 if (layoutQualifier.matrixPacking != EmpUnspecified)
3123 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003124 if (typeQualifier.qualifier == EvqUniform)
3125 {
3126 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3127 }
3128 else if (typeQualifier.qualifier == EvqBuffer)
3129 {
3130 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3131 }
Martin Radev802abe02016-08-04 17:48:32 +03003132 }
3133
3134 if (layoutQualifier.blockStorage != EbsUnspecified)
3135 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003136 if (typeQualifier.qualifier == EvqUniform)
3137 {
3138 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3139 }
3140 else if (typeQualifier.qualifier == EvqBuffer)
3141 {
3142 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3143 }
Martin Radev802abe02016-08-04 17:48:32 +03003144 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003145 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003146}
3147
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003148TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3149 const TFunction &function,
3150 const TSourceLoc &location,
3151 bool insertParametersToSymbolTable)
3152{
Olli Etuahobed35d72017-12-20 16:36:26 +02003153 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003154
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003155 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003156 prototype->setLine(location);
3157
3158 for (size_t i = 0; i < function.getParamCount(); i++)
3159 {
3160 const TConstParameter &param = function.getParam(i);
3161
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003162 TIntermSymbol *symbol = nullptr;
3163
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003164 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3165 // be used for unused args).
3166 if (param.name != nullptr)
3167 {
Olli Etuaho195be942017-12-04 23:40:14 +02003168 TVariable *variable =
3169 new TVariable(&symbolTable, param.name, *param.type, SymbolType::UserDefined);
3170 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003171 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003172 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003173 {
Olli Etuaho195be942017-12-04 23:40:14 +02003174 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003175 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003176 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003177 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003178 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003179 // Unsized type of a named parameter should have already been checked and sanitized.
3180 ASSERT(!param.type->isUnsizedArray());
3181 }
3182 else
3183 {
3184 if (param.type->isUnsizedArray())
3185 {
3186 error(location, "function parameter array must be sized at compile time", "[]");
3187 // We don't need to size the arrays since the parameter is unnamed and hence
3188 // inaccessible.
3189 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003190 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003191 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003192 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003193 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3194 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003195 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003196 new TVariable(&symbolTable, nullptr, *param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003197 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003198 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003199 symbol->setLine(location);
3200 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003201 }
3202 return prototype;
3203}
3204
Olli Etuaho16c745a2017-01-16 17:02:27 +00003205TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3206 const TFunction &parsedFunction,
3207 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003208{
Olli Etuaho476197f2016-10-11 13:59:08 +01003209 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3210 // first declaration. Either way the instance in the symbol table is used to track whether the
3211 // function is declared multiple times.
3212 TFunction *function = static_cast<TFunction *>(
3213 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3214 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003215 {
3216 // ESSL 1.00.17 section 4.2.7.
3217 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3218 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003219 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003220 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003221
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003222 TIntermFunctionPrototype *prototype =
3223 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003224
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003225 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003226
3227 if (!symbolTable.atGlobalLevel())
3228 {
3229 // ESSL 3.00.4 section 4.2.4.
3230 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003231 }
3232
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003233 return prototype;
3234}
3235
Olli Etuaho336b1472016-10-05 16:37:55 +01003236TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003237 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003238 TIntermBlock *functionBody,
3239 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003240{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003241 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003242 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3243 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003244 error(location, "function does not return a value:",
Olli Etuahobed35d72017-12-20 16:36:26 +02003245 functionPrototype->getFunction()->name().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003246 }
3247
Olli Etuahof51fdd22016-10-03 10:03:40 +01003248 if (functionBody == nullptr)
3249 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003250 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003251 functionBody->setLine(location);
3252 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003253 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003254 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003255 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003256
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003257 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003258 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003259}
3260
Olli Etuaho476197f2016-10-11 13:59:08 +01003261void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3262 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003263 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003264{
Olli Etuaho476197f2016-10-11 13:59:08 +01003265 ASSERT(function);
3266 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003267 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003268 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003269
3270 if (builtIn)
3271 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003272 error(location, "built-in functions cannot be redefined", (*function)->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003273 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003274 else
Jamie Madill185fb402015-06-12 15:48:48 -04003275 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003276 TFunction *prevDec = static_cast<TFunction *>(
3277 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3278
3279 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3280 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3281 // occurance.
3282 if (*function != prevDec)
3283 {
3284 // Swap the parameters of the previous declaration to the parameters of the function
3285 // definition (parameter names may differ).
3286 prevDec->swapParameters(**function);
3287
3288 // The function definition will share the same symbol as any previous declaration.
3289 *function = prevDec;
3290 }
3291
3292 if ((*function)->isDefined())
3293 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003294 error(location, "function already has a body", (*function)->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003295 }
3296
3297 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003298 }
Jamie Madill185fb402015-06-12 15:48:48 -04003299
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003300 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003301 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003302 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003303
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003304 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003305 setLoopNestingLevel(0);
3306}
3307
Jamie Madillb98c3a82015-07-23 14:26:04 -04003308TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003309{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003310 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003311 // We don't know at this point whether this is a function definition or a prototype.
3312 // The definition production code will check for redefinitions.
3313 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003314 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003315 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3316 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003317 //
3318 TFunction *prevDec =
3319 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303320
Olli Etuahod80f2942017-11-06 12:44:45 +02003321 for (size_t i = 0u; i < function->getParamCount(); ++i)
3322 {
3323 auto &param = function->getParam(i);
3324 if (param.type->isStructSpecifier())
3325 {
3326 // ESSL 3.00.6 section 12.10.
3327 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahobed35d72017-12-20 16:36:26 +02003328 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003329 }
3330 }
3331
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003332 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahobed35d72017-12-20 16:36:26 +02003333 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303334 {
Martin Radevda6254b2016-12-14 17:00:36 +02003335 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303336 // Therefore overloading or redefining builtin functions is an error.
3337 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahobed35d72017-12-20 16:36:26 +02003338 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303339 }
3340 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003341 {
3342 if (prevDec->getReturnType() != function->getReturnType())
3343 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003344 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003345 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003346 }
3347 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3348 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003349 if (prevDec->getParam(i).type->getQualifier() !=
3350 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003351 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003352 error(location,
3353 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003354 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003355 }
3356 }
3357 }
3358
3359 //
3360 // Check for previously declared variables using the same name.
3361 //
Olli Etuahobed35d72017-12-20 16:36:26 +02003362 TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003363 if (prevSym)
3364 {
3365 if (!prevSym->isFunction())
3366 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003367 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003368 }
3369 }
3370 else
3371 {
3372 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003373 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003374 }
3375
3376 // We're at the inner scope level of the function's arguments and body statement.
3377 // Add the function prototype to the surrounding scope instead.
3378 symbolTable.getOuterLevel()->insert(function);
3379
Olli Etuaho78d13742017-01-18 13:06:10 +00003380 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahobed35d72017-12-20 16:36:26 +02003381 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003382 {
3383 if (function->getParamCount() > 0)
3384 {
3385 error(location, "function cannot take any parameter(s)", "main");
3386 }
3387 if (function->getReturnType().getBasicType() != EbtVoid)
3388 {
3389 error(location, "main function cannot return a value",
3390 function->getReturnType().getBasicString());
3391 }
3392 }
3393
Jamie Madill185fb402015-06-12 15:48:48 -04003394 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003395 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3396 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003397 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3398 //
3399 return function;
3400}
3401
Olli Etuaho9de84a52016-06-14 17:36:01 +03003402TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3403 const TString *name,
3404 const TSourceLoc &location)
3405{
3406 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3407 {
3408 error(location, "no qualifiers allowed for function return",
3409 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003410 }
3411 if (!type.layoutQualifier.isEmpty())
3412 {
3413 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003414 }
jchen10cc2a10e2017-05-03 14:05:12 +08003415 // make sure an opaque type is not involved as well...
3416 std::string reason(getBasicString(type.getBasicType()));
3417 reason += "s can't be function return values";
3418 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003419 if (mShaderVersion < 300)
3420 {
3421 // Array return values are forbidden, but there's also no valid syntax for declaring array
3422 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003423 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003424
3425 if (type.isStructureContainingArrays())
3426 {
3427 // ESSL 1.00.17 section 6.1 Function Definitions
3428 error(location, "structures containing arrays can't be function return values",
3429 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003430 }
3431 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003432
3433 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003434 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003435}
3436
Olli Etuahocce89652017-06-19 16:04:09 +03003437TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3438{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003439 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003440 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3441 // really only interested in the mangled name of the function to look up the actual function
3442 // from the symbol table. If we just had the name string and the types of the parameters that
3443 // would be enough, but TFunction carries a lot of extra information in addition to that.
3444 // Besides function calls we do have to store constructor calls in the same data structure, for
3445 // them we need to store a TType.
Olli Etuaho0c371002017-12-13 17:00:25 +04003446 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved, false);
Olli Etuahocce89652017-06-19 16:04:09 +03003447}
3448
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003449TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003450{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003451 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003452 {
3453 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3454 "[]");
3455 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003456 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003457 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003458 error(publicType.getLine(), "constructor can't be a structure definition",
3459 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003460 }
3461
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003462 TType *type = new TType(publicType);
3463 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003464 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003465 error(publicType.getLine(), "cannot construct this type",
3466 getBasicString(publicType.getBasicType()));
3467 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003468 }
3469
Olli Etuaho0c371002017-12-13 17:00:25 +04003470 return new TFunction(&symbolTable, nullptr, type, SymbolType::NotResolved, true, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003471}
3472
Olli Etuaho55bde912017-10-25 13:41:13 +03003473void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3474 const char *errorMessage,
3475 const char *token,
3476 TType *arrayType)
3477{
3478 if (arrayType->isUnsizedArray())
3479 {
3480 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003481 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003482 }
3483}
3484
3485TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003486 const TString *name,
3487 const TSourceLoc &nameLoc)
3488{
Olli Etuaho55bde912017-10-25 13:41:13 +03003489 ASSERT(type);
3490 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3491 type);
3492 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003493 {
3494 error(nameLoc, "illegal use of type 'void'", name->c_str());
3495 }
3496 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003497 TParameter param = {name, type};
3498 return param;
3499}
3500
Olli Etuaho55bde912017-10-25 13:41:13 +03003501TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3502 const TString *name,
3503 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003504{
Olli Etuaho55bde912017-10-25 13:41:13 +03003505 TType *type = new TType(publicType);
3506 return parseParameterDeclarator(type, name, nameLoc);
3507}
3508
3509TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3510 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003511 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003512 const TSourceLoc &arrayLoc,
3513 TPublicType *elementType)
3514{
3515 checkArrayElementIsNotArray(arrayLoc, *elementType);
3516 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003517 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003518 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003519}
3520
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003521bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3522 TType type,
3523 const TSourceLoc &line)
3524{
3525 if (arguments->empty())
3526 {
3527 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3528 return false;
3529 }
3530 for (TIntermNode *arg : *arguments)
3531 {
3532 TIntermTyped *element = arg->getAsTyped();
3533 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003534 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3535 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003536 {
3537 error(line, "constructing from a non-dereferenced array", "constructor");
3538 return false;
3539 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003540 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003541 {
3542 if (dimensionalityFromElement == 1u)
3543 {
3544 error(line, "implicitly sized array of arrays constructor argument is not an array",
3545 "constructor");
3546 }
3547 else
3548 {
3549 error(line,
3550 "implicitly sized array of arrays constructor argument dimensionality is too "
3551 "low",
3552 "constructor");
3553 }
3554 return false;
3555 }
3556 }
3557 return true;
3558}
3559
Jamie Madillb98c3a82015-07-23 14:26:04 -04003560// This function is used to test for the correctness of the parameters passed to various constructor
3561// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003562//
Olli Etuaho856c4972016-08-08 11:38:39 +03003563// 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 +00003564//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003565TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003566 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303567 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003568{
Olli Etuaho856c4972016-08-08 11:38:39 +03003569 if (type.isUnsizedArray())
3570 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003571 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003572 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003573 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003574 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003575 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003576 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3577 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003578 if (type.getOutermostArraySize() == 0u)
3579 {
3580 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3581 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003582 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003583 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003584 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003585 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003586 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003587 }
3588 }
3589 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003590 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003591
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003592 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003593 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003594 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003595 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003596
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003597 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003598 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003599
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003600 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3601 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003602 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003603 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003604 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003605 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003606}
3607
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003608//
3609// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003610// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003611//
Olli Etuaho13389b62016-10-16 11:48:18 +01003612TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003613 const TTypeQualifierBuilder &typeQualifierBuilder,
3614 const TSourceLoc &nameLine,
3615 const TString &blockName,
3616 TFieldList *fieldList,
3617 const TString *instanceName,
3618 const TSourceLoc &instanceLine,
3619 TIntermTyped *arrayIndex,
3620 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003621{
Olli Etuaho856c4972016-08-08 11:38:39 +03003622 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003623
Olli Etuaho77ba4082016-12-16 12:01:18 +00003624 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003625
Jiajia Qinbc585152017-06-23 15:42:17 +08003626 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003627 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003628 error(typeQualifier.line,
3629 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3630 "3.10",
3631 getQualifierString(typeQualifier.qualifier));
3632 }
3633 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3634 {
3635 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003636 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003637 }
3638
Martin Radev70866b82016-07-22 15:27:42 +03003639 if (typeQualifier.invariant)
3640 {
3641 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3642 }
3643
Jiajia Qinbc585152017-06-23 15:42:17 +08003644 if (typeQualifier.qualifier != EvqBuffer)
3645 {
3646 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3647 }
Olli Etuaho43364892017-02-13 16:00:12 +00003648
jchen10af713a22017-04-19 09:10:56 +08003649 // add array index
3650 unsigned int arraySize = 0;
3651 if (arrayIndex != nullptr)
3652 {
3653 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3654 }
3655
3656 if (mShaderVersion < 310)
3657 {
3658 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3659 }
3660 else
3661 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003662 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3663 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003664 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003665
Andrei Volykhina5527072017-03-22 16:46:30 +03003666 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3667
Jamie Madill099c0f32013-06-20 11:55:52 -04003668 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003669 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003670 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3671 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003672
Jamie Madill099c0f32013-06-20 11:55:52 -04003673 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3674 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003675 if (typeQualifier.qualifier == EvqUniform)
3676 {
3677 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3678 }
3679 else if (typeQualifier.qualifier == EvqBuffer)
3680 {
3681 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3682 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003683 }
3684
Jamie Madill1566ef72013-06-20 11:55:54 -04003685 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3686 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003687 if (typeQualifier.qualifier == EvqUniform)
3688 {
3689 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3690 }
3691 else if (typeQualifier.qualifier == EvqBuffer)
3692 {
3693 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3694 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003695 }
3696
Olli Etuaho856c4972016-08-08 11:38:39 +03003697 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003698
Martin Radev2cc85b32016-08-05 16:22:53 +03003699 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3700
Jamie Madill98493dd2013-07-08 14:39:03 -04003701 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303702 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3703 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003704 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303705 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003706 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303707 {
jchen10cc2a10e2017-05-03 14:05:12 +08003708 std::string reason("unsupported type - ");
3709 reason += fieldType->getBasicString();
3710 reason += " types are not allowed in interface blocks";
3711 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003712 }
3713
Jamie Madill98493dd2013-07-08 14:39:03 -04003714 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003715 switch (qualifier)
3716 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003717 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003718 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003719 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003720 if (typeQualifier.qualifier == EvqBuffer)
3721 {
3722 error(field->line(), "invalid qualifier on shader storage block member",
3723 getQualifierString(qualifier));
3724 }
3725 break;
3726 case EvqBuffer:
3727 if (typeQualifier.qualifier == EvqUniform)
3728 {
3729 error(field->line(), "invalid qualifier on uniform block member",
3730 getQualifierString(qualifier));
3731 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003732 break;
3733 default:
3734 error(field->line(), "invalid qualifier on interface block member",
3735 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003736 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003737 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003738
Martin Radev70866b82016-07-22 15:27:42 +03003739 if (fieldType->isInvariant())
3740 {
3741 error(field->line(), "invalid qualifier on interface block member", "invariant");
3742 }
3743
Jamie Madilla5efff92013-06-06 11:56:47 -04003744 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003745 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003746 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003747 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003748
Jamie Madill98493dd2013-07-08 14:39:03 -04003749 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003750 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003751 error(field->line(), "invalid layout qualifier: cannot be used here",
3752 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003753 }
3754
Jamie Madill98493dd2013-07-08 14:39:03 -04003755 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003756 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003757 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003758 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003759 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003760 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003761 warning(field->line(),
3762 "extraneous layout qualifier: only has an effect on matrix types",
3763 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003764 }
3765
Jamie Madill98493dd2013-07-08 14:39:03 -04003766 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003767
Olli Etuahoebee5b32017-11-23 12:56:32 +02003768 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3769 typeQualifier.qualifier != EvqBuffer)
3770 {
3771 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3772 checkIsNotUnsizedArray(field->line(),
3773 "array members of interface blocks must specify a size",
3774 field->name().c_str(), field->type());
3775 }
3776
Jiajia Qinbc585152017-06-23 15:42:17 +08003777 if (typeQualifier.qualifier == EvqBuffer)
3778 {
3779 // set memory qualifiers
3780 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3781 // qualified with a memory qualifier, it is as if all of its members were declared with
3782 // the same memory qualifier.
3783 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3784 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3785 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3786 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3787 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3788 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3789 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3790 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3791 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3792 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3793 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003794 }
3795
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003796 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3797 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003798 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3799 {
3800 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3801 }
3802
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003803 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3804 if (arrayIndex != nullptr)
3805 {
3806 interfaceBlockType.makeArray(arraySize);
3807 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003808
Olli Etuaho195be942017-12-04 23:40:14 +02003809 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003810 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3811 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003812 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003813 new TVariable(&symbolTable, instanceName, interfaceBlockType,
3814 instanceName ? SymbolType::UserDefined : SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003815
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003816 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003817 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003818 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003819 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3820 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003821 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303822 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003823
3824 // set parent pointer of the field variable
3825 fieldType->setInterfaceBlock(interfaceBlock);
3826
Olli Etuaho195be942017-12-04 23:40:14 +02003827 TVariable *fieldVariable =
3828 new TVariable(&symbolTable, &field->name(), *fieldType, SymbolType::UserDefined);
3829 if (symbolTable.declareVariable(fieldVariable))
Olli Etuaho0f684632017-07-13 12:42:15 +03003830 {
3831 fieldVariable->setQualifier(typeQualifier.qualifier);
3832 }
3833 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303834 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003835 error(field->line(), "redefinition of an interface block member name",
3836 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003837 }
3838 }
3839 }
3840 else
3841 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003842 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003843
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003844 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003845 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303846 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003847 error(instanceLine, "redefinition of an interface block instance name",
3848 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003849 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003850 }
3851
Olli Etuaho195be942017-12-04 23:40:14 +02003852 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3853 blockSymbol->setLine(typeQualifier.line);
3854 TIntermDeclaration *declaration = new TIntermDeclaration();
3855 declaration->appendDeclarator(blockSymbol);
3856 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003857
3858 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003859 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003860}
3861
Olli Etuaho383b7912016-08-05 11:22:59 +03003862void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003863{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003864 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003865
3866 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003867 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303868 if (mStructNestingLevel > 1)
3869 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003870 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003871 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003872}
3873
3874void TParseContext::exitStructDeclaration()
3875{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003876 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003877}
3878
Olli Etuaho8a176262016-08-16 14:23:01 +03003879void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003880{
Jamie Madillacb4b812016-11-07 13:50:29 -05003881 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303882 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003883 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003884 }
3885
Arun Patole7e7e68d2015-05-22 12:02:25 +05303886 if (field.type()->getBasicType() != EbtStruct)
3887 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003888 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003889 }
3890
3891 // We're already inside a structure definition at this point, so add
3892 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303893 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3894 {
Jamie Madill41a49272014-03-18 16:10:13 -04003895 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003896 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3897 {
3898 // This may happen in case there are nested struct definitions. While they are also
3899 // invalid GLSL, they don't cause a syntax error.
3900 reasonStream << "Struct nesting";
3901 }
3902 else
3903 {
3904 reasonStream << "Reference of struct type "
Olli Etuahobed35d72017-12-20 16:36:26 +02003905 << field.type()->getStruct()->name().c_str();
Olli Etuahof0957992017-12-22 11:10:04 +02003906 }
3907 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003908 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003909 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003910 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003911 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003912}
3913
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003914//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003915// Parse an array index expression
3916//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003917TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3918 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303919 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003920{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003921 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3922 {
3923 if (baseExpression->getAsSymbolNode())
3924 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303925 error(location, " left of '[' is not of type array, matrix, or vector ",
3926 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003927 }
3928 else
3929 {
3930 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3931 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003932
Olli Etuaho3ec75682017-07-05 17:02:55 +03003933 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003934 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003935
Jiawei Shaod8105a02017-08-08 09:54:36 +08003936 if (baseExpression->getQualifier() == EvqPerVertexIn)
3937 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003938 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003939 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3940 {
3941 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3942 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3943 }
3944 }
3945
Jamie Madill21c1e452014-12-29 11:33:41 -05003946 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3947
Olli Etuaho36b05142015-11-12 13:10:42 +02003948 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3949 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3950 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3951 // index is a constant expression.
3952 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3953 {
3954 if (baseExpression->isInterfaceBlock())
3955 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003956 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003957 switch (baseExpression->getQualifier())
3958 {
3959 case EvqPerVertexIn:
3960 break;
3961 case EvqUniform:
3962 case EvqBuffer:
3963 error(location,
3964 "array indexes for uniform block arrays and shader storage block arrays "
3965 "must be constant integral expressions",
3966 "[");
3967 break;
3968 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003969 // We can reach here only in error cases.
3970 ASSERT(mDiagnostics->numErrors() > 0);
3971 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003972 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003973 }
3974 else if (baseExpression->getQualifier() == EvqFragmentOut)
3975 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003976 error(location,
3977 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003978 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003979 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3980 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003981 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003982 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003983 }
3984
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003985 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003986 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003987 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3988 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3989 // constant fold expressions that are not constant expressions). The most compatible way to
3990 // handle this case is to report a warning instead of an error and force the index to be in
3991 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003992 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003993 int index = 0;
3994 if (indexConstantUnion->getBasicType() == EbtInt)
3995 {
3996 index = indexConstantUnion->getIConst(0);
3997 }
3998 else if (indexConstantUnion->getBasicType() == EbtUInt)
3999 {
4000 index = static_cast<int>(indexConstantUnion->getUConst(0));
4001 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004002
4003 int safeIndex = -1;
4004
Olli Etuahoebee5b32017-11-23 12:56:32 +02004005 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004006 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004007 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4008 safeIndex = 0;
4009 }
4010
4011 if (!baseExpression->getType().isUnsizedArray())
4012 {
4013 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004014 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004015 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004016 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004017 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4018 {
4019 outOfRangeError(outOfRangeIndexIsError, location,
4020 "array index for gl_FragData must be zero when "
4021 "GL_EXT_draw_buffers is disabled",
4022 "[]");
4023 safeIndex = 0;
4024 }
4025 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004026 }
4027 // Only do generic out-of-range check if similar error hasn't already been reported.
4028 if (safeIndex < 0)
4029 {
4030 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004031 {
4032 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4033 baseExpression->getOutermostArraySize(),
4034 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004035 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004036 else if (baseExpression->isMatrix())
4037 {
4038 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4039 baseExpression->getType().getCols(),
4040 "matrix field selection out of range");
4041 }
4042 else
4043 {
4044 ASSERT(baseExpression->isVector());
4045 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4046 baseExpression->getType().getNominalSize(),
4047 "vector field selection out of range");
4048 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004049 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004050
Olli Etuahoebee5b32017-11-23 12:56:32 +02004051 ASSERT(safeIndex >= 0);
4052 // Data of constant unions can't be changed, because it may be shared with other
4053 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4054 // sanitized object.
4055 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4056 {
4057 TConstantUnion *safeConstantUnion = new TConstantUnion();
4058 safeConstantUnion->setIConst(safeIndex);
4059 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4060 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4061 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004062
Olli Etuahoebee5b32017-11-23 12:56:32 +02004063 TIntermBinary *node =
4064 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4065 node->setLine(location);
4066 return node->fold(mDiagnostics);
4067 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004068 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004069
4070 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4071 node->setLine(location);
4072 // Indirect indexing can never be constant folded.
4073 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004074}
4075
Olli Etuahoebee5b32017-11-23 12:56:32 +02004076int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4077 const TSourceLoc &location,
4078 int index,
4079 int arraySize,
4080 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004081{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004082 // Should not reach here with an unsized / runtime-sized array.
4083 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004084 // A negative index should already have been checked.
4085 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004086 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004087 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004088 std::stringstream reasonStream;
4089 reasonStream << reason << " '" << index << "'";
4090 std::string token = reasonStream.str();
4091 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004092 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004093 }
4094 return index;
4095}
4096
Jamie Madillb98c3a82015-07-23 14:26:04 -04004097TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4098 const TSourceLoc &dotLocation,
4099 const TString &fieldString,
4100 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004101{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004102 if (baseExpression->isArray())
4103 {
4104 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004105 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004106 }
4107
4108 if (baseExpression->isVector())
4109 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004110 TVector<int> fieldOffsets;
4111 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4112 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004113 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004114 fieldOffsets.resize(1);
4115 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004116 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004117 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4118 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004119
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004120 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004122 else if (baseExpression->getBasicType() == EbtStruct)
4123 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304124 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004125 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004126 {
4127 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004128 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004129 }
4130 else
4131 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004132 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004133 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004134 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004135 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004136 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004137 {
4138 fieldFound = true;
4139 break;
4140 }
4141 }
4142 if (fieldFound)
4143 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004144 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004145 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004146 TIntermBinary *node =
4147 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4148 node->setLine(dotLocation);
4149 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004150 }
4151 else
4152 {
4153 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004154 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004155 }
4156 }
4157 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004158 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004159 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304160 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004161 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004162 {
4163 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004164 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004165 }
4166 else
4167 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004168 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004169 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004170 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004171 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004172 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004173 {
4174 fieldFound = true;
4175 break;
4176 }
4177 }
4178 if (fieldFound)
4179 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004180 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004181 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004182 TIntermBinary *node =
4183 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4184 node->setLine(dotLocation);
4185 // Indexing interface blocks can never be constant folded.
4186 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004187 }
4188 else
4189 {
4190 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004191 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004192 }
4193 }
4194 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004195 else
4196 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004197 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004198 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004199 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304200 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004201 }
4202 else
4203 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304204 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004205 " field selection requires structure, vector, or interface block on left hand "
4206 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304207 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004208 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004209 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004210 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004211}
4212
Jamie Madillb98c3a82015-07-23 14:26:04 -04004213TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4214 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004215{
Jamie Madill2f294c92017-11-20 14:47:26 -05004216 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004217
4218 if (qualifierType == "shared")
4219 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004220 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004221 {
4222 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4223 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004224 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004225 }
4226 else if (qualifierType == "packed")
4227 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004228 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004229 {
4230 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4231 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004232 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004233 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004234 else if (qualifierType == "std430")
4235 {
4236 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4237 qualifier.blockStorage = EbsStd430;
4238 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004239 else if (qualifierType == "std140")
4240 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004241 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004242 }
4243 else if (qualifierType == "row_major")
4244 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004245 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004246 }
4247 else if (qualifierType == "column_major")
4248 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004249 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004250 }
4251 else if (qualifierType == "location")
4252 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004253 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4254 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004255 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004256 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004257 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004258 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4259 {
4260 qualifier.yuv = true;
4261 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004262 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004263 else if (qualifierType == "rgba32f")
4264 {
4265 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4266 qualifier.imageInternalFormat = EiifRGBA32F;
4267 }
4268 else if (qualifierType == "rgba16f")
4269 {
4270 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4271 qualifier.imageInternalFormat = EiifRGBA16F;
4272 }
4273 else if (qualifierType == "r32f")
4274 {
4275 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4276 qualifier.imageInternalFormat = EiifR32F;
4277 }
4278 else if (qualifierType == "rgba8")
4279 {
4280 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4281 qualifier.imageInternalFormat = EiifRGBA8;
4282 }
4283 else if (qualifierType == "rgba8_snorm")
4284 {
4285 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4286 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4287 }
4288 else if (qualifierType == "rgba32i")
4289 {
4290 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4291 qualifier.imageInternalFormat = EiifRGBA32I;
4292 }
4293 else if (qualifierType == "rgba16i")
4294 {
4295 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4296 qualifier.imageInternalFormat = EiifRGBA16I;
4297 }
4298 else if (qualifierType == "rgba8i")
4299 {
4300 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4301 qualifier.imageInternalFormat = EiifRGBA8I;
4302 }
4303 else if (qualifierType == "r32i")
4304 {
4305 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4306 qualifier.imageInternalFormat = EiifR32I;
4307 }
4308 else if (qualifierType == "rgba32ui")
4309 {
4310 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4311 qualifier.imageInternalFormat = EiifRGBA32UI;
4312 }
4313 else if (qualifierType == "rgba16ui")
4314 {
4315 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4316 qualifier.imageInternalFormat = EiifRGBA16UI;
4317 }
4318 else if (qualifierType == "rgba8ui")
4319 {
4320 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4321 qualifier.imageInternalFormat = EiifRGBA8UI;
4322 }
4323 else if (qualifierType == "r32ui")
4324 {
4325 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4326 qualifier.imageInternalFormat = EiifR32UI;
4327 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004328 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4329 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004330 {
4331 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4332 qualifier.primitiveType = EptPoints;
4333 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004334 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4335 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004336 {
4337 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4338 qualifier.primitiveType = EptLines;
4339 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004340 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4341 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004342 {
4343 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4344 qualifier.primitiveType = EptLinesAdjacency;
4345 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004346 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4347 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004348 {
4349 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4350 qualifier.primitiveType = EptTriangles;
4351 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004352 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4353 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004354 {
4355 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4356 qualifier.primitiveType = EptTrianglesAdjacency;
4357 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004358 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4359 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004360 {
4361 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4362 qualifier.primitiveType = EptLineStrip;
4363 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004364 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4365 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004366 {
4367 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4368 qualifier.primitiveType = EptTriangleStrip;
4369 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004370
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004371 else
4372 {
4373 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004374 }
4375
Jamie Madilla5efff92013-06-06 11:56:47 -04004376 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004377}
4378
Martin Radev802abe02016-08-04 17:48:32 +03004379void TParseContext::parseLocalSize(const TString &qualifierType,
4380 const TSourceLoc &qualifierTypeLine,
4381 int intValue,
4382 const TSourceLoc &intValueLine,
4383 const std::string &intValueString,
4384 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004385 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004386{
Olli Etuaho856c4972016-08-08 11:38:39 +03004387 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004388 if (intValue < 1)
4389 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004390 std::stringstream reasonStream;
4391 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4392 std::string reason = reasonStream.str();
4393 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004394 }
4395 (*localSize)[index] = intValue;
4396}
4397
Olli Etuaho09b04a22016-12-15 13:30:26 +00004398void TParseContext::parseNumViews(int intValue,
4399 const TSourceLoc &intValueLine,
4400 const std::string &intValueString,
4401 int *numViews)
4402{
4403 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4404 // specification.
4405 if (intValue < 1)
4406 {
4407 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4408 }
4409 *numViews = intValue;
4410}
4411
Shaob5cc1192017-07-06 10:47:20 +08004412void TParseContext::parseInvocations(int intValue,
4413 const TSourceLoc &intValueLine,
4414 const std::string &intValueString,
4415 int *numInvocations)
4416{
4417 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4418 // it doesn't make sense to accept invocations <= 0.
4419 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4420 {
4421 error(intValueLine,
4422 "out of range: invocations must be in the range of [1, "
4423 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4424 intValueString.c_str());
4425 }
4426 else
4427 {
4428 *numInvocations = intValue;
4429 }
4430}
4431
4432void TParseContext::parseMaxVertices(int intValue,
4433 const TSourceLoc &intValueLine,
4434 const std::string &intValueString,
4435 int *maxVertices)
4436{
4437 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4438 // it doesn't make sense to accept max_vertices < 0.
4439 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4440 {
4441 error(
4442 intValueLine,
4443 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4444 intValueString.c_str());
4445 }
4446 else
4447 {
4448 *maxVertices = intValue;
4449 }
4450}
4451
Jamie Madillb98c3a82015-07-23 14:26:04 -04004452TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4453 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004454 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304455 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004456{
Jamie Madill2f294c92017-11-20 14:47:26 -05004457 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004458
Martin Radev802abe02016-08-04 17:48:32 +03004459 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004460
Martin Radev802abe02016-08-04 17:48:32 +03004461 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004462 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004463 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004464 if (intValue < 0)
4465 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004466 error(intValueLine, "out of range: location must be non-negative",
4467 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004468 }
4469 else
4470 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004471 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004472 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004473 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004474 }
Olli Etuaho43364892017-02-13 16:00:12 +00004475 else if (qualifierType == "binding")
4476 {
4477 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4478 if (intValue < 0)
4479 {
4480 error(intValueLine, "out of range: binding must be non-negative",
4481 intValueString.c_str());
4482 }
4483 else
4484 {
4485 qualifier.binding = intValue;
4486 }
4487 }
jchen104cdac9e2017-05-08 11:01:20 +08004488 else if (qualifierType == "offset")
4489 {
4490 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4491 if (intValue < 0)
4492 {
4493 error(intValueLine, "out of range: offset must be non-negative",
4494 intValueString.c_str());
4495 }
4496 else
4497 {
4498 qualifier.offset = intValue;
4499 }
4500 }
Martin Radev802abe02016-08-04 17:48:32 +03004501 else if (qualifierType == "local_size_x")
4502 {
4503 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4504 &qualifier.localSize);
4505 }
4506 else if (qualifierType == "local_size_y")
4507 {
4508 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4509 &qualifier.localSize);
4510 }
4511 else if (qualifierType == "local_size_z")
4512 {
4513 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4514 &qualifier.localSize);
4515 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004516 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004517 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004518 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4519 {
4520 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4521 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004522 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004523 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4524 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004525 {
4526 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4527 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004528 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4529 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004530 {
4531 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4532 }
4533
Martin Radev802abe02016-08-04 17:48:32 +03004534 else
4535 {
4536 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004537 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004538
Jamie Madilla5efff92013-06-06 11:56:47 -04004539 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004540}
4541
Olli Etuaho613b9592016-09-05 12:05:53 +03004542TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4543{
4544 return new TTypeQualifierBuilder(
4545 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4546 mShaderVersion);
4547}
4548
Olli Etuahocce89652017-06-19 16:04:09 +03004549TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4550 const TSourceLoc &loc)
4551{
4552 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4553 return new TStorageQualifierWrapper(qualifier, loc);
4554}
4555
4556TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4557{
4558 if (getShaderType() == GL_VERTEX_SHADER)
4559 {
4560 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4561 }
4562 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4563}
4564
4565TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4566{
4567 if (declaringFunction())
4568 {
4569 return new TStorageQualifierWrapper(EvqIn, loc);
4570 }
Shaob5cc1192017-07-06 10:47:20 +08004571
4572 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004573 {
Shaob5cc1192017-07-06 10:47:20 +08004574 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004575 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004576 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004577 {
4578 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4579 }
4580 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004581 }
Shaob5cc1192017-07-06 10:47:20 +08004582 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004583 {
Shaob5cc1192017-07-06 10:47:20 +08004584 if (mShaderVersion < 300)
4585 {
4586 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4587 }
4588 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004589 }
Shaob5cc1192017-07-06 10:47:20 +08004590 case GL_COMPUTE_SHADER:
4591 {
4592 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4593 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004594 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004595 {
4596 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4597 }
4598 default:
4599 {
4600 UNREACHABLE();
4601 return new TStorageQualifierWrapper(EvqLast, loc);
4602 }
Olli Etuahocce89652017-06-19 16:04:09 +03004603 }
Olli Etuahocce89652017-06-19 16:04:09 +03004604}
4605
4606TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4607{
4608 if (declaringFunction())
4609 {
4610 return new TStorageQualifierWrapper(EvqOut, loc);
4611 }
Shaob5cc1192017-07-06 10:47:20 +08004612 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004613 {
Shaob5cc1192017-07-06 10:47:20 +08004614 case GL_VERTEX_SHADER:
4615 {
4616 if (mShaderVersion < 300)
4617 {
4618 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4619 }
4620 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4621 }
4622 case GL_FRAGMENT_SHADER:
4623 {
4624 if (mShaderVersion < 300)
4625 {
4626 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4627 }
4628 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4629 }
4630 case GL_COMPUTE_SHADER:
4631 {
4632 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4633 return new TStorageQualifierWrapper(EvqLast, loc);
4634 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004635 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004636 {
4637 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4638 }
4639 default:
4640 {
4641 UNREACHABLE();
4642 return new TStorageQualifierWrapper(EvqLast, loc);
4643 }
Olli Etuahocce89652017-06-19 16:04:09 +03004644 }
Olli Etuahocce89652017-06-19 16:04:09 +03004645}
4646
4647TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4648{
4649 if (!declaringFunction())
4650 {
4651 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4652 }
4653 return new TStorageQualifierWrapper(EvqInOut, loc);
4654}
4655
Jamie Madillb98c3a82015-07-23 14:26:04 -04004656TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004657 TLayoutQualifier rightQualifier,
4658 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004659{
Martin Radevc28888b2016-07-22 15:27:42 +03004660 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004661 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004662}
4663
Olli Etuahod5f44c92017-11-29 17:15:40 +02004664TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004665{
4666 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004667 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004668}
4669
Olli Etuahod5f44c92017-11-29 17:15:40 +02004670TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4671 const TSourceLoc &loc,
4672 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004673{
4674 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004675 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004676}
4677
Olli Etuaho722bfb52017-10-26 17:00:11 +03004678void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4679 const TFieldList::const_iterator end,
4680 const TString &name,
4681 const TSourceLoc &location)
4682{
4683 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4684 {
4685 if ((*fieldIter)->name() == name)
4686 {
4687 error(location, "duplicate field name in structure", name.c_str());
4688 }
4689 }
4690}
4691
4692TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4693{
4694 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4695 ++fieldIter)
4696 {
4697 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4698 location);
4699 }
4700 return fields;
4701}
4702
Olli Etuaho4de340a2016-12-16 09:32:03 +00004703TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4704 const TFieldList *newlyAddedFields,
4705 const TSourceLoc &location)
4706{
4707 for (TField *field : *newlyAddedFields)
4708 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004709 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4710 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004711 processedFields->push_back(field);
4712 }
4713 return processedFields;
4714}
4715
Martin Radev70866b82016-07-22 15:27:42 +03004716TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4717 const TTypeQualifierBuilder &typeQualifierBuilder,
4718 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004719 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004720{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004721 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004722
Martin Radev70866b82016-07-22 15:27:42 +03004723 typeSpecifier->qualifier = typeQualifier.qualifier;
4724 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004725 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004726 typeSpecifier->invariant = typeQualifier.invariant;
4727 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304728 {
Martin Radev70866b82016-07-22 15:27:42 +03004729 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004730 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004731 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004732}
4733
Jamie Madillb98c3a82015-07-23 14:26:04 -04004734TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004735 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004736{
Martin Radev4a9cd802016-09-01 16:51:51 +03004737 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4738 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004739
Olli Etuahod5f44c92017-11-29 17:15:40 +02004740 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004741 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004742
Martin Radev4a9cd802016-09-01 16:51:51 +03004743 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004744
Olli Etuahod5f44c92017-11-29 17:15:40 +02004745 TFieldList *fieldList = new TFieldList();
4746
4747 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304748 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004749 TType *type = new TType(typeSpecifier);
4750 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304751 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004752 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004753 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004754 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004755 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004756
Olli Etuahod5f44c92017-11-29 17:15:40 +02004757 TField *field = new TField(type, declarator->name(), declarator->line());
4758 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4759 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004760 }
4761
Olli Etuahod5f44c92017-11-29 17:15:40 +02004762 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004763}
4764
Martin Radev4a9cd802016-09-01 16:51:51 +03004765TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4766 const TSourceLoc &nameLine,
4767 const TString *structName,
4768 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004769{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004770 SymbolType structSymbolType = SymbolType::UserDefined;
4771 if (structName == nullptr)
4772 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004773 structSymbolType = SymbolType::Empty;
4774 }
4775 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004776
Jamie Madill9b820842015-02-12 10:40:10 -05004777 // Store a bool in the struct if we're at global scope, to allow us to
4778 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004779 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004780
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004781 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004782 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004783 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004784 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304785 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004786 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004787 }
4788 }
4789
4790 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004791 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004792 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004793 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004794 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004795 switch (qualifier)
4796 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004797 case EvqGlobal:
4798 case EvqTemporary:
4799 break;
4800 default:
4801 error(field.line(), "invalid qualifier on struct member",
4802 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004803 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004804 }
Martin Radev70866b82016-07-22 15:27:42 +03004805 if (field.type()->isInvariant())
4806 {
4807 error(field.line(), "invalid qualifier on struct member", "invariant");
4808 }
jchen104cdac9e2017-05-08 11:01:20 +08004809 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4810 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004811 {
4812 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4813 }
4814
Olli Etuahoebee5b32017-11-23 12:56:32 +02004815 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4816 field.name().c_str(), field.type());
4817
Olli Etuaho43364892017-02-13 16:00:12 +00004818 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4819
4820 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004821
4822 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004823 }
4824
Martin Radev4a9cd802016-09-01 16:51:51 +03004825 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004826 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004827 exitStructDeclaration();
4828
Martin Radev4a9cd802016-09-01 16:51:51 +03004829 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004830}
4831
Jamie Madillb98c3a82015-07-23 14:26:04 -04004832TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004833 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004834 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004835{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004836 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004837 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004838 init->isVector())
4839 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004840 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4841 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004842 return nullptr;
4843 }
4844
Olli Etuaho923ecef2017-10-11 12:01:38 +03004845 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004846 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004847 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004848 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004849 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004850 }
4851
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004852 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4853 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004854 return node;
4855}
4856
4857TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4858{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004859 if (mSwitchNestingLevel == 0)
4860 {
4861 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004862 return nullptr;
4863 }
4864 if (condition == nullptr)
4865 {
4866 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004867 return nullptr;
4868 }
4869 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004870 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004871 {
4872 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004873 }
4874 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004875 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4876 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4877 // fold in case labels.
4878 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004879 {
4880 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004881 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004882 TIntermCase *node = new TIntermCase(condition);
4883 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004884 return node;
4885}
4886
4887TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4888{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004889 if (mSwitchNestingLevel == 0)
4890 {
4891 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004892 return nullptr;
4893 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004894 TIntermCase *node = new TIntermCase(nullptr);
4895 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004896 return node;
4897}
4898
Jamie Madillb98c3a82015-07-23 14:26:04 -04004899TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4900 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004901 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004902{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004903 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004904
4905 switch (op)
4906 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004907 case EOpLogicalNot:
4908 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4909 child->isVector())
4910 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004911 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004912 return nullptr;
4913 }
4914 break;
4915 case EOpBitwiseNot:
4916 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4917 child->isMatrix() || child->isArray())
4918 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004919 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004920 return nullptr;
4921 }
4922 break;
4923 case EOpPostIncrement:
4924 case EOpPreIncrement:
4925 case EOpPostDecrement:
4926 case EOpPreDecrement:
4927 case EOpNegative:
4928 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004929 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4930 child->getBasicType() == EbtBool || child->isArray() ||
4931 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004932 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004933 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004934 return nullptr;
4935 }
4936 // Operators for built-ins are already type checked against their prototype.
4937 default:
4938 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004939 }
4940
Jiajia Qinbc585152017-06-23 15:42:17 +08004941 if (child->getMemoryQualifier().writeonly)
4942 {
4943 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4944 return nullptr;
4945 }
4946
Olli Etuahof119a262016-08-19 15:54:22 +03004947 TIntermUnary *node = new TIntermUnary(op, child);
4948 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004949
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004950 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004951}
4952
Olli Etuaho09b22472015-02-11 11:47:26 +02004953TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4954{
Olli Etuahocce89652017-06-19 16:04:09 +03004955 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004956 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004957 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004958 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004959 return child;
4960 }
4961 return node;
4962}
4963
Jamie Madillb98c3a82015-07-23 14:26:04 -04004964TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4965 TIntermTyped *child,
4966 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004967{
Olli Etuaho856c4972016-08-08 11:38:39 +03004968 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004969 return addUnaryMath(op, child, loc);
4970}
4971
Jamie Madillb98c3a82015-07-23 14:26:04 -04004972bool TParseContext::binaryOpCommonCheck(TOperator op,
4973 TIntermTyped *left,
4974 TIntermTyped *right,
4975 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004976{
jchen10b4cf5652017-05-05 18:51:17 +08004977 // Check opaque types are not allowed to be operands in expressions other than array indexing
4978 // and structure member selection.
4979 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4980 {
4981 switch (op)
4982 {
4983 case EOpIndexDirect:
4984 case EOpIndexIndirect:
4985 break;
4986 case EOpIndexDirectStruct:
4987 UNREACHABLE();
4988
4989 default:
4990 error(loc, "Invalid operation for variables with an opaque type",
4991 GetOperatorString(op));
4992 return false;
4993 }
4994 }
jchen10cc2a10e2017-05-03 14:05:12 +08004995
Jiajia Qinbc585152017-06-23 15:42:17 +08004996 if (right->getMemoryQualifier().writeonly)
4997 {
4998 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4999 return false;
5000 }
5001
5002 if (left->getMemoryQualifier().writeonly)
5003 {
5004 switch (op)
5005 {
5006 case EOpAssign:
5007 case EOpInitialize:
5008 case EOpIndexDirect:
5009 case EOpIndexIndirect:
5010 case EOpIndexDirectStruct:
5011 case EOpIndexDirectInterfaceBlock:
5012 break;
5013 default:
5014 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5015 return false;
5016 }
5017 }
5018
Olli Etuaho244be012016-08-18 15:26:02 +03005019 if (left->getType().getStruct() || right->getType().getStruct())
5020 {
5021 switch (op)
5022 {
5023 case EOpIndexDirectStruct:
5024 ASSERT(left->getType().getStruct());
5025 break;
5026 case EOpEqual:
5027 case EOpNotEqual:
5028 case EOpAssign:
5029 case EOpInitialize:
5030 if (left->getType() != right->getType())
5031 {
5032 return false;
5033 }
5034 break;
5035 default:
5036 error(loc, "Invalid operation for structs", GetOperatorString(op));
5037 return false;
5038 }
5039 }
5040
Olli Etuaho94050052017-05-08 14:17:44 +03005041 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5042 {
5043 switch (op)
5044 {
5045 case EOpIndexDirectInterfaceBlock:
5046 ASSERT(left->getType().getInterfaceBlock());
5047 break;
5048 default:
5049 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5050 return false;
5051 }
5052 }
5053
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005054 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005055 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005056 error(loc, "array / non-array mismatch", GetOperatorString(op));
5057 return false;
5058 }
5059
5060 if (left->isArray())
5061 {
5062 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005063 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005064 {
5065 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5066 return false;
5067 }
5068
Olli Etuahoe79904c2015-03-18 16:56:42 +02005069 switch (op)
5070 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005071 case EOpEqual:
5072 case EOpNotEqual:
5073 case EOpAssign:
5074 case EOpInitialize:
5075 break;
5076 default:
5077 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5078 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005079 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005080 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005081 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005082 {
5083 error(loc, "array size mismatch", GetOperatorString(op));
5084 return false;
5085 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005086 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005087
5088 // Check ops which require integer / ivec parameters
5089 bool isBitShift = false;
5090 switch (op)
5091 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005092 case EOpBitShiftLeft:
5093 case EOpBitShiftRight:
5094 case EOpBitShiftLeftAssign:
5095 case EOpBitShiftRightAssign:
5096 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5097 // check that the basic type is an integer type.
5098 isBitShift = true;
5099 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5100 {
5101 return false;
5102 }
5103 break;
5104 case EOpBitwiseAnd:
5105 case EOpBitwiseXor:
5106 case EOpBitwiseOr:
5107 case EOpBitwiseAndAssign:
5108 case EOpBitwiseXorAssign:
5109 case EOpBitwiseOrAssign:
5110 // It is enough to check the type of only one operand, since later it
5111 // is checked that the operand types match.
5112 if (!IsInteger(left->getBasicType()))
5113 {
5114 return false;
5115 }
5116 break;
5117 default:
5118 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005119 }
5120
5121 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5122 // So the basic type should usually match.
5123 if (!isBitShift && left->getBasicType() != right->getBasicType())
5124 {
5125 return false;
5126 }
5127
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005128 // Check that:
5129 // 1. Type sizes match exactly on ops that require that.
5130 // 2. Restrictions for structs that contain arrays or samplers are respected.
5131 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005132 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005133 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005134 case EOpAssign:
5135 case EOpInitialize:
5136 case EOpEqual:
5137 case EOpNotEqual:
5138 // ESSL 1.00 sections 5.7, 5.8, 5.9
5139 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5140 {
5141 error(loc, "undefined operation for structs containing arrays",
5142 GetOperatorString(op));
5143 return false;
5144 }
5145 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5146 // we interpret the spec so that this extends to structs containing samplers,
5147 // similarly to ESSL 1.00 spec.
5148 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5149 left->getType().isStructureContainingSamplers())
5150 {
5151 error(loc, "undefined operation for structs containing samplers",
5152 GetOperatorString(op));
5153 return false;
5154 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005155
Olli Etuahoe1805592017-01-02 16:41:20 +00005156 if ((left->getNominalSize() != right->getNominalSize()) ||
5157 (left->getSecondarySize() != right->getSecondarySize()))
5158 {
5159 error(loc, "dimension mismatch", GetOperatorString(op));
5160 return false;
5161 }
5162 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005163 case EOpLessThan:
5164 case EOpGreaterThan:
5165 case EOpLessThanEqual:
5166 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005167 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005168 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005169 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005170 return false;
5171 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005172 break;
5173 case EOpAdd:
5174 case EOpSub:
5175 case EOpDiv:
5176 case EOpIMod:
5177 case EOpBitShiftLeft:
5178 case EOpBitShiftRight:
5179 case EOpBitwiseAnd:
5180 case EOpBitwiseXor:
5181 case EOpBitwiseOr:
5182 case EOpAddAssign:
5183 case EOpSubAssign:
5184 case EOpDivAssign:
5185 case EOpIModAssign:
5186 case EOpBitShiftLeftAssign:
5187 case EOpBitShiftRightAssign:
5188 case EOpBitwiseAndAssign:
5189 case EOpBitwiseXorAssign:
5190 case EOpBitwiseOrAssign:
5191 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5192 {
5193 return false;
5194 }
5195
5196 // Are the sizes compatible?
5197 if (left->getNominalSize() != right->getNominalSize() ||
5198 left->getSecondarySize() != right->getSecondarySize())
5199 {
5200 // If the nominal sizes of operands do not match:
5201 // One of them must be a scalar.
5202 if (!left->isScalar() && !right->isScalar())
5203 return false;
5204
5205 // In the case of compound assignment other than multiply-assign,
5206 // the right side needs to be a scalar. Otherwise a vector/matrix
5207 // would be assigned to a scalar. A scalar can't be shifted by a
5208 // vector either.
5209 if (!right->isScalar() &&
5210 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5211 return false;
5212 }
5213 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005214 default:
5215 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005216 }
5217
Olli Etuahod6b14282015-03-17 14:31:35 +02005218 return true;
5219}
5220
Olli Etuaho1dded802016-08-18 18:13:13 +03005221bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5222 const TType &left,
5223 const TType &right)
5224{
5225 switch (op)
5226 {
5227 case EOpMul:
5228 case EOpMulAssign:
5229 return left.getNominalSize() == right.getNominalSize() &&
5230 left.getSecondarySize() == right.getSecondarySize();
5231 case EOpVectorTimesScalar:
5232 return true;
5233 case EOpVectorTimesScalarAssign:
5234 ASSERT(!left.isMatrix() && !right.isMatrix());
5235 return left.isVector() && !right.isVector();
5236 case EOpVectorTimesMatrix:
5237 return left.getNominalSize() == right.getRows();
5238 case EOpVectorTimesMatrixAssign:
5239 ASSERT(!left.isMatrix() && right.isMatrix());
5240 return left.isVector() && left.getNominalSize() == right.getRows() &&
5241 left.getNominalSize() == right.getCols();
5242 case EOpMatrixTimesVector:
5243 return left.getCols() == right.getNominalSize();
5244 case EOpMatrixTimesScalar:
5245 return true;
5246 case EOpMatrixTimesScalarAssign:
5247 ASSERT(left.isMatrix() && !right.isMatrix());
5248 return !right.isVector();
5249 case EOpMatrixTimesMatrix:
5250 return left.getCols() == right.getRows();
5251 case EOpMatrixTimesMatrixAssign:
5252 ASSERT(left.isMatrix() && right.isMatrix());
5253 // We need to check two things:
5254 // 1. The matrix multiplication step is valid.
5255 // 2. The result will have the same number of columns as the lvalue.
5256 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5257
5258 default:
5259 UNREACHABLE();
5260 return false;
5261 }
5262}
5263
Jamie Madillb98c3a82015-07-23 14:26:04 -04005264TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5265 TIntermTyped *left,
5266 TIntermTyped *right,
5267 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005268{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005269 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005270 return nullptr;
5271
Olli Etuahofc1806e2015-03-17 13:03:11 +02005272 switch (op)
5273 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005274 case EOpEqual:
5275 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005276 case EOpLessThan:
5277 case EOpGreaterThan:
5278 case EOpLessThanEqual:
5279 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005280 break;
5281 case EOpLogicalOr:
5282 case EOpLogicalXor:
5283 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005284 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5285 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005286 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005287 {
5288 return nullptr;
5289 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005290 // Basic types matching should have been already checked.
5291 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005292 break;
5293 case EOpAdd:
5294 case EOpSub:
5295 case EOpDiv:
5296 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005297 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5298 !right->getType().getStruct());
5299 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005300 {
5301 return nullptr;
5302 }
5303 break;
5304 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005305 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5306 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005307 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005308 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005309 {
5310 return nullptr;
5311 }
5312 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005313 default:
5314 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005315 }
5316
Olli Etuaho1dded802016-08-18 18:13:13 +03005317 if (op == EOpMul)
5318 {
5319 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5320 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5321 {
5322 return nullptr;
5323 }
5324 }
5325
Olli Etuaho3fdec912016-08-18 15:08:06 +03005326 TIntermBinary *node = new TIntermBinary(op, left, right);
5327 node->setLine(loc);
5328
Olli Etuaho3fdec912016-08-18 15:08:06 +03005329 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005330 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005331}
5332
Jamie Madillb98c3a82015-07-23 14:26:04 -04005333TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5334 TIntermTyped *left,
5335 TIntermTyped *right,
5336 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005337{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005338 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005339 if (node == 0)
5340 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005341 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5342 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005343 return left;
5344 }
5345 return node;
5346}
5347
Jamie Madillb98c3a82015-07-23 14:26:04 -04005348TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5349 TIntermTyped *left,
5350 TIntermTyped *right,
5351 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005352{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005353 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005354 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005355 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005356 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5357 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005358 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005359 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005360 }
5361 return node;
5362}
5363
Olli Etuaho13389b62016-10-16 11:48:18 +01005364TIntermBinary *TParseContext::createAssign(TOperator op,
5365 TIntermTyped *left,
5366 TIntermTyped *right,
5367 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005368{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005369 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005370 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005371 if (op == EOpMulAssign)
5372 {
5373 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5374 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5375 {
5376 return nullptr;
5377 }
5378 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005379 TIntermBinary *node = new TIntermBinary(op, left, right);
5380 node->setLine(loc);
5381
Olli Etuaho3fdec912016-08-18 15:08:06 +03005382 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005383 }
5384 return nullptr;
5385}
5386
Jamie Madillb98c3a82015-07-23 14:26:04 -04005387TIntermTyped *TParseContext::addAssign(TOperator op,
5388 TIntermTyped *left,
5389 TIntermTyped *right,
5390 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005391{
Olli Etuahocce89652017-06-19 16:04:09 +03005392 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005393 TIntermTyped *node = createAssign(op, left, right, loc);
5394 if (node == nullptr)
5395 {
5396 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005397 return left;
5398 }
5399 return node;
5400}
5401
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005402TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5403 TIntermTyped *right,
5404 const TSourceLoc &loc)
5405{
Corentin Wallez0d959252016-07-12 17:26:32 -04005406 // WebGL2 section 5.26, the following results in an error:
5407 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005408 if (mShaderSpec == SH_WEBGL2_SPEC &&
5409 (left->isArray() || left->getBasicType() == EbtVoid ||
5410 left->getType().isStructureContainingArrays() || right->isArray() ||
5411 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005412 {
5413 error(loc,
5414 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5415 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005416 }
5417
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005418 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5419 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5420 commaNode->getTypePointer()->setQualifier(resultQualifier);
5421 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005422}
5423
Olli Etuaho49300862015-02-20 14:54:49 +02005424TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5425{
5426 switch (op)
5427 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005428 case EOpContinue:
5429 if (mLoopNestingLevel <= 0)
5430 {
5431 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005432 }
5433 break;
5434 case EOpBreak:
5435 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5436 {
5437 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005438 }
5439 break;
5440 case EOpReturn:
5441 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5442 {
5443 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005444 }
5445 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005446 case EOpKill:
5447 if (mShaderType != GL_FRAGMENT_SHADER)
5448 {
5449 error(loc, "discard supported in fragment shaders only", "discard");
5450 }
5451 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005452 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005453 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005454 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005455 }
Olli Etuahocce89652017-06-19 16:04:09 +03005456 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005457}
5458
Jamie Madillb98c3a82015-07-23 14:26:04 -04005459TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005460 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005461 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005462{
Olli Etuahocce89652017-06-19 16:04:09 +03005463 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005464 {
Olli Etuahocce89652017-06-19 16:04:09 +03005465 ASSERT(op == EOpReturn);
5466 mFunctionReturnsValue = true;
5467 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5468 {
5469 error(loc, "void function cannot return a value", "return");
5470 }
5471 else if (*mCurrentFunctionType != expression->getType())
5472 {
5473 error(loc, "function return is not matching type:", "return");
5474 }
Olli Etuaho49300862015-02-20 14:54:49 +02005475 }
Olli Etuahocce89652017-06-19 16:04:09 +03005476 TIntermBranch *node = new TIntermBranch(op, expression);
5477 node->setLine(loc);
5478 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005479}
5480
Martin Radev84aa2dc2017-09-11 15:51:02 +03005481void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5482{
5483 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005484 const TString &name = functionCall->getFunction()->name();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005485 bool isTextureGather = (name == "textureGather");
5486 bool isTextureGatherOffset = (name == "textureGatherOffset");
5487 if (isTextureGather || isTextureGatherOffset)
5488 {
5489 TIntermNode *componentNode = nullptr;
5490 TIntermSequence *arguments = functionCall->getSequence();
5491 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5492 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5493 ASSERT(sampler != nullptr);
5494 switch (sampler->getBasicType())
5495 {
5496 case EbtSampler2D:
5497 case EbtISampler2D:
5498 case EbtUSampler2D:
5499 case EbtSampler2DArray:
5500 case EbtISampler2DArray:
5501 case EbtUSampler2DArray:
5502 if ((isTextureGather && arguments->size() == 3u) ||
5503 (isTextureGatherOffset && arguments->size() == 4u))
5504 {
5505 componentNode = arguments->back();
5506 }
5507 break;
5508 case EbtSamplerCube:
5509 case EbtISamplerCube:
5510 case EbtUSamplerCube:
5511 ASSERT(!isTextureGatherOffset);
5512 if (arguments->size() == 3u)
5513 {
5514 componentNode = arguments->back();
5515 }
5516 break;
5517 case EbtSampler2DShadow:
5518 case EbtSampler2DArrayShadow:
5519 case EbtSamplerCubeShadow:
5520 break;
5521 default:
5522 UNREACHABLE();
5523 break;
5524 }
5525 if (componentNode)
5526 {
5527 const TIntermConstantUnion *componentConstantUnion =
5528 componentNode->getAsConstantUnion();
5529 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5530 {
5531 error(functionCall->getLine(), "Texture component must be a constant expression",
5532 name.c_str());
5533 }
5534 else
5535 {
5536 int component = componentConstantUnion->getIConst(0);
5537 if (component < 0 || component > 3)
5538 {
5539 error(functionCall->getLine(), "Component must be in the range [0;3]",
5540 name.c_str());
5541 }
5542 }
5543 }
5544 }
5545}
5546
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005547void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5548{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005549 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005550 const TString &name = functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005551 TIntermNode *offset = nullptr;
5552 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005553 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005554 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5555 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5556 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005557 {
5558 offset = arguments->back();
5559 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005560 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005561 {
5562 // A bias parameter might follow the offset parameter.
5563 ASSERT(arguments->size() >= 3);
5564 offset = (*arguments)[2];
5565 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005566 else if (name == "textureGatherOffset")
5567 {
5568 ASSERT(arguments->size() >= 3u);
5569 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5570 ASSERT(sampler != nullptr);
5571 switch (sampler->getBasicType())
5572 {
5573 case EbtSampler2D:
5574 case EbtISampler2D:
5575 case EbtUSampler2D:
5576 case EbtSampler2DArray:
5577 case EbtISampler2DArray:
5578 case EbtUSampler2DArray:
5579 offset = (*arguments)[2];
5580 break;
5581 case EbtSampler2DShadow:
5582 case EbtSampler2DArrayShadow:
5583 offset = (*arguments)[3];
5584 break;
5585 default:
5586 UNREACHABLE();
5587 break;
5588 }
5589 useTextureGatherOffsetConstraints = true;
5590 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005591 if (offset != nullptr)
5592 {
5593 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5594 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5595 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005596 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005597 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005598 }
5599 else
5600 {
5601 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5602 size_t size = offsetConstantUnion->getType().getObjectSize();
5603 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005604 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5605 : mMinProgramTexelOffset;
5606 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5607 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005608 for (size_t i = 0u; i < size; ++i)
5609 {
5610 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005611 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005612 {
5613 std::stringstream tokenStream;
5614 tokenStream << offsetValue;
5615 std::string token = tokenStream.str();
5616 error(offset->getLine(), "Texture offset value out of valid range",
5617 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005618 }
5619 }
5620 }
5621 }
5622}
5623
Jiajia Qina3106c52017-11-03 09:39:39 +08005624void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5625{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005626 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005627 const TString &functionName = functionCall->getFunction()->name();
5628 if (IsAtomicBuiltin(functionName))
Jiajia Qina3106c52017-11-03 09:39:39 +08005629 {
5630 TIntermSequence *arguments = functionCall->getSequence();
5631 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5632
5633 if (IsBufferOrSharedVariable(memNode))
5634 {
5635 return;
5636 }
5637
5638 while (memNode->getAsBinaryNode())
5639 {
5640 memNode = memNode->getAsBinaryNode()->getLeft();
5641 if (IsBufferOrSharedVariable(memNode))
5642 {
5643 return;
5644 }
5645 }
5646
5647 error(memNode->getLine(),
5648 "The value passed to the mem argument of an atomic memory function does not "
5649 "correspond to a buffer or shared variable.",
Olli Etuahobed35d72017-12-20 16:36:26 +02005650 functionName.c_str());
Jiajia Qina3106c52017-11-03 09:39:39 +08005651 }
5652}
5653
Martin Radev2cc85b32016-08-05 16:22:53 +03005654// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5655void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5656{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005657 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005658 const TString &name = functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005659
5660 if (name.compare(0, 5, "image") == 0)
5661 {
5662 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005663 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005664
Olli Etuaho485eefd2017-02-14 17:40:06 +00005665 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005666
5667 if (name.compare(5, 5, "Store") == 0)
5668 {
5669 if (memoryQualifier.readonly)
5670 {
5671 error(imageNode->getLine(),
5672 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005673 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005674 }
5675 }
5676 else if (name.compare(5, 4, "Load") == 0)
5677 {
5678 if (memoryQualifier.writeonly)
5679 {
5680 error(imageNode->getLine(),
5681 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005682 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005683 }
5684 }
5685 }
5686}
5687
5688// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5689void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5690 const TFunction *functionDefinition,
5691 const TIntermAggregate *functionCall)
5692{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005693 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005694
5695 const TIntermSequence &arguments = *functionCall->getSequence();
5696
5697 ASSERT(functionDefinition->getParamCount() == arguments.size());
5698
5699 for (size_t i = 0; i < arguments.size(); ++i)
5700 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005701 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5702 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005703 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5704 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5705
5706 if (IsImage(functionArgumentType.getBasicType()))
5707 {
5708 const TMemoryQualifier &functionArgumentMemoryQualifier =
5709 functionArgumentType.getMemoryQualifier();
5710 const TMemoryQualifier &functionParameterMemoryQualifier =
5711 functionParameterType.getMemoryQualifier();
5712 if (functionArgumentMemoryQualifier.readonly &&
5713 !functionParameterMemoryQualifier.readonly)
5714 {
5715 error(functionCall->getLine(),
5716 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005717 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005718 }
5719
5720 if (functionArgumentMemoryQualifier.writeonly &&
5721 !functionParameterMemoryQualifier.writeonly)
5722 {
5723 error(functionCall->getLine(),
5724 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005725 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005726 }
Martin Radev049edfa2016-11-11 14:35:37 +02005727
5728 if (functionArgumentMemoryQualifier.coherent &&
5729 !functionParameterMemoryQualifier.coherent)
5730 {
5731 error(functionCall->getLine(),
5732 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005733 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005734 }
5735
5736 if (functionArgumentMemoryQualifier.volatileQualifier &&
5737 !functionParameterMemoryQualifier.volatileQualifier)
5738 {
5739 error(functionCall->getLine(),
5740 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005741 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005742 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005743 }
5744 }
5745}
5746
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005747TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005748{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005749 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005750}
5751
5752TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005753 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005754 TIntermNode *thisNode,
5755 const TSourceLoc &loc)
5756{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005757 if (thisNode != nullptr)
5758 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005759 return addMethod(fnCall->name(), arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005760 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005761
5762 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005763 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005764 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005765 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005766 }
5767 else
5768 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005769 ASSERT(op == EOpNull);
Olli Etuaho0c371002017-12-13 17:00:25 +04005770 return addNonConstructorFunctionCall(fnCall->name(), arguments, loc);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005771 }
5772}
5773
Olli Etuahobed35d72017-12-20 16:36:26 +02005774TIntermTyped *TParseContext::addMethod(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005775 TIntermSequence *arguments,
5776 TIntermNode *thisNode,
5777 const TSourceLoc &loc)
5778{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005779 TIntermTyped *typedThis = thisNode->getAsTyped();
5780 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5781 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5782 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005783 // So accessing fnCall->name() below is safe.
Olli Etuahobed35d72017-12-20 16:36:26 +02005784 if (name != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005785 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005786 error(loc, "invalid method", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005787 }
5788 else if (!arguments->empty())
5789 {
5790 error(loc, "method takes no parameters", "length");
5791 }
5792 else if (typedThis == nullptr || !typedThis->isArray())
5793 {
5794 error(loc, "length can only be called on arrays", "length");
5795 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005796 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5797 mGeometryShaderInputPrimitiveType == EptUndefined)
5798 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005799 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005800 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5801 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005802 else
5803 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005804 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5805 node->setLine(loc);
5806 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005807 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005808 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005809}
5810
Olli Etuahobed35d72017-12-20 16:36:26 +02005811TIntermTyped *TParseContext::addNonConstructorFunctionCall(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005812 TIntermSequence *arguments,
5813 const TSourceLoc &loc)
5814{
5815 // First find by unmangled name to check whether the function name has been
5816 // hidden by a variable name or struct typename.
5817 // If a function is found, check for one with a matching argument list.
5818 bool builtIn;
Olli Etuahobed35d72017-12-20 16:36:26 +02005819 const TSymbol *symbol = symbolTable.find(name, mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005820 if (symbol != nullptr && !symbol->isFunction())
5821 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005822 error(loc, "function name expected", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005823 }
5824 else
5825 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005826 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(name, *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005827 mShaderVersion, &builtIn);
5828 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005829 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005830 error(loc, "no matching overloaded function found", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005831 }
5832 else
5833 {
5834 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005835 //
5836 // A declared function.
5837 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005838 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005839 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005840 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005841 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005842 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005843 if (builtIn && op != EOpNull)
5844 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005845 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005846 if (fnCandidate->getParamCount() == 1)
5847 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005848 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005849 TIntermNode *unaryParamNode = arguments->front();
5850 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005851 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005852 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005853 }
5854 else
5855 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005856 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005857 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005858 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005859
5860 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005861 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305862
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005863 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305864 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005865 // See if we can constant fold a built-in. Note that this may be possible
5866 // even if it is not const-qualified.
5867 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305868 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005869 else
5870 {
5871 return callNode;
5872 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005873 }
5874 }
5875 else
5876 {
5877 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005878 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005879
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005880 // If builtIn == false, the function is user defined - could be an overloaded
5881 // built-in as well.
5882 // if builtIn == true, it's a builtIn function with no op associated with it.
5883 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005884 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005885 {
Olli Etuahofe486322017-03-21 09:30:54 +00005886 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005887 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005888 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005889 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005890 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005891 }
5892 else
5893 {
Olli Etuahofe486322017-03-21 09:30:54 +00005894 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005895 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005896 }
5897
Jiajia Qinbc585152017-06-23 15:42:17 +08005898 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005899
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005900 callNode->setLine(loc);
5901
5902 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005903 }
5904 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005905 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005906
5907 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005908 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005909}
5910
Jamie Madillb98c3a82015-07-23 14:26:04 -04005911TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005912 TIntermTyped *trueExpression,
5913 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005914 const TSourceLoc &loc)
5915{
Olli Etuaho56229f12017-07-10 14:16:33 +03005916 if (!checkIsScalarBool(loc, cond))
5917 {
5918 return falseExpression;
5919 }
Olli Etuaho52901742015-04-15 13:42:45 +03005920
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005921 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005922 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005923 std::stringstream reasonStream;
5924 reasonStream << "mismatching ternary operator operand types '"
5925 << trueExpression->getCompleteString() << " and '"
5926 << falseExpression->getCompleteString() << "'";
5927 std::string reason = reasonStream.str();
5928 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005929 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005930 }
Olli Etuahode318b22016-10-25 16:18:25 +01005931 if (IsOpaqueType(trueExpression->getBasicType()))
5932 {
5933 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005934 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005935 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5936 // Note that structs containing opaque types don't need to be checked as structs are
5937 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005938 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005939 return falseExpression;
5940 }
5941
Jiajia Qinbc585152017-06-23 15:42:17 +08005942 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5943 falseExpression->getMemoryQualifier().writeonly)
5944 {
5945 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5946 return falseExpression;
5947 }
5948
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005949 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005950 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005951 // ESSL 3.00.6 section 5.7:
5952 // Ternary operator support is optional for arrays. No certainty that it works across all
5953 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5954 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005955 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005956 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005957 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005958 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005959 }
Olli Etuaho94050052017-05-08 14:17:44 +03005960 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5961 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005962 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005963 return falseExpression;
5964 }
5965
Corentin Wallez0d959252016-07-12 17:26:32 -04005966 // WebGL2 section 5.26, the following results in an error:
5967 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005968 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005969 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005970 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005971 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005972 }
5973
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005974 // Note that the node resulting from here can be a constant union without being qualified as
5975 // constant.
5976 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5977 node->setLine(loc);
5978
5979 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005980}
Olli Etuaho49300862015-02-20 14:54:49 +02005981
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005982//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005983// Parse an array of strings using yyparse.
5984//
5985// Returns 0 for success.
5986//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005987int PaParseStrings(size_t count,
5988 const char *const string[],
5989 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305990 TParseContext *context)
5991{
Yunchao He4f285442017-04-21 12:15:49 +08005992 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005993 return 1;
5994
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005995 if (glslang_initialize(context))
5996 return 1;
5997
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005998 int error = glslang_scan(count, string, length, context);
5999 if (!error)
6000 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006001
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006002 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006003
alokp@chromium.org6b495712012-06-29 00:06:58 +00006004 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006005}
Jamie Madill45bcc782016-11-07 13:58:48 -05006006
6007} // namespace sh