blob: ed513672915224cee77e1256a8e78f30b58791d3 [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.",
1685 fnCall->getFunctionSymbolInfo()->getName().c_str());
1686 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 Etuahoec9232b2017-03-27 17:01:37 +03001695 fnCall->getFunctionSymbolInfo()->getName().c_str());
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 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001999 const TSymbol *symbol =
2000 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
2001 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002002
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002003 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002004 if (constArray)
2005 {
2006 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002007 ASSERT(*initNode == nullptr);
2008 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002009 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002010 }
2011 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002012
Olli Etuaho195be942017-12-04 23:40:14 +02002013 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002014 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002015 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2016 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002017 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002018 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002019 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002020 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002021
Olli Etuaho914b79a2017-06-19 16:03:19 +03002022 return true;
2023}
2024
2025TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2026 const TString &identifier,
2027 TIntermTyped *initializer,
2028 const TSourceLoc &loc)
2029{
2030 checkIsScalarBool(loc, pType);
2031 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002032 TType type(pType);
2033 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002034 {
2035 // The initializer is valid. The init condition needs to have a node - either the
2036 // initializer node, or a constant node in case the initialized variable is const and won't
2037 // be recorded in the AST.
2038 if (initNode == nullptr)
2039 {
2040 return initializer;
2041 }
2042 else
2043 {
2044 TIntermDeclaration *declaration = new TIntermDeclaration();
2045 declaration->appendDeclarator(initNode);
2046 return declaration;
2047 }
2048 }
2049 return nullptr;
2050}
2051
2052TIntermNode *TParseContext::addLoop(TLoopType type,
2053 TIntermNode *init,
2054 TIntermNode *cond,
2055 TIntermTyped *expr,
2056 TIntermNode *body,
2057 const TSourceLoc &line)
2058{
2059 TIntermNode *node = nullptr;
2060 TIntermTyped *typedCond = nullptr;
2061 if (cond)
2062 {
2063 typedCond = cond->getAsTyped();
2064 }
2065 if (cond == nullptr || typedCond)
2066 {
Olli Etuahocce89652017-06-19 16:04:09 +03002067 if (type == ELoopDoWhile)
2068 {
2069 checkIsScalarBool(line, typedCond);
2070 }
2071 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2072 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2073 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2074 !typedCond->isVector()));
2075
Olli Etuaho3ec75682017-07-05 17:02:55 +03002076 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002077 node->setLine(line);
2078 return node;
2079 }
2080
Olli Etuahocce89652017-06-19 16:04:09 +03002081 ASSERT(type != ELoopDoWhile);
2082
Olli Etuaho914b79a2017-06-19 16:03:19 +03002083 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2084 ASSERT(declaration);
2085 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2086 ASSERT(declarator->getLeft()->getAsSymbolNode());
2087
2088 // The condition is a declaration. In the AST representation we don't support declarations as
2089 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2090 // the loop.
2091 TIntermBlock *block = new TIntermBlock();
2092
2093 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2094 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2095 block->appendStatement(declareCondition);
2096
2097 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2098 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002099 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002100 block->appendStatement(loop);
2101 loop->setLine(line);
2102 block->setLine(line);
2103 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104}
2105
Olli Etuahocce89652017-06-19 16:04:09 +03002106TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2107 TIntermNodePair code,
2108 const TSourceLoc &loc)
2109{
Olli Etuaho56229f12017-07-10 14:16:33 +03002110 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002111
2112 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002113 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002114 {
2115 if (cond->getAsConstantUnion()->getBConst(0) == true)
2116 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002117 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002118 }
2119 else
2120 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002121 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002122 }
2123 }
2124
Olli Etuaho3ec75682017-07-05 17:02:55 +03002125 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002126 node->setLine(loc);
2127
2128 return node;
2129}
2130
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002131void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2132{
2133 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2134 typeSpecifier->getBasicType());
2135
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002136 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002137 {
2138 error(typeSpecifier->getLine(), "not supported", "first-class array");
2139 typeSpecifier->clearArrayness();
2140 }
2141}
2142
Martin Radev70866b82016-07-22 15:27:42 +03002143TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302144 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002145{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002146 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002147
Martin Radev70866b82016-07-22 15:27:42 +03002148 TPublicType returnType = typeSpecifier;
2149 returnType.qualifier = typeQualifier.qualifier;
2150 returnType.invariant = typeQualifier.invariant;
2151 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002152 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002153 returnType.precision = typeSpecifier.precision;
2154
2155 if (typeQualifier.precision != EbpUndefined)
2156 {
2157 returnType.precision = typeQualifier.precision;
2158 }
2159
Martin Radev4a9cd802016-09-01 16:51:51 +03002160 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2161 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002162
Martin Radev4a9cd802016-09-01 16:51:51 +03002163 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2164 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002165
Martin Radev4a9cd802016-09-01 16:51:51 +03002166 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002167
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002168 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002169 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002170 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002171 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002172 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002173 returnType.clearArrayness();
2174 }
2175
Martin Radev70866b82016-07-22 15:27:42 +03002176 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002177 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002178 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002179 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002180 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002181 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002182
Martin Radev70866b82016-07-22 15:27:42 +03002183 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002184 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002185 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002186 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002187 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002188 }
2189 }
2190 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002191 {
Martin Radev70866b82016-07-22 15:27:42 +03002192 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002193 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002194 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002195 }
Martin Radev70866b82016-07-22 15:27:42 +03002196 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2197 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002198 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002199 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2200 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002201 }
Martin Radev70866b82016-07-22 15:27:42 +03002202 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002203 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002204 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002205 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002206 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002207 }
2208
2209 return returnType;
2210}
2211
Olli Etuaho856c4972016-08-08 11:38:39 +03002212void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2213 const TPublicType &type,
2214 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002215{
2216 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002217 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002218 {
2219 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002220 }
2221
2222 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2223 switch (qualifier)
2224 {
2225 case EvqVertexIn:
2226 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002227 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002228 {
2229 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002230 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002231 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002232 return;
2233 case EvqFragmentOut:
2234 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002235 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002236 {
2237 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002238 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002239 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002240 return;
2241 default:
2242 break;
2243 }
2244
2245 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2246 // restrictions.
2247 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002248 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2249 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002250 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2251 {
2252 error(qualifierLocation, "must use 'flat' interpolation here",
2253 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002254 }
2255
Martin Radev4a9cd802016-09-01 16:51:51 +03002256 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002257 {
2258 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2259 // These restrictions are only implied by the ESSL 3.00 spec, but
2260 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002261 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002262 {
2263 error(qualifierLocation, "cannot be an array of structures",
2264 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002265 }
2266 if (type.isStructureContainingArrays())
2267 {
2268 error(qualifierLocation, "cannot be a structure containing an array",
2269 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002270 }
2271 if (type.isStructureContainingType(EbtStruct))
2272 {
2273 error(qualifierLocation, "cannot be a structure containing a structure",
2274 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002275 }
2276 if (type.isStructureContainingType(EbtBool))
2277 {
2278 error(qualifierLocation, "cannot be a structure containing a bool",
2279 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002280 }
2281 }
2282}
2283
Martin Radev2cc85b32016-08-05 16:22:53 +03002284void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2285{
2286 if (qualifier.getType() == QtStorage)
2287 {
2288 const TStorageQualifierWrapper &storageQualifier =
2289 static_cast<const TStorageQualifierWrapper &>(qualifier);
2290 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2291 !symbolTable.atGlobalLevel())
2292 {
2293 error(storageQualifier.getLine(),
2294 "Local variables can only use the const storage qualifier.",
2295 storageQualifier.getQualifierString().c_str());
2296 }
2297 }
2298}
2299
Olli Etuaho43364892017-02-13 16:00:12 +00002300void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002301 const TSourceLoc &location)
2302{
Jiajia Qinbc585152017-06-23 15:42:17 +08002303 const std::string reason(
2304 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2305 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002306 if (memoryQualifier.readonly)
2307 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002308 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002309 }
2310 if (memoryQualifier.writeonly)
2311 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002312 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002313 }
Martin Radev049edfa2016-11-11 14:35:37 +02002314 if (memoryQualifier.coherent)
2315 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002316 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002317 }
2318 if (memoryQualifier.restrictQualifier)
2319 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002320 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002321 }
2322 if (memoryQualifier.volatileQualifier)
2323 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002324 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002325 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002326}
2327
jchen104cdac9e2017-05-08 11:01:20 +08002328// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2329// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002330void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2331 const TSourceLoc &loc,
2332 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002333{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002334 if (!IsAtomicCounter(type->getBasicType()))
2335 {
2336 return;
2337 }
2338
2339 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2340 : kAtomicCounterSize;
2341 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2342 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002343 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002344 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002345 {
2346 offset = bindingState.appendSpan(size);
2347 }
2348 else
2349 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002350 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002351 }
2352 if (offset == -1)
2353 {
2354 error(loc, "Offset overlapping", "atomic counter");
2355 return;
2356 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002357 layoutQualifier.offset = offset;
2358 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002359}
2360
Olli Etuaho454c34c2017-10-25 16:35:56 +03002361void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2362 const char *token,
2363 TType *type)
2364{
2365 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2366 {
2367 if (type->isArray() && type->getOutermostArraySize() == 0u)
2368 {
2369 // Set size for the unsized geometry shader inputs if they are declared after a valid
2370 // input primitive declaration.
2371 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2372 {
2373 ASSERT(mGeometryShaderInputArraySize > 0u);
2374 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2375 }
2376 else
2377 {
2378 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2379 // An input can be declared without an array size if there is a previous layout
2380 // which specifies the size.
2381 error(location,
2382 "Missing a valid input primitive declaration before declaring an unsized "
2383 "array input",
2384 token);
2385 }
2386 }
2387 else if (type->isArray())
2388 {
2389 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2390 }
2391 else
2392 {
2393 error(location, "Geometry shader input variable must be declared as an array", token);
2394 }
2395 }
2396}
2397
Olli Etuaho13389b62016-10-16 11:48:18 +01002398TIntermDeclaration *TParseContext::parseSingleDeclaration(
2399 TPublicType &publicType,
2400 const TSourceLoc &identifierOrTypeLocation,
2401 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002402{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002403 TType type(publicType);
2404 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2405 mDirectiveHandler.pragma().stdgl.invariantAll)
2406 {
2407 TQualifier qualifier = type.getQualifier();
2408
2409 // The directive handler has already taken care of rejecting invalid uses of this pragma
2410 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2411 // affected variable declarations:
2412 //
2413 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2414 // elsewhere, in TranslatorGLSL.)
2415 //
2416 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2417 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2418 // the way this is currently implemented we have to enable this compiler option before
2419 // parsing the shader and determining the shading language version it uses. If this were
2420 // implemented as a post-pass, the workaround could be more targeted.
2421 //
2422 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2423 // the specification, but there are desktop OpenGL drivers that expect that this is the
2424 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2425 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2426 {
2427 type.setInvariant(true);
2428 }
2429 }
2430
Olli Etuaho454c34c2017-10-25 16:35:56 +03002431 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002432
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002433 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2434 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002435
Olli Etuahobab4c082015-04-24 16:38:49 +03002436 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002437 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002438
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002439 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002440 if (emptyDeclaration)
2441 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002442 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002443 // In most cases we don't need to create a symbol node for an empty declaration.
2444 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2445 if (type.getBasicType() == EbtStruct)
2446 {
Olli Etuaho195be942017-12-04 23:40:14 +02002447 TVariable *emptyVariable =
2448 new TVariable(&symbolTable, NewPoolTString(""), type, SymbolType::Empty);
2449 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002450 }
jchen104cdac9e2017-05-08 11:01:20 +08002451 else if (IsAtomicCounter(publicType.getBasicType()))
2452 {
2453 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2454 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002455 }
2456 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002457 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002458 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002459
Olli Etuaho55bde912017-10-25 13:41:13 +03002460 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002461
Olli Etuaho55bc9052017-10-25 17:33:06 +03002462 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002463
Olli Etuaho2935c582015-04-08 14:32:06 +03002464 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002465 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002466 {
Olli Etuaho195be942017-12-04 23:40:14 +02002467 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002468 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002469 }
2470
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002471 TIntermDeclaration *declaration = new TIntermDeclaration();
2472 declaration->setLine(identifierOrTypeLocation);
2473 if (symbol)
2474 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002475 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002476 declaration->appendDeclarator(symbol);
2477 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002478 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002479}
2480
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002481TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2482 TPublicType &elementType,
2483 const TSourceLoc &identifierLocation,
2484 const TString &identifier,
2485 const TSourceLoc &indexLocation,
2486 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002487{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002488 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002489
Olli Etuaho55bde912017-10-25 13:41:13 +03002490 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002491 identifierLocation);
2492
Olli Etuaho55bde912017-10-25 13:41:13 +03002493 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002494
Olli Etuaho55bde912017-10-25 13:41:13 +03002495 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002496
Olli Etuaho55bde912017-10-25 13:41:13 +03002497 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002498 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002499
Olli Etuaho454c34c2017-10-25 16:35:56 +03002500 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002501
2502 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2503
Olli Etuaho55bc9052017-10-25 17:33:06 +03002504 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002505
Olli Etuaho13389b62016-10-16 11:48:18 +01002506 TIntermDeclaration *declaration = new TIntermDeclaration();
2507 declaration->setLine(identifierLocation);
2508
Olli Etuaho195be942017-12-04 23:40:14 +02002509 TVariable *variable = nullptr;
2510 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002511 {
Olli Etuaho195be942017-12-04 23:40:14 +02002512 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002513 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002514 declaration->appendDeclarator(symbol);
2515 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002516
Olli Etuaho13389b62016-10-16 11:48:18 +01002517 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002518}
2519
Olli Etuaho13389b62016-10-16 11:48:18 +01002520TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2521 const TSourceLoc &identifierLocation,
2522 const TString &identifier,
2523 const TSourceLoc &initLocation,
2524 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002525{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002526 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002527
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002528 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2529 identifierLocation);
2530
2531 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002532
Olli Etuaho13389b62016-10-16 11:48:18 +01002533 TIntermDeclaration *declaration = new TIntermDeclaration();
2534 declaration->setLine(identifierLocation);
2535
2536 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002537 TType type(publicType);
2538 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002539 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002540 if (initNode)
2541 {
2542 declaration->appendDeclarator(initNode);
2543 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002544 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002545 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002546}
2547
Olli Etuaho13389b62016-10-16 11:48:18 +01002548TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002549 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002550 const TSourceLoc &identifierLocation,
2551 const TString &identifier,
2552 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002553 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002554 const TSourceLoc &initLocation,
2555 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002556{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002557 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002558
Olli Etuaho55bde912017-10-25 13:41:13 +03002559 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002560 identifierLocation);
2561
Olli Etuaho55bde912017-10-25 13:41:13 +03002562 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002563
Olli Etuaho55bde912017-10-25 13:41:13 +03002564 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565
Olli Etuaho55bde912017-10-25 13:41:13 +03002566 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002567 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002568
Olli Etuaho13389b62016-10-16 11:48:18 +01002569 TIntermDeclaration *declaration = new TIntermDeclaration();
2570 declaration->setLine(identifierLocation);
2571
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002573 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002574 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002575 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002576 if (initNode)
2577 {
2578 declaration->appendDeclarator(initNode);
2579 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002580 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002581
2582 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002583}
2584
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002585TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002586 const TTypeQualifierBuilder &typeQualifierBuilder,
2587 const TSourceLoc &identifierLoc,
2588 const TString *identifier,
2589 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002590{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002591 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002592
Martin Radev70866b82016-07-22 15:27:42 +03002593 if (!typeQualifier.invariant)
2594 {
2595 error(identifierLoc, "Expected invariant", identifier->c_str());
2596 return nullptr;
2597 }
2598 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2599 {
2600 return nullptr;
2601 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002602 if (!symbol)
2603 {
2604 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002605 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002606 }
Martin Radev70866b82016-07-22 15:27:42 +03002607 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002608 {
Martin Radev70866b82016-07-22 15:27:42 +03002609 error(identifierLoc, "invariant declaration specifies qualifier",
2610 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002611 }
Martin Radev70866b82016-07-22 15:27:42 +03002612 if (typeQualifier.precision != EbpUndefined)
2613 {
2614 error(identifierLoc, "invariant declaration specifies precision",
2615 getPrecisionString(typeQualifier.precision));
2616 }
2617 if (!typeQualifier.layoutQualifier.isEmpty())
2618 {
2619 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2620 }
2621
2622 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002623 if (!variable)
2624 {
2625 return nullptr;
2626 }
Martin Radev70866b82016-07-22 15:27:42 +03002627 const TType &type = variable->getType();
2628
2629 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2630 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002631 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002632
2633 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2634
Olli Etuaho195be942017-12-04 23:40:14 +02002635 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002636 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002637
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002638 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002639}
2640
Olli Etuaho13389b62016-10-16 11:48:18 +01002641void TParseContext::parseDeclarator(TPublicType &publicType,
2642 const TSourceLoc &identifierLocation,
2643 const TString &identifier,
2644 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002645{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002646 // If the declaration starting this declarator list was empty (example: int,), some checks were
2647 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002648 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002649 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002650 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2651 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002652 }
2653
Olli Etuaho856c4972016-08-08 11:38:39 +03002654 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002655
Olli Etuaho43364892017-02-13 16:00:12 +00002656 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002657
2658 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2659
Olli Etuaho55bde912017-10-25 13:41:13 +03002660 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2661
Olli Etuaho55bc9052017-10-25 17:33:06 +03002662 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2663
Olli Etuaho195be942017-12-04 23:40:14 +02002664 TVariable *variable = nullptr;
2665 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002666 {
Olli Etuaho195be942017-12-04 23:40:14 +02002667 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002668 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002669 declarationOut->appendDeclarator(symbol);
2670 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002671}
2672
Olli Etuaho55bde912017-10-25 13:41:13 +03002673void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002674 const TSourceLoc &identifierLocation,
2675 const TString &identifier,
2676 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002677 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002678 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002679{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002680 // If the declaration starting this declarator list was empty (example: int,), some checks were
2681 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002682 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002683 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002684 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002685 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002686 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002687
Olli Etuaho55bde912017-10-25 13:41:13 +03002688 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002689
Olli Etuaho55bde912017-10-25 13:41:13 +03002690 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002691 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002692 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002693 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002694
Olli Etuaho454c34c2017-10-25 16:35:56 +03002695 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2696
Olli Etuaho55bde912017-10-25 13:41:13 +03002697 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2698
Olli Etuaho55bc9052017-10-25 17:33:06 +03002699 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002700
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002701 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002702 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002703 {
Olli Etuaho195be942017-12-04 23:40:14 +02002704 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002705 symbol->setLine(identifierLocation);
2706 declarationOut->appendDeclarator(symbol);
2707 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002708 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002709}
2710
Olli Etuaho13389b62016-10-16 11:48:18 +01002711void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2712 const TSourceLoc &identifierLocation,
2713 const TString &identifier,
2714 const TSourceLoc &initLocation,
2715 TIntermTyped *initializer,
2716 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002717{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002718 // If the declaration starting this declarator list was empty (example: int,), some checks were
2719 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002720 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002721 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002722 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2723 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002724 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002725
Olli Etuaho856c4972016-08-08 11:38:39 +03002726 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002727
Olli Etuaho13389b62016-10-16 11:48:18 +01002728 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002729 TType type(publicType);
2730 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002731 {
2732 //
2733 // build the intermediate representation
2734 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002735 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002736 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002737 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002738 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002739 }
2740}
2741
Olli Etuaho55bde912017-10-25 13:41:13 +03002742void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002743 const TSourceLoc &identifierLocation,
2744 const TString &identifier,
2745 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002746 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002747 const TSourceLoc &initLocation,
2748 TIntermTyped *initializer,
2749 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002750{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002751 // If the declaration starting this declarator list was empty (example: int,), some checks were
2752 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002753 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002754 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002755 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002756 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002757 }
2758
Olli Etuaho55bde912017-10-25 13:41:13 +03002759 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002760
Olli Etuaho55bde912017-10-25 13:41:13 +03002761 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762
Olli Etuaho55bde912017-10-25 13:41:13 +03002763 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002764 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002765
2766 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002767 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002768 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002769 {
2770 if (initNode)
2771 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002772 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002773 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002774 }
2775}
2776
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002777TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2778{
2779 // It's simpler to parse an empty statement as a constant expression rather than having a
2780 // different type of node just for empty statements, that will be pruned from the AST anyway.
2781 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2782 node->setLine(location);
2783 return node;
2784}
2785
jchen104cdac9e2017-05-08 11:01:20 +08002786void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2787 const TSourceLoc &location)
2788{
2789 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2790 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2791 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2792 {
2793 error(location, "Requires both binding and offset", "layout");
2794 return;
2795 }
2796 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2797}
2798
Olli Etuahocce89652017-06-19 16:04:09 +03002799void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2800 const TPublicType &type,
2801 const TSourceLoc &loc)
2802{
2803 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2804 !getFragmentPrecisionHigh())
2805 {
2806 error(loc, "precision is not supported in fragment shader", "highp");
2807 }
2808
2809 if (!CanSetDefaultPrecisionOnType(type))
2810 {
2811 error(loc, "illegal type argument for default precision qualifier",
2812 getBasicString(type.getBasicType()));
2813 return;
2814 }
2815 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2816}
2817
Shaob5cc1192017-07-06 10:47:20 +08002818bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2819{
2820 switch (typeQualifier.layoutQualifier.primitiveType)
2821 {
2822 case EptLines:
2823 case EptLinesAdjacency:
2824 case EptTriangles:
2825 case EptTrianglesAdjacency:
2826 return typeQualifier.qualifier == EvqGeometryIn;
2827
2828 case EptLineStrip:
2829 case EptTriangleStrip:
2830 return typeQualifier.qualifier == EvqGeometryOut;
2831
2832 case EptPoints:
2833 return true;
2834
2835 default:
2836 UNREACHABLE();
2837 return false;
2838 }
2839}
2840
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002841void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2842 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002843{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002844 if (mGeometryShaderInputArraySize == 0u)
2845 {
2846 mGeometryShaderInputArraySize = inputArraySize;
2847 }
2848 else if (mGeometryShaderInputArraySize != inputArraySize)
2849 {
2850 error(line,
2851 "Array size or input primitive declaration doesn't match the size of earlier sized "
2852 "array inputs.",
2853 "layout");
2854 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002855}
2856
Shaob5cc1192017-07-06 10:47:20 +08002857bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2858{
2859 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2860
2861 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2862
2863 if (layoutQualifier.maxVertices != -1)
2864 {
2865 error(typeQualifier.line,
2866 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2867 return false;
2868 }
2869
2870 // Set mGeometryInputPrimitiveType if exists
2871 if (layoutQualifier.primitiveType != EptUndefined)
2872 {
2873 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2874 {
2875 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2876 return false;
2877 }
2878
2879 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2880 {
2881 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002882 setGeometryShaderInputArraySize(
2883 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2884 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002885 }
2886 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2887 {
2888 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2889 "layout");
2890 return false;
2891 }
2892 }
2893
2894 // Set mGeometryInvocations if exists
2895 if (layoutQualifier.invocations > 0)
2896 {
2897 if (mGeometryShaderInvocations == 0)
2898 {
2899 mGeometryShaderInvocations = layoutQualifier.invocations;
2900 }
2901 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2902 {
2903 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2904 "layout");
2905 return false;
2906 }
2907 }
2908
2909 return true;
2910}
2911
2912bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2913{
2914 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2915
2916 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2917
2918 if (layoutQualifier.invocations > 0)
2919 {
2920 error(typeQualifier.line,
2921 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2922 return false;
2923 }
2924
2925 // Set mGeometryOutputPrimitiveType if exists
2926 if (layoutQualifier.primitiveType != EptUndefined)
2927 {
2928 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2929 {
2930 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2931 return false;
2932 }
2933
2934 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2935 {
2936 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2937 }
2938 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2939 {
2940 error(typeQualifier.line,
2941 "primitive doesn't match earlier output primitive declaration", "layout");
2942 return false;
2943 }
2944 }
2945
2946 // Set mGeometryMaxVertices if exists
2947 if (layoutQualifier.maxVertices > -1)
2948 {
2949 if (mGeometryShaderMaxVertices == -1)
2950 {
2951 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2952 }
2953 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2954 {
2955 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2956 "layout");
2957 return false;
2958 }
2959 }
2960
2961 return true;
2962}
2963
Martin Radev70866b82016-07-22 15:27:42 +03002964void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002965{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002966 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002967 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002968
Martin Radev70866b82016-07-22 15:27:42 +03002969 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2970 typeQualifier.line);
2971
Jamie Madillc2128ff2016-07-04 10:26:17 -04002972 // It should never be the case, but some strange parser errors can send us here.
2973 if (layoutQualifier.isEmpty())
2974 {
2975 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002976 return;
2977 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002978
Martin Radev802abe02016-08-04 17:48:32 +03002979 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002980 {
Olli Etuaho43364892017-02-13 16:00:12 +00002981 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002982 return;
2983 }
2984
Olli Etuaho43364892017-02-13 16:00:12 +00002985 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2986
2987 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002988
2989 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2990
Andrei Volykhina5527072017-03-22 16:46:30 +03002991 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2992
jchen104cdac9e2017-05-08 11:01:20 +08002993 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2994
Qin Jiajiaca68d982017-09-18 16:41:56 +08002995 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2996 typeQualifier.qualifier);
2997
Martin Radev802abe02016-08-04 17:48:32 +03002998 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002999 {
Martin Radev802abe02016-08-04 17:48:32 +03003000 if (mComputeShaderLocalSizeDeclared &&
3001 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3002 {
3003 error(typeQualifier.line, "Work group size does not match the previous declaration",
3004 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003005 return;
3006 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003007
Martin Radev802abe02016-08-04 17:48:32 +03003008 if (mShaderVersion < 310)
3009 {
3010 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003011 return;
3012 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003013
Martin Radev4c4c8e72016-08-04 12:25:34 +03003014 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003015 {
3016 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003017 return;
3018 }
3019
3020 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3021 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3022
3023 const TConstantUnion *maxComputeWorkGroupSizeData =
3024 maxComputeWorkGroupSize->getConstPointer();
3025
3026 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3027 {
3028 if (layoutQualifier.localSize[i] != -1)
3029 {
3030 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3031 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3032 if (mComputeShaderLocalSize[i] < 1 ||
3033 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3034 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003035 std::stringstream reasonStream;
3036 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3037 << maxComputeWorkGroupSizeValue;
3038 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003039
Olli Etuaho4de340a2016-12-16 09:32:03 +00003040 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003041 return;
3042 }
3043 }
3044 }
3045
3046 mComputeShaderLocalSizeDeclared = true;
3047 }
Shaob5cc1192017-07-06 10:47:20 +08003048 else if (typeQualifier.qualifier == EvqGeometryIn)
3049 {
3050 if (mShaderVersion < 310)
3051 {
3052 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3053 return;
3054 }
3055
3056 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3057 {
3058 return;
3059 }
3060 }
3061 else if (typeQualifier.qualifier == EvqGeometryOut)
3062 {
3063 if (mShaderVersion < 310)
3064 {
3065 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3066 "layout");
3067 return;
3068 }
3069
3070 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3071 {
3072 return;
3073 }
3074 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003075 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3076 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003077 {
3078 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3079 // specification.
3080 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3081 {
3082 error(typeQualifier.line, "Number of views does not match the previous declaration",
3083 "layout");
3084 return;
3085 }
3086
3087 if (layoutQualifier.numViews == -1)
3088 {
3089 error(typeQualifier.line, "No num_views specified", "layout");
3090 return;
3091 }
3092
3093 if (layoutQualifier.numViews > mMaxNumViews)
3094 {
3095 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3096 "layout");
3097 return;
3098 }
3099
3100 mNumViews = layoutQualifier.numViews;
3101 }
Martin Radev802abe02016-08-04 17:48:32 +03003102 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003103 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003104 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003105 {
Martin Radev802abe02016-08-04 17:48:32 +03003106 return;
3107 }
3108
Jiajia Qinbc585152017-06-23 15:42:17 +08003109 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003110 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003111 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003112 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003113 return;
3114 }
3115
3116 if (mShaderVersion < 300)
3117 {
3118 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3119 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003120 return;
3121 }
3122
Olli Etuaho09b04a22016-12-15 13:30:26 +00003123 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003124
3125 if (layoutQualifier.matrixPacking != EmpUnspecified)
3126 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003127 if (typeQualifier.qualifier == EvqUniform)
3128 {
3129 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3130 }
3131 else if (typeQualifier.qualifier == EvqBuffer)
3132 {
3133 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3134 }
Martin Radev802abe02016-08-04 17:48:32 +03003135 }
3136
3137 if (layoutQualifier.blockStorage != EbsUnspecified)
3138 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003139 if (typeQualifier.qualifier == EvqUniform)
3140 {
3141 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3142 }
3143 else if (typeQualifier.qualifier == EvqBuffer)
3144 {
3145 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3146 }
Martin Radev802abe02016-08-04 17:48:32 +03003147 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003148 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003149}
3150
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003151TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3152 const TFunction &function,
3153 const TSourceLoc &location,
3154 bool insertParametersToSymbolTable)
3155{
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003156 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003157
Olli Etuahofe486322017-03-21 09:30:54 +00003158 TIntermFunctionPrototype *prototype =
3159 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003160 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3161 // point to the data that already exists in the symbol table.
3162 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3163 prototype->setLine(location);
3164
3165 for (size_t i = 0; i < function.getParamCount(); i++)
3166 {
3167 const TConstParameter &param = function.getParam(i);
3168
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003169 TIntermSymbol *symbol = nullptr;
3170
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003171 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3172 // be used for unused args).
3173 if (param.name != nullptr)
3174 {
Olli Etuaho195be942017-12-04 23:40:14 +02003175 TVariable *variable =
3176 new TVariable(&symbolTable, param.name, *param.type, SymbolType::UserDefined);
3177 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003178 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003179 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003180 {
Olli Etuaho195be942017-12-04 23:40:14 +02003181 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003182 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003183 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003184 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003185 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003186 // Unsized type of a named parameter should have already been checked and sanitized.
3187 ASSERT(!param.type->isUnsizedArray());
3188 }
3189 else
3190 {
3191 if (param.type->isUnsizedArray())
3192 {
3193 error(location, "function parameter array must be sized at compile time", "[]");
3194 // We don't need to size the arrays since the parameter is unnamed and hence
3195 // inaccessible.
3196 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003197 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003198 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003199 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003200 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3201 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003202 TVariable *emptyVariable =
3203 new TVariable(&symbolTable, NewPoolTString(""), *param.type, SymbolType::Empty);
3204 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003205 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003206 symbol->setLine(location);
3207 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003208 }
3209 return prototype;
3210}
3211
Olli Etuaho16c745a2017-01-16 17:02:27 +00003212TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3213 const TFunction &parsedFunction,
3214 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003215{
Olli Etuaho476197f2016-10-11 13:59:08 +01003216 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3217 // first declaration. Either way the instance in the symbol table is used to track whether the
3218 // function is declared multiple times.
3219 TFunction *function = static_cast<TFunction *>(
3220 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3221 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003222 {
3223 // ESSL 1.00.17 section 4.2.7.
3224 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3225 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003226 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003227 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003228
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003229 TIntermFunctionPrototype *prototype =
3230 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003231
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003232 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003233
3234 if (!symbolTable.atGlobalLevel())
3235 {
3236 // ESSL 3.00.4 section 4.2.4.
3237 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003238 }
3239
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003240 return prototype;
3241}
3242
Olli Etuaho336b1472016-10-05 16:37:55 +01003243TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003244 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003245 TIntermBlock *functionBody,
3246 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003247{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003248 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003249 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3250 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003251 error(location, "function does not return a value:",
3252 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003253 }
3254
Olli Etuahof51fdd22016-10-03 10:03:40 +01003255 if (functionBody == nullptr)
3256 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003257 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003258 functionBody->setLine(location);
3259 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003260 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003261 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003262 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003263
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003264 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003265 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003266}
3267
Olli Etuaho476197f2016-10-11 13:59:08 +01003268void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3269 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003270 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003271{
Olli Etuaho476197f2016-10-11 13:59:08 +01003272 ASSERT(function);
3273 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003274 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003275 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003276
3277 if (builtIn)
3278 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003279 error(location, "built-in functions cannot be redefined", (*function)->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003280 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003281 else
Jamie Madill185fb402015-06-12 15:48:48 -04003282 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003283 TFunction *prevDec = static_cast<TFunction *>(
3284 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3285
3286 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3287 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3288 // occurance.
3289 if (*function != prevDec)
3290 {
3291 // Swap the parameters of the previous declaration to the parameters of the function
3292 // definition (parameter names may differ).
3293 prevDec->swapParameters(**function);
3294
3295 // The function definition will share the same symbol as any previous declaration.
3296 *function = prevDec;
3297 }
3298
3299 if ((*function)->isDefined())
3300 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003301 error(location, "function already has a body", (*function)->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003302 }
3303
3304 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003305 }
Jamie Madill185fb402015-06-12 15:48:48 -04003306
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003307 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003308 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003309 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003310
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003311 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003312 setLoopNestingLevel(0);
3313}
3314
Jamie Madillb98c3a82015-07-23 14:26:04 -04003315TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003316{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003317 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003318 // We don't know at this point whether this is a function definition or a prototype.
3319 // The definition production code will check for redefinitions.
3320 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003321 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003322 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3323 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003324 //
3325 TFunction *prevDec =
3326 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303327
Olli Etuahod80f2942017-11-06 12:44:45 +02003328 for (size_t i = 0u; i < function->getParamCount(); ++i)
3329 {
3330 auto &param = function->getParam(i);
3331 if (param.type->isStructSpecifier())
3332 {
3333 // ESSL 3.00.6 section 12.10.
3334 error(location, "Function parameter type cannot be a structure definition",
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003335 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003336 }
3337 }
3338
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003339 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
3340 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303341 {
Martin Radevda6254b2016-12-14 17:00:36 +02003342 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303343 // Therefore overloading or redefining builtin functions is an error.
3344 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003345 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303346 }
3347 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003348 {
3349 if (prevDec->getReturnType() != function->getReturnType())
3350 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003351 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003352 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003353 }
3354 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3355 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003356 if (prevDec->getParam(i).type->getQualifier() !=
3357 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003358 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003359 error(location,
3360 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003361 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003362 }
3363 }
3364 }
3365
3366 //
3367 // Check for previously declared variables using the same name.
3368 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003369 TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003370 if (prevSym)
3371 {
3372 if (!prevSym->isFunction())
3373 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003374 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003375 }
3376 }
3377 else
3378 {
3379 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003380 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003381 }
3382
3383 // We're at the inner scope level of the function's arguments and body statement.
3384 // Add the function prototype to the surrounding scope instead.
3385 symbolTable.getOuterLevel()->insert(function);
3386
Olli Etuaho78d13742017-01-18 13:06:10 +00003387 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003388 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003389 {
3390 if (function->getParamCount() > 0)
3391 {
3392 error(location, "function cannot take any parameter(s)", "main");
3393 }
3394 if (function->getReturnType().getBasicType() != EbtVoid)
3395 {
3396 error(location, "main function cannot return a value",
3397 function->getReturnType().getBasicString());
3398 }
3399 }
3400
Jamie Madill185fb402015-06-12 15:48:48 -04003401 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003402 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3403 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003404 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3405 //
3406 return function;
3407}
3408
Olli Etuaho9de84a52016-06-14 17:36:01 +03003409TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3410 const TString *name,
3411 const TSourceLoc &location)
3412{
3413 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3414 {
3415 error(location, "no qualifiers allowed for function return",
3416 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003417 }
3418 if (!type.layoutQualifier.isEmpty())
3419 {
3420 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003421 }
jchen10cc2a10e2017-05-03 14:05:12 +08003422 // make sure an opaque type is not involved as well...
3423 std::string reason(getBasicString(type.getBasicType()));
3424 reason += "s can't be function return values";
3425 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003426 if (mShaderVersion < 300)
3427 {
3428 // Array return values are forbidden, but there's also no valid syntax for declaring array
3429 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003430 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003431
3432 if (type.isStructureContainingArrays())
3433 {
3434 // ESSL 1.00.17 section 6.1 Function Definitions
3435 error(location, "structures containing arrays can't be function return values",
3436 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003437 }
3438 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003439
3440 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003441 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003442}
3443
Olli Etuahocce89652017-06-19 16:04:09 +03003444TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3445{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003446 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003447 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3448 // really only interested in the mangled name of the function to look up the actual function
3449 // from the symbol table. If we just had the name string and the types of the parameters that
3450 // would be enough, but TFunction carries a lot of extra information in addition to that.
3451 // Besides function calls we do have to store constructor calls in the same data structure, for
3452 // them we need to store a TType.
3453 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved);
Olli Etuahocce89652017-06-19 16:04:09 +03003454}
3455
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003456TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003457{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003458 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003459 {
3460 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3461 "[]");
3462 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003463 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003464 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003465 error(publicType.getLine(), "constructor can't be a structure definition",
3466 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003467 }
3468
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003469 TType *type = new TType(publicType);
3470 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003471 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003472 error(publicType.getLine(), "cannot construct this type",
3473 getBasicString(publicType.getBasicType()));
3474 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003475 }
3476
Olli Etuaho195be942017-12-04 23:40:14 +02003477 return new TFunction(&symbolTable, nullptr, type, SymbolType::NotResolved, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003478}
3479
Olli Etuaho55bde912017-10-25 13:41:13 +03003480void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3481 const char *errorMessage,
3482 const char *token,
3483 TType *arrayType)
3484{
3485 if (arrayType->isUnsizedArray())
3486 {
3487 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003488 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003489 }
3490}
3491
3492TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003493 const TString *name,
3494 const TSourceLoc &nameLoc)
3495{
Olli Etuaho55bde912017-10-25 13:41:13 +03003496 ASSERT(type);
3497 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3498 type);
3499 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003500 {
3501 error(nameLoc, "illegal use of type 'void'", name->c_str());
3502 }
3503 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003504 TParameter param = {name, type};
3505 return param;
3506}
3507
Olli Etuaho55bde912017-10-25 13:41:13 +03003508TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3509 const TString *name,
3510 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003511{
Olli Etuaho55bde912017-10-25 13:41:13 +03003512 TType *type = new TType(publicType);
3513 return parseParameterDeclarator(type, name, nameLoc);
3514}
3515
3516TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3517 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003518 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003519 const TSourceLoc &arrayLoc,
3520 TPublicType *elementType)
3521{
3522 checkArrayElementIsNotArray(arrayLoc, *elementType);
3523 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003524 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003525 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003526}
3527
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003528bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3529 TType type,
3530 const TSourceLoc &line)
3531{
3532 if (arguments->empty())
3533 {
3534 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3535 return false;
3536 }
3537 for (TIntermNode *arg : *arguments)
3538 {
3539 TIntermTyped *element = arg->getAsTyped();
3540 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003541 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3542 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003543 {
3544 error(line, "constructing from a non-dereferenced array", "constructor");
3545 return false;
3546 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003547 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003548 {
3549 if (dimensionalityFromElement == 1u)
3550 {
3551 error(line, "implicitly sized array of arrays constructor argument is not an array",
3552 "constructor");
3553 }
3554 else
3555 {
3556 error(line,
3557 "implicitly sized array of arrays constructor argument dimensionality is too "
3558 "low",
3559 "constructor");
3560 }
3561 return false;
3562 }
3563 }
3564 return true;
3565}
3566
Jamie Madillb98c3a82015-07-23 14:26:04 -04003567// This function is used to test for the correctness of the parameters passed to various constructor
3568// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003569//
Olli Etuaho856c4972016-08-08 11:38:39 +03003570// 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 +00003571//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003572TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003573 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303574 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003575{
Olli Etuaho856c4972016-08-08 11:38:39 +03003576 if (type.isUnsizedArray())
3577 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003578 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003579 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003580 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003581 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003582 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003583 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3584 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003585 if (type.getOutermostArraySize() == 0u)
3586 {
3587 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3588 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003589 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003590 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003591 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003592 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003593 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003594 }
3595 }
3596 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003597 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003598
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003599 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003600 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003601 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003602 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003603
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003604 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003605 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003606
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003607 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3608 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003609 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003610 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003611 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003612 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003613}
3614
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003615//
3616// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003617// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003618//
Olli Etuaho13389b62016-10-16 11:48:18 +01003619TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003620 const TTypeQualifierBuilder &typeQualifierBuilder,
3621 const TSourceLoc &nameLine,
3622 const TString &blockName,
3623 TFieldList *fieldList,
3624 const TString *instanceName,
3625 const TSourceLoc &instanceLine,
3626 TIntermTyped *arrayIndex,
3627 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003628{
Olli Etuaho856c4972016-08-08 11:38:39 +03003629 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003630
Olli Etuaho77ba4082016-12-16 12:01:18 +00003631 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003632
Jiajia Qinbc585152017-06-23 15:42:17 +08003633 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003634 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003635 error(typeQualifier.line,
3636 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3637 "3.10",
3638 getQualifierString(typeQualifier.qualifier));
3639 }
3640 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3641 {
3642 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003643 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003644 }
3645
Martin Radev70866b82016-07-22 15:27:42 +03003646 if (typeQualifier.invariant)
3647 {
3648 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3649 }
3650
Jiajia Qinbc585152017-06-23 15:42:17 +08003651 if (typeQualifier.qualifier != EvqBuffer)
3652 {
3653 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3654 }
Olli Etuaho43364892017-02-13 16:00:12 +00003655
jchen10af713a22017-04-19 09:10:56 +08003656 // add array index
3657 unsigned int arraySize = 0;
3658 if (arrayIndex != nullptr)
3659 {
3660 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3661 }
3662
3663 if (mShaderVersion < 310)
3664 {
3665 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3666 }
3667 else
3668 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003669 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3670 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003671 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003672
Andrei Volykhina5527072017-03-22 16:46:30 +03003673 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3674
Jamie Madill099c0f32013-06-20 11:55:52 -04003675 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003676 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003677 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3678 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003679
Jamie Madill099c0f32013-06-20 11:55:52 -04003680 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3681 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003682 if (typeQualifier.qualifier == EvqUniform)
3683 {
3684 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3685 }
3686 else if (typeQualifier.qualifier == EvqBuffer)
3687 {
3688 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3689 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003690 }
3691
Jamie Madill1566ef72013-06-20 11:55:54 -04003692 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3693 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003694 if (typeQualifier.qualifier == EvqUniform)
3695 {
3696 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3697 }
3698 else if (typeQualifier.qualifier == EvqBuffer)
3699 {
3700 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3701 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003702 }
3703
Olli Etuaho856c4972016-08-08 11:38:39 +03003704 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003705
Martin Radev2cc85b32016-08-05 16:22:53 +03003706 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3707
Jamie Madill98493dd2013-07-08 14:39:03 -04003708 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303709 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3710 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003711 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303712 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003713 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303714 {
jchen10cc2a10e2017-05-03 14:05:12 +08003715 std::string reason("unsupported type - ");
3716 reason += fieldType->getBasicString();
3717 reason += " types are not allowed in interface blocks";
3718 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003719 }
3720
Jamie Madill98493dd2013-07-08 14:39:03 -04003721 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003722 switch (qualifier)
3723 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003724 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003725 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003726 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003727 if (typeQualifier.qualifier == EvqBuffer)
3728 {
3729 error(field->line(), "invalid qualifier on shader storage block member",
3730 getQualifierString(qualifier));
3731 }
3732 break;
3733 case EvqBuffer:
3734 if (typeQualifier.qualifier == EvqUniform)
3735 {
3736 error(field->line(), "invalid qualifier on uniform block member",
3737 getQualifierString(qualifier));
3738 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003739 break;
3740 default:
3741 error(field->line(), "invalid qualifier on interface block member",
3742 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003743 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003744 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003745
Martin Radev70866b82016-07-22 15:27:42 +03003746 if (fieldType->isInvariant())
3747 {
3748 error(field->line(), "invalid qualifier on interface block member", "invariant");
3749 }
3750
Jamie Madilla5efff92013-06-06 11:56:47 -04003751 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003752 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003753 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003754 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003755
Jamie Madill98493dd2013-07-08 14:39:03 -04003756 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003757 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003758 error(field->line(), "invalid layout qualifier: cannot be used here",
3759 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003760 }
3761
Jamie Madill98493dd2013-07-08 14:39:03 -04003762 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003763 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003764 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003765 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003766 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003767 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003768 warning(field->line(),
3769 "extraneous layout qualifier: only has an effect on matrix types",
3770 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003771 }
3772
Jamie Madill98493dd2013-07-08 14:39:03 -04003773 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003774
Olli Etuahoebee5b32017-11-23 12:56:32 +02003775 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3776 typeQualifier.qualifier != EvqBuffer)
3777 {
3778 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3779 checkIsNotUnsizedArray(field->line(),
3780 "array members of interface blocks must specify a size",
3781 field->name().c_str(), field->type());
3782 }
3783
Jiajia Qinbc585152017-06-23 15:42:17 +08003784 if (typeQualifier.qualifier == EvqBuffer)
3785 {
3786 // set memory qualifiers
3787 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3788 // qualified with a memory qualifier, it is as if all of its members were declared with
3789 // the same memory qualifier.
3790 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3791 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3792 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3793 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3794 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3795 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3796 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3797 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3798 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3799 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3800 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003801 }
3802
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003803 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3804 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003805 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3806 {
3807 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3808 }
3809
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003810 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3811 if (arrayIndex != nullptr)
3812 {
3813 interfaceBlockType.makeArray(arraySize);
3814 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003815
Olli Etuaho195be942017-12-04 23:40:14 +02003816 // The instance variable gets created to refer to the interface block type from the AST
3817 // regardless of if there's an instance name. It just has an empty name if there's no instance
3818 // name.
Jamie Madill98493dd2013-07-08 14:39:03 -04003819 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003820 {
Olli Etuaho195be942017-12-04 23:40:14 +02003821 instanceName = NewPoolTString("");
3822 }
3823 TVariable *instanceVariable =
3824 new TVariable(&symbolTable, instanceName, interfaceBlockType, SymbolType::UserDefined);
3825
3826 if (instanceName->empty())
3827 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003828 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003829 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3830 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003831 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303832 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003833
3834 // set parent pointer of the field variable
3835 fieldType->setInterfaceBlock(interfaceBlock);
3836
Olli Etuaho195be942017-12-04 23:40:14 +02003837 TVariable *fieldVariable =
3838 new TVariable(&symbolTable, &field->name(), *fieldType, SymbolType::UserDefined);
3839 if (symbolTable.declareVariable(fieldVariable))
Olli Etuaho0f684632017-07-13 12:42:15 +03003840 {
3841 fieldVariable->setQualifier(typeQualifier.qualifier);
3842 }
3843 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303844 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003845 error(field->line(), "redefinition of an interface block member name",
3846 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003847 }
3848 }
3849 }
3850 else
3851 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003852 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003853
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003854 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003855 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303856 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003857 error(instanceLine, "redefinition of an interface block instance name",
3858 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003859 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003860 }
3861
Olli Etuaho195be942017-12-04 23:40:14 +02003862 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3863 blockSymbol->setLine(typeQualifier.line);
3864 TIntermDeclaration *declaration = new TIntermDeclaration();
3865 declaration->appendDeclarator(blockSymbol);
3866 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003867
3868 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003869 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003870}
3871
Olli Etuaho383b7912016-08-05 11:22:59 +03003872void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003873{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003874 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003875
3876 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003877 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303878 if (mStructNestingLevel > 1)
3879 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003880 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003881 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003882}
3883
3884void TParseContext::exitStructDeclaration()
3885{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003886 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003887}
3888
Olli Etuaho8a176262016-08-16 14:23:01 +03003889void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003890{
Jamie Madillacb4b812016-11-07 13:50:29 -05003891 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303892 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003893 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003894 }
3895
Arun Patole7e7e68d2015-05-22 12:02:25 +05303896 if (field.type()->getBasicType() != EbtStruct)
3897 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003898 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003899 }
3900
3901 // We're already inside a structure definition at this point, so add
3902 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303903 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3904 {
Jamie Madill41a49272014-03-18 16:10:13 -04003905 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003906 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3907 << " 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 {
4773 structName = NewPoolTString("");
4774 structSymbolType = SymbolType::Empty;
4775 }
4776 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004777
Jamie Madill9b820842015-02-12 10:40:10 -05004778 // Store a bool in the struct if we're at global scope, to allow us to
4779 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004780 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004781
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004782 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004783 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004784 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004785 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304786 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004787 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004788 }
4789 }
4790
4791 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004792 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004793 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004794 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004795 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004796 switch (qualifier)
4797 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004798 case EvqGlobal:
4799 case EvqTemporary:
4800 break;
4801 default:
4802 error(field.line(), "invalid qualifier on struct member",
4803 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004804 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004805 }
Martin Radev70866b82016-07-22 15:27:42 +03004806 if (field.type()->isInvariant())
4807 {
4808 error(field.line(), "invalid qualifier on struct member", "invariant");
4809 }
jchen104cdac9e2017-05-08 11:01:20 +08004810 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4811 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004812 {
4813 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4814 }
4815
Olli Etuahoebee5b32017-11-23 12:56:32 +02004816 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4817 field.name().c_str(), field.type());
4818
Olli Etuaho43364892017-02-13 16:00:12 +00004819 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4820
4821 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004822
4823 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004824 }
4825
Martin Radev4a9cd802016-09-01 16:51:51 +03004826 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004827 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004828 exitStructDeclaration();
4829
Martin Radev4a9cd802016-09-01 16:51:51 +03004830 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004831}
4832
Jamie Madillb98c3a82015-07-23 14:26:04 -04004833TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004834 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004835 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004836{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004837 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004838 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004839 init->isVector())
4840 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004841 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4842 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004843 return nullptr;
4844 }
4845
Olli Etuaho923ecef2017-10-11 12:01:38 +03004846 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004847 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004848 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004849 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004850 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004851 }
4852
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004853 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4854 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004855 return node;
4856}
4857
4858TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4859{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004860 if (mSwitchNestingLevel == 0)
4861 {
4862 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004863 return nullptr;
4864 }
4865 if (condition == nullptr)
4866 {
4867 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004868 return nullptr;
4869 }
4870 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004871 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004872 {
4873 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004874 }
4875 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004876 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4877 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4878 // fold in case labels.
4879 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004880 {
4881 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004882 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004883 TIntermCase *node = new TIntermCase(condition);
4884 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004885 return node;
4886}
4887
4888TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4889{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004890 if (mSwitchNestingLevel == 0)
4891 {
4892 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004893 return nullptr;
4894 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004895 TIntermCase *node = new TIntermCase(nullptr);
4896 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004897 return node;
4898}
4899
Jamie Madillb98c3a82015-07-23 14:26:04 -04004900TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4901 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004902 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004903{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004904 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004905
4906 switch (op)
4907 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004908 case EOpLogicalNot:
4909 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4910 child->isVector())
4911 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004912 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004913 return nullptr;
4914 }
4915 break;
4916 case EOpBitwiseNot:
4917 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4918 child->isMatrix() || child->isArray())
4919 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004920 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004921 return nullptr;
4922 }
4923 break;
4924 case EOpPostIncrement:
4925 case EOpPreIncrement:
4926 case EOpPostDecrement:
4927 case EOpPreDecrement:
4928 case EOpNegative:
4929 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004930 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4931 child->getBasicType() == EbtBool || child->isArray() ||
4932 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004933 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004934 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004935 return nullptr;
4936 }
4937 // Operators for built-ins are already type checked against their prototype.
4938 default:
4939 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004940 }
4941
Jiajia Qinbc585152017-06-23 15:42:17 +08004942 if (child->getMemoryQualifier().writeonly)
4943 {
4944 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4945 return nullptr;
4946 }
4947
Olli Etuahof119a262016-08-19 15:54:22 +03004948 TIntermUnary *node = new TIntermUnary(op, child);
4949 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004950
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004951 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004952}
4953
Olli Etuaho09b22472015-02-11 11:47:26 +02004954TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4955{
Olli Etuahocce89652017-06-19 16:04:09 +03004956 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004957 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004958 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004959 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004960 return child;
4961 }
4962 return node;
4963}
4964
Jamie Madillb98c3a82015-07-23 14:26:04 -04004965TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4966 TIntermTyped *child,
4967 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004968{
Olli Etuaho856c4972016-08-08 11:38:39 +03004969 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004970 return addUnaryMath(op, child, loc);
4971}
4972
Jamie Madillb98c3a82015-07-23 14:26:04 -04004973bool TParseContext::binaryOpCommonCheck(TOperator op,
4974 TIntermTyped *left,
4975 TIntermTyped *right,
4976 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004977{
jchen10b4cf5652017-05-05 18:51:17 +08004978 // Check opaque types are not allowed to be operands in expressions other than array indexing
4979 // and structure member selection.
4980 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4981 {
4982 switch (op)
4983 {
4984 case EOpIndexDirect:
4985 case EOpIndexIndirect:
4986 break;
4987 case EOpIndexDirectStruct:
4988 UNREACHABLE();
4989
4990 default:
4991 error(loc, "Invalid operation for variables with an opaque type",
4992 GetOperatorString(op));
4993 return false;
4994 }
4995 }
jchen10cc2a10e2017-05-03 14:05:12 +08004996
Jiajia Qinbc585152017-06-23 15:42:17 +08004997 if (right->getMemoryQualifier().writeonly)
4998 {
4999 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5000 return false;
5001 }
5002
5003 if (left->getMemoryQualifier().writeonly)
5004 {
5005 switch (op)
5006 {
5007 case EOpAssign:
5008 case EOpInitialize:
5009 case EOpIndexDirect:
5010 case EOpIndexIndirect:
5011 case EOpIndexDirectStruct:
5012 case EOpIndexDirectInterfaceBlock:
5013 break;
5014 default:
5015 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5016 return false;
5017 }
5018 }
5019
Olli Etuaho244be012016-08-18 15:26:02 +03005020 if (left->getType().getStruct() || right->getType().getStruct())
5021 {
5022 switch (op)
5023 {
5024 case EOpIndexDirectStruct:
5025 ASSERT(left->getType().getStruct());
5026 break;
5027 case EOpEqual:
5028 case EOpNotEqual:
5029 case EOpAssign:
5030 case EOpInitialize:
5031 if (left->getType() != right->getType())
5032 {
5033 return false;
5034 }
5035 break;
5036 default:
5037 error(loc, "Invalid operation for structs", GetOperatorString(op));
5038 return false;
5039 }
5040 }
5041
Olli Etuaho94050052017-05-08 14:17:44 +03005042 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5043 {
5044 switch (op)
5045 {
5046 case EOpIndexDirectInterfaceBlock:
5047 ASSERT(left->getType().getInterfaceBlock());
5048 break;
5049 default:
5050 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5051 return false;
5052 }
5053 }
5054
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005055 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005056 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005057 error(loc, "array / non-array mismatch", GetOperatorString(op));
5058 return false;
5059 }
5060
5061 if (left->isArray())
5062 {
5063 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005064 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005065 {
5066 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5067 return false;
5068 }
5069
Olli Etuahoe79904c2015-03-18 16:56:42 +02005070 switch (op)
5071 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005072 case EOpEqual:
5073 case EOpNotEqual:
5074 case EOpAssign:
5075 case EOpInitialize:
5076 break;
5077 default:
5078 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5079 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005080 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005081 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005082 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005083 {
5084 error(loc, "array size mismatch", GetOperatorString(op));
5085 return false;
5086 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005087 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005088
5089 // Check ops which require integer / ivec parameters
5090 bool isBitShift = false;
5091 switch (op)
5092 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005093 case EOpBitShiftLeft:
5094 case EOpBitShiftRight:
5095 case EOpBitShiftLeftAssign:
5096 case EOpBitShiftRightAssign:
5097 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5098 // check that the basic type is an integer type.
5099 isBitShift = true;
5100 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5101 {
5102 return false;
5103 }
5104 break;
5105 case EOpBitwiseAnd:
5106 case EOpBitwiseXor:
5107 case EOpBitwiseOr:
5108 case EOpBitwiseAndAssign:
5109 case EOpBitwiseXorAssign:
5110 case EOpBitwiseOrAssign:
5111 // It is enough to check the type of only one operand, since later it
5112 // is checked that the operand types match.
5113 if (!IsInteger(left->getBasicType()))
5114 {
5115 return false;
5116 }
5117 break;
5118 default:
5119 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005120 }
5121
5122 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5123 // So the basic type should usually match.
5124 if (!isBitShift && left->getBasicType() != right->getBasicType())
5125 {
5126 return false;
5127 }
5128
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005129 // Check that:
5130 // 1. Type sizes match exactly on ops that require that.
5131 // 2. Restrictions for structs that contain arrays or samplers are respected.
5132 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005133 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005134 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005135 case EOpAssign:
5136 case EOpInitialize:
5137 case EOpEqual:
5138 case EOpNotEqual:
5139 // ESSL 1.00 sections 5.7, 5.8, 5.9
5140 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5141 {
5142 error(loc, "undefined operation for structs containing arrays",
5143 GetOperatorString(op));
5144 return false;
5145 }
5146 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5147 // we interpret the spec so that this extends to structs containing samplers,
5148 // similarly to ESSL 1.00 spec.
5149 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5150 left->getType().isStructureContainingSamplers())
5151 {
5152 error(loc, "undefined operation for structs containing samplers",
5153 GetOperatorString(op));
5154 return false;
5155 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005156
Olli Etuahoe1805592017-01-02 16:41:20 +00005157 if ((left->getNominalSize() != right->getNominalSize()) ||
5158 (left->getSecondarySize() != right->getSecondarySize()))
5159 {
5160 error(loc, "dimension mismatch", GetOperatorString(op));
5161 return false;
5162 }
5163 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005164 case EOpLessThan:
5165 case EOpGreaterThan:
5166 case EOpLessThanEqual:
5167 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005168 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005169 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005170 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005171 return false;
5172 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005173 break;
5174 case EOpAdd:
5175 case EOpSub:
5176 case EOpDiv:
5177 case EOpIMod:
5178 case EOpBitShiftLeft:
5179 case EOpBitShiftRight:
5180 case EOpBitwiseAnd:
5181 case EOpBitwiseXor:
5182 case EOpBitwiseOr:
5183 case EOpAddAssign:
5184 case EOpSubAssign:
5185 case EOpDivAssign:
5186 case EOpIModAssign:
5187 case EOpBitShiftLeftAssign:
5188 case EOpBitShiftRightAssign:
5189 case EOpBitwiseAndAssign:
5190 case EOpBitwiseXorAssign:
5191 case EOpBitwiseOrAssign:
5192 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5193 {
5194 return false;
5195 }
5196
5197 // Are the sizes compatible?
5198 if (left->getNominalSize() != right->getNominalSize() ||
5199 left->getSecondarySize() != right->getSecondarySize())
5200 {
5201 // If the nominal sizes of operands do not match:
5202 // One of them must be a scalar.
5203 if (!left->isScalar() && !right->isScalar())
5204 return false;
5205
5206 // In the case of compound assignment other than multiply-assign,
5207 // the right side needs to be a scalar. Otherwise a vector/matrix
5208 // would be assigned to a scalar. A scalar can't be shifted by a
5209 // vector either.
5210 if (!right->isScalar() &&
5211 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5212 return false;
5213 }
5214 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005215 default:
5216 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005217 }
5218
Olli Etuahod6b14282015-03-17 14:31:35 +02005219 return true;
5220}
5221
Olli Etuaho1dded802016-08-18 18:13:13 +03005222bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5223 const TType &left,
5224 const TType &right)
5225{
5226 switch (op)
5227 {
5228 case EOpMul:
5229 case EOpMulAssign:
5230 return left.getNominalSize() == right.getNominalSize() &&
5231 left.getSecondarySize() == right.getSecondarySize();
5232 case EOpVectorTimesScalar:
5233 return true;
5234 case EOpVectorTimesScalarAssign:
5235 ASSERT(!left.isMatrix() && !right.isMatrix());
5236 return left.isVector() && !right.isVector();
5237 case EOpVectorTimesMatrix:
5238 return left.getNominalSize() == right.getRows();
5239 case EOpVectorTimesMatrixAssign:
5240 ASSERT(!left.isMatrix() && right.isMatrix());
5241 return left.isVector() && left.getNominalSize() == right.getRows() &&
5242 left.getNominalSize() == right.getCols();
5243 case EOpMatrixTimesVector:
5244 return left.getCols() == right.getNominalSize();
5245 case EOpMatrixTimesScalar:
5246 return true;
5247 case EOpMatrixTimesScalarAssign:
5248 ASSERT(left.isMatrix() && !right.isMatrix());
5249 return !right.isVector();
5250 case EOpMatrixTimesMatrix:
5251 return left.getCols() == right.getRows();
5252 case EOpMatrixTimesMatrixAssign:
5253 ASSERT(left.isMatrix() && right.isMatrix());
5254 // We need to check two things:
5255 // 1. The matrix multiplication step is valid.
5256 // 2. The result will have the same number of columns as the lvalue.
5257 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5258
5259 default:
5260 UNREACHABLE();
5261 return false;
5262 }
5263}
5264
Jamie Madillb98c3a82015-07-23 14:26:04 -04005265TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5266 TIntermTyped *left,
5267 TIntermTyped *right,
5268 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005269{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005270 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005271 return nullptr;
5272
Olli Etuahofc1806e2015-03-17 13:03:11 +02005273 switch (op)
5274 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005275 case EOpEqual:
5276 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005277 case EOpLessThan:
5278 case EOpGreaterThan:
5279 case EOpLessThanEqual:
5280 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005281 break;
5282 case EOpLogicalOr:
5283 case EOpLogicalXor:
5284 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005285 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5286 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005287 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005288 {
5289 return nullptr;
5290 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005291 // Basic types matching should have been already checked.
5292 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005293 break;
5294 case EOpAdd:
5295 case EOpSub:
5296 case EOpDiv:
5297 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005298 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5299 !right->getType().getStruct());
5300 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005301 {
5302 return nullptr;
5303 }
5304 break;
5305 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005306 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5307 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005308 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005309 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005310 {
5311 return nullptr;
5312 }
5313 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005314 default:
5315 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005316 }
5317
Olli Etuaho1dded802016-08-18 18:13:13 +03005318 if (op == EOpMul)
5319 {
5320 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5321 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5322 {
5323 return nullptr;
5324 }
5325 }
5326
Olli Etuaho3fdec912016-08-18 15:08:06 +03005327 TIntermBinary *node = new TIntermBinary(op, left, right);
5328 node->setLine(loc);
5329
Olli Etuaho3fdec912016-08-18 15:08:06 +03005330 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005331 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005332}
5333
Jamie Madillb98c3a82015-07-23 14:26:04 -04005334TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5335 TIntermTyped *left,
5336 TIntermTyped *right,
5337 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005338{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005339 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005340 if (node == 0)
5341 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005342 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5343 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005344 return left;
5345 }
5346 return node;
5347}
5348
Jamie Madillb98c3a82015-07-23 14:26:04 -04005349TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5350 TIntermTyped *left,
5351 TIntermTyped *right,
5352 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005353{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005354 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005355 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005356 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005357 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5358 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005359 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005360 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005361 }
5362 return node;
5363}
5364
Olli Etuaho13389b62016-10-16 11:48:18 +01005365TIntermBinary *TParseContext::createAssign(TOperator op,
5366 TIntermTyped *left,
5367 TIntermTyped *right,
5368 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005369{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005370 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005371 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005372 if (op == EOpMulAssign)
5373 {
5374 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5375 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5376 {
5377 return nullptr;
5378 }
5379 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005380 TIntermBinary *node = new TIntermBinary(op, left, right);
5381 node->setLine(loc);
5382
Olli Etuaho3fdec912016-08-18 15:08:06 +03005383 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005384 }
5385 return nullptr;
5386}
5387
Jamie Madillb98c3a82015-07-23 14:26:04 -04005388TIntermTyped *TParseContext::addAssign(TOperator op,
5389 TIntermTyped *left,
5390 TIntermTyped *right,
5391 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005392{
Olli Etuahocce89652017-06-19 16:04:09 +03005393 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005394 TIntermTyped *node = createAssign(op, left, right, loc);
5395 if (node == nullptr)
5396 {
5397 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005398 return left;
5399 }
5400 return node;
5401}
5402
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005403TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5404 TIntermTyped *right,
5405 const TSourceLoc &loc)
5406{
Corentin Wallez0d959252016-07-12 17:26:32 -04005407 // WebGL2 section 5.26, the following results in an error:
5408 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005409 if (mShaderSpec == SH_WEBGL2_SPEC &&
5410 (left->isArray() || left->getBasicType() == EbtVoid ||
5411 left->getType().isStructureContainingArrays() || right->isArray() ||
5412 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005413 {
5414 error(loc,
5415 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5416 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005417 }
5418
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005419 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5420 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5421 commaNode->getTypePointer()->setQualifier(resultQualifier);
5422 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005423}
5424
Olli Etuaho49300862015-02-20 14:54:49 +02005425TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5426{
5427 switch (op)
5428 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005429 case EOpContinue:
5430 if (mLoopNestingLevel <= 0)
5431 {
5432 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005433 }
5434 break;
5435 case EOpBreak:
5436 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5437 {
5438 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005439 }
5440 break;
5441 case EOpReturn:
5442 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5443 {
5444 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005445 }
5446 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005447 case EOpKill:
5448 if (mShaderType != GL_FRAGMENT_SHADER)
5449 {
5450 error(loc, "discard supported in fragment shaders only", "discard");
5451 }
5452 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005453 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005454 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005455 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005456 }
Olli Etuahocce89652017-06-19 16:04:09 +03005457 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005458}
5459
Jamie Madillb98c3a82015-07-23 14:26:04 -04005460TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005461 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005462 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005463{
Olli Etuahocce89652017-06-19 16:04:09 +03005464 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005465 {
Olli Etuahocce89652017-06-19 16:04:09 +03005466 ASSERT(op == EOpReturn);
5467 mFunctionReturnsValue = true;
5468 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5469 {
5470 error(loc, "void function cannot return a value", "return");
5471 }
5472 else if (*mCurrentFunctionType != expression->getType())
5473 {
5474 error(loc, "function return is not matching type:", "return");
5475 }
Olli Etuaho49300862015-02-20 14:54:49 +02005476 }
Olli Etuahocce89652017-06-19 16:04:09 +03005477 TIntermBranch *node = new TIntermBranch(op, expression);
5478 node->setLine(loc);
5479 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005480}
5481
Martin Radev84aa2dc2017-09-11 15:51:02 +03005482void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5483{
5484 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5485 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5486 bool isTextureGather = (name == "textureGather");
5487 bool isTextureGatherOffset = (name == "textureGatherOffset");
5488 if (isTextureGather || isTextureGatherOffset)
5489 {
5490 TIntermNode *componentNode = nullptr;
5491 TIntermSequence *arguments = functionCall->getSequence();
5492 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5493 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5494 ASSERT(sampler != nullptr);
5495 switch (sampler->getBasicType())
5496 {
5497 case EbtSampler2D:
5498 case EbtISampler2D:
5499 case EbtUSampler2D:
5500 case EbtSampler2DArray:
5501 case EbtISampler2DArray:
5502 case EbtUSampler2DArray:
5503 if ((isTextureGather && arguments->size() == 3u) ||
5504 (isTextureGatherOffset && arguments->size() == 4u))
5505 {
5506 componentNode = arguments->back();
5507 }
5508 break;
5509 case EbtSamplerCube:
5510 case EbtISamplerCube:
5511 case EbtUSamplerCube:
5512 ASSERT(!isTextureGatherOffset);
5513 if (arguments->size() == 3u)
5514 {
5515 componentNode = arguments->back();
5516 }
5517 break;
5518 case EbtSampler2DShadow:
5519 case EbtSampler2DArrayShadow:
5520 case EbtSamplerCubeShadow:
5521 break;
5522 default:
5523 UNREACHABLE();
5524 break;
5525 }
5526 if (componentNode)
5527 {
5528 const TIntermConstantUnion *componentConstantUnion =
5529 componentNode->getAsConstantUnion();
5530 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5531 {
5532 error(functionCall->getLine(), "Texture component must be a constant expression",
5533 name.c_str());
5534 }
5535 else
5536 {
5537 int component = componentConstantUnion->getIConst(0);
5538 if (component < 0 || component > 3)
5539 {
5540 error(functionCall->getLine(), "Component must be in the range [0;3]",
5541 name.c_str());
5542 }
5543 }
5544 }
5545 }
5546}
5547
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005548void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5549{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005550 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005551 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005552 TIntermNode *offset = nullptr;
5553 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005554 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005555 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5556 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5557 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005558 {
5559 offset = arguments->back();
5560 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005561 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005562 {
5563 // A bias parameter might follow the offset parameter.
5564 ASSERT(arguments->size() >= 3);
5565 offset = (*arguments)[2];
5566 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005567 else if (name == "textureGatherOffset")
5568 {
5569 ASSERT(arguments->size() >= 3u);
5570 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5571 ASSERT(sampler != nullptr);
5572 switch (sampler->getBasicType())
5573 {
5574 case EbtSampler2D:
5575 case EbtISampler2D:
5576 case EbtUSampler2D:
5577 case EbtSampler2DArray:
5578 case EbtISampler2DArray:
5579 case EbtUSampler2DArray:
5580 offset = (*arguments)[2];
5581 break;
5582 case EbtSampler2DShadow:
5583 case EbtSampler2DArrayShadow:
5584 offset = (*arguments)[3];
5585 break;
5586 default:
5587 UNREACHABLE();
5588 break;
5589 }
5590 useTextureGatherOffsetConstraints = true;
5591 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005592 if (offset != nullptr)
5593 {
5594 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5595 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5596 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005597 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005598 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005599 }
5600 else
5601 {
5602 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5603 size_t size = offsetConstantUnion->getType().getObjectSize();
5604 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005605 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5606 : mMinProgramTexelOffset;
5607 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5608 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005609 for (size_t i = 0u; i < size; ++i)
5610 {
5611 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005612 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005613 {
5614 std::stringstream tokenStream;
5615 tokenStream << offsetValue;
5616 std::string token = tokenStream.str();
5617 error(offset->getLine(), "Texture offset value out of valid range",
5618 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005619 }
5620 }
5621 }
5622 }
5623}
5624
Jiajia Qina3106c52017-11-03 09:39:39 +08005625void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5626{
5627 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5628 if (IsAtomicBuiltin(name))
5629 {
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.",
5650 functionCall->getFunctionSymbolInfo()->getName().c_str());
5651 }
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);
Martin Radev2cc85b32016-08-05 16:22:53 +03005658 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5659
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 Etuahoaf6fc1b2017-01-26 17:45:35 -08005759 return addMethod(fnCall, 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 Etuahoaf6fc1b2017-01-26 17:45:35 -08005770 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5771 }
5772}
5773
5774TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5775 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.
5783 // So accessing fnCall->getName() below is safe.
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005784 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005785 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005786 error(loc, "invalid method", fnCall->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
5811TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5812 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 Etuaho54a29ff2017-11-28 17:35:20 +02005819 const TSymbol *symbol = symbolTable.find(fnCall->name(), mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005820 if (symbol != nullptr && !symbol->isFunction())
5821 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005822 error(loc, "function name expected", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005823 }
5824 else
5825 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005826 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->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 Etuaho54a29ff2017-11-28 17:35:20 +02005830 error(loc, "no matching overloaded function found", fnCall->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