blob: b8fd3a455f6fabb56c43bbc0e82d0e498af8b013 [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).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001057 // Geometry shader requires each user-defined input be declared as arrays or inside input
1058 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1059 // interface matching, such variables and blocks are treated as though they were not declared
1060 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001061 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001062 sh::IsVarying(elementType.qualifier) &&
1063 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001064 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001065 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001066 TType(elementType).getCompleteString().c_str());
1067 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001068 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001069 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070}
1071
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001073void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1074 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001075 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076{
Olli Etuaho3739d232015-04-08 12:23:44 +03001077 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001078 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001079 {
1080 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001081 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001082
1083 // Generate informative error messages for ESSL1.
1084 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001085 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001086 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301087 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001088 "structures containing arrays may not be declared constant since they cannot be "
1089 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301090 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001091 }
1092 else
1093 {
1094 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1095 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001096 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001097 // This will make the type sized if it isn't sized yet.
1098 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1099 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100}
1101
Olli Etuaho2935c582015-04-08 14:32:06 +03001102// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103// and update the symbol table.
1104//
Olli Etuaho2935c582015-04-08 14:32:06 +03001105// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001107bool TParseContext::declareVariable(const TSourceLoc &line,
1108 const TString &identifier,
1109 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001110 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111{
Olli Etuaho2935c582015-04-08 14:32:06 +03001112 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001113
Olli Etuaho195be942017-12-04 23:40:14 +02001114 (*variable) = new TVariable(&symbolTable, &identifier, type, SymbolType::UserDefined);
1115
Olli Etuaho43364892017-02-13 16:00:12 +00001116 checkBindingIsValid(line, type);
1117
Olli Etuaho856c4972016-08-08 11:38:39 +03001118 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001119
Olli Etuaho2935c582015-04-08 14:32:06 +03001120 // gl_LastFragData may be redeclared with a new precision qualifier
1121 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1122 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001123 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1124 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001125 if (type.isArrayOfArrays())
1126 {
1127 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1128 identifier.c_str());
1129 return false;
1130 }
1131 else if (static_cast<int>(type.getOutermostArraySize()) ==
1132 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001133 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001134 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001135 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001136 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001137 }
1138 }
1139 else
1140 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001141 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1142 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001143 return false;
1144 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001145 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146
Olli Etuaho8a176262016-08-16 14:23:01 +03001147 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001148 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001149
Olli Etuaho195be942017-12-04 23:40:14 +02001150 if (!symbolTable.declareVariable(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001151 {
1152 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001153 return false;
1154 }
1155
Olli Etuaho8a176262016-08-16 14:23:01 +03001156 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001157 return false;
1158
1159 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001160}
1161
Martin Radev70866b82016-07-22 15:27:42 +03001162void TParseContext::checkIsParameterQualifierValid(
1163 const TSourceLoc &line,
1164 const TTypeQualifierBuilder &typeQualifierBuilder,
1165 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301166{
Olli Etuahocce89652017-06-19 16:04:09 +03001167 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001168 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001169
1170 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301171 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001172 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1173 }
1174
1175 if (!IsImage(type->getBasicType()))
1176 {
Olli Etuaho43364892017-02-13 16:00:12 +00001177 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001178 }
1179 else
1180 {
1181 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001182 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183
Martin Radev70866b82016-07-22 15:27:42 +03001184 type->setQualifier(typeQualifier.qualifier);
1185
1186 if (typeQualifier.precision != EbpUndefined)
1187 {
1188 type->setPrecision(typeQualifier.precision);
1189 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001190}
1191
Olli Etuaho703671e2017-11-08 17:47:18 +02001192template <size_t size>
1193bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1194 const std::array<TExtension, size> &extensions)
1195{
1196 ASSERT(!extensions.empty());
1197 const TExtensionBehavior &extBehavior = extensionBehavior();
1198
1199 bool canUseWithWarning = false;
1200 bool canUseWithoutWarning = false;
1201
1202 const char *errorMsgString = "";
1203 TExtension errorMsgExtension = TExtension::UNDEFINED;
1204
1205 for (TExtension extension : extensions)
1206 {
1207 auto extIter = extBehavior.find(extension);
1208 if (canUseWithWarning)
1209 {
1210 // We already have an extension that we can use, but with a warning.
1211 // See if we can use the alternative extension without a warning.
1212 if (extIter == extBehavior.end())
1213 {
1214 continue;
1215 }
1216 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1217 {
1218 canUseWithoutWarning = true;
1219 break;
1220 }
1221 continue;
1222 }
1223 if (extIter == extBehavior.end())
1224 {
1225 errorMsgString = "extension is not supported";
1226 errorMsgExtension = extension;
1227 }
1228 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1229 {
1230 errorMsgString = "extension is disabled";
1231 errorMsgExtension = extension;
1232 }
1233 else if (extIter->second == EBhWarn)
1234 {
1235 errorMsgExtension = extension;
1236 canUseWithWarning = true;
1237 }
1238 else
1239 {
1240 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1241 canUseWithoutWarning = true;
1242 break;
1243 }
1244 }
1245
1246 if (canUseWithoutWarning)
1247 {
1248 return true;
1249 }
1250 if (canUseWithWarning)
1251 {
1252 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1253 return true;
1254 }
1255 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1256 return false;
1257}
1258
1259template bool TParseContext::checkCanUseOneOfExtensions(
1260 const TSourceLoc &line,
1261 const std::array<TExtension, 1> &extensions);
1262template bool TParseContext::checkCanUseOneOfExtensions(
1263 const TSourceLoc &line,
1264 const std::array<TExtension, 2> &extensions);
1265template bool TParseContext::checkCanUseOneOfExtensions(
1266 const TSourceLoc &line,
1267 const std::array<TExtension, 3> &extensions);
1268
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001269bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001270{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001271 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001272 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001273}
1274
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001275// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1276// compile-time or link-time errors are the same whether or not the declaration is empty".
1277// This function implements all the checks that are done on qualifiers regardless of if the
1278// declaration is empty.
1279void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1280 const sh::TLayoutQualifier &layoutQualifier,
1281 const TSourceLoc &location)
1282{
1283 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1284 {
1285 error(location, "Shared memory declarations cannot have layout specified", "layout");
1286 }
1287
1288 if (layoutQualifier.matrixPacking != EmpUnspecified)
1289 {
1290 error(location, "layout qualifier only valid for interface blocks",
1291 getMatrixPackingString(layoutQualifier.matrixPacking));
1292 return;
1293 }
1294
1295 if (layoutQualifier.blockStorage != EbsUnspecified)
1296 {
1297 error(location, "layout qualifier only valid for interface blocks",
1298 getBlockStorageString(layoutQualifier.blockStorage));
1299 return;
1300 }
1301
1302 if (qualifier == EvqFragmentOut)
1303 {
1304 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1305 {
1306 error(location, "invalid layout qualifier combination", "yuv");
1307 return;
1308 }
1309 }
1310 else
1311 {
1312 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1313 }
1314
Olli Etuaho95468d12017-05-04 11:14:34 +03001315 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1316 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001317 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1318 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001319 {
1320 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1321 }
1322
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001323 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001324 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001325 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001326 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001327 // We're not checking whether the uniform location is in range here since that depends on
1328 // the type of the variable.
1329 // The type can only be fully determined for non-empty declarations.
1330 }
1331 if (!canHaveLocation)
1332 {
1333 checkLocationIsNotSpecified(location, layoutQualifier);
1334 }
1335}
1336
jchen104cdac9e2017-05-08 11:01:20 +08001337void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1338 const TSourceLoc &location)
1339{
1340 if (publicType.precision != EbpHigh)
1341 {
1342 error(location, "Can only be highp", "atomic counter");
1343 }
1344 // dEQP enforces compile error if location is specified. See uniform_location.test.
1345 if (publicType.layoutQualifier.location != -1)
1346 {
1347 error(location, "location must not be set for atomic_uint", "layout");
1348 }
1349 if (publicType.layoutQualifier.binding == -1)
1350 {
1351 error(location, "no binding specified", "atomic counter");
1352 }
1353}
1354
Olli Etuaho55bde912017-10-25 13:41:13 +03001355void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001356{
Olli Etuaho55bde912017-10-25 13:41:13 +03001357 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001358 {
1359 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1360 // error. It is assumed that this applies to empty declarations as well.
1361 error(location, "empty array declaration needs to specify a size", "");
1362 }
Martin Radevb8b01222016-11-20 23:25:53 +02001363}
1364
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001365// These checks are done for all declarations that are non-empty. They're done for non-empty
1366// declarations starting a declarator list, and declarators that follow an empty declaration.
1367void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1368 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001369{
Olli Etuahofa33d582015-04-09 14:33:12 +03001370 switch (publicType.qualifier)
1371 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001372 case EvqVaryingIn:
1373 case EvqVaryingOut:
1374 case EvqAttribute:
1375 case EvqVertexIn:
1376 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001377 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001378 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001379 {
1380 error(identifierLocation, "cannot be used with a structure",
1381 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001382 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001383 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001384 break;
1385 case EvqBuffer:
1386 if (publicType.getBasicType() != EbtInterfaceBlock)
1387 {
1388 error(identifierLocation,
1389 "cannot declare buffer variables at global scope(outside a block)",
1390 getQualifierString(publicType.qualifier));
1391 return;
1392 }
1393 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001394 default:
1395 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001396 }
jchen10cc2a10e2017-05-03 14:05:12 +08001397 std::string reason(getBasicString(publicType.getBasicType()));
1398 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001399 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001400 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001401 {
1402 return;
1403 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001404
Andrei Volykhina5527072017-03-22 16:46:30 +03001405 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1406 publicType.qualifier != EvqConst) &&
1407 publicType.getBasicType() == EbtYuvCscStandardEXT)
1408 {
1409 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1410 getQualifierString(publicType.qualifier));
1411 return;
1412 }
1413
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001414 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1415 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001416 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1417 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001418 TType type(publicType);
1419 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001420 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001421 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1422 publicType.layoutQualifier);
1423 }
1424 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001425
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001426 // check for layout qualifier issues
1427 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001428
Martin Radev2cc85b32016-08-05 16:22:53 +03001429 if (IsImage(publicType.getBasicType()))
1430 {
1431
1432 switch (layoutQualifier.imageInternalFormat)
1433 {
1434 case EiifRGBA32F:
1435 case EiifRGBA16F:
1436 case EiifR32F:
1437 case EiifRGBA8:
1438 case EiifRGBA8_SNORM:
1439 if (!IsFloatImage(publicType.getBasicType()))
1440 {
1441 error(identifierLocation,
1442 "internal image format requires a floating image type",
1443 getBasicString(publicType.getBasicType()));
1444 return;
1445 }
1446 break;
1447 case EiifRGBA32I:
1448 case EiifRGBA16I:
1449 case EiifRGBA8I:
1450 case EiifR32I:
1451 if (!IsIntegerImage(publicType.getBasicType()))
1452 {
1453 error(identifierLocation,
1454 "internal image format requires an integer image type",
1455 getBasicString(publicType.getBasicType()));
1456 return;
1457 }
1458 break;
1459 case EiifRGBA32UI:
1460 case EiifRGBA16UI:
1461 case EiifRGBA8UI:
1462 case EiifR32UI:
1463 if (!IsUnsignedImage(publicType.getBasicType()))
1464 {
1465 error(identifierLocation,
1466 "internal image format requires an unsigned image type",
1467 getBasicString(publicType.getBasicType()));
1468 return;
1469 }
1470 break;
1471 case EiifUnspecified:
1472 error(identifierLocation, "layout qualifier", "No image internal format specified");
1473 return;
1474 default:
1475 error(identifierLocation, "layout qualifier", "unrecognized token");
1476 return;
1477 }
1478
1479 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1480 switch (layoutQualifier.imageInternalFormat)
1481 {
1482 case EiifR32F:
1483 case EiifR32I:
1484 case EiifR32UI:
1485 break;
1486 default:
1487 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1488 {
1489 error(identifierLocation, "layout qualifier",
1490 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1491 "image variables must be qualified readonly and/or writeonly");
1492 return;
1493 }
1494 break;
1495 }
1496 }
1497 else
1498 {
Olli Etuaho43364892017-02-13 16:00:12 +00001499 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001500 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1501 }
jchen104cdac9e2017-05-08 11:01:20 +08001502
1503 if (IsAtomicCounter(publicType.getBasicType()))
1504 {
1505 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1506 }
1507 else
1508 {
1509 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1510 }
Olli Etuaho43364892017-02-13 16:00:12 +00001511}
Martin Radev2cc85b32016-08-05 16:22:53 +03001512
Olli Etuaho43364892017-02-13 16:00:12 +00001513void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1514{
1515 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001516 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1517 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1518 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1519 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1520 // when it comes to which shaders are accepted by the compiler.
1521 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001522 if (IsImage(type.getBasicType()))
1523 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001524 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1525 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001526 }
1527 else if (IsSampler(type.getBasicType()))
1528 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001529 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1530 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001531 }
jchen104cdac9e2017-05-08 11:01:20 +08001532 else if (IsAtomicCounter(type.getBasicType()))
1533 {
1534 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1535 }
Olli Etuaho43364892017-02-13 16:00:12 +00001536 else
1537 {
1538 ASSERT(!IsOpaqueType(type.getBasicType()));
1539 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001540 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001541}
1542
Olli Etuaho856c4972016-08-08 11:38:39 +03001543void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1544 const TString &layoutQualifierName,
1545 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001546{
1547
1548 if (mShaderVersion < versionRequired)
1549 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001550 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001551 }
1552}
1553
Olli Etuaho856c4972016-08-08 11:38:39 +03001554bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1555 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001556{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001557 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001558 for (size_t i = 0u; i < localSize.size(); ++i)
1559 {
1560 if (localSize[i] != -1)
1561 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001562 error(location,
1563 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1564 "global layout declaration",
1565 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001566 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001567 }
1568 }
1569
Olli Etuaho8a176262016-08-16 14:23:01 +03001570 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001571}
1572
Olli Etuaho43364892017-02-13 16:00:12 +00001573void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001574 TLayoutImageInternalFormat internalFormat)
1575{
1576 if (internalFormat != EiifUnspecified)
1577 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001578 error(location, "invalid layout qualifier: only valid when used with images",
1579 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001580 }
Olli Etuaho43364892017-02-13 16:00:12 +00001581}
1582
1583void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1584{
1585 if (binding != -1)
1586 {
1587 error(location,
1588 "invalid layout qualifier: only valid when used with opaque types or blocks",
1589 "binding");
1590 }
1591}
1592
jchen104cdac9e2017-05-08 11:01:20 +08001593void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1594{
1595 if (offset != -1)
1596 {
1597 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1598 "offset");
1599 }
1600}
1601
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001602void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1603 int binding,
1604 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001605{
1606 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001607 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001608 {
1609 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1610 }
1611}
1612
1613void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1614 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001615 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001616{
1617 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001618 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001619 {
1620 error(location, "sampler binding greater than maximum texture units", "binding");
1621 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001622}
1623
Jiajia Qinbc585152017-06-23 15:42:17 +08001624void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1625 const TQualifier &qualifier,
1626 int binding,
1627 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001628{
1629 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001630 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001631 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001632 if (binding + size > mMaxUniformBufferBindings)
1633 {
1634 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1635 "binding");
1636 }
1637 }
1638 else if (qualifier == EvqBuffer)
1639 {
1640 if (binding + size > mMaxShaderStorageBufferBindings)
1641 {
1642 error(location,
1643 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1644 "binding");
1645 }
jchen10af713a22017-04-19 09:10:56 +08001646 }
1647}
jchen104cdac9e2017-05-08 11:01:20 +08001648void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1649{
1650 if (binding >= mMaxAtomicCounterBindings)
1651 {
1652 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1653 "binding");
1654 }
1655}
jchen10af713a22017-04-19 09:10:56 +08001656
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001657void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1658 int objectLocationCount,
1659 const TLayoutQualifier &layoutQualifier)
1660{
1661 int loc = layoutQualifier.location;
1662 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1663 {
1664 error(location, "Uniform location out of range", "location");
1665 }
1666}
1667
Andrei Volykhina5527072017-03-22 16:46:30 +03001668void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1669{
1670 if (yuv != false)
1671 {
1672 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1673 }
1674}
1675
Jiajia Qinbc585152017-06-23 15:42:17 +08001676void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1677 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001678{
1679 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1680 {
1681 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001682 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1683 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1684 qual == EvqInOut || qual == EvqConstReadOnly))
1685 {
1686 if (argument->getMemoryQualifier().writeonly)
1687 {
1688 error(argument->getLine(),
1689 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001690 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001691 return;
1692 }
1693 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001694 if (qual == EvqOut || qual == EvqInOut)
1695 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001696 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001697 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001698 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001699 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001700 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001701 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001702 }
1703 }
1704 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001705}
1706
Martin Radev70866b82016-07-22 15:27:42 +03001707void TParseContext::checkInvariantVariableQualifier(bool invariant,
1708 const TQualifier qualifier,
1709 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001710{
Martin Radev70866b82016-07-22 15:27:42 +03001711 if (!invariant)
1712 return;
1713
1714 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001715 {
Martin Radev70866b82016-07-22 15:27:42 +03001716 // input variables in the fragment shader can be also qualified as invariant
1717 if (!sh::CanBeInvariantESSL1(qualifier))
1718 {
1719 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1720 }
1721 }
1722 else
1723 {
1724 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1725 {
1726 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1727 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001728 }
1729}
1730
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001731bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001732{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001733 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001734}
1735
Jamie Madillb98c3a82015-07-23 14:26:04 -04001736void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1737 const char *extName,
1738 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001739{
1740 pp::SourceLocation srcLoc;
1741 srcLoc.file = loc.first_file;
1742 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001743 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001744}
1745
Jamie Madillb98c3a82015-07-23 14:26:04 -04001746void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1747 const char *name,
1748 const char *value,
1749 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001750{
1751 pp::SourceLocation srcLoc;
1752 srcLoc.file = loc.first_file;
1753 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001754 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001755}
1756
Martin Radev4c4c8e72016-08-04 12:25:34 +03001757sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001758{
Jamie Madill2f294c92017-11-20 14:47:26 -05001759 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001760 for (size_t i = 0u; i < result.size(); ++i)
1761 {
1762 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1763 {
1764 result[i] = 1;
1765 }
1766 else
1767 {
1768 result[i] = mComputeShaderLocalSize[i];
1769 }
1770 }
1771 return result;
1772}
1773
Olli Etuaho56229f12017-07-10 14:16:33 +03001774TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1775 const TSourceLoc &line)
1776{
1777 TIntermConstantUnion *node = new TIntermConstantUnion(
1778 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1779 node->setLine(line);
1780 return node;
1781}
1782
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783/////////////////////////////////////////////////////////////////////////////////
1784//
1785// Non-Errors.
1786//
1787/////////////////////////////////////////////////////////////////////////////////
1788
Jamie Madill5c097022014-08-20 16:38:32 -04001789const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1790 const TString *name,
1791 const TSymbol *symbol)
1792{
Jamie Madill5c097022014-08-20 16:38:32 -04001793 if (!symbol)
1794 {
1795 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001796 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001797 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001798
1799 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001800 {
1801 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001802 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001803 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001804
1805 const TVariable *variable = static_cast<const TVariable *>(symbol);
1806
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001807 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001808 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001809 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001810 }
1811
Olli Etuaho0f684632017-07-13 12:42:15 +03001812 // Reject shaders using both gl_FragData and gl_FragColor
1813 TQualifier qualifier = variable->getType().getQualifier();
1814 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001815 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001816 mUsesFragData = true;
1817 }
1818 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1819 {
1820 mUsesFragColor = true;
1821 }
1822 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1823 {
1824 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001825 }
1826
Olli Etuaho0f684632017-07-13 12:42:15 +03001827 // This validation is not quite correct - it's only an error to write to
1828 // both FragData and FragColor. For simplicity, and because users shouldn't
1829 // be rewarded for reading from undefined varaibles, return an error
1830 // if they are both referenced, rather than assigned.
1831 if (mUsesFragData && mUsesFragColor)
1832 {
1833 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1834 if (mUsesSecondaryOutputs)
1835 {
1836 errorMessage =
1837 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1838 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1839 }
1840 error(location, errorMessage, name->c_str());
1841 }
1842
1843 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1844 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1845 qualifier == EvqWorkGroupSize)
1846 {
1847 error(location,
1848 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1849 "gl_WorkGroupSize");
1850 }
Jamie Madill5c097022014-08-20 16:38:32 -04001851 return variable;
1852}
1853
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001854TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1855 const TString *name,
1856 const TSymbol *symbol)
1857{
1858 const TVariable *variable = getNamedVariable(location, name, symbol);
1859
Olli Etuaho0f684632017-07-13 12:42:15 +03001860 if (!variable)
1861 {
1862 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1863 node->setLine(location);
1864 return node;
1865 }
1866
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001867 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001868 TIntermTyped *node = nullptr;
1869
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001870 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001871 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001872 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001873 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001874 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001875 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001876 {
1877 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1878 // needs to be added to the AST as a constant and not as a symbol.
1879 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1880 TConstantUnion *constArray = new TConstantUnion[3];
1881 for (size_t i = 0; i < 3; ++i)
1882 {
1883 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1884 }
1885
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001886 ASSERT(variableType.getBasicType() == EbtUInt);
1887 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001888
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001889 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001890 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001891 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001892 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001893 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1894 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001895 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001896 ASSERT(mGeometryShaderInputArraySize > 0u);
1897
Olli Etuaho195be942017-12-04 23:40:14 +02001898 node = new TIntermSymbol(variable);
Olli Etuaho55bde912017-10-25 13:41:13 +03001899 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001900 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001901 else
1902 {
Olli Etuaho195be942017-12-04 23:40:14 +02001903 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001904 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001905 ASSERT(node != nullptr);
1906 node->setLine(location);
1907 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001908}
1909
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001910// Initializers show up in several places in the grammar. Have one set of
1911// code to handle them here.
1912//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001913// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001914bool TParseContext::executeInitializer(const TSourceLoc &line,
1915 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001916 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001917 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001918 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919{
Olli Etuaho13389b62016-10-16 11:48:18 +01001920 ASSERT(initNode != nullptr);
1921 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922
Olli Etuaho376f1b52015-04-13 13:23:41 +03001923 if (type.isUnsizedArray())
1924 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001925 // In case initializer is not an array or type has more dimensions than initializer, this
1926 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1927 // actually is an array or not. Having a non-array initializer for an unsized array will
1928 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001929 auto *arraySizes = initializer->getType().getArraySizes();
1930 type.sizeUnsizedArrays(arraySizes);
Olli Etuaho376f1b52015-04-13 13:23:41 +03001931 }
Olli Etuaho195be942017-12-04 23:40:14 +02001932
1933 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001934 if (!declareVariable(line, identifier, type, &variable))
1935 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001936 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001937 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001938
Olli Etuahob0c645e2015-05-12 14:25:36 +03001939 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001940 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001941 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001942 {
1943 // Error message does not completely match behavior with ESSL 1.00, but
1944 // we want to steer developers towards only using constant expressions.
1945 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001946 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001947 }
1948 if (globalInitWarning)
1949 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001950 warning(
1951 line,
1952 "global variable initializers should be constant expressions "
1953 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1954 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001955 }
1956
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001957 //
1958 // identifier must be of type constant, a global, or a temporary
1959 //
1960 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301961 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1962 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001963 error(line, " cannot initialize this type of qualifier ",
1964 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001965 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001966 }
1967 //
1968 // test for and propagate constant
1969 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001970
Arun Patole7e7e68d2015-05-22 12:02:25 +05301971 if (qualifier == EvqConst)
1972 {
1973 if (qualifier != initializer->getType().getQualifier())
1974 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001975 std::stringstream reasonStream;
1976 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1977 << "'";
1978 std::string reason = reasonStream.str();
1979 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001980 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001981 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001982 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301983 if (type != initializer->getType())
1984 {
1985 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001986 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001987 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001988 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001989 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001990
1991 // Save the constant folded value to the variable if possible. For example array
1992 // initializers are not folded, since that way copying the array literal to multiple places
1993 // in the shader is avoided.
1994 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1995 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301996 if (initializer->getAsConstantUnion())
1997 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001998 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001999 ASSERT(*initNode == nullptr);
2000 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302001 }
2002 else if (initializer->getAsSymbolNode())
2003 {
Olli Etuahob6af22b2017-12-15 14:05:44 +02002004 const TVariable &var = initializer->getAsSymbolNode()->variable();
2005 const TConstantUnion *constArray = var.getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002006 if (constArray)
2007 {
2008 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002009 ASSERT(*initNode == nullptr);
2010 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002011 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002012 }
2013 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002014
Olli Etuaho195be942017-12-04 23:40:14 +02002015 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002016 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002017 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2018 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002019 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002020 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002021 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002022 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023
Olli Etuaho914b79a2017-06-19 16:03:19 +03002024 return true;
2025}
2026
2027TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2028 const TString &identifier,
2029 TIntermTyped *initializer,
2030 const TSourceLoc &loc)
2031{
2032 checkIsScalarBool(loc, pType);
2033 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002034 TType type(pType);
2035 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002036 {
2037 // The initializer is valid. The init condition needs to have a node - either the
2038 // initializer node, or a constant node in case the initialized variable is const and won't
2039 // be recorded in the AST.
2040 if (initNode == nullptr)
2041 {
2042 return initializer;
2043 }
2044 else
2045 {
2046 TIntermDeclaration *declaration = new TIntermDeclaration();
2047 declaration->appendDeclarator(initNode);
2048 return declaration;
2049 }
2050 }
2051 return nullptr;
2052}
2053
2054TIntermNode *TParseContext::addLoop(TLoopType type,
2055 TIntermNode *init,
2056 TIntermNode *cond,
2057 TIntermTyped *expr,
2058 TIntermNode *body,
2059 const TSourceLoc &line)
2060{
2061 TIntermNode *node = nullptr;
2062 TIntermTyped *typedCond = nullptr;
2063 if (cond)
2064 {
2065 typedCond = cond->getAsTyped();
2066 }
2067 if (cond == nullptr || typedCond)
2068 {
Olli Etuahocce89652017-06-19 16:04:09 +03002069 if (type == ELoopDoWhile)
2070 {
2071 checkIsScalarBool(line, typedCond);
2072 }
2073 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2074 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2075 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2076 !typedCond->isVector()));
2077
Olli Etuaho3ec75682017-07-05 17:02:55 +03002078 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002079 node->setLine(line);
2080 return node;
2081 }
2082
Olli Etuahocce89652017-06-19 16:04:09 +03002083 ASSERT(type != ELoopDoWhile);
2084
Olli Etuaho914b79a2017-06-19 16:03:19 +03002085 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2086 ASSERT(declaration);
2087 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2088 ASSERT(declarator->getLeft()->getAsSymbolNode());
2089
2090 // The condition is a declaration. In the AST representation we don't support declarations as
2091 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2092 // the loop.
2093 TIntermBlock *block = new TIntermBlock();
2094
2095 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2096 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2097 block->appendStatement(declareCondition);
2098
2099 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2100 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002101 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002102 block->appendStatement(loop);
2103 loop->setLine(line);
2104 block->setLine(line);
2105 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106}
2107
Olli Etuahocce89652017-06-19 16:04:09 +03002108TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2109 TIntermNodePair code,
2110 const TSourceLoc &loc)
2111{
Olli Etuaho56229f12017-07-10 14:16:33 +03002112 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002113
2114 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002115 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002116 {
2117 if (cond->getAsConstantUnion()->getBConst(0) == true)
2118 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002119 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002120 }
2121 else
2122 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002123 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002124 }
2125 }
2126
Olli Etuaho3ec75682017-07-05 17:02:55 +03002127 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002128 node->setLine(loc);
2129
2130 return node;
2131}
2132
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002133void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2134{
2135 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2136 typeSpecifier->getBasicType());
2137
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002138 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002139 {
2140 error(typeSpecifier->getLine(), "not supported", "first-class array");
2141 typeSpecifier->clearArrayness();
2142 }
2143}
2144
Martin Radev70866b82016-07-22 15:27:42 +03002145TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302146 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002147{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002148 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002149
Martin Radev70866b82016-07-22 15:27:42 +03002150 TPublicType returnType = typeSpecifier;
2151 returnType.qualifier = typeQualifier.qualifier;
2152 returnType.invariant = typeQualifier.invariant;
2153 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002154 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002155 returnType.precision = typeSpecifier.precision;
2156
2157 if (typeQualifier.precision != EbpUndefined)
2158 {
2159 returnType.precision = typeQualifier.precision;
2160 }
2161
Martin Radev4a9cd802016-09-01 16:51:51 +03002162 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2163 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002164
Martin Radev4a9cd802016-09-01 16:51:51 +03002165 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2166 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002167
Martin Radev4a9cd802016-09-01 16:51:51 +03002168 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002169
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002170 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002171 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002172 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002173 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002174 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002175 returnType.clearArrayness();
2176 }
2177
Martin Radev70866b82016-07-22 15:27:42 +03002178 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002179 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002180 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002181 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002182 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002183 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002184
Martin Radev70866b82016-07-22 15:27:42 +03002185 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002186 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002187 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002188 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002189 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002190 }
2191 }
2192 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002193 {
Martin Radev70866b82016-07-22 15:27:42 +03002194 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002195 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002196 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002197 }
Martin Radev70866b82016-07-22 15:27:42 +03002198 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2199 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002200 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002201 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2202 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002203 }
Martin Radev70866b82016-07-22 15:27:42 +03002204 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002205 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002206 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002207 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002208 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002209 }
2210
2211 return returnType;
2212}
2213
Olli Etuaho856c4972016-08-08 11:38:39 +03002214void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2215 const TPublicType &type,
2216 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002217{
2218 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002219 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002220 {
2221 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002222 }
2223
2224 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2225 switch (qualifier)
2226 {
2227 case EvqVertexIn:
2228 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002229 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002230 {
2231 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002232 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002233 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002234 return;
2235 case EvqFragmentOut:
2236 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002237 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002238 {
2239 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002240 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002241 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002242 return;
2243 default:
2244 break;
2245 }
2246
2247 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2248 // restrictions.
2249 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002250 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2251 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002252 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2253 {
2254 error(qualifierLocation, "must use 'flat' interpolation here",
2255 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002256 }
2257
Martin Radev4a9cd802016-09-01 16:51:51 +03002258 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002259 {
2260 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2261 // These restrictions are only implied by the ESSL 3.00 spec, but
2262 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002263 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002264 {
2265 error(qualifierLocation, "cannot be an array of structures",
2266 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 }
2268 if (type.isStructureContainingArrays())
2269 {
2270 error(qualifierLocation, "cannot be a structure containing an array",
2271 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002272 }
2273 if (type.isStructureContainingType(EbtStruct))
2274 {
2275 error(qualifierLocation, "cannot be a structure containing a structure",
2276 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002277 }
2278 if (type.isStructureContainingType(EbtBool))
2279 {
2280 error(qualifierLocation, "cannot be a structure containing a bool",
2281 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002282 }
2283 }
2284}
2285
Martin Radev2cc85b32016-08-05 16:22:53 +03002286void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2287{
2288 if (qualifier.getType() == QtStorage)
2289 {
2290 const TStorageQualifierWrapper &storageQualifier =
2291 static_cast<const TStorageQualifierWrapper &>(qualifier);
2292 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2293 !symbolTable.atGlobalLevel())
2294 {
2295 error(storageQualifier.getLine(),
2296 "Local variables can only use the const storage qualifier.",
2297 storageQualifier.getQualifierString().c_str());
2298 }
2299 }
2300}
2301
Olli Etuaho43364892017-02-13 16:00:12 +00002302void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002303 const TSourceLoc &location)
2304{
Jiajia Qinbc585152017-06-23 15:42:17 +08002305 const std::string reason(
2306 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2307 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002308 if (memoryQualifier.readonly)
2309 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002310 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002311 }
2312 if (memoryQualifier.writeonly)
2313 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002314 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002315 }
Martin Radev049edfa2016-11-11 14:35:37 +02002316 if (memoryQualifier.coherent)
2317 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002318 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002319 }
2320 if (memoryQualifier.restrictQualifier)
2321 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002322 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002323 }
2324 if (memoryQualifier.volatileQualifier)
2325 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002326 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002327 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002328}
2329
jchen104cdac9e2017-05-08 11:01:20 +08002330// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2331// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002332void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2333 const TSourceLoc &loc,
2334 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002335{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002336 if (!IsAtomicCounter(type->getBasicType()))
2337 {
2338 return;
2339 }
2340
2341 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2342 : kAtomicCounterSize;
2343 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2344 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002345 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002346 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002347 {
2348 offset = bindingState.appendSpan(size);
2349 }
2350 else
2351 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002352 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002353 }
2354 if (offset == -1)
2355 {
2356 error(loc, "Offset overlapping", "atomic counter");
2357 return;
2358 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002359 layoutQualifier.offset = offset;
2360 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002361}
2362
Olli Etuaho454c34c2017-10-25 16:35:56 +03002363void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2364 const char *token,
2365 TType *type)
2366{
2367 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2368 {
2369 if (type->isArray() && type->getOutermostArraySize() == 0u)
2370 {
2371 // Set size for the unsized geometry shader inputs if they are declared after a valid
2372 // input primitive declaration.
2373 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2374 {
2375 ASSERT(mGeometryShaderInputArraySize > 0u);
2376 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2377 }
2378 else
2379 {
2380 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2381 // An input can be declared without an array size if there is a previous layout
2382 // which specifies the size.
2383 error(location,
2384 "Missing a valid input primitive declaration before declaring an unsized "
2385 "array input",
2386 token);
2387 }
2388 }
2389 else if (type->isArray())
2390 {
2391 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2392 }
2393 else
2394 {
2395 error(location, "Geometry shader input variable must be declared as an array", token);
2396 }
2397 }
2398}
2399
Olli Etuaho13389b62016-10-16 11:48:18 +01002400TIntermDeclaration *TParseContext::parseSingleDeclaration(
2401 TPublicType &publicType,
2402 const TSourceLoc &identifierOrTypeLocation,
2403 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002404{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002405 TType type(publicType);
2406 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2407 mDirectiveHandler.pragma().stdgl.invariantAll)
2408 {
2409 TQualifier qualifier = type.getQualifier();
2410
2411 // The directive handler has already taken care of rejecting invalid uses of this pragma
2412 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2413 // affected variable declarations:
2414 //
2415 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2416 // elsewhere, in TranslatorGLSL.)
2417 //
2418 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2419 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2420 // the way this is currently implemented we have to enable this compiler option before
2421 // parsing the shader and determining the shading language version it uses. If this were
2422 // implemented as a post-pass, the workaround could be more targeted.
2423 //
2424 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2425 // the specification, but there are desktop OpenGL drivers that expect that this is the
2426 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2427 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2428 {
2429 type.setInvariant(true);
2430 }
2431 }
2432
Olli Etuaho454c34c2017-10-25 16:35:56 +03002433 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002434
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002435 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2436 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002437
Olli Etuahobab4c082015-04-24 16:38:49 +03002438 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002439 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002440
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002441 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002442 if (emptyDeclaration)
2443 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002444 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002445 // In most cases we don't need to create a symbol node for an empty declaration.
2446 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2447 if (type.getBasicType() == EbtStruct)
2448 {
Olli Etuaho195be942017-12-04 23:40:14 +02002449 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01002450 new TVariable(&symbolTable, nullptr, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002451 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002452 }
jchen104cdac9e2017-05-08 11:01:20 +08002453 else if (IsAtomicCounter(publicType.getBasicType()))
2454 {
2455 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2456 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002457 }
2458 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002459 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002460 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002461
Olli Etuaho55bde912017-10-25 13:41:13 +03002462 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002463
Olli Etuaho55bc9052017-10-25 17:33:06 +03002464 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002465
Olli Etuaho2935c582015-04-08 14:32:06 +03002466 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002467 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002468 {
Olli Etuaho195be942017-12-04 23:40:14 +02002469 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002470 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002471 }
2472
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002473 TIntermDeclaration *declaration = new TIntermDeclaration();
2474 declaration->setLine(identifierOrTypeLocation);
2475 if (symbol)
2476 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002477 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002478 declaration->appendDeclarator(symbol);
2479 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002480 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002481}
2482
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002483TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2484 TPublicType &elementType,
2485 const TSourceLoc &identifierLocation,
2486 const TString &identifier,
2487 const TSourceLoc &indexLocation,
2488 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002489{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002490 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002491
Olli Etuaho55bde912017-10-25 13:41:13 +03002492 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002493 identifierLocation);
2494
Olli Etuaho55bde912017-10-25 13:41:13 +03002495 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002496
Olli Etuaho55bde912017-10-25 13:41:13 +03002497 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002498
Olli Etuaho55bde912017-10-25 13:41:13 +03002499 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002500 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002501
Olli Etuaho454c34c2017-10-25 16:35:56 +03002502 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002503
2504 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2505
Olli Etuaho55bc9052017-10-25 17:33:06 +03002506 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002507
Olli Etuaho13389b62016-10-16 11:48:18 +01002508 TIntermDeclaration *declaration = new TIntermDeclaration();
2509 declaration->setLine(identifierLocation);
2510
Olli Etuaho195be942017-12-04 23:40:14 +02002511 TVariable *variable = nullptr;
2512 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002513 {
Olli Etuaho195be942017-12-04 23:40:14 +02002514 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002515 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002516 declaration->appendDeclarator(symbol);
2517 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002518
Olli Etuaho13389b62016-10-16 11:48:18 +01002519 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002520}
2521
Olli Etuaho13389b62016-10-16 11:48:18 +01002522TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2523 const TSourceLoc &identifierLocation,
2524 const TString &identifier,
2525 const TSourceLoc &initLocation,
2526 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002527{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002528 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002529
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002530 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2531 identifierLocation);
2532
2533 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002534
Olli Etuaho13389b62016-10-16 11:48:18 +01002535 TIntermDeclaration *declaration = new TIntermDeclaration();
2536 declaration->setLine(identifierLocation);
2537
2538 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002539 TType type(publicType);
2540 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002541 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002542 if (initNode)
2543 {
2544 declaration->appendDeclarator(initNode);
2545 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002546 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002547 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002548}
2549
Olli Etuaho13389b62016-10-16 11:48:18 +01002550TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002551 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002552 const TSourceLoc &identifierLocation,
2553 const TString &identifier,
2554 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002555 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002556 const TSourceLoc &initLocation,
2557 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002558{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002559 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002560
Olli Etuaho55bde912017-10-25 13:41:13 +03002561 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002562 identifierLocation);
2563
Olli Etuaho55bde912017-10-25 13:41:13 +03002564 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565
Olli Etuaho55bde912017-10-25 13:41:13 +03002566 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002567
Olli Etuaho55bde912017-10-25 13:41:13 +03002568 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002569 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002570
Olli Etuaho13389b62016-10-16 11:48:18 +01002571 TIntermDeclaration *declaration = new TIntermDeclaration();
2572 declaration->setLine(identifierLocation);
2573
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002574 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002575 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002576 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002577 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002578 if (initNode)
2579 {
2580 declaration->appendDeclarator(initNode);
2581 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002582 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002583
2584 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002585}
2586
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002587TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002588 const TTypeQualifierBuilder &typeQualifierBuilder,
2589 const TSourceLoc &identifierLoc,
2590 const TString *identifier,
2591 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002592{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002593 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002594
Martin Radev70866b82016-07-22 15:27:42 +03002595 if (!typeQualifier.invariant)
2596 {
2597 error(identifierLoc, "Expected invariant", identifier->c_str());
2598 return nullptr;
2599 }
2600 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2601 {
2602 return nullptr;
2603 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002604 if (!symbol)
2605 {
2606 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002607 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002608 }
Martin Radev70866b82016-07-22 15:27:42 +03002609 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002610 {
Martin Radev70866b82016-07-22 15:27:42 +03002611 error(identifierLoc, "invariant declaration specifies qualifier",
2612 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002613 }
Martin Radev70866b82016-07-22 15:27:42 +03002614 if (typeQualifier.precision != EbpUndefined)
2615 {
2616 error(identifierLoc, "invariant declaration specifies precision",
2617 getPrecisionString(typeQualifier.precision));
2618 }
2619 if (!typeQualifier.layoutQualifier.isEmpty())
2620 {
2621 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2622 }
2623
2624 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002625 if (!variable)
2626 {
2627 return nullptr;
2628 }
Martin Radev70866b82016-07-22 15:27:42 +03002629 const TType &type = variable->getType();
2630
2631 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2632 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002633 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002634
2635 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2636
Olli Etuaho195be942017-12-04 23:40:14 +02002637 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002638 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002639
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002640 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002641}
2642
Olli Etuaho13389b62016-10-16 11:48:18 +01002643void TParseContext::parseDeclarator(TPublicType &publicType,
2644 const TSourceLoc &identifierLocation,
2645 const TString &identifier,
2646 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002647{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002648 // If the declaration starting this declarator list was empty (example: int,), some checks were
2649 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002650 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002651 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002652 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2653 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002654 }
2655
Olli Etuaho856c4972016-08-08 11:38:39 +03002656 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002657
Olli Etuaho43364892017-02-13 16:00:12 +00002658 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002659
2660 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2661
Olli Etuaho55bde912017-10-25 13:41:13 +03002662 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2663
Olli Etuaho55bc9052017-10-25 17:33:06 +03002664 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2665
Olli Etuaho195be942017-12-04 23:40:14 +02002666 TVariable *variable = nullptr;
2667 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002668 {
Olli Etuaho195be942017-12-04 23:40:14 +02002669 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002670 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002671 declarationOut->appendDeclarator(symbol);
2672 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002673}
2674
Olli Etuaho55bde912017-10-25 13:41:13 +03002675void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002676 const TSourceLoc &identifierLocation,
2677 const TString &identifier,
2678 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002679 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002680 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002681{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002682 // If the declaration starting this declarator list was empty (example: int,), some checks were
2683 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002684 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002685 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002686 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002687 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002688 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002689
Olli Etuaho55bde912017-10-25 13:41:13 +03002690 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002691
Olli Etuaho55bde912017-10-25 13:41:13 +03002692 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002693 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002694 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002695 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002696
Olli Etuaho454c34c2017-10-25 16:35:56 +03002697 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2698
Olli Etuaho55bde912017-10-25 13:41:13 +03002699 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2700
Olli Etuaho55bc9052017-10-25 17:33:06 +03002701 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002702
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002703 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002704 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002705 {
Olli Etuaho195be942017-12-04 23:40:14 +02002706 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002707 symbol->setLine(identifierLocation);
2708 declarationOut->appendDeclarator(symbol);
2709 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002710 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002711}
2712
Olli Etuaho13389b62016-10-16 11:48:18 +01002713void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2714 const TSourceLoc &identifierLocation,
2715 const TString &identifier,
2716 const TSourceLoc &initLocation,
2717 TIntermTyped *initializer,
2718 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002719{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002720 // If the declaration starting this declarator list was empty (example: int,), some checks were
2721 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002722 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002723 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002724 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2725 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002726 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002727
Olli Etuaho856c4972016-08-08 11:38:39 +03002728 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002729
Olli Etuaho13389b62016-10-16 11:48:18 +01002730 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002731 TType type(publicType);
2732 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002733 {
2734 //
2735 // build the intermediate representation
2736 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002737 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002738 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002739 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002740 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002741 }
2742}
2743
Olli Etuaho55bde912017-10-25 13:41:13 +03002744void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002745 const TSourceLoc &identifierLocation,
2746 const TString &identifier,
2747 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002748 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002749 const TSourceLoc &initLocation,
2750 TIntermTyped *initializer,
2751 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002752{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002753 // If the declaration starting this declarator list was empty (example: int,), some checks were
2754 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002755 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002756 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002757 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002758 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002759 }
2760
Olli Etuaho55bde912017-10-25 13:41:13 +03002761 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762
Olli Etuaho55bde912017-10-25 13:41:13 +03002763 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002764
Olli Etuaho55bde912017-10-25 13:41:13 +03002765 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002766 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002767
2768 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002769 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002770 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002771 {
2772 if (initNode)
2773 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002774 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002775 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002776 }
2777}
2778
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002779TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2780{
2781 // It's simpler to parse an empty statement as a constant expression rather than having a
2782 // different type of node just for empty statements, that will be pruned from the AST anyway.
2783 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2784 node->setLine(location);
2785 return node;
2786}
2787
jchen104cdac9e2017-05-08 11:01:20 +08002788void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2789 const TSourceLoc &location)
2790{
2791 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2792 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2793 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2794 {
2795 error(location, "Requires both binding and offset", "layout");
2796 return;
2797 }
2798 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2799}
2800
Olli Etuahocce89652017-06-19 16:04:09 +03002801void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2802 const TPublicType &type,
2803 const TSourceLoc &loc)
2804{
2805 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2806 !getFragmentPrecisionHigh())
2807 {
2808 error(loc, "precision is not supported in fragment shader", "highp");
2809 }
2810
2811 if (!CanSetDefaultPrecisionOnType(type))
2812 {
2813 error(loc, "illegal type argument for default precision qualifier",
2814 getBasicString(type.getBasicType()));
2815 return;
2816 }
2817 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2818}
2819
Shaob5cc1192017-07-06 10:47:20 +08002820bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2821{
2822 switch (typeQualifier.layoutQualifier.primitiveType)
2823 {
2824 case EptLines:
2825 case EptLinesAdjacency:
2826 case EptTriangles:
2827 case EptTrianglesAdjacency:
2828 return typeQualifier.qualifier == EvqGeometryIn;
2829
2830 case EptLineStrip:
2831 case EptTriangleStrip:
2832 return typeQualifier.qualifier == EvqGeometryOut;
2833
2834 case EptPoints:
2835 return true;
2836
2837 default:
2838 UNREACHABLE();
2839 return false;
2840 }
2841}
2842
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002843void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2844 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002845{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002846 if (mGeometryShaderInputArraySize == 0u)
2847 {
2848 mGeometryShaderInputArraySize = inputArraySize;
2849 }
2850 else if (mGeometryShaderInputArraySize != inputArraySize)
2851 {
2852 error(line,
2853 "Array size or input primitive declaration doesn't match the size of earlier sized "
2854 "array inputs.",
2855 "layout");
2856 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002857}
2858
Shaob5cc1192017-07-06 10:47:20 +08002859bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2860{
2861 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2862
2863 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2864
2865 if (layoutQualifier.maxVertices != -1)
2866 {
2867 error(typeQualifier.line,
2868 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2869 return false;
2870 }
2871
2872 // Set mGeometryInputPrimitiveType if exists
2873 if (layoutQualifier.primitiveType != EptUndefined)
2874 {
2875 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2876 {
2877 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2878 return false;
2879 }
2880
2881 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2882 {
2883 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002884 setGeometryShaderInputArraySize(
2885 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2886 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002887 }
2888 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2889 {
2890 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2891 "layout");
2892 return false;
2893 }
2894 }
2895
2896 // Set mGeometryInvocations if exists
2897 if (layoutQualifier.invocations > 0)
2898 {
2899 if (mGeometryShaderInvocations == 0)
2900 {
2901 mGeometryShaderInvocations = layoutQualifier.invocations;
2902 }
2903 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2904 {
2905 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2906 "layout");
2907 return false;
2908 }
2909 }
2910
2911 return true;
2912}
2913
2914bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2915{
2916 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2917
2918 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2919
2920 if (layoutQualifier.invocations > 0)
2921 {
2922 error(typeQualifier.line,
2923 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2924 return false;
2925 }
2926
2927 // Set mGeometryOutputPrimitiveType if exists
2928 if (layoutQualifier.primitiveType != EptUndefined)
2929 {
2930 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2931 {
2932 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2933 return false;
2934 }
2935
2936 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2937 {
2938 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2939 }
2940 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2941 {
2942 error(typeQualifier.line,
2943 "primitive doesn't match earlier output primitive declaration", "layout");
2944 return false;
2945 }
2946 }
2947
2948 // Set mGeometryMaxVertices if exists
2949 if (layoutQualifier.maxVertices > -1)
2950 {
2951 if (mGeometryShaderMaxVertices == -1)
2952 {
2953 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2954 }
2955 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2956 {
2957 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2958 "layout");
2959 return false;
2960 }
2961 }
2962
2963 return true;
2964}
2965
Martin Radev70866b82016-07-22 15:27:42 +03002966void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002967{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002968 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002969 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002970
Martin Radev70866b82016-07-22 15:27:42 +03002971 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2972 typeQualifier.line);
2973
Jamie Madillc2128ff2016-07-04 10:26:17 -04002974 // It should never be the case, but some strange parser errors can send us here.
2975 if (layoutQualifier.isEmpty())
2976 {
2977 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002978 return;
2979 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002980
Martin Radev802abe02016-08-04 17:48:32 +03002981 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002982 {
Olli Etuaho43364892017-02-13 16:00:12 +00002983 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002984 return;
2985 }
2986
Olli Etuaho43364892017-02-13 16:00:12 +00002987 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2988
2989 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002990
2991 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2992
Andrei Volykhina5527072017-03-22 16:46:30 +03002993 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2994
jchen104cdac9e2017-05-08 11:01:20 +08002995 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2996
Qin Jiajiaca68d982017-09-18 16:41:56 +08002997 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2998 typeQualifier.qualifier);
2999
Martin Radev802abe02016-08-04 17:48:32 +03003000 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003001 {
Martin Radev802abe02016-08-04 17:48:32 +03003002 if (mComputeShaderLocalSizeDeclared &&
3003 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3004 {
3005 error(typeQualifier.line, "Work group size does not match the previous declaration",
3006 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003007 return;
3008 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003009
Martin Radev802abe02016-08-04 17:48:32 +03003010 if (mShaderVersion < 310)
3011 {
3012 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003013 return;
3014 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003015
Martin Radev4c4c8e72016-08-04 12:25:34 +03003016 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003017 {
3018 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003019 return;
3020 }
3021
3022 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3023 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3024
3025 const TConstantUnion *maxComputeWorkGroupSizeData =
3026 maxComputeWorkGroupSize->getConstPointer();
3027
3028 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3029 {
3030 if (layoutQualifier.localSize[i] != -1)
3031 {
3032 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3033 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3034 if (mComputeShaderLocalSize[i] < 1 ||
3035 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3036 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003037 std::stringstream reasonStream;
3038 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3039 << maxComputeWorkGroupSizeValue;
3040 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003041
Olli Etuaho4de340a2016-12-16 09:32:03 +00003042 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003043 return;
3044 }
3045 }
3046 }
3047
3048 mComputeShaderLocalSizeDeclared = true;
3049 }
Shaob5cc1192017-07-06 10:47:20 +08003050 else if (typeQualifier.qualifier == EvqGeometryIn)
3051 {
3052 if (mShaderVersion < 310)
3053 {
3054 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3055 return;
3056 }
3057
3058 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3059 {
3060 return;
3061 }
3062 }
3063 else if (typeQualifier.qualifier == EvqGeometryOut)
3064 {
3065 if (mShaderVersion < 310)
3066 {
3067 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3068 "layout");
3069 return;
3070 }
3071
3072 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3073 {
3074 return;
3075 }
3076 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003077 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3078 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003079 {
3080 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3081 // specification.
3082 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3083 {
3084 error(typeQualifier.line, "Number of views does not match the previous declaration",
3085 "layout");
3086 return;
3087 }
3088
3089 if (layoutQualifier.numViews == -1)
3090 {
3091 error(typeQualifier.line, "No num_views specified", "layout");
3092 return;
3093 }
3094
3095 if (layoutQualifier.numViews > mMaxNumViews)
3096 {
3097 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3098 "layout");
3099 return;
3100 }
3101
3102 mNumViews = layoutQualifier.numViews;
3103 }
Martin Radev802abe02016-08-04 17:48:32 +03003104 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003105 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003106 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003107 {
Martin Radev802abe02016-08-04 17:48:32 +03003108 return;
3109 }
3110
Jiajia Qinbc585152017-06-23 15:42:17 +08003111 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003112 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003113 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003114 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003115 return;
3116 }
3117
3118 if (mShaderVersion < 300)
3119 {
3120 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3121 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003122 return;
3123 }
3124
Olli Etuaho09b04a22016-12-15 13:30:26 +00003125 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003126
3127 if (layoutQualifier.matrixPacking != EmpUnspecified)
3128 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003129 if (typeQualifier.qualifier == EvqUniform)
3130 {
3131 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3132 }
3133 else if (typeQualifier.qualifier == EvqBuffer)
3134 {
3135 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3136 }
Martin Radev802abe02016-08-04 17:48:32 +03003137 }
3138
3139 if (layoutQualifier.blockStorage != EbsUnspecified)
3140 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003141 if (typeQualifier.qualifier == EvqUniform)
3142 {
3143 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3144 }
3145 else if (typeQualifier.qualifier == EvqBuffer)
3146 {
3147 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3148 }
Martin Radev802abe02016-08-04 17:48:32 +03003149 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003150 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003151}
3152
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003153TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3154 const TFunction &function,
3155 const TSourceLoc &location,
3156 bool insertParametersToSymbolTable)
3157{
Olli Etuahobed35d72017-12-20 16:36:26 +02003158 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003159
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003160 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003161 prototype->setLine(location);
3162
3163 for (size_t i = 0; i < function.getParamCount(); i++)
3164 {
3165 const TConstParameter &param = function.getParam(i);
3166
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003167 TIntermSymbol *symbol = nullptr;
3168
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003169 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3170 // be used for unused args).
3171 if (param.name != nullptr)
3172 {
Olli Etuaho195be942017-12-04 23:40:14 +02003173 TVariable *variable =
3174 new TVariable(&symbolTable, param.name, *param.type, SymbolType::UserDefined);
3175 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003176 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003177 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003178 {
Olli Etuaho195be942017-12-04 23:40:14 +02003179 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003180 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003181 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003182 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003183 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003184 // Unsized type of a named parameter should have already been checked and sanitized.
3185 ASSERT(!param.type->isUnsizedArray());
3186 }
3187 else
3188 {
3189 if (param.type->isUnsizedArray())
3190 {
3191 error(location, "function parameter array must be sized at compile time", "[]");
3192 // We don't need to size the arrays since the parameter is unnamed and hence
3193 // inaccessible.
3194 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003195 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003196 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003197 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003198 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3199 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003200 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003201 new TVariable(&symbolTable, nullptr, *param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003202 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003203 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003204 symbol->setLine(location);
3205 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003206 }
3207 return prototype;
3208}
3209
Olli Etuaho16c745a2017-01-16 17:02:27 +00003210TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3211 const TFunction &parsedFunction,
3212 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003213{
Olli Etuaho476197f2016-10-11 13:59:08 +01003214 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3215 // first declaration. Either way the instance in the symbol table is used to track whether the
3216 // function is declared multiple times.
3217 TFunction *function = static_cast<TFunction *>(
3218 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3219 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003220 {
3221 // ESSL 1.00.17 section 4.2.7.
3222 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3223 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003224 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003225 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003226
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003227 TIntermFunctionPrototype *prototype =
3228 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003229
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003230 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003231
3232 if (!symbolTable.atGlobalLevel())
3233 {
3234 // ESSL 3.00.4 section 4.2.4.
3235 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003236 }
3237
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003238 return prototype;
3239}
3240
Olli Etuaho336b1472016-10-05 16:37:55 +01003241TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003242 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003243 TIntermBlock *functionBody,
3244 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003245{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003246 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003247 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3248 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003249 error(location, "function does not return a value:",
Olli Etuahobed35d72017-12-20 16:36:26 +02003250 functionPrototype->getFunction()->name().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003251 }
3252
Olli Etuahof51fdd22016-10-03 10:03:40 +01003253 if (functionBody == nullptr)
3254 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003255 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003256 functionBody->setLine(location);
3257 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003258 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003259 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003260 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003261
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003262 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003263 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003264}
3265
Olli Etuaho476197f2016-10-11 13:59:08 +01003266void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3267 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003268 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003269{
Olli Etuaho476197f2016-10-11 13:59:08 +01003270 ASSERT(function);
3271 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003272 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003273 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003274
3275 if (builtIn)
3276 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003277 error(location, "built-in functions cannot be redefined", (*function)->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003278 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003279 else
Jamie Madill185fb402015-06-12 15:48:48 -04003280 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003281 TFunction *prevDec = static_cast<TFunction *>(
3282 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3283
3284 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3285 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3286 // occurance.
3287 if (*function != prevDec)
3288 {
3289 // Swap the parameters of the previous declaration to the parameters of the function
3290 // definition (parameter names may differ).
3291 prevDec->swapParameters(**function);
3292
3293 // The function definition will share the same symbol as any previous declaration.
3294 *function = prevDec;
3295 }
3296
3297 if ((*function)->isDefined())
3298 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003299 error(location, "function already has a body", (*function)->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003300 }
3301
3302 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003303 }
Jamie Madill185fb402015-06-12 15:48:48 -04003304
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003305 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003306 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003307 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003308
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003309 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003310 setLoopNestingLevel(0);
3311}
3312
Jamie Madillb98c3a82015-07-23 14:26:04 -04003313TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003314{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003315 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003316 // We don't know at this point whether this is a function definition or a prototype.
3317 // The definition production code will check for redefinitions.
3318 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003319 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003320 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3321 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003322 //
3323 TFunction *prevDec =
3324 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303325
Olli Etuahod80f2942017-11-06 12:44:45 +02003326 for (size_t i = 0u; i < function->getParamCount(); ++i)
3327 {
3328 auto &param = function->getParam(i);
3329 if (param.type->isStructSpecifier())
3330 {
3331 // ESSL 3.00.6 section 12.10.
3332 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahobed35d72017-12-20 16:36:26 +02003333 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003334 }
3335 }
3336
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003337 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahobed35d72017-12-20 16:36:26 +02003338 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303339 {
Martin Radevda6254b2016-12-14 17:00:36 +02003340 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303341 // Therefore overloading or redefining builtin functions is an error.
3342 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahobed35d72017-12-20 16:36:26 +02003343 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303344 }
3345 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003346 {
3347 if (prevDec->getReturnType() != function->getReturnType())
3348 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003349 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003350 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003351 }
3352 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3353 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003354 if (prevDec->getParam(i).type->getQualifier() !=
3355 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003356 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003357 error(location,
3358 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003359 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003360 }
3361 }
3362 }
3363
3364 //
3365 // Check for previously declared variables using the same name.
3366 //
Olli Etuahobed35d72017-12-20 16:36:26 +02003367 TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003368 if (prevSym)
3369 {
3370 if (!prevSym->isFunction())
3371 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003372 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003373 }
3374 }
3375 else
3376 {
3377 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003378 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003379 }
3380
3381 // We're at the inner scope level of the function's arguments and body statement.
3382 // Add the function prototype to the surrounding scope instead.
3383 symbolTable.getOuterLevel()->insert(function);
3384
Olli Etuaho78d13742017-01-18 13:06:10 +00003385 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahobed35d72017-12-20 16:36:26 +02003386 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003387 {
3388 if (function->getParamCount() > 0)
3389 {
3390 error(location, "function cannot take any parameter(s)", "main");
3391 }
3392 if (function->getReturnType().getBasicType() != EbtVoid)
3393 {
3394 error(location, "main function cannot return a value",
3395 function->getReturnType().getBasicString());
3396 }
3397 }
3398
Jamie Madill185fb402015-06-12 15:48:48 -04003399 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003400 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3401 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003402 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3403 //
3404 return function;
3405}
3406
Olli Etuaho9de84a52016-06-14 17:36:01 +03003407TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3408 const TString *name,
3409 const TSourceLoc &location)
3410{
3411 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3412 {
3413 error(location, "no qualifiers allowed for function return",
3414 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003415 }
3416 if (!type.layoutQualifier.isEmpty())
3417 {
3418 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003419 }
jchen10cc2a10e2017-05-03 14:05:12 +08003420 // make sure an opaque type is not involved as well...
3421 std::string reason(getBasicString(type.getBasicType()));
3422 reason += "s can't be function return values";
3423 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003424 if (mShaderVersion < 300)
3425 {
3426 // Array return values are forbidden, but there's also no valid syntax for declaring array
3427 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003428 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003429
3430 if (type.isStructureContainingArrays())
3431 {
3432 // ESSL 1.00.17 section 6.1 Function Definitions
3433 error(location, "structures containing arrays can't be function return values",
3434 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003435 }
3436 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003437
3438 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003439 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003440}
3441
Olli Etuahocce89652017-06-19 16:04:09 +03003442TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3443{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003444 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003445 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3446 // really only interested in the mangled name of the function to look up the actual function
3447 // from the symbol table. If we just had the name string and the types of the parameters that
3448 // would be enough, but TFunction carries a lot of extra information in addition to that.
3449 // Besides function calls we do have to store constructor calls in the same data structure, for
3450 // them we need to store a TType.
Olli Etuaho0c371002017-12-13 17:00:25 +04003451 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved, false);
Olli Etuahocce89652017-06-19 16:04:09 +03003452}
3453
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003454TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003455{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003456 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003457 {
3458 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3459 "[]");
3460 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003461 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003462 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003463 error(publicType.getLine(), "constructor can't be a structure definition",
3464 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003465 }
3466
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003467 TType *type = new TType(publicType);
3468 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003469 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003470 error(publicType.getLine(), "cannot construct this type",
3471 getBasicString(publicType.getBasicType()));
3472 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003473 }
3474
Olli Etuaho0c371002017-12-13 17:00:25 +04003475 return new TFunction(&symbolTable, nullptr, type, SymbolType::NotResolved, true, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003476}
3477
Olli Etuaho55bde912017-10-25 13:41:13 +03003478void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3479 const char *errorMessage,
3480 const char *token,
3481 TType *arrayType)
3482{
3483 if (arrayType->isUnsizedArray())
3484 {
3485 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003486 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003487 }
3488}
3489
3490TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003491 const TString *name,
3492 const TSourceLoc &nameLoc)
3493{
Olli Etuaho55bde912017-10-25 13:41:13 +03003494 ASSERT(type);
3495 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3496 type);
3497 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003498 {
3499 error(nameLoc, "illegal use of type 'void'", name->c_str());
3500 }
3501 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003502 TParameter param = {name, type};
3503 return param;
3504}
3505
Olli Etuaho55bde912017-10-25 13:41:13 +03003506TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3507 const TString *name,
3508 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003509{
Olli Etuaho55bde912017-10-25 13:41:13 +03003510 TType *type = new TType(publicType);
3511 return parseParameterDeclarator(type, name, nameLoc);
3512}
3513
3514TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3515 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003516 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003517 const TSourceLoc &arrayLoc,
3518 TPublicType *elementType)
3519{
3520 checkArrayElementIsNotArray(arrayLoc, *elementType);
3521 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003522 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003523 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003524}
3525
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003526bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3527 TType type,
3528 const TSourceLoc &line)
3529{
3530 if (arguments->empty())
3531 {
3532 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3533 return false;
3534 }
3535 for (TIntermNode *arg : *arguments)
3536 {
3537 TIntermTyped *element = arg->getAsTyped();
3538 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003539 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3540 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003541 {
3542 error(line, "constructing from a non-dereferenced array", "constructor");
3543 return false;
3544 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003545 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003546 {
3547 if (dimensionalityFromElement == 1u)
3548 {
3549 error(line, "implicitly sized array of arrays constructor argument is not an array",
3550 "constructor");
3551 }
3552 else
3553 {
3554 error(line,
3555 "implicitly sized array of arrays constructor argument dimensionality is too "
3556 "low",
3557 "constructor");
3558 }
3559 return false;
3560 }
3561 }
3562 return true;
3563}
3564
Jamie Madillb98c3a82015-07-23 14:26:04 -04003565// This function is used to test for the correctness of the parameters passed to various constructor
3566// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003567//
Olli Etuaho856c4972016-08-08 11:38:39 +03003568// 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 +00003569//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003570TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003571 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303572 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003573{
Olli Etuaho856c4972016-08-08 11:38:39 +03003574 if (type.isUnsizedArray())
3575 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003576 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003577 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003578 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003579 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003580 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003581 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3582 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003583 if (type.getOutermostArraySize() == 0u)
3584 {
3585 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3586 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003587 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003588 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003589 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003590 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003591 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003592 }
3593 }
3594 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003595 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003596
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003597 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003598 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003599 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003600 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003601
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003602 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003603 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003604
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003605 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3606 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003607 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003608 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003609 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003610 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003611}
3612
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003613//
3614// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003615// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003616//
Olli Etuaho13389b62016-10-16 11:48:18 +01003617TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003618 const TTypeQualifierBuilder &typeQualifierBuilder,
3619 const TSourceLoc &nameLine,
3620 const TString &blockName,
3621 TFieldList *fieldList,
3622 const TString *instanceName,
3623 const TSourceLoc &instanceLine,
3624 TIntermTyped *arrayIndex,
3625 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003626{
Olli Etuaho856c4972016-08-08 11:38:39 +03003627 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003628
Olli Etuaho77ba4082016-12-16 12:01:18 +00003629 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003630
Jiajia Qinbc585152017-06-23 15:42:17 +08003631 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003632 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003633 error(typeQualifier.line,
3634 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3635 "3.10",
3636 getQualifierString(typeQualifier.qualifier));
3637 }
3638 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3639 {
3640 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003641 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003642 }
3643
Martin Radev70866b82016-07-22 15:27:42 +03003644 if (typeQualifier.invariant)
3645 {
3646 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3647 }
3648
Jiajia Qinbc585152017-06-23 15:42:17 +08003649 if (typeQualifier.qualifier != EvqBuffer)
3650 {
3651 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3652 }
Olli Etuaho43364892017-02-13 16:00:12 +00003653
jchen10af713a22017-04-19 09:10:56 +08003654 // add array index
3655 unsigned int arraySize = 0;
3656 if (arrayIndex != nullptr)
3657 {
3658 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3659 }
3660
3661 if (mShaderVersion < 310)
3662 {
3663 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3664 }
3665 else
3666 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003667 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3668 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003669 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003670
Andrei Volykhina5527072017-03-22 16:46:30 +03003671 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3672
Jamie Madill099c0f32013-06-20 11:55:52 -04003673 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003674 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003675 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3676 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003677
Jamie Madill099c0f32013-06-20 11:55:52 -04003678 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3679 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003680 if (typeQualifier.qualifier == EvqUniform)
3681 {
3682 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3683 }
3684 else if (typeQualifier.qualifier == EvqBuffer)
3685 {
3686 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3687 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003688 }
3689
Jamie Madill1566ef72013-06-20 11:55:54 -04003690 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3691 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003692 if (typeQualifier.qualifier == EvqUniform)
3693 {
3694 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3695 }
3696 else if (typeQualifier.qualifier == EvqBuffer)
3697 {
3698 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3699 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003700 }
3701
Olli Etuaho856c4972016-08-08 11:38:39 +03003702 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003703
Martin Radev2cc85b32016-08-05 16:22:53 +03003704 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3705
Jamie Madill98493dd2013-07-08 14:39:03 -04003706 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303707 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3708 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003709 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303710 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003711 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303712 {
jchen10cc2a10e2017-05-03 14:05:12 +08003713 std::string reason("unsupported type - ");
3714 reason += fieldType->getBasicString();
3715 reason += " types are not allowed in interface blocks";
3716 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003717 }
3718
Jamie Madill98493dd2013-07-08 14:39:03 -04003719 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003720 switch (qualifier)
3721 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003722 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003723 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003724 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003725 if (typeQualifier.qualifier == EvqBuffer)
3726 {
3727 error(field->line(), "invalid qualifier on shader storage block member",
3728 getQualifierString(qualifier));
3729 }
3730 break;
3731 case EvqBuffer:
3732 if (typeQualifier.qualifier == EvqUniform)
3733 {
3734 error(field->line(), "invalid qualifier on uniform block member",
3735 getQualifierString(qualifier));
3736 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003737 break;
3738 default:
3739 error(field->line(), "invalid qualifier on interface block member",
3740 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003741 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003742 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003743
Martin Radev70866b82016-07-22 15:27:42 +03003744 if (fieldType->isInvariant())
3745 {
3746 error(field->line(), "invalid qualifier on interface block member", "invariant");
3747 }
3748
Jamie Madilla5efff92013-06-06 11:56:47 -04003749 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003750 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003751 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003752 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003753
Jamie Madill98493dd2013-07-08 14:39:03 -04003754 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003755 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003756 error(field->line(), "invalid layout qualifier: cannot be used here",
3757 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003758 }
3759
Jamie Madill98493dd2013-07-08 14:39:03 -04003760 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003761 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003762 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003763 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003764 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003765 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003766 warning(field->line(),
3767 "extraneous layout qualifier: only has an effect on matrix types",
3768 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003769 }
3770
Jamie Madill98493dd2013-07-08 14:39:03 -04003771 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003772
Olli Etuahoebee5b32017-11-23 12:56:32 +02003773 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3774 typeQualifier.qualifier != EvqBuffer)
3775 {
3776 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3777 checkIsNotUnsizedArray(field->line(),
3778 "array members of interface blocks must specify a size",
3779 field->name().c_str(), field->type());
3780 }
3781
Jiajia Qinbc585152017-06-23 15:42:17 +08003782 if (typeQualifier.qualifier == EvqBuffer)
3783 {
3784 // set memory qualifiers
3785 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3786 // qualified with a memory qualifier, it is as if all of its members were declared with
3787 // the same memory qualifier.
3788 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3789 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3790 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3791 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3792 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3793 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3794 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3795 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3796 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3797 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3798 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003799 }
3800
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003801 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3802 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003803 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3804 {
3805 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3806 }
3807
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003808 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3809 if (arrayIndex != nullptr)
3810 {
3811 interfaceBlockType.makeArray(arraySize);
3812 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003813
Olli Etuaho195be942017-12-04 23:40:14 +02003814 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003815 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3816 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003817 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003818 new TVariable(&symbolTable, instanceName, interfaceBlockType,
3819 instanceName ? SymbolType::UserDefined : SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003820
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003821 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003822 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003823 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003824 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3825 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003826 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303827 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003828
3829 // set parent pointer of the field variable
3830 fieldType->setInterfaceBlock(interfaceBlock);
3831
Olli Etuaho195be942017-12-04 23:40:14 +02003832 TVariable *fieldVariable =
3833 new TVariable(&symbolTable, &field->name(), *fieldType, SymbolType::UserDefined);
3834 if (symbolTable.declareVariable(fieldVariable))
Olli Etuaho0f684632017-07-13 12:42:15 +03003835 {
3836 fieldVariable->setQualifier(typeQualifier.qualifier);
3837 }
3838 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303839 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003840 error(field->line(), "redefinition of an interface block member name",
3841 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003842 }
3843 }
3844 }
3845 else
3846 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003847 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003848
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003849 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003850 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303851 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003852 error(instanceLine, "redefinition of an interface block instance name",
3853 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003854 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003855 }
3856
Olli Etuaho195be942017-12-04 23:40:14 +02003857 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3858 blockSymbol->setLine(typeQualifier.line);
3859 TIntermDeclaration *declaration = new TIntermDeclaration();
3860 declaration->appendDeclarator(blockSymbol);
3861 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003862
3863 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003864 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003865}
3866
Olli Etuaho383b7912016-08-05 11:22:59 +03003867void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003868{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003869 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003870
3871 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003872 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303873 if (mStructNestingLevel > 1)
3874 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003875 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003876 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003877}
3878
3879void TParseContext::exitStructDeclaration()
3880{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003881 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003882}
3883
Olli Etuaho8a176262016-08-16 14:23:01 +03003884void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003885{
Jamie Madillacb4b812016-11-07 13:50:29 -05003886 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303887 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003888 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003889 }
3890
Arun Patole7e7e68d2015-05-22 12:02:25 +05303891 if (field.type()->getBasicType() != EbtStruct)
3892 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003893 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003894 }
3895
3896 // We're already inside a structure definition at this point, so add
3897 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303898 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3899 {
Jamie Madill41a49272014-03-18 16:10:13 -04003900 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003901 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3902 {
3903 // This may happen in case there are nested struct definitions. While they are also
3904 // invalid GLSL, they don't cause a syntax error.
3905 reasonStream << "Struct nesting";
3906 }
3907 else
3908 {
3909 reasonStream << "Reference of struct type "
Olli Etuahobed35d72017-12-20 16:36:26 +02003910 << field.type()->getStruct()->name().c_str();
Olli Etuahof0957992017-12-22 11:10:04 +02003911 }
3912 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003913 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003914 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003915 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003916 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003917}
3918
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003919//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003920// Parse an array index expression
3921//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003922TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3923 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303924 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003925{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003926 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3927 {
3928 if (baseExpression->getAsSymbolNode())
3929 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303930 error(location, " left of '[' is not of type array, matrix, or vector ",
3931 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003932 }
3933 else
3934 {
3935 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3936 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003937
Olli Etuaho3ec75682017-07-05 17:02:55 +03003938 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003939 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003940
Jiawei Shaod8105a02017-08-08 09:54:36 +08003941 if (baseExpression->getQualifier() == EvqPerVertexIn)
3942 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003943 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003944 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3945 {
3946 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3947 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3948 }
3949 }
3950
Jamie Madill21c1e452014-12-29 11:33:41 -05003951 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3952
Olli Etuaho36b05142015-11-12 13:10:42 +02003953 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3954 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3955 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3956 // index is a constant expression.
3957 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3958 {
3959 if (baseExpression->isInterfaceBlock())
3960 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003961 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003962 switch (baseExpression->getQualifier())
3963 {
3964 case EvqPerVertexIn:
3965 break;
3966 case EvqUniform:
3967 case EvqBuffer:
3968 error(location,
3969 "array indexes for uniform block arrays and shader storage block arrays "
3970 "must be constant integral expressions",
3971 "[");
3972 break;
3973 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003974 // We can reach here only in error cases.
3975 ASSERT(mDiagnostics->numErrors() > 0);
3976 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003977 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003978 }
3979 else if (baseExpression->getQualifier() == EvqFragmentOut)
3980 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003981 error(location,
3982 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003983 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003984 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3985 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003986 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003987 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003988 }
3989
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003990 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003991 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003992 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3993 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3994 // constant fold expressions that are not constant expressions). The most compatible way to
3995 // handle this case is to report a warning instead of an error and force the index to be in
3996 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003997 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003998 int index = 0;
3999 if (indexConstantUnion->getBasicType() == EbtInt)
4000 {
4001 index = indexConstantUnion->getIConst(0);
4002 }
4003 else if (indexConstantUnion->getBasicType() == EbtUInt)
4004 {
4005 index = static_cast<int>(indexConstantUnion->getUConst(0));
4006 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004007
4008 int safeIndex = -1;
4009
Olli Etuahoebee5b32017-11-23 12:56:32 +02004010 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004011 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004012 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4013 safeIndex = 0;
4014 }
4015
4016 if (!baseExpression->getType().isUnsizedArray())
4017 {
4018 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004019 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004020 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004021 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004022 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4023 {
4024 outOfRangeError(outOfRangeIndexIsError, location,
4025 "array index for gl_FragData must be zero when "
4026 "GL_EXT_draw_buffers is disabled",
4027 "[]");
4028 safeIndex = 0;
4029 }
4030 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004031 }
4032 // Only do generic out-of-range check if similar error hasn't already been reported.
4033 if (safeIndex < 0)
4034 {
4035 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004036 {
4037 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4038 baseExpression->getOutermostArraySize(),
4039 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004040 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004041 else if (baseExpression->isMatrix())
4042 {
4043 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4044 baseExpression->getType().getCols(),
4045 "matrix field selection out of range");
4046 }
4047 else
4048 {
4049 ASSERT(baseExpression->isVector());
4050 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4051 baseExpression->getType().getNominalSize(),
4052 "vector field selection out of range");
4053 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004054 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004055
Olli Etuahoebee5b32017-11-23 12:56:32 +02004056 ASSERT(safeIndex >= 0);
4057 // Data of constant unions can't be changed, because it may be shared with other
4058 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4059 // sanitized object.
4060 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4061 {
4062 TConstantUnion *safeConstantUnion = new TConstantUnion();
4063 safeConstantUnion->setIConst(safeIndex);
4064 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4065 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4066 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004067
Olli Etuahoebee5b32017-11-23 12:56:32 +02004068 TIntermBinary *node =
4069 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4070 node->setLine(location);
4071 return node->fold(mDiagnostics);
4072 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004073 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004074
4075 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4076 node->setLine(location);
4077 // Indirect indexing can never be constant folded.
4078 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004079}
4080
Olli Etuahoebee5b32017-11-23 12:56:32 +02004081int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4082 const TSourceLoc &location,
4083 int index,
4084 int arraySize,
4085 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004086{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004087 // Should not reach here with an unsized / runtime-sized array.
4088 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004089 // A negative index should already have been checked.
4090 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004091 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004092 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004093 std::stringstream reasonStream;
4094 reasonStream << reason << " '" << index << "'";
4095 std::string token = reasonStream.str();
4096 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004097 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004098 }
4099 return index;
4100}
4101
Jamie Madillb98c3a82015-07-23 14:26:04 -04004102TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4103 const TSourceLoc &dotLocation,
4104 const TString &fieldString,
4105 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004106{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004107 if (baseExpression->isArray())
4108 {
4109 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004110 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004111 }
4112
4113 if (baseExpression->isVector())
4114 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004115 TVector<int> fieldOffsets;
4116 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4117 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004118 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004119 fieldOffsets.resize(1);
4120 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004122 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4123 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004125 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004126 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004127 else if (baseExpression->getBasicType() == EbtStruct)
4128 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304129 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004130 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004131 {
4132 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004133 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004134 }
4135 else
4136 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004137 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004138 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004139 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004140 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004141 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004142 {
4143 fieldFound = true;
4144 break;
4145 }
4146 }
4147 if (fieldFound)
4148 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004149 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004150 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004151 TIntermBinary *node =
4152 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4153 node->setLine(dotLocation);
4154 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004155 }
4156 else
4157 {
4158 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004159 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004160 }
4161 }
4162 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004163 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004164 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304165 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004166 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004167 {
4168 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004169 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004170 }
4171 else
4172 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004173 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004174 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004175 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004176 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004177 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004178 {
4179 fieldFound = true;
4180 break;
4181 }
4182 }
4183 if (fieldFound)
4184 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004185 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004186 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004187 TIntermBinary *node =
4188 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4189 node->setLine(dotLocation);
4190 // Indexing interface blocks can never be constant folded.
4191 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004192 }
4193 else
4194 {
4195 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004196 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004197 }
4198 }
4199 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004200 else
4201 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004202 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004203 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004204 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304205 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004206 }
4207 else
4208 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304209 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004210 " field selection requires structure, vector, or interface block on left hand "
4211 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304212 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004213 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004214 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004215 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004216}
4217
Jamie Madillb98c3a82015-07-23 14:26:04 -04004218TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4219 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004220{
Jamie Madill2f294c92017-11-20 14:47:26 -05004221 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004222
4223 if (qualifierType == "shared")
4224 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004225 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004226 {
4227 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4228 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004229 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004230 }
4231 else if (qualifierType == "packed")
4232 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004233 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004234 {
4235 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4236 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004237 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004238 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004239 else if (qualifierType == "std430")
4240 {
4241 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4242 qualifier.blockStorage = EbsStd430;
4243 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004244 else if (qualifierType == "std140")
4245 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004246 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004247 }
4248 else if (qualifierType == "row_major")
4249 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004250 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004251 }
4252 else if (qualifierType == "column_major")
4253 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004254 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004255 }
4256 else if (qualifierType == "location")
4257 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004258 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4259 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004260 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004261 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004262 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004263 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4264 {
4265 qualifier.yuv = true;
4266 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004267 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004268 else if (qualifierType == "rgba32f")
4269 {
4270 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4271 qualifier.imageInternalFormat = EiifRGBA32F;
4272 }
4273 else if (qualifierType == "rgba16f")
4274 {
4275 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4276 qualifier.imageInternalFormat = EiifRGBA16F;
4277 }
4278 else if (qualifierType == "r32f")
4279 {
4280 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4281 qualifier.imageInternalFormat = EiifR32F;
4282 }
4283 else if (qualifierType == "rgba8")
4284 {
4285 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4286 qualifier.imageInternalFormat = EiifRGBA8;
4287 }
4288 else if (qualifierType == "rgba8_snorm")
4289 {
4290 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4291 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4292 }
4293 else if (qualifierType == "rgba32i")
4294 {
4295 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4296 qualifier.imageInternalFormat = EiifRGBA32I;
4297 }
4298 else if (qualifierType == "rgba16i")
4299 {
4300 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4301 qualifier.imageInternalFormat = EiifRGBA16I;
4302 }
4303 else if (qualifierType == "rgba8i")
4304 {
4305 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4306 qualifier.imageInternalFormat = EiifRGBA8I;
4307 }
4308 else if (qualifierType == "r32i")
4309 {
4310 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4311 qualifier.imageInternalFormat = EiifR32I;
4312 }
4313 else if (qualifierType == "rgba32ui")
4314 {
4315 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4316 qualifier.imageInternalFormat = EiifRGBA32UI;
4317 }
4318 else if (qualifierType == "rgba16ui")
4319 {
4320 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4321 qualifier.imageInternalFormat = EiifRGBA16UI;
4322 }
4323 else if (qualifierType == "rgba8ui")
4324 {
4325 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4326 qualifier.imageInternalFormat = EiifRGBA8UI;
4327 }
4328 else if (qualifierType == "r32ui")
4329 {
4330 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4331 qualifier.imageInternalFormat = EiifR32UI;
4332 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004333 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4334 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004335 {
4336 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4337 qualifier.primitiveType = EptPoints;
4338 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004339 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4340 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004341 {
4342 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4343 qualifier.primitiveType = EptLines;
4344 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004345 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4346 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004347 {
4348 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4349 qualifier.primitiveType = EptLinesAdjacency;
4350 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004351 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4352 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004353 {
4354 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4355 qualifier.primitiveType = EptTriangles;
4356 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004357 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4358 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004359 {
4360 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4361 qualifier.primitiveType = EptTrianglesAdjacency;
4362 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004363 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4364 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004365 {
4366 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4367 qualifier.primitiveType = EptLineStrip;
4368 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004369 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4370 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004371 {
4372 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4373 qualifier.primitiveType = EptTriangleStrip;
4374 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004375
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004376 else
4377 {
4378 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004379 }
4380
Jamie Madilla5efff92013-06-06 11:56:47 -04004381 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004382}
4383
Martin Radev802abe02016-08-04 17:48:32 +03004384void TParseContext::parseLocalSize(const TString &qualifierType,
4385 const TSourceLoc &qualifierTypeLine,
4386 int intValue,
4387 const TSourceLoc &intValueLine,
4388 const std::string &intValueString,
4389 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004390 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004391{
Olli Etuaho856c4972016-08-08 11:38:39 +03004392 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004393 if (intValue < 1)
4394 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004395 std::stringstream reasonStream;
4396 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4397 std::string reason = reasonStream.str();
4398 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004399 }
4400 (*localSize)[index] = intValue;
4401}
4402
Olli Etuaho09b04a22016-12-15 13:30:26 +00004403void TParseContext::parseNumViews(int intValue,
4404 const TSourceLoc &intValueLine,
4405 const std::string &intValueString,
4406 int *numViews)
4407{
4408 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4409 // specification.
4410 if (intValue < 1)
4411 {
4412 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4413 }
4414 *numViews = intValue;
4415}
4416
Shaob5cc1192017-07-06 10:47:20 +08004417void TParseContext::parseInvocations(int intValue,
4418 const TSourceLoc &intValueLine,
4419 const std::string &intValueString,
4420 int *numInvocations)
4421{
4422 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4423 // it doesn't make sense to accept invocations <= 0.
4424 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4425 {
4426 error(intValueLine,
4427 "out of range: invocations must be in the range of [1, "
4428 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4429 intValueString.c_str());
4430 }
4431 else
4432 {
4433 *numInvocations = intValue;
4434 }
4435}
4436
4437void TParseContext::parseMaxVertices(int intValue,
4438 const TSourceLoc &intValueLine,
4439 const std::string &intValueString,
4440 int *maxVertices)
4441{
4442 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4443 // it doesn't make sense to accept max_vertices < 0.
4444 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4445 {
4446 error(
4447 intValueLine,
4448 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4449 intValueString.c_str());
4450 }
4451 else
4452 {
4453 *maxVertices = intValue;
4454 }
4455}
4456
Jamie Madillb98c3a82015-07-23 14:26:04 -04004457TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4458 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004459 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304460 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004461{
Jamie Madill2f294c92017-11-20 14:47:26 -05004462 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004463
Martin Radev802abe02016-08-04 17:48:32 +03004464 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004465
Martin Radev802abe02016-08-04 17:48:32 +03004466 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004467 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004468 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004469 if (intValue < 0)
4470 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004471 error(intValueLine, "out of range: location must be non-negative",
4472 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004473 }
4474 else
4475 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004476 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004477 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004478 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004479 }
Olli Etuaho43364892017-02-13 16:00:12 +00004480 else if (qualifierType == "binding")
4481 {
4482 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4483 if (intValue < 0)
4484 {
4485 error(intValueLine, "out of range: binding must be non-negative",
4486 intValueString.c_str());
4487 }
4488 else
4489 {
4490 qualifier.binding = intValue;
4491 }
4492 }
jchen104cdac9e2017-05-08 11:01:20 +08004493 else if (qualifierType == "offset")
4494 {
4495 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4496 if (intValue < 0)
4497 {
4498 error(intValueLine, "out of range: offset must be non-negative",
4499 intValueString.c_str());
4500 }
4501 else
4502 {
4503 qualifier.offset = intValue;
4504 }
4505 }
Martin Radev802abe02016-08-04 17:48:32 +03004506 else if (qualifierType == "local_size_x")
4507 {
4508 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4509 &qualifier.localSize);
4510 }
4511 else if (qualifierType == "local_size_y")
4512 {
4513 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4514 &qualifier.localSize);
4515 }
4516 else if (qualifierType == "local_size_z")
4517 {
4518 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4519 &qualifier.localSize);
4520 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004521 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004522 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004523 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4524 {
4525 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4526 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004527 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004528 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4529 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004530 {
4531 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4532 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004533 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4534 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004535 {
4536 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4537 }
4538
Martin Radev802abe02016-08-04 17:48:32 +03004539 else
4540 {
4541 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004542 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004543
Jamie Madilla5efff92013-06-06 11:56:47 -04004544 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004545}
4546
Olli Etuaho613b9592016-09-05 12:05:53 +03004547TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4548{
4549 return new TTypeQualifierBuilder(
4550 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4551 mShaderVersion);
4552}
4553
Olli Etuahocce89652017-06-19 16:04:09 +03004554TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4555 const TSourceLoc &loc)
4556{
4557 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4558 return new TStorageQualifierWrapper(qualifier, loc);
4559}
4560
4561TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4562{
4563 if (getShaderType() == GL_VERTEX_SHADER)
4564 {
4565 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4566 }
4567 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4568}
4569
4570TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4571{
4572 if (declaringFunction())
4573 {
4574 return new TStorageQualifierWrapper(EvqIn, loc);
4575 }
Shaob5cc1192017-07-06 10:47:20 +08004576
4577 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004578 {
Shaob5cc1192017-07-06 10:47:20 +08004579 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004580 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004581 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004582 {
4583 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4584 }
4585 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004586 }
Shaob5cc1192017-07-06 10:47:20 +08004587 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004588 {
Shaob5cc1192017-07-06 10:47:20 +08004589 if (mShaderVersion < 300)
4590 {
4591 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4592 }
4593 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004594 }
Shaob5cc1192017-07-06 10:47:20 +08004595 case GL_COMPUTE_SHADER:
4596 {
4597 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4598 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004599 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004600 {
4601 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4602 }
4603 default:
4604 {
4605 UNREACHABLE();
4606 return new TStorageQualifierWrapper(EvqLast, loc);
4607 }
Olli Etuahocce89652017-06-19 16:04:09 +03004608 }
Olli Etuahocce89652017-06-19 16:04:09 +03004609}
4610
4611TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4612{
4613 if (declaringFunction())
4614 {
4615 return new TStorageQualifierWrapper(EvqOut, loc);
4616 }
Shaob5cc1192017-07-06 10:47:20 +08004617 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004618 {
Shaob5cc1192017-07-06 10:47:20 +08004619 case GL_VERTEX_SHADER:
4620 {
4621 if (mShaderVersion < 300)
4622 {
4623 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4624 }
4625 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4626 }
4627 case GL_FRAGMENT_SHADER:
4628 {
4629 if (mShaderVersion < 300)
4630 {
4631 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4632 }
4633 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4634 }
4635 case GL_COMPUTE_SHADER:
4636 {
4637 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4638 return new TStorageQualifierWrapper(EvqLast, loc);
4639 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004640 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004641 {
4642 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4643 }
4644 default:
4645 {
4646 UNREACHABLE();
4647 return new TStorageQualifierWrapper(EvqLast, loc);
4648 }
Olli Etuahocce89652017-06-19 16:04:09 +03004649 }
Olli Etuahocce89652017-06-19 16:04:09 +03004650}
4651
4652TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4653{
4654 if (!declaringFunction())
4655 {
4656 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4657 }
4658 return new TStorageQualifierWrapper(EvqInOut, loc);
4659}
4660
Jamie Madillb98c3a82015-07-23 14:26:04 -04004661TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004662 TLayoutQualifier rightQualifier,
4663 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004664{
Martin Radevc28888b2016-07-22 15:27:42 +03004665 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004666 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004667}
4668
Olli Etuahod5f44c92017-11-29 17:15:40 +02004669TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004670{
4671 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004672 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004673}
4674
Olli Etuahod5f44c92017-11-29 17:15:40 +02004675TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4676 const TSourceLoc &loc,
4677 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004678{
4679 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004680 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004681}
4682
Olli Etuaho722bfb52017-10-26 17:00:11 +03004683void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4684 const TFieldList::const_iterator end,
4685 const TString &name,
4686 const TSourceLoc &location)
4687{
4688 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4689 {
4690 if ((*fieldIter)->name() == name)
4691 {
4692 error(location, "duplicate field name in structure", name.c_str());
4693 }
4694 }
4695}
4696
4697TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4698{
4699 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4700 ++fieldIter)
4701 {
4702 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4703 location);
4704 }
4705 return fields;
4706}
4707
Olli Etuaho4de340a2016-12-16 09:32:03 +00004708TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4709 const TFieldList *newlyAddedFields,
4710 const TSourceLoc &location)
4711{
4712 for (TField *field : *newlyAddedFields)
4713 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004714 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4715 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004716 processedFields->push_back(field);
4717 }
4718 return processedFields;
4719}
4720
Martin Radev70866b82016-07-22 15:27:42 +03004721TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4722 const TTypeQualifierBuilder &typeQualifierBuilder,
4723 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004724 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004725{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004726 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004727
Martin Radev70866b82016-07-22 15:27:42 +03004728 typeSpecifier->qualifier = typeQualifier.qualifier;
4729 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004730 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004731 typeSpecifier->invariant = typeQualifier.invariant;
4732 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304733 {
Martin Radev70866b82016-07-22 15:27:42 +03004734 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004735 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004736 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004737}
4738
Jamie Madillb98c3a82015-07-23 14:26:04 -04004739TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004740 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004741{
Martin Radev4a9cd802016-09-01 16:51:51 +03004742 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4743 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004744
Olli Etuahod5f44c92017-11-29 17:15:40 +02004745 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004746 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004747
Martin Radev4a9cd802016-09-01 16:51:51 +03004748 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004749
Olli Etuahod5f44c92017-11-29 17:15:40 +02004750 TFieldList *fieldList = new TFieldList();
4751
4752 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304753 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004754 TType *type = new TType(typeSpecifier);
4755 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304756 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004757 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004758 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004759 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004760 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004761
Olli Etuahod5f44c92017-11-29 17:15:40 +02004762 TField *field = new TField(type, declarator->name(), declarator->line());
4763 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4764 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004765 }
4766
Olli Etuahod5f44c92017-11-29 17:15:40 +02004767 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004768}
4769
Martin Radev4a9cd802016-09-01 16:51:51 +03004770TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4771 const TSourceLoc &nameLine,
4772 const TString *structName,
4773 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004774{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004775 SymbolType structSymbolType = SymbolType::UserDefined;
4776 if (structName == nullptr)
4777 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004778 structSymbolType = SymbolType::Empty;
4779 }
4780 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004781
Jamie Madill9b820842015-02-12 10:40:10 -05004782 // Store a bool in the struct if we're at global scope, to allow us to
4783 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004784 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004785
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004786 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004787 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004788 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004789 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304790 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004791 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004792 }
4793 }
4794
4795 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004796 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004797 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004798 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004799 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004800 switch (qualifier)
4801 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004802 case EvqGlobal:
4803 case EvqTemporary:
4804 break;
4805 default:
4806 error(field.line(), "invalid qualifier on struct member",
4807 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004808 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004809 }
Martin Radev70866b82016-07-22 15:27:42 +03004810 if (field.type()->isInvariant())
4811 {
4812 error(field.line(), "invalid qualifier on struct member", "invariant");
4813 }
jchen104cdac9e2017-05-08 11:01:20 +08004814 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4815 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004816 {
4817 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4818 }
4819
Olli Etuahoebee5b32017-11-23 12:56:32 +02004820 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4821 field.name().c_str(), field.type());
4822
Olli Etuaho43364892017-02-13 16:00:12 +00004823 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4824
4825 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004826
4827 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004828 }
4829
Martin Radev4a9cd802016-09-01 16:51:51 +03004830 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004831 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004832 exitStructDeclaration();
4833
Martin Radev4a9cd802016-09-01 16:51:51 +03004834 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004835}
4836
Jamie Madillb98c3a82015-07-23 14:26:04 -04004837TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004838 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004839 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004840{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004841 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004842 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004843 init->isVector())
4844 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004845 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4846 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004847 return nullptr;
4848 }
4849
Olli Etuaho923ecef2017-10-11 12:01:38 +03004850 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004851 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004852 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004853 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004854 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004855 }
4856
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004857 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4858 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004859 return node;
4860}
4861
4862TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4863{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004864 if (mSwitchNestingLevel == 0)
4865 {
4866 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004867 return nullptr;
4868 }
4869 if (condition == nullptr)
4870 {
4871 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004872 return nullptr;
4873 }
4874 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004875 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004876 {
4877 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004878 }
4879 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004880 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4881 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4882 // fold in case labels.
4883 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004884 {
4885 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004886 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004887 TIntermCase *node = new TIntermCase(condition);
4888 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004889 return node;
4890}
4891
4892TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4893{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004894 if (mSwitchNestingLevel == 0)
4895 {
4896 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004897 return nullptr;
4898 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004899 TIntermCase *node = new TIntermCase(nullptr);
4900 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004901 return node;
4902}
4903
Jamie Madillb98c3a82015-07-23 14:26:04 -04004904TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4905 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004906 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004907{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004908 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004909
4910 switch (op)
4911 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004912 case EOpLogicalNot:
4913 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4914 child->isVector())
4915 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004916 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004917 return nullptr;
4918 }
4919 break;
4920 case EOpBitwiseNot:
4921 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4922 child->isMatrix() || child->isArray())
4923 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004924 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004925 return nullptr;
4926 }
4927 break;
4928 case EOpPostIncrement:
4929 case EOpPreIncrement:
4930 case EOpPostDecrement:
4931 case EOpPreDecrement:
4932 case EOpNegative:
4933 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004934 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4935 child->getBasicType() == EbtBool || child->isArray() ||
4936 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004937 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004938 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004939 return nullptr;
4940 }
4941 // Operators for built-ins are already type checked against their prototype.
4942 default:
4943 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004944 }
4945
Jiajia Qinbc585152017-06-23 15:42:17 +08004946 if (child->getMemoryQualifier().writeonly)
4947 {
4948 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4949 return nullptr;
4950 }
4951
Olli Etuahof119a262016-08-19 15:54:22 +03004952 TIntermUnary *node = new TIntermUnary(op, child);
4953 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004954
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004955 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004956}
4957
Olli Etuaho09b22472015-02-11 11:47:26 +02004958TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4959{
Olli Etuahocce89652017-06-19 16:04:09 +03004960 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004961 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004962 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004963 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004964 return child;
4965 }
4966 return node;
4967}
4968
Jamie Madillb98c3a82015-07-23 14:26:04 -04004969TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4970 TIntermTyped *child,
4971 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004972{
Olli Etuaho856c4972016-08-08 11:38:39 +03004973 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004974 return addUnaryMath(op, child, loc);
4975}
4976
Jamie Madillb98c3a82015-07-23 14:26:04 -04004977bool TParseContext::binaryOpCommonCheck(TOperator op,
4978 TIntermTyped *left,
4979 TIntermTyped *right,
4980 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004981{
jchen10b4cf5652017-05-05 18:51:17 +08004982 // Check opaque types are not allowed to be operands in expressions other than array indexing
4983 // and structure member selection.
4984 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4985 {
4986 switch (op)
4987 {
4988 case EOpIndexDirect:
4989 case EOpIndexIndirect:
4990 break;
4991 case EOpIndexDirectStruct:
4992 UNREACHABLE();
4993
4994 default:
4995 error(loc, "Invalid operation for variables with an opaque type",
4996 GetOperatorString(op));
4997 return false;
4998 }
4999 }
jchen10cc2a10e2017-05-03 14:05:12 +08005000
Jiajia Qinbc585152017-06-23 15:42:17 +08005001 if (right->getMemoryQualifier().writeonly)
5002 {
5003 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5004 return false;
5005 }
5006
5007 if (left->getMemoryQualifier().writeonly)
5008 {
5009 switch (op)
5010 {
5011 case EOpAssign:
5012 case EOpInitialize:
5013 case EOpIndexDirect:
5014 case EOpIndexIndirect:
5015 case EOpIndexDirectStruct:
5016 case EOpIndexDirectInterfaceBlock:
5017 break;
5018 default:
5019 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5020 return false;
5021 }
5022 }
5023
Olli Etuaho244be012016-08-18 15:26:02 +03005024 if (left->getType().getStruct() || right->getType().getStruct())
5025 {
5026 switch (op)
5027 {
5028 case EOpIndexDirectStruct:
5029 ASSERT(left->getType().getStruct());
5030 break;
5031 case EOpEqual:
5032 case EOpNotEqual:
5033 case EOpAssign:
5034 case EOpInitialize:
5035 if (left->getType() != right->getType())
5036 {
5037 return false;
5038 }
5039 break;
5040 default:
5041 error(loc, "Invalid operation for structs", GetOperatorString(op));
5042 return false;
5043 }
5044 }
5045
Olli Etuaho94050052017-05-08 14:17:44 +03005046 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5047 {
5048 switch (op)
5049 {
5050 case EOpIndexDirectInterfaceBlock:
5051 ASSERT(left->getType().getInterfaceBlock());
5052 break;
5053 default:
5054 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5055 return false;
5056 }
5057 }
5058
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005059 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005060 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005061 error(loc, "array / non-array mismatch", GetOperatorString(op));
5062 return false;
5063 }
5064
5065 if (left->isArray())
5066 {
5067 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005068 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005069 {
5070 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5071 return false;
5072 }
5073
Olli Etuahoe79904c2015-03-18 16:56:42 +02005074 switch (op)
5075 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005076 case EOpEqual:
5077 case EOpNotEqual:
5078 case EOpAssign:
5079 case EOpInitialize:
5080 break;
5081 default:
5082 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5083 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005084 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005085 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005086 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005087 {
5088 error(loc, "array size mismatch", GetOperatorString(op));
5089 return false;
5090 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005091 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005092
5093 // Check ops which require integer / ivec parameters
5094 bool isBitShift = false;
5095 switch (op)
5096 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005097 case EOpBitShiftLeft:
5098 case EOpBitShiftRight:
5099 case EOpBitShiftLeftAssign:
5100 case EOpBitShiftRightAssign:
5101 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5102 // check that the basic type is an integer type.
5103 isBitShift = true;
5104 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5105 {
5106 return false;
5107 }
5108 break;
5109 case EOpBitwiseAnd:
5110 case EOpBitwiseXor:
5111 case EOpBitwiseOr:
5112 case EOpBitwiseAndAssign:
5113 case EOpBitwiseXorAssign:
5114 case EOpBitwiseOrAssign:
5115 // It is enough to check the type of only one operand, since later it
5116 // is checked that the operand types match.
5117 if (!IsInteger(left->getBasicType()))
5118 {
5119 return false;
5120 }
5121 break;
5122 default:
5123 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005124 }
5125
5126 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5127 // So the basic type should usually match.
5128 if (!isBitShift && left->getBasicType() != right->getBasicType())
5129 {
5130 return false;
5131 }
5132
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005133 // Check that:
5134 // 1. Type sizes match exactly on ops that require that.
5135 // 2. Restrictions for structs that contain arrays or samplers are respected.
5136 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005137 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005138 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005139 case EOpAssign:
5140 case EOpInitialize:
5141 case EOpEqual:
5142 case EOpNotEqual:
5143 // ESSL 1.00 sections 5.7, 5.8, 5.9
5144 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5145 {
5146 error(loc, "undefined operation for structs containing arrays",
5147 GetOperatorString(op));
5148 return false;
5149 }
5150 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5151 // we interpret the spec so that this extends to structs containing samplers,
5152 // similarly to ESSL 1.00 spec.
5153 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5154 left->getType().isStructureContainingSamplers())
5155 {
5156 error(loc, "undefined operation for structs containing samplers",
5157 GetOperatorString(op));
5158 return false;
5159 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005160
Olli Etuahoe1805592017-01-02 16:41:20 +00005161 if ((left->getNominalSize() != right->getNominalSize()) ||
5162 (left->getSecondarySize() != right->getSecondarySize()))
5163 {
5164 error(loc, "dimension mismatch", GetOperatorString(op));
5165 return false;
5166 }
5167 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005168 case EOpLessThan:
5169 case EOpGreaterThan:
5170 case EOpLessThanEqual:
5171 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005172 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005173 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005174 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005175 return false;
5176 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005177 break;
5178 case EOpAdd:
5179 case EOpSub:
5180 case EOpDiv:
5181 case EOpIMod:
5182 case EOpBitShiftLeft:
5183 case EOpBitShiftRight:
5184 case EOpBitwiseAnd:
5185 case EOpBitwiseXor:
5186 case EOpBitwiseOr:
5187 case EOpAddAssign:
5188 case EOpSubAssign:
5189 case EOpDivAssign:
5190 case EOpIModAssign:
5191 case EOpBitShiftLeftAssign:
5192 case EOpBitShiftRightAssign:
5193 case EOpBitwiseAndAssign:
5194 case EOpBitwiseXorAssign:
5195 case EOpBitwiseOrAssign:
5196 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5197 {
5198 return false;
5199 }
5200
5201 // Are the sizes compatible?
5202 if (left->getNominalSize() != right->getNominalSize() ||
5203 left->getSecondarySize() != right->getSecondarySize())
5204 {
5205 // If the nominal sizes of operands do not match:
5206 // One of them must be a scalar.
5207 if (!left->isScalar() && !right->isScalar())
5208 return false;
5209
5210 // In the case of compound assignment other than multiply-assign,
5211 // the right side needs to be a scalar. Otherwise a vector/matrix
5212 // would be assigned to a scalar. A scalar can't be shifted by a
5213 // vector either.
5214 if (!right->isScalar() &&
5215 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5216 return false;
5217 }
5218 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005219 default:
5220 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005221 }
5222
Olli Etuahod6b14282015-03-17 14:31:35 +02005223 return true;
5224}
5225
Olli Etuaho1dded802016-08-18 18:13:13 +03005226bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5227 const TType &left,
5228 const TType &right)
5229{
5230 switch (op)
5231 {
5232 case EOpMul:
5233 case EOpMulAssign:
5234 return left.getNominalSize() == right.getNominalSize() &&
5235 left.getSecondarySize() == right.getSecondarySize();
5236 case EOpVectorTimesScalar:
5237 return true;
5238 case EOpVectorTimesScalarAssign:
5239 ASSERT(!left.isMatrix() && !right.isMatrix());
5240 return left.isVector() && !right.isVector();
5241 case EOpVectorTimesMatrix:
5242 return left.getNominalSize() == right.getRows();
5243 case EOpVectorTimesMatrixAssign:
5244 ASSERT(!left.isMatrix() && right.isMatrix());
5245 return left.isVector() && left.getNominalSize() == right.getRows() &&
5246 left.getNominalSize() == right.getCols();
5247 case EOpMatrixTimesVector:
5248 return left.getCols() == right.getNominalSize();
5249 case EOpMatrixTimesScalar:
5250 return true;
5251 case EOpMatrixTimesScalarAssign:
5252 ASSERT(left.isMatrix() && !right.isMatrix());
5253 return !right.isVector();
5254 case EOpMatrixTimesMatrix:
5255 return left.getCols() == right.getRows();
5256 case EOpMatrixTimesMatrixAssign:
5257 ASSERT(left.isMatrix() && right.isMatrix());
5258 // We need to check two things:
5259 // 1. The matrix multiplication step is valid.
5260 // 2. The result will have the same number of columns as the lvalue.
5261 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5262
5263 default:
5264 UNREACHABLE();
5265 return false;
5266 }
5267}
5268
Jamie Madillb98c3a82015-07-23 14:26:04 -04005269TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5270 TIntermTyped *left,
5271 TIntermTyped *right,
5272 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005273{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005274 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005275 return nullptr;
5276
Olli Etuahofc1806e2015-03-17 13:03:11 +02005277 switch (op)
5278 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005279 case EOpEqual:
5280 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005281 case EOpLessThan:
5282 case EOpGreaterThan:
5283 case EOpLessThanEqual:
5284 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005285 break;
5286 case EOpLogicalOr:
5287 case EOpLogicalXor:
5288 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005289 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5290 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005291 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005292 {
5293 return nullptr;
5294 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005295 // Basic types matching should have been already checked.
5296 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005297 break;
5298 case EOpAdd:
5299 case EOpSub:
5300 case EOpDiv:
5301 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005302 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5303 !right->getType().getStruct());
5304 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005305 {
5306 return nullptr;
5307 }
5308 break;
5309 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005310 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5311 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005312 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005313 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005314 {
5315 return nullptr;
5316 }
5317 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005318 default:
5319 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005320 }
5321
Olli Etuaho1dded802016-08-18 18:13:13 +03005322 if (op == EOpMul)
5323 {
5324 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5325 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5326 {
5327 return nullptr;
5328 }
5329 }
5330
Olli Etuaho3fdec912016-08-18 15:08:06 +03005331 TIntermBinary *node = new TIntermBinary(op, left, right);
5332 node->setLine(loc);
5333
Olli Etuaho3fdec912016-08-18 15:08:06 +03005334 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005335 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005336}
5337
Jamie Madillb98c3a82015-07-23 14:26:04 -04005338TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5339 TIntermTyped *left,
5340 TIntermTyped *right,
5341 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005342{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005343 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005344 if (node == 0)
5345 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005346 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5347 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005348 return left;
5349 }
5350 return node;
5351}
5352
Jamie Madillb98c3a82015-07-23 14:26:04 -04005353TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5354 TIntermTyped *left,
5355 TIntermTyped *right,
5356 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005357{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005358 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005359 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005360 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005361 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5362 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005363 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005364 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005365 }
5366 return node;
5367}
5368
Olli Etuaho13389b62016-10-16 11:48:18 +01005369TIntermBinary *TParseContext::createAssign(TOperator op,
5370 TIntermTyped *left,
5371 TIntermTyped *right,
5372 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005373{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005374 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005375 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005376 if (op == EOpMulAssign)
5377 {
5378 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5379 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5380 {
5381 return nullptr;
5382 }
5383 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005384 TIntermBinary *node = new TIntermBinary(op, left, right);
5385 node->setLine(loc);
5386
Olli Etuaho3fdec912016-08-18 15:08:06 +03005387 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005388 }
5389 return nullptr;
5390}
5391
Jamie Madillb98c3a82015-07-23 14:26:04 -04005392TIntermTyped *TParseContext::addAssign(TOperator op,
5393 TIntermTyped *left,
5394 TIntermTyped *right,
5395 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005396{
Olli Etuahocce89652017-06-19 16:04:09 +03005397 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005398 TIntermTyped *node = createAssign(op, left, right, loc);
5399 if (node == nullptr)
5400 {
5401 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005402 return left;
5403 }
5404 return node;
5405}
5406
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005407TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5408 TIntermTyped *right,
5409 const TSourceLoc &loc)
5410{
Corentin Wallez0d959252016-07-12 17:26:32 -04005411 // WebGL2 section 5.26, the following results in an error:
5412 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005413 if (mShaderSpec == SH_WEBGL2_SPEC &&
5414 (left->isArray() || left->getBasicType() == EbtVoid ||
5415 left->getType().isStructureContainingArrays() || right->isArray() ||
5416 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005417 {
5418 error(loc,
5419 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5420 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005421 }
5422
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005423 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5424 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5425 commaNode->getTypePointer()->setQualifier(resultQualifier);
5426 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005427}
5428
Olli Etuaho49300862015-02-20 14:54:49 +02005429TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5430{
5431 switch (op)
5432 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005433 case EOpContinue:
5434 if (mLoopNestingLevel <= 0)
5435 {
5436 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005437 }
5438 break;
5439 case EOpBreak:
5440 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5441 {
5442 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005443 }
5444 break;
5445 case EOpReturn:
5446 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5447 {
5448 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005449 }
5450 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005451 case EOpKill:
5452 if (mShaderType != GL_FRAGMENT_SHADER)
5453 {
5454 error(loc, "discard supported in fragment shaders only", "discard");
5455 }
5456 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005457 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005458 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005459 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005460 }
Olli Etuahocce89652017-06-19 16:04:09 +03005461 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005462}
5463
Jamie Madillb98c3a82015-07-23 14:26:04 -04005464TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005465 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005466 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005467{
Olli Etuahocce89652017-06-19 16:04:09 +03005468 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005469 {
Olli Etuahocce89652017-06-19 16:04:09 +03005470 ASSERT(op == EOpReturn);
5471 mFunctionReturnsValue = true;
5472 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5473 {
5474 error(loc, "void function cannot return a value", "return");
5475 }
5476 else if (*mCurrentFunctionType != expression->getType())
5477 {
5478 error(loc, "function return is not matching type:", "return");
5479 }
Olli Etuaho49300862015-02-20 14:54:49 +02005480 }
Olli Etuahocce89652017-06-19 16:04:09 +03005481 TIntermBranch *node = new TIntermBranch(op, expression);
5482 node->setLine(loc);
5483 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005484}
5485
Martin Radev84aa2dc2017-09-11 15:51:02 +03005486void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5487{
5488 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005489 const TString &name = functionCall->getFunction()->name();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005490 bool isTextureGather = (name == "textureGather");
5491 bool isTextureGatherOffset = (name == "textureGatherOffset");
5492 if (isTextureGather || isTextureGatherOffset)
5493 {
5494 TIntermNode *componentNode = nullptr;
5495 TIntermSequence *arguments = functionCall->getSequence();
5496 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5497 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5498 ASSERT(sampler != nullptr);
5499 switch (sampler->getBasicType())
5500 {
5501 case EbtSampler2D:
5502 case EbtISampler2D:
5503 case EbtUSampler2D:
5504 case EbtSampler2DArray:
5505 case EbtISampler2DArray:
5506 case EbtUSampler2DArray:
5507 if ((isTextureGather && arguments->size() == 3u) ||
5508 (isTextureGatherOffset && arguments->size() == 4u))
5509 {
5510 componentNode = arguments->back();
5511 }
5512 break;
5513 case EbtSamplerCube:
5514 case EbtISamplerCube:
5515 case EbtUSamplerCube:
5516 ASSERT(!isTextureGatherOffset);
5517 if (arguments->size() == 3u)
5518 {
5519 componentNode = arguments->back();
5520 }
5521 break;
5522 case EbtSampler2DShadow:
5523 case EbtSampler2DArrayShadow:
5524 case EbtSamplerCubeShadow:
5525 break;
5526 default:
5527 UNREACHABLE();
5528 break;
5529 }
5530 if (componentNode)
5531 {
5532 const TIntermConstantUnion *componentConstantUnion =
5533 componentNode->getAsConstantUnion();
5534 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5535 {
5536 error(functionCall->getLine(), "Texture component must be a constant expression",
5537 name.c_str());
5538 }
5539 else
5540 {
5541 int component = componentConstantUnion->getIConst(0);
5542 if (component < 0 || component > 3)
5543 {
5544 error(functionCall->getLine(), "Component must be in the range [0;3]",
5545 name.c_str());
5546 }
5547 }
5548 }
5549 }
5550}
5551
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005552void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5553{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005554 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005555 const TString &name = functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005556 TIntermNode *offset = nullptr;
5557 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005558 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005559 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5560 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5561 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005562 {
5563 offset = arguments->back();
5564 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005565 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005566 {
5567 // A bias parameter might follow the offset parameter.
5568 ASSERT(arguments->size() >= 3);
5569 offset = (*arguments)[2];
5570 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005571 else if (name == "textureGatherOffset")
5572 {
5573 ASSERT(arguments->size() >= 3u);
5574 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5575 ASSERT(sampler != nullptr);
5576 switch (sampler->getBasicType())
5577 {
5578 case EbtSampler2D:
5579 case EbtISampler2D:
5580 case EbtUSampler2D:
5581 case EbtSampler2DArray:
5582 case EbtISampler2DArray:
5583 case EbtUSampler2DArray:
5584 offset = (*arguments)[2];
5585 break;
5586 case EbtSampler2DShadow:
5587 case EbtSampler2DArrayShadow:
5588 offset = (*arguments)[3];
5589 break;
5590 default:
5591 UNREACHABLE();
5592 break;
5593 }
5594 useTextureGatherOffsetConstraints = true;
5595 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005596 if (offset != nullptr)
5597 {
5598 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5599 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5600 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005601 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005602 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005603 }
5604 else
5605 {
5606 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5607 size_t size = offsetConstantUnion->getType().getObjectSize();
5608 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005609 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5610 : mMinProgramTexelOffset;
5611 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5612 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005613 for (size_t i = 0u; i < size; ++i)
5614 {
5615 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005616 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005617 {
5618 std::stringstream tokenStream;
5619 tokenStream << offsetValue;
5620 std::string token = tokenStream.str();
5621 error(offset->getLine(), "Texture offset value out of valid range",
5622 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005623 }
5624 }
5625 }
5626 }
5627}
5628
Jiajia Qina3106c52017-11-03 09:39:39 +08005629void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5630{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005631 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005632 const TString &functionName = functionCall->getFunction()->name();
5633 if (IsAtomicBuiltin(functionName))
Jiajia Qina3106c52017-11-03 09:39:39 +08005634 {
5635 TIntermSequence *arguments = functionCall->getSequence();
5636 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5637
5638 if (IsBufferOrSharedVariable(memNode))
5639 {
5640 return;
5641 }
5642
5643 while (memNode->getAsBinaryNode())
5644 {
5645 memNode = memNode->getAsBinaryNode()->getLeft();
5646 if (IsBufferOrSharedVariable(memNode))
5647 {
5648 return;
5649 }
5650 }
5651
5652 error(memNode->getLine(),
5653 "The value passed to the mem argument of an atomic memory function does not "
5654 "correspond to a buffer or shared variable.",
Olli Etuahobed35d72017-12-20 16:36:26 +02005655 functionName.c_str());
Jiajia Qina3106c52017-11-03 09:39:39 +08005656 }
5657}
5658
Martin Radev2cc85b32016-08-05 16:22:53 +03005659// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5660void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5661{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005662 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005663 const TString &name = functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005664
5665 if (name.compare(0, 5, "image") == 0)
5666 {
5667 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005668 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005669
Olli Etuaho485eefd2017-02-14 17:40:06 +00005670 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005671
5672 if (name.compare(5, 5, "Store") == 0)
5673 {
5674 if (memoryQualifier.readonly)
5675 {
5676 error(imageNode->getLine(),
5677 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005678 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005679 }
5680 }
5681 else if (name.compare(5, 4, "Load") == 0)
5682 {
5683 if (memoryQualifier.writeonly)
5684 {
5685 error(imageNode->getLine(),
5686 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005687 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005688 }
5689 }
5690 }
5691}
5692
5693// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5694void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5695 const TFunction *functionDefinition,
5696 const TIntermAggregate *functionCall)
5697{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005698 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005699
5700 const TIntermSequence &arguments = *functionCall->getSequence();
5701
5702 ASSERT(functionDefinition->getParamCount() == arguments.size());
5703
5704 for (size_t i = 0; i < arguments.size(); ++i)
5705 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005706 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5707 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005708 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5709 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5710
5711 if (IsImage(functionArgumentType.getBasicType()))
5712 {
5713 const TMemoryQualifier &functionArgumentMemoryQualifier =
5714 functionArgumentType.getMemoryQualifier();
5715 const TMemoryQualifier &functionParameterMemoryQualifier =
5716 functionParameterType.getMemoryQualifier();
5717 if (functionArgumentMemoryQualifier.readonly &&
5718 !functionParameterMemoryQualifier.readonly)
5719 {
5720 error(functionCall->getLine(),
5721 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005722 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005723 }
5724
5725 if (functionArgumentMemoryQualifier.writeonly &&
5726 !functionParameterMemoryQualifier.writeonly)
5727 {
5728 error(functionCall->getLine(),
5729 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005730 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005731 }
Martin Radev049edfa2016-11-11 14:35:37 +02005732
5733 if (functionArgumentMemoryQualifier.coherent &&
5734 !functionParameterMemoryQualifier.coherent)
5735 {
5736 error(functionCall->getLine(),
5737 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005738 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005739 }
5740
5741 if (functionArgumentMemoryQualifier.volatileQualifier &&
5742 !functionParameterMemoryQualifier.volatileQualifier)
5743 {
5744 error(functionCall->getLine(),
5745 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005746 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005747 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005748 }
5749 }
5750}
5751
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005752TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005753{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005754 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005755}
5756
5757TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005758 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005759 TIntermNode *thisNode,
5760 const TSourceLoc &loc)
5761{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005762 if (thisNode != nullptr)
5763 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005764 return addMethod(fnCall->name(), arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005765 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005766
5767 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005768 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005769 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005770 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005771 }
5772 else
5773 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005774 ASSERT(op == EOpNull);
Olli Etuaho0c371002017-12-13 17:00:25 +04005775 return addNonConstructorFunctionCall(fnCall->name(), arguments, loc);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005776 }
5777}
5778
Olli Etuahobed35d72017-12-20 16:36:26 +02005779TIntermTyped *TParseContext::addMethod(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005780 TIntermSequence *arguments,
5781 TIntermNode *thisNode,
5782 const TSourceLoc &loc)
5783{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005784 TIntermTyped *typedThis = thisNode->getAsTyped();
5785 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5786 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5787 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005788 // So accessing fnCall->name() below is safe.
Olli Etuahobed35d72017-12-20 16:36:26 +02005789 if (name != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005790 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005791 error(loc, "invalid method", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005792 }
5793 else if (!arguments->empty())
5794 {
5795 error(loc, "method takes no parameters", "length");
5796 }
5797 else if (typedThis == nullptr || !typedThis->isArray())
5798 {
5799 error(loc, "length can only be called on arrays", "length");
5800 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005801 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5802 mGeometryShaderInputPrimitiveType == EptUndefined)
5803 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005804 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005805 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5806 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005807 else
5808 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005809 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5810 node->setLine(loc);
5811 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005812 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005813 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005814}
5815
Olli Etuahobed35d72017-12-20 16:36:26 +02005816TIntermTyped *TParseContext::addNonConstructorFunctionCall(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005817 TIntermSequence *arguments,
5818 const TSourceLoc &loc)
5819{
5820 // First find by unmangled name to check whether the function name has been
5821 // hidden by a variable name or struct typename.
5822 // If a function is found, check for one with a matching argument list.
5823 bool builtIn;
Olli Etuahobed35d72017-12-20 16:36:26 +02005824 const TSymbol *symbol = symbolTable.find(name, mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005825 if (symbol != nullptr && !symbol->isFunction())
5826 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005827 error(loc, "function name expected", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005828 }
5829 else
5830 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005831 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(name, *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005832 mShaderVersion, &builtIn);
5833 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005834 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005835 error(loc, "no matching overloaded function found", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005836 }
5837 else
5838 {
5839 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005840 //
5841 // A declared function.
5842 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005843 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005844 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005845 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005846 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005847 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005848 if (builtIn && op != EOpNull)
5849 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005850 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005851 if (fnCandidate->getParamCount() == 1)
5852 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005853 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005854 TIntermNode *unaryParamNode = arguments->front();
5855 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005856 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005857 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005858 }
5859 else
5860 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005861 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005862 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005863 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005864
5865 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005866 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305867
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005868 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305869 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005870 // See if we can constant fold a built-in. Note that this may be possible
5871 // even if it is not const-qualified.
5872 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305873 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005874 else
5875 {
5876 return callNode;
5877 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005878 }
5879 }
5880 else
5881 {
5882 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005883 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005884
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005885 // If builtIn == false, the function is user defined - could be an overloaded
5886 // built-in as well.
5887 // if builtIn == true, it's a builtIn function with no op associated with it.
5888 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005889 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005890 {
Olli Etuahofe486322017-03-21 09:30:54 +00005891 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005892 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005893 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005894 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005895 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005896 }
5897 else
5898 {
Olli Etuahofe486322017-03-21 09:30:54 +00005899 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005900 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005901 }
5902
Jiajia Qinbc585152017-06-23 15:42:17 +08005903 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005904
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005905 callNode->setLine(loc);
5906
5907 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005908 }
5909 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005910 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005911
5912 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005913 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005914}
5915
Jamie Madillb98c3a82015-07-23 14:26:04 -04005916TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005917 TIntermTyped *trueExpression,
5918 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005919 const TSourceLoc &loc)
5920{
Olli Etuaho56229f12017-07-10 14:16:33 +03005921 if (!checkIsScalarBool(loc, cond))
5922 {
5923 return falseExpression;
5924 }
Olli Etuaho52901742015-04-15 13:42:45 +03005925
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005926 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005927 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005928 std::stringstream reasonStream;
5929 reasonStream << "mismatching ternary operator operand types '"
5930 << trueExpression->getCompleteString() << " and '"
5931 << falseExpression->getCompleteString() << "'";
5932 std::string reason = reasonStream.str();
5933 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005934 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005935 }
Olli Etuahode318b22016-10-25 16:18:25 +01005936 if (IsOpaqueType(trueExpression->getBasicType()))
5937 {
5938 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005939 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005940 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5941 // Note that structs containing opaque types don't need to be checked as structs are
5942 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005943 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005944 return falseExpression;
5945 }
5946
Jiajia Qinbc585152017-06-23 15:42:17 +08005947 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5948 falseExpression->getMemoryQualifier().writeonly)
5949 {
5950 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5951 return falseExpression;
5952 }
5953
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005954 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005955 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005956 // ESSL 3.00.6 section 5.7:
5957 // Ternary operator support is optional for arrays. No certainty that it works across all
5958 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5959 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005960 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005961 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005962 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005963 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005964 }
Olli Etuaho94050052017-05-08 14:17:44 +03005965 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5966 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005967 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005968 return falseExpression;
5969 }
5970
Corentin Wallez0d959252016-07-12 17:26:32 -04005971 // WebGL2 section 5.26, the following results in an error:
5972 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005973 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005974 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005975 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005976 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005977 }
5978
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005979 // Note that the node resulting from here can be a constant union without being qualified as
5980 // constant.
5981 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5982 node->setLine(loc);
5983
5984 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005985}
Olli Etuaho49300862015-02-20 14:54:49 +02005986
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005987//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005988// Parse an array of strings using yyparse.
5989//
5990// Returns 0 for success.
5991//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005992int PaParseStrings(size_t count,
5993 const char *const string[],
5994 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305995 TParseContext *context)
5996{
Yunchao He4f285442017-04-21 12:15:49 +08005997 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005998 return 1;
5999
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006000 if (glslang_initialize(context))
6001 return 1;
6002
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006003 int error = glslang_scan(count, string, length, context);
6004 if (!error)
6005 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006006
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006007 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006008
alokp@chromium.org6b495712012-06-29 00:06:58 +00006009 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006010}
Jamie Madill45bcc782016-11-07 13:58:48 -05006011
6012} // namespace sh