blob: 5b92aceb02ee8600c7a84ca28e5e0995674af3b1 [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"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080017#include "compiler/translator/ValidateSwitch.h"
18#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030019#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000020
Jamie Madill45bcc782016-11-07 13:58:48 -050021namespace sh
22{
23
alokp@chromium.org8b851c62012-06-15 16:25:11 +000024///////////////////////////////////////////////////////////////////////
25//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026// Sub- vector and matrix fields
27//
28////////////////////////////////////////////////////////////////////////
29
Martin Radev2cc85b32016-08-05 16:22:53 +030030namespace
31{
32
33const int kWebGLMaxStructNesting = 4;
34
Jiajia Qina3106c52017-11-03 09:39:39 +080035const std::array<const char *, 8> kAtomicBuiltin = {{"atomicAdd", "atomicMin", "atomicMax",
36 "atomicAnd", "atomicOr", "atomicXor",
37 "atomicExchange", "atomicCompSwap"}};
38
39bool IsAtomicBuiltin(const TString &name)
40{
41 for (size_t i = 0; i < kAtomicBuiltin.size(); ++i)
42 {
43 if (name.compare(kAtomicBuiltin[i]) == 0)
44 {
45 return true;
46 }
47 }
48 return false;
49}
50
Olli Etuaho0f684632017-07-13 12:42:15 +030051bool ContainsSampler(const TStructure *structType);
52
Martin Radev2cc85b32016-08-05 16:22:53 +030053bool ContainsSampler(const TType &type)
54{
55 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030056 {
Martin Radev2cc85b32016-08-05 16:22:53 +030057 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030058 }
jchen10cc2a10e2017-05-03 14:05:12 +080059 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030060 {
Olli Etuaho0f684632017-07-13 12:42:15 +030061 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030062 }
63
64 return false;
65}
66
Olli Etuaho0f684632017-07-13 12:42:15 +030067bool ContainsSampler(const TStructure *structType)
68{
69 for (const auto &field : structType->fields())
70 {
71 if (ContainsSampler(*field->type()))
72 return true;
73 }
74 return false;
75}
76
Olli Etuaho485eefd2017-02-14 17:40:06 +000077// Get a token from an image argument to use as an error message token.
78const char *GetImageArgumentToken(TIntermTyped *imageNode)
79{
80 ASSERT(IsImage(imageNode->getBasicType()));
81 while (imageNode->getAsBinaryNode() &&
82 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
83 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
84 {
85 imageNode = imageNode->getAsBinaryNode()->getLeft();
86 }
87 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
88 if (imageSymbol)
89 {
90 return imageSymbol->getSymbol().c_str();
91 }
92 return "image";
93}
94
Olli Etuahocce89652017-06-19 16:04:09 +030095bool CanSetDefaultPrecisionOnType(const TPublicType &type)
96{
97 if (!SupportsPrecision(type.getBasicType()))
98 {
99 return false;
100 }
101 if (type.getBasicType() == EbtUInt)
102 {
103 // ESSL 3.00.4 section 4.5.4
104 return false;
105 }
106 if (type.isAggregate())
107 {
108 // Not allowed to set for aggregate types
109 return false;
110 }
111 return true;
112}
113
Jiawei Shaod8105a02017-08-08 09:54:36 +0800114// Map input primitive types to input array sizes in a geometry shader.
115GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
116{
117 switch (primitiveType)
118 {
119 case EptPoints:
120 return 1u;
121 case EptLines:
122 return 2u;
123 case EptTriangles:
124 return 3u;
125 case EptLinesAdjacency:
126 return 4u;
127 case EptTrianglesAdjacency:
128 return 6u;
129 default:
130 UNREACHABLE();
131 return 0u;
132 }
133}
134
Jiajia Qina3106c52017-11-03 09:39:39 +0800135bool IsBufferOrSharedVariable(TIntermTyped *var)
136{
137 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
138 var->getQualifier() == EvqShared)
139 {
140 return true;
141 }
142 return false;
143}
144
Martin Radev2cc85b32016-08-05 16:22:53 +0300145} // namespace
146
jchen104cdac9e2017-05-08 11:01:20 +0800147// This tracks each binding point's current default offset for inheritance of subsequent
148// variables using the same binding, and keeps offsets unique and non overlapping.
149// See GLSL ES 3.1, section 4.4.6.
150class TParseContext::AtomicCounterBindingState
151{
152 public:
153 AtomicCounterBindingState() : mDefaultOffset(0) {}
154 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
155 // newly inserted span.
156 int insertSpan(int start, size_t length)
157 {
158 gl::RangeI newSpan(start, start + static_cast<int>(length));
159 for (const auto &span : mSpans)
160 {
161 if (newSpan.intersects(span))
162 {
163 return -1;
164 }
165 }
166 mSpans.push_back(newSpan);
167 mDefaultOffset = newSpan.high();
168 return start;
169 }
170 // Inserts a new span starting from the default offset.
171 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
172 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
173
174 private:
175 int mDefaultOffset;
176 std::vector<gl::RangeI> mSpans;
177};
178
Jamie Madillacb4b812016-11-07 13:50:29 -0500179TParseContext::TParseContext(TSymbolTable &symt,
180 TExtensionBehavior &ext,
181 sh::GLenum type,
182 ShShaderSpec spec,
183 ShCompileOptions options,
184 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000185 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500186 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300187 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300188 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500189 mShaderType(type),
190 mShaderSpec(spec),
191 mCompileOptions(options),
192 mShaderVersion(100),
193 mTreeRoot(nullptr),
194 mLoopNestingLevel(0),
195 mStructNestingLevel(0),
196 mSwitchNestingLevel(0),
197 mCurrentFunctionType(nullptr),
198 mFunctionReturnsValue(false),
199 mChecksPrecisionErrors(checksPrecErrors),
200 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800201 mDefaultUniformMatrixPacking(EmpColumnMajor),
202 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
203 mDefaultBufferMatrixPacking(EmpColumnMajor),
204 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000205 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500206 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000207 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500208 mShaderVersion,
209 mShaderType,
210 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000211 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500212 mScanner(nullptr),
213 mUsesFragData(false),
214 mUsesFragColor(false),
215 mUsesSecondaryOutputs(false),
216 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
217 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300218 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
219 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500220 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500221 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000222 mNumViews(-1),
223 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000224 mMaxImageUnits(resources.MaxImageUnits),
225 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000226 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800227 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800228 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800229 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800230 mDeclaringFunction(false),
231 mGeometryShaderInputPrimitiveType(EptUndefined),
232 mGeometryShaderOutputPrimitiveType(EptUndefined),
233 mGeometryShaderInvocations(0),
234 mGeometryShaderMaxVertices(-1),
235 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800236 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800237 mGeometryShaderInputArraySize(0u)
Jamie Madillacb4b812016-11-07 13:50:29 -0500238{
Jamie Madillacb4b812016-11-07 13:50:29 -0500239}
240
jchen104cdac9e2017-05-08 11:01:20 +0800241TParseContext::~TParseContext()
242{
243}
244
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300245bool TParseContext::parseVectorFields(const TSourceLoc &line,
246 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400247 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300248 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300250 ASSERT(fieldOffsets);
251 size_t fieldCount = compString.size();
252 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530253 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000254 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000255 return false;
256 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300257 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 enum
260 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000261 exyzw,
262 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000263 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000264 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300266 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530267 {
268 switch (compString[i])
269 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400270 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300271 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400272 fieldSet[i] = exyzw;
273 break;
274 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300275 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400276 fieldSet[i] = ergba;
277 break;
278 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300279 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400280 fieldSet[i] = estpq;
281 break;
282 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300283 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400284 fieldSet[i] = exyzw;
285 break;
286 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300287 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400288 fieldSet[i] = ergba;
289 break;
290 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300291 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400292 fieldSet[i] = estpq;
293 break;
294 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300295 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400296 fieldSet[i] = exyzw;
297 break;
298 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300299 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400300 fieldSet[i] = ergba;
301 break;
302 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300303 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400304 fieldSet[i] = estpq;
305 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530306
Jamie Madillb98c3a82015-07-23 14:26:04 -0400307 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300308 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400309 fieldSet[i] = exyzw;
310 break;
311 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300312 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400313 fieldSet[i] = ergba;
314 break;
315 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300316 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400317 fieldSet[i] = estpq;
318 break;
319 default:
320 error(line, "illegal vector field selection", compString.c_str());
321 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000322 }
323 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300325 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530326 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300327 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530328 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400329 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 return false;
331 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332
Arun Patole7e7e68d2015-05-22 12:02:25 +0530333 if (i > 0)
334 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400335 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530336 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400337 error(line, "illegal - vector component fields not from the same set",
338 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000339 return false;
340 }
341 }
342 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000344 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345}
346
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347///////////////////////////////////////////////////////////////////////
348//
349// Errors
350//
351////////////////////////////////////////////////////////////////////////
352
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353//
354// Used by flex/bison to output all syntax and parsing errors.
355//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000356void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000358 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359}
360
Olli Etuaho4de340a2016-12-16 09:32:03 +0000361void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530362{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000363 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000364}
365
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200366void TParseContext::outOfRangeError(bool isError,
367 const TSourceLoc &loc,
368 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000369 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200370{
371 if (isError)
372 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000373 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200374 }
375 else
376 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000377 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200378 }
379}
380
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381//
382// Same error message for all places assignments don't work.
383//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530384void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000386 std::stringstream reasonStream;
387 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
388 std::string reason = reasonStream.str();
389 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390}
391
392//
393// Same error message for all places unary operations don't work.
394//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530395void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000397 std::stringstream reasonStream;
398 reasonStream << "wrong operand type - no operation '" << op
399 << "' exists that takes an operand of type " << operand
400 << " (or there is no acceptable conversion)";
401 std::string reason = reasonStream.str();
402 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403}
404
405//
406// Same error message for all binary operations don't work.
407//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400408void TParseContext::binaryOpError(const TSourceLoc &line,
409 const char *op,
410 TString left,
411 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000413 std::stringstream reasonStream;
414 reasonStream << "wrong operand types - no operation '" << op
415 << "' exists that takes a left-hand operand of type '" << left
416 << "' and a right operand of type '" << right
417 << "' (or there is no acceptable conversion)";
418 std::string reason = reasonStream.str();
419 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
Olli Etuaho856c4972016-08-08 11:38:39 +0300422void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
423 TPrecision precision,
424 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530425{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400426 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300427 return;
Martin Radev70866b82016-07-22 15:27:42 +0300428
429 if (precision != EbpUndefined && !SupportsPrecision(type))
430 {
431 error(line, "illegal type for precision qualifier", getBasicString(type));
432 }
433
Olli Etuaho183d7e22015-11-20 15:59:09 +0200434 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530435 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200436 switch (type)
437 {
438 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400439 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300440 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200441 case EbtInt:
442 case EbtUInt:
443 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400444 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300445 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200446 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800447 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200448 {
jchen10cc2a10e2017-05-03 14:05:12 +0800449 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300450 return;
451 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200452 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000453 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000454}
455
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456// Both test and if necessary, spit out an error, to see if the node is really
457// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300458bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500460 TIntermSymbol *symNode = node->getAsSymbolNode();
461 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100462 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
463
464 if (swizzleNode)
465 {
466 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
467 if (ok && swizzleNode->hasDuplicateOffsets())
468 {
469 error(line, " l-value of swizzle cannot have duplicate components", op);
470 return false;
471 }
472 return ok;
473 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474
Arun Patole7e7e68d2015-05-22 12:02:25 +0530475 if (binaryNode)
476 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400477 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530478 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400479 case EOpIndexDirect:
480 case EOpIndexIndirect:
481 case EOpIndexDirectStruct:
482 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300483 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400484 default:
485 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000486 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000487 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300488 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000489 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490
jchen10cc2a10e2017-05-03 14:05:12 +0800491 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530492 switch (node->getQualifier())
493 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400494 case EvqConst:
495 message = "can't modify a const";
496 break;
497 case EvqConstReadOnly:
498 message = "can't modify a const";
499 break;
500 case EvqAttribute:
501 message = "can't modify an attribute";
502 break;
503 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400504 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800505 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800506 case EvqFlatIn:
507 case EvqSmoothIn:
508 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400509 message = "can't modify an input";
510 break;
511 case EvqUniform:
512 message = "can't modify a uniform";
513 break;
514 case EvqVaryingIn:
515 message = "can't modify a varying";
516 break;
517 case EvqFragCoord:
518 message = "can't modify gl_FragCoord";
519 break;
520 case EvqFrontFacing:
521 message = "can't modify gl_FrontFacing";
522 break;
523 case EvqPointCoord:
524 message = "can't modify gl_PointCoord";
525 break;
Martin Radevb0883602016-08-04 17:48:58 +0300526 case EvqNumWorkGroups:
527 message = "can't modify gl_NumWorkGroups";
528 break;
529 case EvqWorkGroupSize:
530 message = "can't modify gl_WorkGroupSize";
531 break;
532 case EvqWorkGroupID:
533 message = "can't modify gl_WorkGroupID";
534 break;
535 case EvqLocalInvocationID:
536 message = "can't modify gl_LocalInvocationID";
537 break;
538 case EvqGlobalInvocationID:
539 message = "can't modify gl_GlobalInvocationID";
540 break;
541 case EvqLocalInvocationIndex:
542 message = "can't modify gl_LocalInvocationIndex";
543 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300544 case EvqViewIDOVR:
545 message = "can't modify gl_ViewID_OVR";
546 break;
Martin Radev802abe02016-08-04 17:48:32 +0300547 case EvqComputeIn:
548 message = "can't modify work group size variable";
549 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800550 case EvqPerVertexIn:
551 message = "can't modify any member in gl_in";
552 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800553 case EvqPrimitiveIDIn:
554 message = "can't modify gl_PrimitiveIDIn";
555 break;
556 case EvqInvocationID:
557 message = "can't modify gl_InvocationID";
558 break;
559 case EvqPrimitiveID:
560 if (mShaderType == GL_FRAGMENT_SHADER)
561 {
562 message = "can't modify gl_PrimitiveID in a fragment shader";
563 }
564 break;
565 case EvqLayer:
566 if (mShaderType == GL_FRAGMENT_SHADER)
567 {
568 message = "can't modify gl_Layer in a fragment shader";
569 }
570 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400571 default:
572 //
573 // Type that can't be written to?
574 //
575 if (node->getBasicType() == EbtVoid)
576 {
577 message = "can't modify void";
578 }
jchen10cc2a10e2017-05-03 14:05:12 +0800579 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400580 {
jchen10cc2a10e2017-05-03 14:05:12 +0800581 message = "can't modify a variable with type ";
582 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300583 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800584 else if (node->getMemoryQualifier().readonly)
585 {
586 message = "can't modify a readonly variable";
587 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000588 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000589
jchen10cc2a10e2017-05-03 14:05:12 +0800590 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530591 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000592 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000593
Olli Etuaho8a176262016-08-16 14:23:01 +0300594 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000597 //
598 // Everything else is okay, no error.
599 //
jchen10cc2a10e2017-05-03 14:05:12 +0800600 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300601 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000603 //
604 // If we get here, we have an error and a message.
605 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530606 if (symNode)
607 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000608 const char *symbol = symNode->getSymbol().c_str();
609 std::stringstream reasonStream;
610 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
611 std::string reason = reasonStream.str();
612 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000613 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530614 else
615 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000616 std::stringstream reasonStream;
617 reasonStream << "l-value required (" << message << ")";
618 std::string reason = reasonStream.str();
619 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000620 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000621
Olli Etuaho8a176262016-08-16 14:23:01 +0300622 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623}
624
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625// Both test, and if necessary spit out an error, to see if the node is really
626// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300627void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628{
Olli Etuaho383b7912016-08-05 11:22:59 +0300629 if (node->getQualifier() != EvqConst)
630 {
631 error(node->getLine(), "constant expression required", "");
632 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000633}
634
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635// Both test, and if necessary spit out an error, to see if the node is really
636// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300637void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000638{
Olli Etuaho383b7912016-08-05 11:22:59 +0300639 if (!node->isScalarInt())
640 {
641 error(node->getLine(), "integer expression required", token);
642 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643}
644
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645// Both test, and if necessary spit out an error, to see if we are currently
646// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800647bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648{
Olli Etuaho856c4972016-08-08 11:38:39 +0300649 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300650 {
651 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800652 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300653 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800654 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000655}
656
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300657// ESSL 3.00.5 sections 3.8 and 3.9.
658// If it starts "gl_" or contains two consecutive underscores, it's reserved.
659// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300660bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530662 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300663 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530664 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300665 error(line, reservedErrMsg, "gl_");
666 return false;
667 }
668 if (sh::IsWebGLBasedSpec(mShaderSpec))
669 {
670 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530671 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300672 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300673 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000674 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300675 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530676 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300677 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300678 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000679 }
680 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300681 if (identifier.find("__") != TString::npos)
682 {
683 error(line,
684 "identifiers containing two consecutive underscores (__) are reserved as "
685 "possible future keywords",
686 identifier.c_str());
687 return false;
688 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300689 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690}
691
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300692// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300693bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800694 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300695 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800697 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530698 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200699 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300700 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000701 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200702
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300703 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530704 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300705 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200706 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300707 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200708 {
jchen10cc2a10e2017-05-03 14:05:12 +0800709 std::string reason("cannot convert a variable with type ");
710 reason += getBasicString(argTyped->getBasicType());
711 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300712 return false;
713 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800714 else if (argTyped->getMemoryQualifier().writeonly)
715 {
716 error(line, "cannot convert a variable with writeonly", "constructor");
717 return false;
718 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200719 if (argTyped->getBasicType() == EbtVoid)
720 {
721 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300722 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200723 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725
Olli Etuaho856c4972016-08-08 11:38:39 +0300726 if (type.isArray())
727 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300728 // The size of an unsized constructor should already have been determined.
729 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300730 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300731 {
732 error(line, "array constructor needs one argument per array element", "constructor");
733 return false;
734 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300735 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
736 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800737 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300738 {
739 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300740 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500741 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300742 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500743 return false;
744 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300745 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300746 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000747 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300748 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300749 }
750 }
751 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300752 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300753 {
754 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300755 if (fields.size() != arguments->size())
756 {
757 error(line,
758 "Number of constructor parameters does not match the number of structure fields",
759 "constructor");
760 return false;
761 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300762
763 for (size_t i = 0; i < fields.size(); i++)
764 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800765 if (i >= arguments->size() ||
766 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300767 {
768 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000769 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300770 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300771 }
772 }
773 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300774 else
775 {
776 // We're constructing a scalar, vector, or matrix.
777
778 // Note: It's okay to have too many components available, but not okay to have unused
779 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
780 // there is an extra argument, so 'overFull' will become true.
781
782 size_t size = 0;
783 bool full = false;
784 bool overFull = false;
785 bool matrixArg = false;
786 for (TIntermNode *arg : *arguments)
787 {
788 const TIntermTyped *argTyped = arg->getAsTyped();
789 ASSERT(argTyped != nullptr);
790
Olli Etuaho487b63a2017-05-23 15:55:09 +0300791 if (argTyped->getBasicType() == EbtStruct)
792 {
793 error(line, "a struct cannot be used as a constructor argument for this type",
794 "constructor");
795 return false;
796 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300797 if (argTyped->getType().isArray())
798 {
799 error(line, "constructing from a non-dereferenced array", "constructor");
800 return false;
801 }
802 if (argTyped->getType().isMatrix())
803 {
804 matrixArg = true;
805 }
806
807 size += argTyped->getType().getObjectSize();
808 if (full)
809 {
810 overFull = true;
811 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300812 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300813 {
814 full = true;
815 }
816 }
817
818 if (type.isMatrix() && matrixArg)
819 {
820 if (arguments->size() != 1)
821 {
822 error(line, "constructing matrix from matrix can only take one argument",
823 "constructor");
824 return false;
825 }
826 }
827 else
828 {
829 if (size != 1 && size < type.getObjectSize())
830 {
831 error(line, "not enough data provided for construction", "constructor");
832 return false;
833 }
834 if (overFull)
835 {
836 error(line, "too many arguments", "constructor");
837 return false;
838 }
839 }
840 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300841
Olli Etuaho8a176262016-08-16 14:23:01 +0300842 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843}
844
Jamie Madillb98c3a82015-07-23 14:26:04 -0400845// This function checks to see if a void variable has been declared and raise an error message for
846// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847//
848// returns true in case of an error
849//
Olli Etuaho856c4972016-08-08 11:38:39 +0300850bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400851 const TString &identifier,
852 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300854 if (type == EbtVoid)
855 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000856 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300857 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300858 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859
Olli Etuaho8a176262016-08-16 14:23:01 +0300860 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
Jamie Madillb98c3a82015-07-23 14:26:04 -0400863// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300864// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300865bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300867 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530868 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000869 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300870 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530871 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300872 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873}
874
Jamie Madillb98c3a82015-07-23 14:26:04 -0400875// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300876// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300877void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878{
Martin Radev4a9cd802016-09-01 16:51:51 +0300879 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530880 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000881 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530882 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883}
884
jchen10cc2a10e2017-05-03 14:05:12 +0800885bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
886 const TTypeSpecifierNonArray &pType,
887 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530889 if (pType.type == EbtStruct)
890 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300891 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530892 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000893 std::stringstream reasonStream;
894 reasonStream << reason << " (structure contains a sampler)";
895 std::string reasonStr = reasonStream.str();
896 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300897 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000898 }
jchen10cc2a10e2017-05-03 14:05:12 +0800899 // only samplers need to be checked from structs, since other opaque types can't be struct
900 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300901 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530902 }
jchen10cc2a10e2017-05-03 14:05:12 +0800903 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530904 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000905 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300906 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000907 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908
Olli Etuaho8a176262016-08-16 14:23:01 +0300909 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910}
911
Olli Etuaho856c4972016-08-08 11:38:39 +0300912void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
913 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400914{
915 if (pType.layoutQualifier.location != -1)
916 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400917 error(line, "location must only be specified for a single input or output variable",
918 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400919 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400920}
921
Olli Etuaho856c4972016-08-08 11:38:39 +0300922void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
923 const TLayoutQualifier &layoutQualifier)
924{
925 if (layoutQualifier.location != -1)
926 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000927 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
928 if (mShaderVersion >= 310)
929 {
930 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800931 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000932 }
933 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300934 }
935}
936
Qin Jiajiaca68d982017-09-18 16:41:56 +0800937void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
938 const TLayoutBlockStorage &blockStorage,
939 const TQualifier &qualifier)
940{
941 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
942 {
943 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
944 }
945}
946
Martin Radev2cc85b32016-08-05 16:22:53 +0300947void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
948 TQualifier qualifier,
949 const TType &type)
950{
Martin Radev2cc85b32016-08-05 16:22:53 +0300951 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800952 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530953 {
jchen10cc2a10e2017-05-03 14:05:12 +0800954 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000955 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956}
957
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300959unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530961 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000962
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200963 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
964 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
965 // fold as array size.
966 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000967 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000968 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300969 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000970 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
Olli Etuaho856c4972016-08-08 11:38:39 +0300972 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400973
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000974 if (constant->getBasicType() == EbtUInt)
975 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300976 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000977 }
978 else
979 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300980 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000981
Olli Etuaho856c4972016-08-08 11:38:39 +0300982 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000983 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400984 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300985 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000986 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400987
Olli Etuaho856c4972016-08-08 11:38:39 +0300988 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400989 }
990
Olli Etuaho856c4972016-08-08 11:38:39 +0300991 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400992 {
993 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300994 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400995 }
996
997 // The size of arrays is restricted here to prevent issues further down the
998 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
999 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1000 const unsigned int sizeLimit = 65536;
1001
Olli Etuaho856c4972016-08-08 11:38:39 +03001002 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001003 {
1004 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001005 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001006 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001007
1008 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009}
1010
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001012bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1013 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014{
Olli Etuaho8a176262016-08-16 14:23:01 +03001015 if ((elementQualifier.qualifier == EvqAttribute) ||
1016 (elementQualifier.qualifier == EvqVertexIn) ||
1017 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001018 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001019 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001020 TType(elementQualifier).getQualifierString());
1021 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001022 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
Olli Etuaho8a176262016-08-16 14:23:01 +03001024 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025}
1026
Olli Etuaho8a176262016-08-16 14:23:01 +03001027// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001028bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1029 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001031 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001032 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001033 error(line, "cannot declare arrays of arrays",
1034 TType(elementType).getCompleteString().c_str());
1035 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001036 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001037 return true;
1038}
1039
1040// Check if this qualified element type can be formed into an array. This is only called when array
1041// brackets are associated with an identifier in a declaration, like this:
1042// float a[2];
1043// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1044// are associated with the type, like this:
1045// float[2] a;
1046bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1047 const TPublicType &elementType)
1048{
1049 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1050 {
1051 return false;
1052 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001053 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1054 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1055 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001056 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001057 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001058 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001059 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001060 TType(elementType).getCompleteString().c_str());
1061 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001062 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001063 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064}
1065
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001066// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001067void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1068 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001069 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070{
Olli Etuaho3739d232015-04-08 12:23:44 +03001071 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001072 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001073 {
1074 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001075 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001076
1077 // Generate informative error messages for ESSL1.
1078 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001079 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001080 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301081 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001082 "structures containing arrays may not be declared constant since they cannot be "
1083 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301084 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001085 }
1086 else
1087 {
1088 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1089 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001090 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001091 // This will make the type sized if it isn't sized yet.
1092 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1093 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094}
1095
Olli Etuaho2935c582015-04-08 14:32:06 +03001096// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097// and update the symbol table.
1098//
Olli Etuaho2935c582015-04-08 14:32:06 +03001099// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001101bool TParseContext::declareVariable(const TSourceLoc &line,
1102 const TString &identifier,
1103 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001104 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105{
Olli Etuaho2935c582015-04-08 14:32:06 +03001106 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107
Olli Etuaho43364892017-02-13 16:00:12 +00001108 checkBindingIsValid(line, type);
1109
Olli Etuaho856c4972016-08-08 11:38:39 +03001110 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111
Olli Etuaho2935c582015-04-08 14:32:06 +03001112 // gl_LastFragData may be redeclared with a new precision qualifier
1113 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1114 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001115 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1116 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001117 if (type.isArrayOfArrays())
1118 {
1119 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1120 identifier.c_str());
1121 return false;
1122 }
1123 else if (static_cast<int>(type.getOutermostArraySize()) ==
1124 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001125 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001126 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001127 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001128 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001129 }
1130 }
1131 else
1132 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001133 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1134 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001135 return false;
1136 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001137 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001138
Olli Etuaho8a176262016-08-16 14:23:01 +03001139 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001140 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141
Olli Etuaho0f684632017-07-13 12:42:15 +03001142 (*variable) = symbolTable.declareVariable(&identifier, type);
1143 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001144 {
1145 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001146 return false;
1147 }
1148
Olli Etuaho8a176262016-08-16 14:23:01 +03001149 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001150 return false;
1151
1152 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001153}
1154
Martin Radev70866b82016-07-22 15:27:42 +03001155void TParseContext::checkIsParameterQualifierValid(
1156 const TSourceLoc &line,
1157 const TTypeQualifierBuilder &typeQualifierBuilder,
1158 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301159{
Olli Etuahocce89652017-06-19 16:04:09 +03001160 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001161 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001162
1163 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301164 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001165 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1166 }
1167
1168 if (!IsImage(type->getBasicType()))
1169 {
Olli Etuaho43364892017-02-13 16:00:12 +00001170 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001171 }
1172 else
1173 {
1174 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001175 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001176
Martin Radev70866b82016-07-22 15:27:42 +03001177 type->setQualifier(typeQualifier.qualifier);
1178
1179 if (typeQualifier.precision != EbpUndefined)
1180 {
1181 type->setPrecision(typeQualifier.precision);
1182 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183}
1184
Olli Etuaho703671e2017-11-08 17:47:18 +02001185template <size_t size>
1186bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1187 const std::array<TExtension, size> &extensions)
1188{
1189 ASSERT(!extensions.empty());
1190 const TExtensionBehavior &extBehavior = extensionBehavior();
1191
1192 bool canUseWithWarning = false;
1193 bool canUseWithoutWarning = false;
1194
1195 const char *errorMsgString = "";
1196 TExtension errorMsgExtension = TExtension::UNDEFINED;
1197
1198 for (TExtension extension : extensions)
1199 {
1200 auto extIter = extBehavior.find(extension);
1201 if (canUseWithWarning)
1202 {
1203 // We already have an extension that we can use, but with a warning.
1204 // See if we can use the alternative extension without a warning.
1205 if (extIter == extBehavior.end())
1206 {
1207 continue;
1208 }
1209 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1210 {
1211 canUseWithoutWarning = true;
1212 break;
1213 }
1214 continue;
1215 }
1216 if (extIter == extBehavior.end())
1217 {
1218 errorMsgString = "extension is not supported";
1219 errorMsgExtension = extension;
1220 }
1221 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1222 {
1223 errorMsgString = "extension is disabled";
1224 errorMsgExtension = extension;
1225 }
1226 else if (extIter->second == EBhWarn)
1227 {
1228 errorMsgExtension = extension;
1229 canUseWithWarning = true;
1230 }
1231 else
1232 {
1233 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1234 canUseWithoutWarning = true;
1235 break;
1236 }
1237 }
1238
1239 if (canUseWithoutWarning)
1240 {
1241 return true;
1242 }
1243 if (canUseWithWarning)
1244 {
1245 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1246 return true;
1247 }
1248 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1249 return false;
1250}
1251
1252template bool TParseContext::checkCanUseOneOfExtensions(
1253 const TSourceLoc &line,
1254 const std::array<TExtension, 1> &extensions);
1255template bool TParseContext::checkCanUseOneOfExtensions(
1256 const TSourceLoc &line,
1257 const std::array<TExtension, 2> &extensions);
1258template bool TParseContext::checkCanUseOneOfExtensions(
1259 const TSourceLoc &line,
1260 const std::array<TExtension, 3> &extensions);
1261
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001262bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001263{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001264 ASSERT(extension != TExtension::UNDEFINED);
Jiawei Shao0e883132017-10-26 09:53:50 +08001265 ASSERT(extension != TExtension::EXT_geometry_shader);
Olli Etuaho703671e2017-11-08 17:47:18 +02001266 if (extension == TExtension::OES_geometry_shader)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301267 {
Olli Etuaho703671e2017-11-08 17:47:18 +02001268 // OES_geometry_shader and EXT_geometry_shader are always interchangeable.
1269 constexpr std::array<TExtension, 2u> extensions{
1270 {TExtension::EXT_geometry_shader, TExtension::OES_geometry_shader}};
1271 return checkCanUseOneOfExtensions(line, extensions);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001272 }
Corentin Wallez1d33c212017-11-13 10:21:39 -08001273 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274}
1275
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001276// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1277// compile-time or link-time errors are the same whether or not the declaration is empty".
1278// This function implements all the checks that are done on qualifiers regardless of if the
1279// declaration is empty.
1280void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1281 const sh::TLayoutQualifier &layoutQualifier,
1282 const TSourceLoc &location)
1283{
1284 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1285 {
1286 error(location, "Shared memory declarations cannot have layout specified", "layout");
1287 }
1288
1289 if (layoutQualifier.matrixPacking != EmpUnspecified)
1290 {
1291 error(location, "layout qualifier only valid for interface blocks",
1292 getMatrixPackingString(layoutQualifier.matrixPacking));
1293 return;
1294 }
1295
1296 if (layoutQualifier.blockStorage != EbsUnspecified)
1297 {
1298 error(location, "layout qualifier only valid for interface blocks",
1299 getBlockStorageString(layoutQualifier.blockStorage));
1300 return;
1301 }
1302
1303 if (qualifier == EvqFragmentOut)
1304 {
1305 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1306 {
1307 error(location, "invalid layout qualifier combination", "yuv");
1308 return;
1309 }
1310 }
1311 else
1312 {
1313 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1314 }
1315
Olli Etuaho95468d12017-05-04 11:14:34 +03001316 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1317 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001318 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1319 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001320 {
1321 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1322 }
1323
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001324 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001325 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001326 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001327 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001328 // We're not checking whether the uniform location is in range here since that depends on
1329 // the type of the variable.
1330 // The type can only be fully determined for non-empty declarations.
1331 }
1332 if (!canHaveLocation)
1333 {
1334 checkLocationIsNotSpecified(location, layoutQualifier);
1335 }
1336}
1337
jchen104cdac9e2017-05-08 11:01:20 +08001338void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1339 const TSourceLoc &location)
1340{
1341 if (publicType.precision != EbpHigh)
1342 {
1343 error(location, "Can only be highp", "atomic counter");
1344 }
1345 // dEQP enforces compile error if location is specified. See uniform_location.test.
1346 if (publicType.layoutQualifier.location != -1)
1347 {
1348 error(location, "location must not be set for atomic_uint", "layout");
1349 }
1350 if (publicType.layoutQualifier.binding == -1)
1351 {
1352 error(location, "no binding specified", "atomic counter");
1353 }
1354}
1355
Olli Etuaho55bde912017-10-25 13:41:13 +03001356void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001357{
Olli Etuaho55bde912017-10-25 13:41:13 +03001358 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001359 {
1360 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1361 // error. It is assumed that this applies to empty declarations as well.
1362 error(location, "empty array declaration needs to specify a size", "");
1363 }
Martin Radevb8b01222016-11-20 23:25:53 +02001364}
1365
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001366// These checks are done for all declarations that are non-empty. They're done for non-empty
1367// declarations starting a declarator list, and declarators that follow an empty declaration.
1368void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1369 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001370{
Olli Etuahofa33d582015-04-09 14:33:12 +03001371 switch (publicType.qualifier)
1372 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001373 case EvqVaryingIn:
1374 case EvqVaryingOut:
1375 case EvqAttribute:
1376 case EvqVertexIn:
1377 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001378 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001379 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001380 {
1381 error(identifierLocation, "cannot be used with a structure",
1382 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001383 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001384 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001385 break;
1386 case EvqBuffer:
1387 if (publicType.getBasicType() != EbtInterfaceBlock)
1388 {
1389 error(identifierLocation,
1390 "cannot declare buffer variables at global scope(outside a block)",
1391 getQualifierString(publicType.qualifier));
1392 return;
1393 }
1394 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001395 default:
1396 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001397 }
jchen10cc2a10e2017-05-03 14:05:12 +08001398 std::string reason(getBasicString(publicType.getBasicType()));
1399 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001400 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001401 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001402 {
1403 return;
1404 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001405
Andrei Volykhina5527072017-03-22 16:46:30 +03001406 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1407 publicType.qualifier != EvqConst) &&
1408 publicType.getBasicType() == EbtYuvCscStandardEXT)
1409 {
1410 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1411 getQualifierString(publicType.qualifier));
1412 return;
1413 }
1414
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001415 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1416 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001417 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1418 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001419 TType type(publicType);
1420 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001421 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001422 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1423 publicType.layoutQualifier);
1424 }
1425 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001426
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001427 // check for layout qualifier issues
1428 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001429
Martin Radev2cc85b32016-08-05 16:22:53 +03001430 if (IsImage(publicType.getBasicType()))
1431 {
1432
1433 switch (layoutQualifier.imageInternalFormat)
1434 {
1435 case EiifRGBA32F:
1436 case EiifRGBA16F:
1437 case EiifR32F:
1438 case EiifRGBA8:
1439 case EiifRGBA8_SNORM:
1440 if (!IsFloatImage(publicType.getBasicType()))
1441 {
1442 error(identifierLocation,
1443 "internal image format requires a floating image type",
1444 getBasicString(publicType.getBasicType()));
1445 return;
1446 }
1447 break;
1448 case EiifRGBA32I:
1449 case EiifRGBA16I:
1450 case EiifRGBA8I:
1451 case EiifR32I:
1452 if (!IsIntegerImage(publicType.getBasicType()))
1453 {
1454 error(identifierLocation,
1455 "internal image format requires an integer image type",
1456 getBasicString(publicType.getBasicType()));
1457 return;
1458 }
1459 break;
1460 case EiifRGBA32UI:
1461 case EiifRGBA16UI:
1462 case EiifRGBA8UI:
1463 case EiifR32UI:
1464 if (!IsUnsignedImage(publicType.getBasicType()))
1465 {
1466 error(identifierLocation,
1467 "internal image format requires an unsigned image type",
1468 getBasicString(publicType.getBasicType()));
1469 return;
1470 }
1471 break;
1472 case EiifUnspecified:
1473 error(identifierLocation, "layout qualifier", "No image internal format specified");
1474 return;
1475 default:
1476 error(identifierLocation, "layout qualifier", "unrecognized token");
1477 return;
1478 }
1479
1480 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1481 switch (layoutQualifier.imageInternalFormat)
1482 {
1483 case EiifR32F:
1484 case EiifR32I:
1485 case EiifR32UI:
1486 break;
1487 default:
1488 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1489 {
1490 error(identifierLocation, "layout qualifier",
1491 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1492 "image variables must be qualified readonly and/or writeonly");
1493 return;
1494 }
1495 break;
1496 }
1497 }
1498 else
1499 {
Olli Etuaho43364892017-02-13 16:00:12 +00001500 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001501 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1502 }
jchen104cdac9e2017-05-08 11:01:20 +08001503
1504 if (IsAtomicCounter(publicType.getBasicType()))
1505 {
1506 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1507 }
1508 else
1509 {
1510 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1511 }
Olli Etuaho43364892017-02-13 16:00:12 +00001512}
Martin Radev2cc85b32016-08-05 16:22:53 +03001513
Olli Etuaho43364892017-02-13 16:00:12 +00001514void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1515{
1516 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001517 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1518 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1519 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1520 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1521 // when it comes to which shaders are accepted by the compiler.
1522 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001523 if (IsImage(type.getBasicType()))
1524 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001525 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1526 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001527 }
1528 else if (IsSampler(type.getBasicType()))
1529 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001530 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1531 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001532 }
jchen104cdac9e2017-05-08 11:01:20 +08001533 else if (IsAtomicCounter(type.getBasicType()))
1534 {
1535 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1536 }
Olli Etuaho43364892017-02-13 16:00:12 +00001537 else
1538 {
1539 ASSERT(!IsOpaqueType(type.getBasicType()));
1540 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001541 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001542}
1543
Olli Etuaho856c4972016-08-08 11:38:39 +03001544void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1545 const TString &layoutQualifierName,
1546 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001547{
1548
1549 if (mShaderVersion < versionRequired)
1550 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001551 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001552 }
1553}
1554
Olli Etuaho856c4972016-08-08 11:38:39 +03001555bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1556 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001557{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001558 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001559 for (size_t i = 0u; i < localSize.size(); ++i)
1560 {
1561 if (localSize[i] != -1)
1562 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001563 error(location,
1564 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1565 "global layout declaration",
1566 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001567 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001568 }
1569 }
1570
Olli Etuaho8a176262016-08-16 14:23:01 +03001571 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001572}
1573
Olli Etuaho43364892017-02-13 16:00:12 +00001574void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001575 TLayoutImageInternalFormat internalFormat)
1576{
1577 if (internalFormat != EiifUnspecified)
1578 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001579 error(location, "invalid layout qualifier: only valid when used with images",
1580 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001581 }
Olli Etuaho43364892017-02-13 16:00:12 +00001582}
1583
1584void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1585{
1586 if (binding != -1)
1587 {
1588 error(location,
1589 "invalid layout qualifier: only valid when used with opaque types or blocks",
1590 "binding");
1591 }
1592}
1593
jchen104cdac9e2017-05-08 11:01:20 +08001594void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1595{
1596 if (offset != -1)
1597 {
1598 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1599 "offset");
1600 }
1601}
1602
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001603void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1604 int binding,
1605 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001606{
1607 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001608 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001609 {
1610 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1611 }
1612}
1613
1614void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1615 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001616 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001617{
1618 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001619 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001620 {
1621 error(location, "sampler binding greater than maximum texture units", "binding");
1622 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001623}
1624
Jiajia Qinbc585152017-06-23 15:42:17 +08001625void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1626 const TQualifier &qualifier,
1627 int binding,
1628 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001629{
1630 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001631 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001632 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001633 if (binding + size > mMaxUniformBufferBindings)
1634 {
1635 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1636 "binding");
1637 }
1638 }
1639 else if (qualifier == EvqBuffer)
1640 {
1641 if (binding + size > mMaxShaderStorageBufferBindings)
1642 {
1643 error(location,
1644 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1645 "binding");
1646 }
jchen10af713a22017-04-19 09:10:56 +08001647 }
1648}
jchen104cdac9e2017-05-08 11:01:20 +08001649void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1650{
1651 if (binding >= mMaxAtomicCounterBindings)
1652 {
1653 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1654 "binding");
1655 }
1656}
jchen10af713a22017-04-19 09:10:56 +08001657
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001658void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1659 int objectLocationCount,
1660 const TLayoutQualifier &layoutQualifier)
1661{
1662 int loc = layoutQualifier.location;
1663 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1664 {
1665 error(location, "Uniform location out of range", "location");
1666 }
1667}
1668
Andrei Volykhina5527072017-03-22 16:46:30 +03001669void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1670{
1671 if (yuv != false)
1672 {
1673 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1674 }
1675}
1676
Jiajia Qinbc585152017-06-23 15:42:17 +08001677void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1678 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001679{
1680 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1681 {
1682 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001683 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1684 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1685 qual == EvqInOut || qual == EvqConstReadOnly))
1686 {
1687 if (argument->getMemoryQualifier().writeonly)
1688 {
1689 error(argument->getLine(),
1690 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1691 fnCall->getFunctionSymbolInfo()->getName().c_str());
1692 return;
1693 }
1694 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001695 if (qual == EvqOut || qual == EvqInOut)
1696 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001697 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001698 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001699 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001700 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001701 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001702 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001703 }
1704 }
1705 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001706}
1707
Martin Radev70866b82016-07-22 15:27:42 +03001708void TParseContext::checkInvariantVariableQualifier(bool invariant,
1709 const TQualifier qualifier,
1710 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001711{
Martin Radev70866b82016-07-22 15:27:42 +03001712 if (!invariant)
1713 return;
1714
1715 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001716 {
Martin Radev70866b82016-07-22 15:27:42 +03001717 // input variables in the fragment shader can be also qualified as invariant
1718 if (!sh::CanBeInvariantESSL1(qualifier))
1719 {
1720 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1721 }
1722 }
1723 else
1724 {
1725 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1726 {
1727 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1728 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001729 }
1730}
1731
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001732bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001733{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001734 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001735}
1736
Jamie Madillb98c3a82015-07-23 14:26:04 -04001737void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1738 const char *extName,
1739 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001740{
1741 pp::SourceLocation srcLoc;
1742 srcLoc.file = loc.first_file;
1743 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001744 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001745}
1746
Jamie Madillb98c3a82015-07-23 14:26:04 -04001747void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1748 const char *name,
1749 const char *value,
1750 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001751{
1752 pp::SourceLocation srcLoc;
1753 srcLoc.file = loc.first_file;
1754 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001755 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001756}
1757
Martin Radev4c4c8e72016-08-04 12:25:34 +03001758sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001759{
Jamie Madill2f294c92017-11-20 14:47:26 -05001760 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001761 for (size_t i = 0u; i < result.size(); ++i)
1762 {
1763 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1764 {
1765 result[i] = 1;
1766 }
1767 else
1768 {
1769 result[i] = mComputeShaderLocalSize[i];
1770 }
1771 }
1772 return result;
1773}
1774
Olli Etuaho56229f12017-07-10 14:16:33 +03001775TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1776 const TSourceLoc &line)
1777{
1778 TIntermConstantUnion *node = new TIntermConstantUnion(
1779 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1780 node->setLine(line);
1781 return node;
1782}
1783
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784/////////////////////////////////////////////////////////////////////////////////
1785//
1786// Non-Errors.
1787//
1788/////////////////////////////////////////////////////////////////////////////////
1789
Jamie Madill5c097022014-08-20 16:38:32 -04001790const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1791 const TString *name,
1792 const TSymbol *symbol)
1793{
Jamie Madill5c097022014-08-20 16:38:32 -04001794 if (!symbol)
1795 {
1796 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001797 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001798 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001799
1800 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001801 {
1802 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001803 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001804 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001805
1806 const TVariable *variable = static_cast<const TVariable *>(symbol);
1807
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001808 if (variable->getExtension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001809 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001810 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001811 }
1812
Olli Etuaho0f684632017-07-13 12:42:15 +03001813 // Reject shaders using both gl_FragData and gl_FragColor
1814 TQualifier qualifier = variable->getType().getQualifier();
1815 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001816 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001817 mUsesFragData = true;
1818 }
1819 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1820 {
1821 mUsesFragColor = true;
1822 }
1823 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1824 {
1825 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001826 }
1827
Olli Etuaho0f684632017-07-13 12:42:15 +03001828 // This validation is not quite correct - it's only an error to write to
1829 // both FragData and FragColor. For simplicity, and because users shouldn't
1830 // be rewarded for reading from undefined varaibles, return an error
1831 // if they are both referenced, rather than assigned.
1832 if (mUsesFragData && mUsesFragColor)
1833 {
1834 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1835 if (mUsesSecondaryOutputs)
1836 {
1837 errorMessage =
1838 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1839 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1840 }
1841 error(location, errorMessage, name->c_str());
1842 }
1843
1844 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1845 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1846 qualifier == EvqWorkGroupSize)
1847 {
1848 error(location,
1849 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1850 "gl_WorkGroupSize");
1851 }
Jamie Madill5c097022014-08-20 16:38:32 -04001852 return variable;
1853}
1854
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001855TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1856 const TString *name,
1857 const TSymbol *symbol)
1858{
1859 const TVariable *variable = getNamedVariable(location, name, symbol);
1860
Olli Etuaho0f684632017-07-13 12:42:15 +03001861 if (!variable)
1862 {
1863 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1864 node->setLine(location);
1865 return node;
1866 }
1867
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001868 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001869 TIntermTyped *node = nullptr;
1870
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001871 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001872 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001873 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001874 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001875 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001876 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001877 {
1878 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1879 // needs to be added to the AST as a constant and not as a symbol.
1880 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1881 TConstantUnion *constArray = new TConstantUnion[3];
1882 for (size_t i = 0; i < 3; ++i)
1883 {
1884 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1885 }
1886
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001887 ASSERT(variableType.getBasicType() == EbtUInt);
1888 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001889
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001890 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001891 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001892 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001893 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001894 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1895 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001896 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001897 ASSERT(mGeometryShaderInputArraySize > 0u);
1898
1899 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho55bde912017-10-25 13:41:13 +03001900 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001901 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001902 else
1903 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001904 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001905 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001906 ASSERT(node != nullptr);
1907 node->setLine(location);
1908 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001909}
1910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911// Initializers show up in several places in the grammar. Have one set of
1912// code to handle them here.
1913//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001914// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001915bool TParseContext::executeInitializer(const TSourceLoc &line,
1916 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001917 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001918 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001919 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920{
Olli Etuaho13389b62016-10-16 11:48:18 +01001921 ASSERT(initNode != nullptr);
1922 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923
Olli Etuaho2935c582015-04-08 14:32:06 +03001924 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001925 if (type.isUnsizedArray())
1926 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001927 // In case initializer is not an array or type has more dimensions than initializer, this
1928 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1929 // actually is an array or not. Having a non-array initializer for an unsized array will
1930 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001931 auto *arraySizes = initializer->getType().getArraySizes();
1932 type.sizeUnsizedArrays(arraySizes);
Olli Etuaho376f1b52015-04-13 13:23:41 +03001933 }
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() &&
1941 !ValidateGlobalInitializer(initializer, this, &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 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002004 const TSymbol *symbol =
2005 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
2006 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002008 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002009 if (constArray)
2010 {
2011 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002012 ASSERT(*initNode == nullptr);
2013 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002014 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002015 }
2016 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002017
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002018 TIntermSymbol *intermSymbol =
2019 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
2020 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002021 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2022 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002023 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002024 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002025 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002026 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027
Olli Etuaho914b79a2017-06-19 16:03:19 +03002028 return true;
2029}
2030
2031TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2032 const TString &identifier,
2033 TIntermTyped *initializer,
2034 const TSourceLoc &loc)
2035{
2036 checkIsScalarBool(loc, pType);
2037 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002038 TType type(pType);
2039 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002040 {
2041 // The initializer is valid. The init condition needs to have a node - either the
2042 // initializer node, or a constant node in case the initialized variable is const and won't
2043 // be recorded in the AST.
2044 if (initNode == nullptr)
2045 {
2046 return initializer;
2047 }
2048 else
2049 {
2050 TIntermDeclaration *declaration = new TIntermDeclaration();
2051 declaration->appendDeclarator(initNode);
2052 return declaration;
2053 }
2054 }
2055 return nullptr;
2056}
2057
2058TIntermNode *TParseContext::addLoop(TLoopType type,
2059 TIntermNode *init,
2060 TIntermNode *cond,
2061 TIntermTyped *expr,
2062 TIntermNode *body,
2063 const TSourceLoc &line)
2064{
2065 TIntermNode *node = nullptr;
2066 TIntermTyped *typedCond = nullptr;
2067 if (cond)
2068 {
2069 typedCond = cond->getAsTyped();
2070 }
2071 if (cond == nullptr || typedCond)
2072 {
Olli Etuahocce89652017-06-19 16:04:09 +03002073 if (type == ELoopDoWhile)
2074 {
2075 checkIsScalarBool(line, typedCond);
2076 }
2077 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2078 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2079 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2080 !typedCond->isVector()));
2081
Olli Etuaho3ec75682017-07-05 17:02:55 +03002082 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002083 node->setLine(line);
2084 return node;
2085 }
2086
Olli Etuahocce89652017-06-19 16:04:09 +03002087 ASSERT(type != ELoopDoWhile);
2088
Olli Etuaho914b79a2017-06-19 16:03:19 +03002089 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2090 ASSERT(declaration);
2091 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2092 ASSERT(declarator->getLeft()->getAsSymbolNode());
2093
2094 // The condition is a declaration. In the AST representation we don't support declarations as
2095 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2096 // the loop.
2097 TIntermBlock *block = new TIntermBlock();
2098
2099 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2100 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2101 block->appendStatement(declareCondition);
2102
2103 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2104 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002105 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002106 block->appendStatement(loop);
2107 loop->setLine(line);
2108 block->setLine(line);
2109 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110}
2111
Olli Etuahocce89652017-06-19 16:04:09 +03002112TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2113 TIntermNodePair code,
2114 const TSourceLoc &loc)
2115{
Olli Etuaho56229f12017-07-10 14:16:33 +03002116 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002117
2118 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002119 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002120 {
2121 if (cond->getAsConstantUnion()->getBConst(0) == true)
2122 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002123 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002124 }
2125 else
2126 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002127 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002128 }
2129 }
2130
Olli Etuaho3ec75682017-07-05 17:02:55 +03002131 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002132 node->setLine(loc);
2133
2134 return node;
2135}
2136
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002137void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2138{
2139 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2140 typeSpecifier->getBasicType());
2141
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002142 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002143 {
2144 error(typeSpecifier->getLine(), "not supported", "first-class array");
2145 typeSpecifier->clearArrayness();
2146 }
2147}
2148
Martin Radev70866b82016-07-22 15:27:42 +03002149TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302150 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002151{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002152 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002153
Martin Radev70866b82016-07-22 15:27:42 +03002154 TPublicType returnType = typeSpecifier;
2155 returnType.qualifier = typeQualifier.qualifier;
2156 returnType.invariant = typeQualifier.invariant;
2157 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002158 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002159 returnType.precision = typeSpecifier.precision;
2160
2161 if (typeQualifier.precision != EbpUndefined)
2162 {
2163 returnType.precision = typeQualifier.precision;
2164 }
2165
Martin Radev4a9cd802016-09-01 16:51:51 +03002166 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2167 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002168
Martin Radev4a9cd802016-09-01 16:51:51 +03002169 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2170 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002171
Martin Radev4a9cd802016-09-01 16:51:51 +03002172 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002173
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002174 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002175 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002176 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002177 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002178 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002179 returnType.clearArrayness();
2180 }
2181
Martin Radev70866b82016-07-22 15:27:42 +03002182 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002183 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002184 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002185 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002186 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002187 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002188
Martin Radev70866b82016-07-22 15:27:42 +03002189 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002190 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002191 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002192 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002193 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002194 }
2195 }
2196 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002197 {
Martin Radev70866b82016-07-22 15:27:42 +03002198 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002199 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002200 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002201 }
Martin Radev70866b82016-07-22 15:27:42 +03002202 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2203 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002204 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002205 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2206 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002207 }
Martin Radev70866b82016-07-22 15:27:42 +03002208 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002209 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002210 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002211 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002212 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002213 }
2214
2215 return returnType;
2216}
2217
Olli Etuaho856c4972016-08-08 11:38:39 +03002218void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2219 const TPublicType &type,
2220 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002221{
2222 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002223 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002224 {
2225 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002226 }
2227
2228 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2229 switch (qualifier)
2230 {
2231 case EvqVertexIn:
2232 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002233 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002234 {
2235 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002236 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002237 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002238 return;
2239 case EvqFragmentOut:
2240 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002241 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002242 {
2243 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002244 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002245 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002246 return;
2247 default:
2248 break;
2249 }
2250
2251 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2252 // restrictions.
2253 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002254 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2255 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002256 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2257 {
2258 error(qualifierLocation, "must use 'flat' interpolation here",
2259 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002260 }
2261
Martin Radev4a9cd802016-09-01 16:51:51 +03002262 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002263 {
2264 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2265 // These restrictions are only implied by the ESSL 3.00 spec, but
2266 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002267 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002268 {
2269 error(qualifierLocation, "cannot be an array of structures",
2270 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002271 }
2272 if (type.isStructureContainingArrays())
2273 {
2274 error(qualifierLocation, "cannot be a structure containing an array",
2275 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002276 }
2277 if (type.isStructureContainingType(EbtStruct))
2278 {
2279 error(qualifierLocation, "cannot be a structure containing a structure",
2280 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002281 }
2282 if (type.isStructureContainingType(EbtBool))
2283 {
2284 error(qualifierLocation, "cannot be a structure containing a bool",
2285 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002286 }
2287 }
2288}
2289
Martin Radev2cc85b32016-08-05 16:22:53 +03002290void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2291{
2292 if (qualifier.getType() == QtStorage)
2293 {
2294 const TStorageQualifierWrapper &storageQualifier =
2295 static_cast<const TStorageQualifierWrapper &>(qualifier);
2296 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2297 !symbolTable.atGlobalLevel())
2298 {
2299 error(storageQualifier.getLine(),
2300 "Local variables can only use the const storage qualifier.",
2301 storageQualifier.getQualifierString().c_str());
2302 }
2303 }
2304}
2305
Olli Etuaho43364892017-02-13 16:00:12 +00002306void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002307 const TSourceLoc &location)
2308{
Jiajia Qinbc585152017-06-23 15:42:17 +08002309 const std::string reason(
2310 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2311 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002312 if (memoryQualifier.readonly)
2313 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002314 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002315 }
2316 if (memoryQualifier.writeonly)
2317 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002318 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002319 }
Martin Radev049edfa2016-11-11 14:35:37 +02002320 if (memoryQualifier.coherent)
2321 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002322 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002323 }
2324 if (memoryQualifier.restrictQualifier)
2325 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002326 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002327 }
2328 if (memoryQualifier.volatileQualifier)
2329 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002330 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002331 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002332}
2333
jchen104cdac9e2017-05-08 11:01:20 +08002334// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2335// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002336void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2337 const TSourceLoc &loc,
2338 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002339{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002340 if (!IsAtomicCounter(type->getBasicType()))
2341 {
2342 return;
2343 }
2344
2345 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2346 : kAtomicCounterSize;
2347 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2348 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002349 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002350 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002351 {
2352 offset = bindingState.appendSpan(size);
2353 }
2354 else
2355 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002356 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002357 }
2358 if (offset == -1)
2359 {
2360 error(loc, "Offset overlapping", "atomic counter");
2361 return;
2362 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002363 layoutQualifier.offset = offset;
2364 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002365}
2366
Olli Etuaho454c34c2017-10-25 16:35:56 +03002367void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2368 const char *token,
2369 TType *type)
2370{
2371 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2372 {
2373 if (type->isArray() && type->getOutermostArraySize() == 0u)
2374 {
2375 // Set size for the unsized geometry shader inputs if they are declared after a valid
2376 // input primitive declaration.
2377 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2378 {
2379 ASSERT(mGeometryShaderInputArraySize > 0u);
2380 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2381 }
2382 else
2383 {
2384 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2385 // An input can be declared without an array size if there is a previous layout
2386 // which specifies the size.
2387 error(location,
2388 "Missing a valid input primitive declaration before declaring an unsized "
2389 "array input",
2390 token);
2391 }
2392 }
2393 else if (type->isArray())
2394 {
2395 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2396 }
2397 else
2398 {
2399 error(location, "Geometry shader input variable must be declared as an array", token);
2400 }
2401 }
2402}
2403
Olli Etuaho13389b62016-10-16 11:48:18 +01002404TIntermDeclaration *TParseContext::parseSingleDeclaration(
2405 TPublicType &publicType,
2406 const TSourceLoc &identifierOrTypeLocation,
2407 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002408{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002409 TType type(publicType);
2410 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2411 mDirectiveHandler.pragma().stdgl.invariantAll)
2412 {
2413 TQualifier qualifier = type.getQualifier();
2414
2415 // The directive handler has already taken care of rejecting invalid uses of this pragma
2416 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2417 // affected variable declarations:
2418 //
2419 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2420 // elsewhere, in TranslatorGLSL.)
2421 //
2422 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2423 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2424 // the way this is currently implemented we have to enable this compiler option before
2425 // parsing the shader and determining the shading language version it uses. If this were
2426 // implemented as a post-pass, the workaround could be more targeted.
2427 //
2428 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2429 // the specification, but there are desktop OpenGL drivers that expect that this is the
2430 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2431 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2432 {
2433 type.setInvariant(true);
2434 }
2435 }
2436
Olli Etuaho454c34c2017-10-25 16:35:56 +03002437 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002438
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002439 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2440 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002441
Olli Etuahobab4c082015-04-24 16:38:49 +03002442 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002443 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002444
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002445 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002446 if (emptyDeclaration)
2447 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002448 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002449 // In most cases we don't need to create a symbol node for an empty declaration.
2450 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2451 if (type.getBasicType() == EbtStruct)
2452 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002453 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002454 }
jchen104cdac9e2017-05-08 11:01:20 +08002455 else if (IsAtomicCounter(publicType.getBasicType()))
2456 {
2457 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2458 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002459 }
2460 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002461 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002462 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002463
Olli Etuaho55bde912017-10-25 13:41:13 +03002464 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002465
Olli Etuaho55bc9052017-10-25 17:33:06 +03002466 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002467
Olli Etuaho2935c582015-04-08 14:32:06 +03002468 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002469 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002470
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002471 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002472 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002473 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002474 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002475 }
2476
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002477 TIntermDeclaration *declaration = new TIntermDeclaration();
2478 declaration->setLine(identifierOrTypeLocation);
2479 if (symbol)
2480 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002481 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002482 declaration->appendDeclarator(symbol);
2483 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002484 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002485}
2486
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002487TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2488 TPublicType &elementType,
2489 const TSourceLoc &identifierLocation,
2490 const TString &identifier,
2491 const TSourceLoc &indexLocation,
2492 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002493{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002494 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002495
Olli Etuaho55bde912017-10-25 13:41:13 +03002496 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002497 identifierLocation);
2498
Olli Etuaho55bde912017-10-25 13:41:13 +03002499 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002500
Olli Etuaho55bde912017-10-25 13:41:13 +03002501 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002502
Olli Etuaho55bde912017-10-25 13:41:13 +03002503 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002504 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002505
Olli Etuaho454c34c2017-10-25 16:35:56 +03002506 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002507
2508 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2509
Olli Etuaho55bc9052017-10-25 17:33:06 +03002510 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002511
Olli Etuaho2935c582015-04-08 14:32:06 +03002512 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002513 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002514
Olli Etuaho13389b62016-10-16 11:48:18 +01002515 TIntermDeclaration *declaration = new TIntermDeclaration();
2516 declaration->setLine(identifierLocation);
2517
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002518 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002519 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002520 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2521 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002522 declaration->appendDeclarator(symbol);
2523 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002524
Olli Etuaho13389b62016-10-16 11:48:18 +01002525 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002526}
2527
Olli Etuaho13389b62016-10-16 11:48:18 +01002528TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2529 const TSourceLoc &identifierLocation,
2530 const TString &identifier,
2531 const TSourceLoc &initLocation,
2532 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002533{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002534 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002535
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002536 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2537 identifierLocation);
2538
2539 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002540
Olli Etuaho13389b62016-10-16 11:48:18 +01002541 TIntermDeclaration *declaration = new TIntermDeclaration();
2542 declaration->setLine(identifierLocation);
2543
2544 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002545 TType type(publicType);
2546 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002547 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002548 if (initNode)
2549 {
2550 declaration->appendDeclarator(initNode);
2551 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002552 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002553 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002554}
2555
Olli Etuaho13389b62016-10-16 11:48:18 +01002556TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002557 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002558 const TSourceLoc &identifierLocation,
2559 const TString &identifier,
2560 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002561 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002562 const TSourceLoc &initLocation,
2563 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002564{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002565 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002566
Olli Etuaho55bde912017-10-25 13:41:13 +03002567 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002568 identifierLocation);
2569
Olli Etuaho55bde912017-10-25 13:41:13 +03002570 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002571
Olli Etuaho55bde912017-10-25 13:41:13 +03002572 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002573
Olli Etuaho55bde912017-10-25 13:41:13 +03002574 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002575 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002576
Olli Etuaho13389b62016-10-16 11:48:18 +01002577 TIntermDeclaration *declaration = new TIntermDeclaration();
2578 declaration->setLine(identifierLocation);
2579
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002580 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002581 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002582 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002583 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002584 if (initNode)
2585 {
2586 declaration->appendDeclarator(initNode);
2587 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002588 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002589
2590 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002591}
2592
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002593TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002594 const TTypeQualifierBuilder &typeQualifierBuilder,
2595 const TSourceLoc &identifierLoc,
2596 const TString *identifier,
2597 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002598{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002599 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002600
Martin Radev70866b82016-07-22 15:27:42 +03002601 if (!typeQualifier.invariant)
2602 {
2603 error(identifierLoc, "Expected invariant", identifier->c_str());
2604 return nullptr;
2605 }
2606 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2607 {
2608 return nullptr;
2609 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002610 if (!symbol)
2611 {
2612 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002613 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002614 }
Martin Radev70866b82016-07-22 15:27:42 +03002615 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002616 {
Martin Radev70866b82016-07-22 15:27:42 +03002617 error(identifierLoc, "invariant declaration specifies qualifier",
2618 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002619 }
Martin Radev70866b82016-07-22 15:27:42 +03002620 if (typeQualifier.precision != EbpUndefined)
2621 {
2622 error(identifierLoc, "invariant declaration specifies precision",
2623 getPrecisionString(typeQualifier.precision));
2624 }
2625 if (!typeQualifier.layoutQualifier.isEmpty())
2626 {
2627 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2628 }
2629
2630 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002631 if (!variable)
2632 {
2633 return nullptr;
2634 }
Martin Radev70866b82016-07-22 15:27:42 +03002635 const TType &type = variable->getType();
2636
2637 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2638 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002639 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002640
2641 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2642
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002643 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2644 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002645
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002646 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002647}
2648
Olli Etuaho13389b62016-10-16 11:48:18 +01002649void TParseContext::parseDeclarator(TPublicType &publicType,
2650 const TSourceLoc &identifierLocation,
2651 const TString &identifier,
2652 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002653{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002654 // If the declaration starting this declarator list was empty (example: int,), some checks were
2655 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002656 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002657 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002658 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2659 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002660 }
2661
Olli Etuaho856c4972016-08-08 11:38:39 +03002662 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002663
Olli Etuaho2935c582015-04-08 14:32:06 +03002664 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002665 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002666
2667 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2668
Olli Etuaho55bde912017-10-25 13:41:13 +03002669 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2670
Olli Etuaho55bc9052017-10-25 17:33:06 +03002671 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2672
Olli Etuaho43364892017-02-13 16:00:12 +00002673 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002674
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002675 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002676 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002677 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2678 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002679 declarationOut->appendDeclarator(symbol);
2680 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002681}
2682
Olli Etuaho55bde912017-10-25 13:41:13 +03002683void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002684 const TSourceLoc &identifierLocation,
2685 const TString &identifier,
2686 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002687 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002688 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002689{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002690 // If the declaration starting this declarator list was empty (example: int,), some checks were
2691 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002692 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002693 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002694 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002695 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002696 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002697
Olli Etuaho55bde912017-10-25 13:41:13 +03002698 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002699
Olli Etuaho55bde912017-10-25 13:41:13 +03002700 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002701 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002702 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002703 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002704
Olli Etuaho454c34c2017-10-25 16:35:56 +03002705 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2706
Olli Etuaho55bde912017-10-25 13:41:13 +03002707 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2708
Olli Etuaho55bc9052017-10-25 17:33:06 +03002709 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002710
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002711 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002712 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002713
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002714 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002715 {
2716 TIntermSymbol *symbol =
2717 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2718 symbol->setLine(identifierLocation);
2719 declarationOut->appendDeclarator(symbol);
2720 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002721 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002722}
2723
Olli Etuaho13389b62016-10-16 11:48:18 +01002724void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2725 const TSourceLoc &identifierLocation,
2726 const TString &identifier,
2727 const TSourceLoc &initLocation,
2728 TIntermTyped *initializer,
2729 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002730{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002731 // If the declaration starting this declarator list was empty (example: int,), some checks were
2732 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002733 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002734 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002735 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2736 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002737 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002738
Olli Etuaho856c4972016-08-08 11:38:39 +03002739 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002740
Olli Etuaho13389b62016-10-16 11:48:18 +01002741 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002742 TType type(publicType);
2743 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002744 {
2745 //
2746 // build the intermediate representation
2747 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002748 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002749 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002750 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002751 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002752 }
2753}
2754
Olli Etuaho55bde912017-10-25 13:41:13 +03002755void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002756 const TSourceLoc &identifierLocation,
2757 const TString &identifier,
2758 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002759 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002760 const TSourceLoc &initLocation,
2761 TIntermTyped *initializer,
2762 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002763{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002764 // If the declaration starting this declarator list was empty (example: int,), some checks were
2765 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002766 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002767 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002768 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002769 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002770 }
2771
Olli Etuaho55bde912017-10-25 13:41:13 +03002772 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002773
Olli Etuaho55bde912017-10-25 13:41:13 +03002774 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002775
Olli Etuaho55bde912017-10-25 13:41:13 +03002776 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002777 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002778
2779 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002780 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002781 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002782 {
2783 if (initNode)
2784 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002785 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002786 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002787 }
2788}
2789
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002790TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2791{
2792 // It's simpler to parse an empty statement as a constant expression rather than having a
2793 // different type of node just for empty statements, that will be pruned from the AST anyway.
2794 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2795 node->setLine(location);
2796 return node;
2797}
2798
jchen104cdac9e2017-05-08 11:01:20 +08002799void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2800 const TSourceLoc &location)
2801{
2802 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2803 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2804 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2805 {
2806 error(location, "Requires both binding and offset", "layout");
2807 return;
2808 }
2809 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2810}
2811
Olli Etuahocce89652017-06-19 16:04:09 +03002812void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2813 const TPublicType &type,
2814 const TSourceLoc &loc)
2815{
2816 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2817 !getFragmentPrecisionHigh())
2818 {
2819 error(loc, "precision is not supported in fragment shader", "highp");
2820 }
2821
2822 if (!CanSetDefaultPrecisionOnType(type))
2823 {
2824 error(loc, "illegal type argument for default precision qualifier",
2825 getBasicString(type.getBasicType()));
2826 return;
2827 }
2828 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2829}
2830
Shaob5cc1192017-07-06 10:47:20 +08002831bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2832{
2833 switch (typeQualifier.layoutQualifier.primitiveType)
2834 {
2835 case EptLines:
2836 case EptLinesAdjacency:
2837 case EptTriangles:
2838 case EptTrianglesAdjacency:
2839 return typeQualifier.qualifier == EvqGeometryIn;
2840
2841 case EptLineStrip:
2842 case EptTriangleStrip:
2843 return typeQualifier.qualifier == EvqGeometryOut;
2844
2845 case EptPoints:
2846 return true;
2847
2848 default:
2849 UNREACHABLE();
2850 return false;
2851 }
2852}
2853
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002854void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2855 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002856{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002857 if (mGeometryShaderInputArraySize == 0u)
2858 {
2859 mGeometryShaderInputArraySize = inputArraySize;
2860 }
2861 else if (mGeometryShaderInputArraySize != inputArraySize)
2862 {
2863 error(line,
2864 "Array size or input primitive declaration doesn't match the size of earlier sized "
2865 "array inputs.",
2866 "layout");
2867 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002868}
2869
Shaob5cc1192017-07-06 10:47:20 +08002870bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2871{
2872 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2873
2874 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2875
2876 if (layoutQualifier.maxVertices != -1)
2877 {
2878 error(typeQualifier.line,
2879 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2880 return false;
2881 }
2882
2883 // Set mGeometryInputPrimitiveType if exists
2884 if (layoutQualifier.primitiveType != EptUndefined)
2885 {
2886 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2887 {
2888 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2889 return false;
2890 }
2891
2892 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2893 {
2894 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002895 setGeometryShaderInputArraySize(
2896 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2897 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002898 }
2899 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2900 {
2901 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2902 "layout");
2903 return false;
2904 }
2905 }
2906
2907 // Set mGeometryInvocations if exists
2908 if (layoutQualifier.invocations > 0)
2909 {
2910 if (mGeometryShaderInvocations == 0)
2911 {
2912 mGeometryShaderInvocations = layoutQualifier.invocations;
2913 }
2914 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2915 {
2916 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2917 "layout");
2918 return false;
2919 }
2920 }
2921
2922 return true;
2923}
2924
2925bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2926{
2927 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2928
2929 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2930
2931 if (layoutQualifier.invocations > 0)
2932 {
2933 error(typeQualifier.line,
2934 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2935 return false;
2936 }
2937
2938 // Set mGeometryOutputPrimitiveType if exists
2939 if (layoutQualifier.primitiveType != EptUndefined)
2940 {
2941 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2942 {
2943 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2944 return false;
2945 }
2946
2947 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2948 {
2949 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2950 }
2951 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2952 {
2953 error(typeQualifier.line,
2954 "primitive doesn't match earlier output primitive declaration", "layout");
2955 return false;
2956 }
2957 }
2958
2959 // Set mGeometryMaxVertices if exists
2960 if (layoutQualifier.maxVertices > -1)
2961 {
2962 if (mGeometryShaderMaxVertices == -1)
2963 {
2964 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2965 }
2966 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2967 {
2968 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2969 "layout");
2970 return false;
2971 }
2972 }
2973
2974 return true;
2975}
2976
Martin Radev70866b82016-07-22 15:27:42 +03002977void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002978{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002979 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002980 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002981
Martin Radev70866b82016-07-22 15:27:42 +03002982 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2983 typeQualifier.line);
2984
Jamie Madillc2128ff2016-07-04 10:26:17 -04002985 // It should never be the case, but some strange parser errors can send us here.
2986 if (layoutQualifier.isEmpty())
2987 {
2988 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002989 return;
2990 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002991
Martin Radev802abe02016-08-04 17:48:32 +03002992 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002993 {
Olli Etuaho43364892017-02-13 16:00:12 +00002994 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002995 return;
2996 }
2997
Olli Etuaho43364892017-02-13 16:00:12 +00002998 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2999
3000 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003001
3002 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3003
Andrei Volykhina5527072017-03-22 16:46:30 +03003004 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3005
jchen104cdac9e2017-05-08 11:01:20 +08003006 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3007
Qin Jiajiaca68d982017-09-18 16:41:56 +08003008 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3009 typeQualifier.qualifier);
3010
Martin Radev802abe02016-08-04 17:48:32 +03003011 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003012 {
Martin Radev802abe02016-08-04 17:48:32 +03003013 if (mComputeShaderLocalSizeDeclared &&
3014 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3015 {
3016 error(typeQualifier.line, "Work group size does not match the previous declaration",
3017 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003018 return;
3019 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003020
Martin Radev802abe02016-08-04 17:48:32 +03003021 if (mShaderVersion < 310)
3022 {
3023 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003024 return;
3025 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003026
Martin Radev4c4c8e72016-08-04 12:25:34 +03003027 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003028 {
3029 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003030 return;
3031 }
3032
3033 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3034 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3035
3036 const TConstantUnion *maxComputeWorkGroupSizeData =
3037 maxComputeWorkGroupSize->getConstPointer();
3038
3039 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3040 {
3041 if (layoutQualifier.localSize[i] != -1)
3042 {
3043 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3044 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3045 if (mComputeShaderLocalSize[i] < 1 ||
3046 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3047 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003048 std::stringstream reasonStream;
3049 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3050 << maxComputeWorkGroupSizeValue;
3051 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003052
Olli Etuaho4de340a2016-12-16 09:32:03 +00003053 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003054 return;
3055 }
3056 }
3057 }
3058
3059 mComputeShaderLocalSizeDeclared = true;
3060 }
Shaob5cc1192017-07-06 10:47:20 +08003061 else if (typeQualifier.qualifier == EvqGeometryIn)
3062 {
3063 if (mShaderVersion < 310)
3064 {
3065 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3066 return;
3067 }
3068
3069 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3070 {
3071 return;
3072 }
3073 }
3074 else if (typeQualifier.qualifier == EvqGeometryOut)
3075 {
3076 if (mShaderVersion < 310)
3077 {
3078 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3079 "layout");
3080 return;
3081 }
3082
3083 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3084 {
3085 return;
3086 }
3087 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003088 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3089 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003090 {
3091 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3092 // specification.
3093 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3094 {
3095 error(typeQualifier.line, "Number of views does not match the previous declaration",
3096 "layout");
3097 return;
3098 }
3099
3100 if (layoutQualifier.numViews == -1)
3101 {
3102 error(typeQualifier.line, "No num_views specified", "layout");
3103 return;
3104 }
3105
3106 if (layoutQualifier.numViews > mMaxNumViews)
3107 {
3108 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3109 "layout");
3110 return;
3111 }
3112
3113 mNumViews = layoutQualifier.numViews;
3114 }
Martin Radev802abe02016-08-04 17:48:32 +03003115 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003116 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003117 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003118 {
Martin Radev802abe02016-08-04 17:48:32 +03003119 return;
3120 }
3121
Jiajia Qinbc585152017-06-23 15:42:17 +08003122 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003123 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003124 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003125 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003126 return;
3127 }
3128
3129 if (mShaderVersion < 300)
3130 {
3131 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3132 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003133 return;
3134 }
3135
Olli Etuaho09b04a22016-12-15 13:30:26 +00003136 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003137
3138 if (layoutQualifier.matrixPacking != EmpUnspecified)
3139 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003140 if (typeQualifier.qualifier == EvqUniform)
3141 {
3142 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3143 }
3144 else if (typeQualifier.qualifier == EvqBuffer)
3145 {
3146 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3147 }
Martin Radev802abe02016-08-04 17:48:32 +03003148 }
3149
3150 if (layoutQualifier.blockStorage != EbsUnspecified)
3151 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003152 if (typeQualifier.qualifier == EvqUniform)
3153 {
3154 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3155 }
3156 else if (typeQualifier.qualifier == EvqBuffer)
3157 {
3158 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3159 }
Martin Radev802abe02016-08-04 17:48:32 +03003160 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003161 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003162}
3163
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003164TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3165 const TFunction &function,
3166 const TSourceLoc &location,
3167 bool insertParametersToSymbolTable)
3168{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003169 checkIsNotReserved(location, function.getName());
3170
Olli Etuahofe486322017-03-21 09:30:54 +00003171 TIntermFunctionPrototype *prototype =
3172 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003173 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3174 // point to the data that already exists in the symbol table.
3175 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3176 prototype->setLine(location);
3177
3178 for (size_t i = 0; i < function.getParamCount(); i++)
3179 {
3180 const TConstParameter &param = function.getParam(i);
3181
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003182 TIntermSymbol *symbol = nullptr;
3183
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003184 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3185 // be used for unused args).
3186 if (param.name != nullptr)
3187 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003188 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003189 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003190 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003191 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3192 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003193 {
3194 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3195 variable->getType());
3196 }
3197 else
3198 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003199 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003200 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003201 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003202 // Unsized type of a named parameter should have already been checked and sanitized.
3203 ASSERT(!param.type->isUnsizedArray());
3204 }
3205 else
3206 {
3207 if (param.type->isUnsizedArray())
3208 {
3209 error(location, "function parameter array must be sized at compile time", "[]");
3210 // We don't need to size the arrays since the parameter is unnamed and hence
3211 // inaccessible.
3212 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003213 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003214 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003215 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003216 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3217 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003218 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003219 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003220 symbol->setLine(location);
3221 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003222 }
3223 return prototype;
3224}
3225
Olli Etuaho16c745a2017-01-16 17:02:27 +00003226TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3227 const TFunction &parsedFunction,
3228 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003229{
Olli Etuaho476197f2016-10-11 13:59:08 +01003230 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3231 // first declaration. Either way the instance in the symbol table is used to track whether the
3232 // function is declared multiple times.
3233 TFunction *function = static_cast<TFunction *>(
3234 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3235 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003236 {
3237 // ESSL 1.00.17 section 4.2.7.
3238 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3239 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003240 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003241 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003242
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003243 TIntermFunctionPrototype *prototype =
3244 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003245
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003246 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003247
3248 if (!symbolTable.atGlobalLevel())
3249 {
3250 // ESSL 3.00.4 section 4.2.4.
3251 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003252 }
3253
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003254 return prototype;
3255}
3256
Olli Etuaho336b1472016-10-05 16:37:55 +01003257TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003258 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003259 TIntermBlock *functionBody,
3260 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003261{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003262 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003263 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3264 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003265 error(location, "function does not return a value:",
3266 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003267 }
3268
Olli Etuahof51fdd22016-10-03 10:03:40 +01003269 if (functionBody == nullptr)
3270 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003271 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003272 functionBody->setLine(location);
3273 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003274 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003275 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003276 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003277
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003278 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003279 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003280}
3281
Olli Etuaho476197f2016-10-11 13:59:08 +01003282void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3283 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003284 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003285{
Olli Etuaho476197f2016-10-11 13:59:08 +01003286 ASSERT(function);
3287 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003288 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003289 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003290
3291 if (builtIn)
3292 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003293 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003294 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003295 else
Jamie Madill185fb402015-06-12 15:48:48 -04003296 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003297 TFunction *prevDec = static_cast<TFunction *>(
3298 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3299
3300 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3301 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3302 // occurance.
3303 if (*function != prevDec)
3304 {
3305 // Swap the parameters of the previous declaration to the parameters of the function
3306 // definition (parameter names may differ).
3307 prevDec->swapParameters(**function);
3308
3309 // The function definition will share the same symbol as any previous declaration.
3310 *function = prevDec;
3311 }
3312
3313 if ((*function)->isDefined())
3314 {
3315 error(location, "function already has a body", (*function)->getName().c_str());
3316 }
3317
3318 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003319 }
Jamie Madill185fb402015-06-12 15:48:48 -04003320
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003321 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003322 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003323 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003324
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003325 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003326 setLoopNestingLevel(0);
3327}
3328
Jamie Madillb98c3a82015-07-23 14:26:04 -04003329TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003330{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003331 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003332 // We don't know at this point whether this is a function definition or a prototype.
3333 // The definition production code will check for redefinitions.
3334 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003335 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003336 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3337 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003338 //
3339 TFunction *prevDec =
3340 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303341
Olli Etuahod80f2942017-11-06 12:44:45 +02003342 for (size_t i = 0u; i < function->getParamCount(); ++i)
3343 {
3344 auto &param = function->getParam(i);
3345 if (param.type->isStructSpecifier())
3346 {
3347 // ESSL 3.00.6 section 12.10.
3348 error(location, "Function parameter type cannot be a structure definition",
Olli Etuaho7af63722017-11-13 15:03:40 +02003349 function->getName().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003350 }
3351 }
3352
Martin Radevda6254b2016-12-14 17:00:36 +02003353 if (getShaderVersion() >= 300 &&
3354 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3355 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303356 {
Martin Radevda6254b2016-12-14 17:00:36 +02003357 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303358 // Therefore overloading or redefining builtin functions is an error.
3359 error(location, "Name of a built-in function cannot be redeclared as function",
3360 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303361 }
3362 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003363 {
3364 if (prevDec->getReturnType() != function->getReturnType())
3365 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003366 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003367 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003368 }
3369 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3370 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003371 if (prevDec->getParam(i).type->getQualifier() !=
3372 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003373 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003374 error(location,
3375 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003376 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003377 }
3378 }
3379 }
3380
3381 //
3382 // Check for previously declared variables using the same name.
3383 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003384 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003385 if (prevSym)
3386 {
3387 if (!prevSym->isFunction())
3388 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003389 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003390 }
3391 }
3392 else
3393 {
3394 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003395 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003396 }
3397
3398 // We're at the inner scope level of the function's arguments and body statement.
3399 // Add the function prototype to the surrounding scope instead.
3400 symbolTable.getOuterLevel()->insert(function);
3401
Olli Etuaho78d13742017-01-18 13:06:10 +00003402 // Raise error message if main function takes any parameters or return anything other than void
3403 if (function->getName() == "main")
3404 {
3405 if (function->getParamCount() > 0)
3406 {
3407 error(location, "function cannot take any parameter(s)", "main");
3408 }
3409 if (function->getReturnType().getBasicType() != EbtVoid)
3410 {
3411 error(location, "main function cannot return a value",
3412 function->getReturnType().getBasicString());
3413 }
3414 }
3415
Jamie Madill185fb402015-06-12 15:48:48 -04003416 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003417 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3418 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003419 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3420 //
3421 return function;
3422}
3423
Olli Etuaho9de84a52016-06-14 17:36:01 +03003424TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3425 const TString *name,
3426 const TSourceLoc &location)
3427{
3428 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3429 {
3430 error(location, "no qualifiers allowed for function return",
3431 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003432 }
3433 if (!type.layoutQualifier.isEmpty())
3434 {
3435 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003436 }
jchen10cc2a10e2017-05-03 14:05:12 +08003437 // make sure an opaque type is not involved as well...
3438 std::string reason(getBasicString(type.getBasicType()));
3439 reason += "s can't be function return values";
3440 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003441 if (mShaderVersion < 300)
3442 {
3443 // Array return values are forbidden, but there's also no valid syntax for declaring array
3444 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003445 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003446
3447 if (type.isStructureContainingArrays())
3448 {
3449 // ESSL 1.00.17 section 6.1 Function Definitions
3450 error(location, "structures containing arrays can't be function return values",
3451 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003452 }
3453 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003454
3455 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003456 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003457}
3458
Olli Etuahocce89652017-06-19 16:04:09 +03003459TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3460{
Olli Etuahocce89652017-06-19 16:04:09 +03003461 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003462 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003463}
3464
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003465TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003466{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003467 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003468 {
3469 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3470 "[]");
3471 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003472 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003473 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003474 error(publicType.getLine(), "constructor can't be a structure definition",
3475 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003476 }
3477
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003478 TType *type = new TType(publicType);
3479 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003480 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003481 error(publicType.getLine(), "cannot construct this type",
3482 getBasicString(publicType.getBasicType()));
3483 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003484 }
3485
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003486 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003487}
3488
Olli Etuaho55bde912017-10-25 13:41:13 +03003489void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3490 const char *errorMessage,
3491 const char *token,
3492 TType *arrayType)
3493{
3494 if (arrayType->isUnsizedArray())
3495 {
3496 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003497 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003498 }
3499}
3500
3501TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003502 const TString *name,
3503 const TSourceLoc &nameLoc)
3504{
Olli Etuaho55bde912017-10-25 13:41:13 +03003505 ASSERT(type);
3506 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3507 type);
3508 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003509 {
3510 error(nameLoc, "illegal use of type 'void'", name->c_str());
3511 }
3512 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003513 TParameter param = {name, type};
3514 return param;
3515}
3516
Olli Etuaho55bde912017-10-25 13:41:13 +03003517TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3518 const TString *name,
3519 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003520{
Olli Etuaho55bde912017-10-25 13:41:13 +03003521 TType *type = new TType(publicType);
3522 return parseParameterDeclarator(type, name, nameLoc);
3523}
3524
3525TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3526 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003527 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003528 const TSourceLoc &arrayLoc,
3529 TPublicType *elementType)
3530{
3531 checkArrayElementIsNotArray(arrayLoc, *elementType);
3532 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003533 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003534 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003535}
3536
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003537bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3538 TType type,
3539 const TSourceLoc &line)
3540{
3541 if (arguments->empty())
3542 {
3543 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3544 return false;
3545 }
3546 for (TIntermNode *arg : *arguments)
3547 {
3548 TIntermTyped *element = arg->getAsTyped();
3549 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003550 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3551 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003552 {
3553 error(line, "constructing from a non-dereferenced array", "constructor");
3554 return false;
3555 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003556 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003557 {
3558 if (dimensionalityFromElement == 1u)
3559 {
3560 error(line, "implicitly sized array of arrays constructor argument is not an array",
3561 "constructor");
3562 }
3563 else
3564 {
3565 error(line,
3566 "implicitly sized array of arrays constructor argument dimensionality is too "
3567 "low",
3568 "constructor");
3569 }
3570 return false;
3571 }
3572 }
3573 return true;
3574}
3575
Jamie Madillb98c3a82015-07-23 14:26:04 -04003576// This function is used to test for the correctness of the parameters passed to various constructor
3577// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003578//
Olli Etuaho856c4972016-08-08 11:38:39 +03003579// 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 +00003580//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003581TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003582 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303583 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003584{
Olli Etuaho856c4972016-08-08 11:38:39 +03003585 if (type.isUnsizedArray())
3586 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003587 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003588 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003589 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003590 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003591 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003592 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3593 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003594 if (type.getOutermostArraySize() == 0u)
3595 {
3596 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3597 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003598 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003599 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003600 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003601 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003602 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003603 }
3604 }
3605 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003606 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003607
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003608 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003609 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003610 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003611 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003612
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003613 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003614 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003615
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003616 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3617 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003618 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003619 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003620 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003621 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003622}
3623
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003624//
3625// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003626// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003627//
Olli Etuaho13389b62016-10-16 11:48:18 +01003628TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003629 const TTypeQualifierBuilder &typeQualifierBuilder,
3630 const TSourceLoc &nameLine,
3631 const TString &blockName,
3632 TFieldList *fieldList,
3633 const TString *instanceName,
3634 const TSourceLoc &instanceLine,
3635 TIntermTyped *arrayIndex,
3636 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003637{
Olli Etuaho856c4972016-08-08 11:38:39 +03003638 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003639
Olli Etuaho77ba4082016-12-16 12:01:18 +00003640 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003641
Jiajia Qinbc585152017-06-23 15:42:17 +08003642 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003643 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003644 error(typeQualifier.line,
3645 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3646 "3.10",
3647 getQualifierString(typeQualifier.qualifier));
3648 }
3649 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3650 {
3651 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003652 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003653 }
3654
Martin Radev70866b82016-07-22 15:27:42 +03003655 if (typeQualifier.invariant)
3656 {
3657 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3658 }
3659
Jiajia Qinbc585152017-06-23 15:42:17 +08003660 if (typeQualifier.qualifier != EvqBuffer)
3661 {
3662 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3663 }
Olli Etuaho43364892017-02-13 16:00:12 +00003664
jchen10af713a22017-04-19 09:10:56 +08003665 // add array index
3666 unsigned int arraySize = 0;
3667 if (arrayIndex != nullptr)
3668 {
3669 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3670 }
3671
3672 if (mShaderVersion < 310)
3673 {
3674 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3675 }
3676 else
3677 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003678 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3679 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003680 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003681
Andrei Volykhina5527072017-03-22 16:46:30 +03003682 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3683
Jamie Madill099c0f32013-06-20 11:55:52 -04003684 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003685 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003686 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3687 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003688
Jamie Madill099c0f32013-06-20 11:55:52 -04003689 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3690 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003691 if (typeQualifier.qualifier == EvqUniform)
3692 {
3693 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3694 }
3695 else if (typeQualifier.qualifier == EvqBuffer)
3696 {
3697 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3698 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003699 }
3700
Jamie Madill1566ef72013-06-20 11:55:54 -04003701 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3702 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003703 if (typeQualifier.qualifier == EvqUniform)
3704 {
3705 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3706 }
3707 else if (typeQualifier.qualifier == EvqBuffer)
3708 {
3709 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3710 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003711 }
3712
Olli Etuaho856c4972016-08-08 11:38:39 +03003713 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003714
Martin Radev2cc85b32016-08-05 16:22:53 +03003715 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3716
Olli Etuaho0f684632017-07-13 12:42:15 +03003717 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303718 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003719 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003720 }
3721
Jamie Madill98493dd2013-07-08 14:39:03 -04003722 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303723 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3724 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003725 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303726 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003727 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303728 {
jchen10cc2a10e2017-05-03 14:05:12 +08003729 std::string reason("unsupported type - ");
3730 reason += fieldType->getBasicString();
3731 reason += " types are not allowed in interface blocks";
3732 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003733 }
3734
Jamie Madill98493dd2013-07-08 14:39:03 -04003735 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003736 switch (qualifier)
3737 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003738 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003739 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003740 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003741 if (typeQualifier.qualifier == EvqBuffer)
3742 {
3743 error(field->line(), "invalid qualifier on shader storage block member",
3744 getQualifierString(qualifier));
3745 }
3746 break;
3747 case EvqBuffer:
3748 if (typeQualifier.qualifier == EvqUniform)
3749 {
3750 error(field->line(), "invalid qualifier on uniform block member",
3751 getQualifierString(qualifier));
3752 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003753 break;
3754 default:
3755 error(field->line(), "invalid qualifier on interface block member",
3756 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003757 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003758 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003759
Martin Radev70866b82016-07-22 15:27:42 +03003760 if (fieldType->isInvariant())
3761 {
3762 error(field->line(), "invalid qualifier on interface block member", "invariant");
3763 }
3764
Jamie Madilla5efff92013-06-06 11:56:47 -04003765 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003766 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003767 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003768 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003769
Jamie Madill98493dd2013-07-08 14:39:03 -04003770 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003771 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003772 error(field->line(), "invalid layout qualifier: cannot be used here",
3773 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003774 }
3775
Jamie Madill98493dd2013-07-08 14:39:03 -04003776 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003777 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003778 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003779 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003780 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003781 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003782 warning(field->line(),
3783 "extraneous layout qualifier: only has an effect on matrix types",
3784 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003785 }
3786
Jamie Madill98493dd2013-07-08 14:39:03 -04003787 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003788
Olli Etuahoebee5b32017-11-23 12:56:32 +02003789 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3790 typeQualifier.qualifier != EvqBuffer)
3791 {
3792 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3793 checkIsNotUnsizedArray(field->line(),
3794 "array members of interface blocks must specify a size",
3795 field->name().c_str(), field->type());
3796 }
3797
Jiajia Qinbc585152017-06-23 15:42:17 +08003798 if (typeQualifier.qualifier == EvqBuffer)
3799 {
3800 // set memory qualifiers
3801 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3802 // qualified with a memory qualifier, it is as if all of its members were declared with
3803 // the same memory qualifier.
3804 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3805 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3806 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3807 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3808 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3809 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3810 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3811 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3812 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3813 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3814 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003815 }
3816
Jamie Madillb98c3a82015-07-23 14:26:04 -04003817 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003818 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003819 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3820 if (arrayIndex != nullptr)
3821 {
3822 interfaceBlockType.makeArray(arraySize);
3823 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003824
3825 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003826 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003827
Jamie Madill98493dd2013-07-08 14:39:03 -04003828 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003829 {
3830 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003831 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3832 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003833 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303834 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003835
3836 // set parent pointer of the field variable
3837 fieldType->setInterfaceBlock(interfaceBlock);
3838
Olli Etuaho0f684632017-07-13 12:42:15 +03003839 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003840
Olli Etuaho0f684632017-07-13 12:42:15 +03003841 if (fieldVariable)
3842 {
3843 fieldVariable->setQualifier(typeQualifier.qualifier);
3844 }
3845 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303846 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003847 error(field->line(), "redefinition of an interface block member name",
3848 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003849 }
3850 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003851 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003852 }
3853 else
3854 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003855 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003856
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003857 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003858 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3859 if (instanceTypeDef)
3860 {
3861 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003862 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003863 }
3864 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303865 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003866 error(instanceLine, "redefinition of an interface block instance name",
3867 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003868 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003869 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003870 }
3871
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003872 TIntermDeclaration *declaration = nullptr;
3873
3874 if (symbolId)
3875 {
3876 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3877 blockSymbol->setLine(typeQualifier.line);
3878 declaration = new TIntermDeclaration();
3879 declaration->appendDeclarator(blockSymbol);
3880 declaration->setLine(nameLine);
3881 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003882
3883 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003884 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003885}
3886
Olli Etuaho383b7912016-08-05 11:22:59 +03003887void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003888{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003889 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003890
3891 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003892 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303893 if (mStructNestingLevel > 1)
3894 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003895 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003896 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003897}
3898
3899void TParseContext::exitStructDeclaration()
3900{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003901 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003902}
3903
Olli Etuaho8a176262016-08-16 14:23:01 +03003904void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003905{
Jamie Madillacb4b812016-11-07 13:50:29 -05003906 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303907 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003908 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003909 }
3910
Arun Patole7e7e68d2015-05-22 12:02:25 +05303911 if (field.type()->getBasicType() != EbtStruct)
3912 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003913 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003914 }
3915
3916 // We're already inside a structure definition at this point, so add
3917 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303918 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3919 {
Jamie Madill41a49272014-03-18 16:10:13 -04003920 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003921 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3922 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003923 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003924 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003925 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003926 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003927}
3928
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003929//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003930// Parse an array index expression
3931//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003932TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3933 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303934 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003935{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003936 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3937 {
3938 if (baseExpression->getAsSymbolNode())
3939 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303940 error(location, " left of '[' is not of type array, matrix, or vector ",
3941 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003942 }
3943 else
3944 {
3945 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3946 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003947
Olli Etuaho3ec75682017-07-05 17:02:55 +03003948 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003949 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003950
Jiawei Shaod8105a02017-08-08 09:54:36 +08003951 if (baseExpression->getQualifier() == EvqPerVertexIn)
3952 {
3953 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3954 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3955 {
3956 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3957 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3958 }
3959 }
3960
Jamie Madill21c1e452014-12-29 11:33:41 -05003961 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3962
Olli Etuaho36b05142015-11-12 13:10:42 +02003963 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3964 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3965 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3966 // index is a constant expression.
3967 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3968 {
3969 if (baseExpression->isInterfaceBlock())
3970 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003971 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3972 switch (baseExpression->getQualifier())
3973 {
3974 case EvqPerVertexIn:
3975 break;
3976 case EvqUniform:
3977 case EvqBuffer:
3978 error(location,
3979 "array indexes for uniform block arrays and shader storage block arrays "
3980 "must be constant integral expressions",
3981 "[");
3982 break;
3983 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003984 // We can reach here only in error cases.
3985 ASSERT(mDiagnostics->numErrors() > 0);
3986 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003987 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003988 }
3989 else if (baseExpression->getQualifier() == EvqFragmentOut)
3990 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003991 error(location,
3992 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003993 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003994 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3995 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003996 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003997 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003998 }
3999
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004000 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04004001 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004002 // If an out-of-range index is not qualified as constant, the behavior in the spec is
4003 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
4004 // constant fold expressions that are not constant expressions). The most compatible way to
4005 // handle this case is to report a warning instead of an error and force the index to be in
4006 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004007 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03004008 int index = 0;
4009 if (indexConstantUnion->getBasicType() == EbtInt)
4010 {
4011 index = indexConstantUnion->getIConst(0);
4012 }
4013 else if (indexConstantUnion->getBasicType() == EbtUInt)
4014 {
4015 index = static_cast<int>(indexConstantUnion->getUConst(0));
4016 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004017
4018 int safeIndex = -1;
4019
Olli Etuahoebee5b32017-11-23 12:56:32 +02004020 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004021 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004022 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4023 safeIndex = 0;
4024 }
4025
4026 if (!baseExpression->getType().isUnsizedArray())
4027 {
4028 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004029 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004030 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004031 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004032 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4033 {
4034 outOfRangeError(outOfRangeIndexIsError, location,
4035 "array index for gl_FragData must be zero when "
4036 "GL_EXT_draw_buffers is disabled",
4037 "[]");
4038 safeIndex = 0;
4039 }
4040 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004041 }
4042 // Only do generic out-of-range check if similar error hasn't already been reported.
4043 if (safeIndex < 0)
4044 {
4045 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004046 {
4047 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4048 baseExpression->getOutermostArraySize(),
4049 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004050 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004051 else if (baseExpression->isMatrix())
4052 {
4053 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4054 baseExpression->getType().getCols(),
4055 "matrix field selection out of range");
4056 }
4057 else
4058 {
4059 ASSERT(baseExpression->isVector());
4060 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4061 baseExpression->getType().getNominalSize(),
4062 "vector field selection out of range");
4063 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004064 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004065
Olli Etuahoebee5b32017-11-23 12:56:32 +02004066 ASSERT(safeIndex >= 0);
4067 // Data of constant unions can't be changed, because it may be shared with other
4068 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4069 // sanitized object.
4070 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4071 {
4072 TConstantUnion *safeConstantUnion = new TConstantUnion();
4073 safeConstantUnion->setIConst(safeIndex);
4074 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4075 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4076 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004077
Olli Etuahoebee5b32017-11-23 12:56:32 +02004078 TIntermBinary *node =
4079 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4080 node->setLine(location);
4081 return node->fold(mDiagnostics);
4082 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004083 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004084
4085 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4086 node->setLine(location);
4087 // Indirect indexing can never be constant folded.
4088 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004089}
4090
Olli Etuahoebee5b32017-11-23 12:56:32 +02004091int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4092 const TSourceLoc &location,
4093 int index,
4094 int arraySize,
4095 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004096{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004097 // Should not reach here with an unsized / runtime-sized array.
4098 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004099 // A negative index should already have been checked.
4100 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004101 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004102 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004103 std::stringstream reasonStream;
4104 reasonStream << reason << " '" << index << "'";
4105 std::string token = reasonStream.str();
4106 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004107 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004108 }
4109 return index;
4110}
4111
Jamie Madillb98c3a82015-07-23 14:26:04 -04004112TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4113 const TSourceLoc &dotLocation,
4114 const TString &fieldString,
4115 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004116{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004117 if (baseExpression->isArray())
4118 {
4119 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004120 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121 }
4122
4123 if (baseExpression->isVector())
4124 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004125 TVector<int> fieldOffsets;
4126 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4127 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004128 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004129 fieldOffsets.resize(1);
4130 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004131 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004132 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4133 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004134
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004135 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004136 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004137 else if (baseExpression->getBasicType() == EbtStruct)
4138 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304139 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004140 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004141 {
4142 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004143 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004144 }
4145 else
4146 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004147 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004148 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004149 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004150 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004151 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004152 {
4153 fieldFound = true;
4154 break;
4155 }
4156 }
4157 if (fieldFound)
4158 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004159 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004160 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004161 TIntermBinary *node =
4162 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4163 node->setLine(dotLocation);
4164 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004165 }
4166 else
4167 {
4168 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004169 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004170 }
4171 }
4172 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004173 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004174 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304175 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004176 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004177 {
4178 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004179 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004180 }
4181 else
4182 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004183 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004184 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004185 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004186 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004187 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004188 {
4189 fieldFound = true;
4190 break;
4191 }
4192 }
4193 if (fieldFound)
4194 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004195 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004196 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004197 TIntermBinary *node =
4198 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4199 node->setLine(dotLocation);
4200 // Indexing interface blocks can never be constant folded.
4201 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004202 }
4203 else
4204 {
4205 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004206 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004207 }
4208 }
4209 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004210 else
4211 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004212 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004213 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004214 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304215 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004216 }
4217 else
4218 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304219 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004220 " field selection requires structure, vector, or interface block on left hand "
4221 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304222 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004223 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004224 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004225 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004226}
4227
Jamie Madillb98c3a82015-07-23 14:26:04 -04004228TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4229 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004230{
Jamie Madill2f294c92017-11-20 14:47:26 -05004231 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004232
4233 if (qualifierType == "shared")
4234 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004235 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004236 {
4237 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4238 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004239 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004240 }
4241 else if (qualifierType == "packed")
4242 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004243 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004244 {
4245 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4246 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004247 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004248 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004249 else if (qualifierType == "std430")
4250 {
4251 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4252 qualifier.blockStorage = EbsStd430;
4253 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004254 else if (qualifierType == "std140")
4255 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004256 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004257 }
4258 else if (qualifierType == "row_major")
4259 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004260 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004261 }
4262 else if (qualifierType == "column_major")
4263 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004264 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004265 }
4266 else if (qualifierType == "location")
4267 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004268 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4269 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004270 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004271 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004272 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004273 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4274 {
4275 qualifier.yuv = true;
4276 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004277 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004278 else if (qualifierType == "rgba32f")
4279 {
4280 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4281 qualifier.imageInternalFormat = EiifRGBA32F;
4282 }
4283 else if (qualifierType == "rgba16f")
4284 {
4285 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4286 qualifier.imageInternalFormat = EiifRGBA16F;
4287 }
4288 else if (qualifierType == "r32f")
4289 {
4290 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4291 qualifier.imageInternalFormat = EiifR32F;
4292 }
4293 else if (qualifierType == "rgba8")
4294 {
4295 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4296 qualifier.imageInternalFormat = EiifRGBA8;
4297 }
4298 else if (qualifierType == "rgba8_snorm")
4299 {
4300 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4301 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4302 }
4303 else if (qualifierType == "rgba32i")
4304 {
4305 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4306 qualifier.imageInternalFormat = EiifRGBA32I;
4307 }
4308 else if (qualifierType == "rgba16i")
4309 {
4310 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4311 qualifier.imageInternalFormat = EiifRGBA16I;
4312 }
4313 else if (qualifierType == "rgba8i")
4314 {
4315 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4316 qualifier.imageInternalFormat = EiifRGBA8I;
4317 }
4318 else if (qualifierType == "r32i")
4319 {
4320 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4321 qualifier.imageInternalFormat = EiifR32I;
4322 }
4323 else if (qualifierType == "rgba32ui")
4324 {
4325 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4326 qualifier.imageInternalFormat = EiifRGBA32UI;
4327 }
4328 else if (qualifierType == "rgba16ui")
4329 {
4330 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4331 qualifier.imageInternalFormat = EiifRGBA16UI;
4332 }
4333 else if (qualifierType == "rgba8ui")
4334 {
4335 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4336 qualifier.imageInternalFormat = EiifRGBA8UI;
4337 }
4338 else if (qualifierType == "r32ui")
4339 {
4340 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4341 qualifier.imageInternalFormat = EiifR32UI;
4342 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004343 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4344 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004345 {
4346 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4347 qualifier.primitiveType = EptPoints;
4348 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004349 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4350 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004351 {
4352 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4353 qualifier.primitiveType = EptLines;
4354 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004355 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4356 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004357 {
4358 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4359 qualifier.primitiveType = EptLinesAdjacency;
4360 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004361 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4362 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004363 {
4364 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4365 qualifier.primitiveType = EptTriangles;
4366 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004367 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4368 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004369 {
4370 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4371 qualifier.primitiveType = EptTrianglesAdjacency;
4372 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004373 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4374 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004375 {
4376 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4377 qualifier.primitiveType = EptLineStrip;
4378 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004379 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4380 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004381 {
4382 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4383 qualifier.primitiveType = EptTriangleStrip;
4384 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004385
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004386 else
4387 {
4388 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004389 }
4390
Jamie Madilla5efff92013-06-06 11:56:47 -04004391 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004392}
4393
Martin Radev802abe02016-08-04 17:48:32 +03004394void TParseContext::parseLocalSize(const TString &qualifierType,
4395 const TSourceLoc &qualifierTypeLine,
4396 int intValue,
4397 const TSourceLoc &intValueLine,
4398 const std::string &intValueString,
4399 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004400 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004401{
Olli Etuaho856c4972016-08-08 11:38:39 +03004402 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004403 if (intValue < 1)
4404 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004405 std::stringstream reasonStream;
4406 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4407 std::string reason = reasonStream.str();
4408 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004409 }
4410 (*localSize)[index] = intValue;
4411}
4412
Olli Etuaho09b04a22016-12-15 13:30:26 +00004413void TParseContext::parseNumViews(int intValue,
4414 const TSourceLoc &intValueLine,
4415 const std::string &intValueString,
4416 int *numViews)
4417{
4418 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4419 // specification.
4420 if (intValue < 1)
4421 {
4422 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4423 }
4424 *numViews = intValue;
4425}
4426
Shaob5cc1192017-07-06 10:47:20 +08004427void TParseContext::parseInvocations(int intValue,
4428 const TSourceLoc &intValueLine,
4429 const std::string &intValueString,
4430 int *numInvocations)
4431{
4432 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4433 // it doesn't make sense to accept invocations <= 0.
4434 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4435 {
4436 error(intValueLine,
4437 "out of range: invocations must be in the range of [1, "
4438 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4439 intValueString.c_str());
4440 }
4441 else
4442 {
4443 *numInvocations = intValue;
4444 }
4445}
4446
4447void TParseContext::parseMaxVertices(int intValue,
4448 const TSourceLoc &intValueLine,
4449 const std::string &intValueString,
4450 int *maxVertices)
4451{
4452 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4453 // it doesn't make sense to accept max_vertices < 0.
4454 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4455 {
4456 error(
4457 intValueLine,
4458 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4459 intValueString.c_str());
4460 }
4461 else
4462 {
4463 *maxVertices = intValue;
4464 }
4465}
4466
Jamie Madillb98c3a82015-07-23 14:26:04 -04004467TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4468 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004469 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304470 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004471{
Jamie Madill2f294c92017-11-20 14:47:26 -05004472 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004473
Martin Radev802abe02016-08-04 17:48:32 +03004474 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004475
Martin Radev802abe02016-08-04 17:48:32 +03004476 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004477 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004478 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004479 if (intValue < 0)
4480 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004481 error(intValueLine, "out of range: location must be non-negative",
4482 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004483 }
4484 else
4485 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004486 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004487 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004488 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004489 }
Olli Etuaho43364892017-02-13 16:00:12 +00004490 else if (qualifierType == "binding")
4491 {
4492 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4493 if (intValue < 0)
4494 {
4495 error(intValueLine, "out of range: binding must be non-negative",
4496 intValueString.c_str());
4497 }
4498 else
4499 {
4500 qualifier.binding = intValue;
4501 }
4502 }
jchen104cdac9e2017-05-08 11:01:20 +08004503 else if (qualifierType == "offset")
4504 {
4505 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4506 if (intValue < 0)
4507 {
4508 error(intValueLine, "out of range: offset must be non-negative",
4509 intValueString.c_str());
4510 }
4511 else
4512 {
4513 qualifier.offset = intValue;
4514 }
4515 }
Martin Radev802abe02016-08-04 17:48:32 +03004516 else if (qualifierType == "local_size_x")
4517 {
4518 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4519 &qualifier.localSize);
4520 }
4521 else if (qualifierType == "local_size_y")
4522 {
4523 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4524 &qualifier.localSize);
4525 }
4526 else if (qualifierType == "local_size_z")
4527 {
4528 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4529 &qualifier.localSize);
4530 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004531 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004532 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004533 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4534 {
4535 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4536 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004537 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004538 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4539 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004540 {
4541 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4542 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004543 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4544 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004545 {
4546 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4547 }
4548
Martin Radev802abe02016-08-04 17:48:32 +03004549 else
4550 {
4551 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004552 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004553
Jamie Madilla5efff92013-06-06 11:56:47 -04004554 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004555}
4556
Olli Etuaho613b9592016-09-05 12:05:53 +03004557TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4558{
4559 return new TTypeQualifierBuilder(
4560 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4561 mShaderVersion);
4562}
4563
Olli Etuahocce89652017-06-19 16:04:09 +03004564TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4565 const TSourceLoc &loc)
4566{
4567 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4568 return new TStorageQualifierWrapper(qualifier, loc);
4569}
4570
4571TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4572{
4573 if (getShaderType() == GL_VERTEX_SHADER)
4574 {
4575 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4576 }
4577 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4578}
4579
4580TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4581{
4582 if (declaringFunction())
4583 {
4584 return new TStorageQualifierWrapper(EvqIn, loc);
4585 }
Shaob5cc1192017-07-06 10:47:20 +08004586
4587 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004588 {
Shaob5cc1192017-07-06 10:47:20 +08004589 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004590 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004591 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004592 {
4593 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4594 }
4595 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004596 }
Shaob5cc1192017-07-06 10:47:20 +08004597 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004598 {
Shaob5cc1192017-07-06 10:47:20 +08004599 if (mShaderVersion < 300)
4600 {
4601 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4602 }
4603 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004604 }
Shaob5cc1192017-07-06 10:47:20 +08004605 case GL_COMPUTE_SHADER:
4606 {
4607 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4608 }
4609 case GL_GEOMETRY_SHADER_OES:
4610 {
4611 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4612 }
4613 default:
4614 {
4615 UNREACHABLE();
4616 return new TStorageQualifierWrapper(EvqLast, loc);
4617 }
Olli Etuahocce89652017-06-19 16:04:09 +03004618 }
Olli Etuahocce89652017-06-19 16:04:09 +03004619}
4620
4621TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4622{
4623 if (declaringFunction())
4624 {
4625 return new TStorageQualifierWrapper(EvqOut, loc);
4626 }
Shaob5cc1192017-07-06 10:47:20 +08004627 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004628 {
Shaob5cc1192017-07-06 10:47:20 +08004629 case GL_VERTEX_SHADER:
4630 {
4631 if (mShaderVersion < 300)
4632 {
4633 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4634 }
4635 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4636 }
4637 case GL_FRAGMENT_SHADER:
4638 {
4639 if (mShaderVersion < 300)
4640 {
4641 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4642 }
4643 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4644 }
4645 case GL_COMPUTE_SHADER:
4646 {
4647 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4648 return new TStorageQualifierWrapper(EvqLast, loc);
4649 }
4650 case GL_GEOMETRY_SHADER_OES:
4651 {
4652 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4653 }
4654 default:
4655 {
4656 UNREACHABLE();
4657 return new TStorageQualifierWrapper(EvqLast, loc);
4658 }
Olli Etuahocce89652017-06-19 16:04:09 +03004659 }
Olli Etuahocce89652017-06-19 16:04:09 +03004660}
4661
4662TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4663{
4664 if (!declaringFunction())
4665 {
4666 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4667 }
4668 return new TStorageQualifierWrapper(EvqInOut, loc);
4669}
4670
Jamie Madillb98c3a82015-07-23 14:26:04 -04004671TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004672 TLayoutQualifier rightQualifier,
4673 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004674{
Martin Radevc28888b2016-07-22 15:27:42 +03004675 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004676 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004677}
4678
Olli Etuahocce89652017-06-19 16:04:09 +03004679TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4680{
4681 checkIsNotReserved(loc, *identifier);
4682 TType *type = new TType(EbtVoid, EbpUndefined);
4683 return new TField(type, identifier, loc);
4684}
4685
4686TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4687 const TSourceLoc &loc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004688 const TVector<unsigned int> &arraySizes,
Olli Etuahocce89652017-06-19 16:04:09 +03004689 const TSourceLoc &arraySizeLoc)
4690{
4691 checkIsNotReserved(loc, *identifier);
4692
4693 TType *type = new TType(EbtVoid, EbpUndefined);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004694 type->makeArrays(arraySizes);
Olli Etuahocce89652017-06-19 16:04:09 +03004695
4696 return new TField(type, identifier, loc);
4697}
4698
Olli Etuaho722bfb52017-10-26 17:00:11 +03004699void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4700 const TFieldList::const_iterator end,
4701 const TString &name,
4702 const TSourceLoc &location)
4703{
4704 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4705 {
4706 if ((*fieldIter)->name() == name)
4707 {
4708 error(location, "duplicate field name in structure", name.c_str());
4709 }
4710 }
4711}
4712
4713TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4714{
4715 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4716 ++fieldIter)
4717 {
4718 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4719 location);
4720 }
4721 return fields;
4722}
4723
Olli Etuaho4de340a2016-12-16 09:32:03 +00004724TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4725 const TFieldList *newlyAddedFields,
4726 const TSourceLoc &location)
4727{
4728 for (TField *field : *newlyAddedFields)
4729 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004730 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4731 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004732 processedFields->push_back(field);
4733 }
4734 return processedFields;
4735}
4736
Martin Radev70866b82016-07-22 15:27:42 +03004737TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4738 const TTypeQualifierBuilder &typeQualifierBuilder,
4739 TPublicType *typeSpecifier,
4740 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004741{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004742 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004743
Martin Radev70866b82016-07-22 15:27:42 +03004744 typeSpecifier->qualifier = typeQualifier.qualifier;
4745 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004746 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004747 typeSpecifier->invariant = typeQualifier.invariant;
4748 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304749 {
Martin Radev70866b82016-07-22 15:27:42 +03004750 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004751 }
Martin Radev70866b82016-07-22 15:27:42 +03004752 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004753}
4754
Jamie Madillb98c3a82015-07-23 14:26:04 -04004755TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004756 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004757{
Martin Radev4a9cd802016-09-01 16:51:51 +03004758 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4759 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004760
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004761 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4762 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004763
Martin Radev4a9cd802016-09-01 16:51:51 +03004764 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004765
Olli Etuaho55bde912017-10-25 13:41:13 +03004766 for (TField *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304767 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004768 // Don't allow arrays of arrays in ESSL < 3.10.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08004769 if (declarator->type()->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304770 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004771 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004772 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004773
Kai Ninomiya57ea5332017-11-22 14:04:48 -08004774 auto *declaratorArraySizes = declarator->type()->getArraySizes();
4775
Olli Etuaho55bde912017-10-25 13:41:13 +03004776 TType *type = declarator->type();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004777 *type = TType(typeSpecifier);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08004778 if (declaratorArraySizes != nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304779 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08004780 for (unsigned int arraySize : *declaratorArraySizes)
4781 {
4782 type->makeArray(arraySize);
4783 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004784 }
4785
Olli Etuaho55bde912017-10-25 13:41:13 +03004786 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *declarator);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004787 }
4788
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004789 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004790}
4791
Martin Radev4a9cd802016-09-01 16:51:51 +03004792TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4793 const TSourceLoc &nameLine,
4794 const TString *structName,
4795 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004796{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004797 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004798
Jamie Madill9b820842015-02-12 10:40:10 -05004799 // Store a bool in the struct if we're at global scope, to allow us to
4800 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004801 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004802
Jamie Madill98493dd2013-07-08 14:39:03 -04004803 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004804 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004805 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004806 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304807 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004808 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004809 }
4810 }
4811
4812 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004813 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004814 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004815 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004816 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004817 switch (qualifier)
4818 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004819 case EvqGlobal:
4820 case EvqTemporary:
4821 break;
4822 default:
4823 error(field.line(), "invalid qualifier on struct member",
4824 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004825 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004826 }
Martin Radev70866b82016-07-22 15:27:42 +03004827 if (field.type()->isInvariant())
4828 {
4829 error(field.line(), "invalid qualifier on struct member", "invariant");
4830 }
jchen104cdac9e2017-05-08 11:01:20 +08004831 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4832 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004833 {
4834 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4835 }
4836
Olli Etuahoebee5b32017-11-23 12:56:32 +02004837 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4838 field.name().c_str(), field.type());
4839
Olli Etuaho43364892017-02-13 16:00:12 +00004840 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4841
4842 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004843
4844 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004845 }
4846
Martin Radev4a9cd802016-09-01 16:51:51 +03004847 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004848 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004849 exitStructDeclaration();
4850
Martin Radev4a9cd802016-09-01 16:51:51 +03004851 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004852}
4853
Jamie Madillb98c3a82015-07-23 14:26:04 -04004854TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004855 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004856 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004857{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004858 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004859 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004860 init->isVector())
4861 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004862 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4863 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004864 return nullptr;
4865 }
4866
Olli Etuaho923ecef2017-10-11 12:01:38 +03004867 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004868 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004869 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004870 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004871 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004872 }
4873
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004874 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4875 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004876 return node;
4877}
4878
4879TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4880{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004881 if (mSwitchNestingLevel == 0)
4882 {
4883 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004884 return nullptr;
4885 }
4886 if (condition == nullptr)
4887 {
4888 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004889 return nullptr;
4890 }
4891 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004892 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004893 {
4894 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004895 }
4896 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004897 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4898 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4899 // fold in case labels.
4900 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004901 {
4902 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004903 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004904 TIntermCase *node = new TIntermCase(condition);
4905 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004906 return node;
4907}
4908
4909TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4910{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004911 if (mSwitchNestingLevel == 0)
4912 {
4913 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004914 return nullptr;
4915 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004916 TIntermCase *node = new TIntermCase(nullptr);
4917 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004918 return node;
4919}
4920
Jamie Madillb98c3a82015-07-23 14:26:04 -04004921TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4922 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004923 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004924{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004925 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004926
4927 switch (op)
4928 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004929 case EOpLogicalNot:
4930 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4931 child->isVector())
4932 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004933 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004934 return nullptr;
4935 }
4936 break;
4937 case EOpBitwiseNot:
4938 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4939 child->isMatrix() || child->isArray())
4940 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004941 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004942 return nullptr;
4943 }
4944 break;
4945 case EOpPostIncrement:
4946 case EOpPreIncrement:
4947 case EOpPostDecrement:
4948 case EOpPreDecrement:
4949 case EOpNegative:
4950 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004951 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4952 child->getBasicType() == EbtBool || child->isArray() ||
4953 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004954 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004955 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004956 return nullptr;
4957 }
4958 // Operators for built-ins are already type checked against their prototype.
4959 default:
4960 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004961 }
4962
Jiajia Qinbc585152017-06-23 15:42:17 +08004963 if (child->getMemoryQualifier().writeonly)
4964 {
4965 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4966 return nullptr;
4967 }
4968
Olli Etuahof119a262016-08-19 15:54:22 +03004969 TIntermUnary *node = new TIntermUnary(op, child);
4970 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004971
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004972 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004973}
4974
Olli Etuaho09b22472015-02-11 11:47:26 +02004975TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4976{
Olli Etuahocce89652017-06-19 16:04:09 +03004977 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004978 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004979 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004980 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004981 return child;
4982 }
4983 return node;
4984}
4985
Jamie Madillb98c3a82015-07-23 14:26:04 -04004986TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4987 TIntermTyped *child,
4988 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004989{
Olli Etuaho856c4972016-08-08 11:38:39 +03004990 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004991 return addUnaryMath(op, child, loc);
4992}
4993
Jamie Madillb98c3a82015-07-23 14:26:04 -04004994bool TParseContext::binaryOpCommonCheck(TOperator op,
4995 TIntermTyped *left,
4996 TIntermTyped *right,
4997 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004998{
jchen10b4cf5652017-05-05 18:51:17 +08004999 // Check opaque types are not allowed to be operands in expressions other than array indexing
5000 // and structure member selection.
5001 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
5002 {
5003 switch (op)
5004 {
5005 case EOpIndexDirect:
5006 case EOpIndexIndirect:
5007 break;
5008 case EOpIndexDirectStruct:
5009 UNREACHABLE();
5010
5011 default:
5012 error(loc, "Invalid operation for variables with an opaque type",
5013 GetOperatorString(op));
5014 return false;
5015 }
5016 }
jchen10cc2a10e2017-05-03 14:05:12 +08005017
Jiajia Qinbc585152017-06-23 15:42:17 +08005018 if (right->getMemoryQualifier().writeonly)
5019 {
5020 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5021 return false;
5022 }
5023
5024 if (left->getMemoryQualifier().writeonly)
5025 {
5026 switch (op)
5027 {
5028 case EOpAssign:
5029 case EOpInitialize:
5030 case EOpIndexDirect:
5031 case EOpIndexIndirect:
5032 case EOpIndexDirectStruct:
5033 case EOpIndexDirectInterfaceBlock:
5034 break;
5035 default:
5036 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5037 return false;
5038 }
5039 }
5040
Olli Etuaho244be012016-08-18 15:26:02 +03005041 if (left->getType().getStruct() || right->getType().getStruct())
5042 {
5043 switch (op)
5044 {
5045 case EOpIndexDirectStruct:
5046 ASSERT(left->getType().getStruct());
5047 break;
5048 case EOpEqual:
5049 case EOpNotEqual:
5050 case EOpAssign:
5051 case EOpInitialize:
5052 if (left->getType() != right->getType())
5053 {
5054 return false;
5055 }
5056 break;
5057 default:
5058 error(loc, "Invalid operation for structs", GetOperatorString(op));
5059 return false;
5060 }
5061 }
5062
Olli Etuaho94050052017-05-08 14:17:44 +03005063 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5064 {
5065 switch (op)
5066 {
5067 case EOpIndexDirectInterfaceBlock:
5068 ASSERT(left->getType().getInterfaceBlock());
5069 break;
5070 default:
5071 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5072 return false;
5073 }
5074 }
5075
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005076 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005077 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005078 error(loc, "array / non-array mismatch", GetOperatorString(op));
5079 return false;
5080 }
5081
5082 if (left->isArray())
5083 {
5084 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005085 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005086 {
5087 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5088 return false;
5089 }
5090
Olli Etuahoe79904c2015-03-18 16:56:42 +02005091 switch (op)
5092 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005093 case EOpEqual:
5094 case EOpNotEqual:
5095 case EOpAssign:
5096 case EOpInitialize:
5097 break;
5098 default:
5099 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5100 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005101 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005102 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005103 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005104 {
5105 error(loc, "array size mismatch", GetOperatorString(op));
5106 return false;
5107 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005108 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005109
5110 // Check ops which require integer / ivec parameters
5111 bool isBitShift = false;
5112 switch (op)
5113 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005114 case EOpBitShiftLeft:
5115 case EOpBitShiftRight:
5116 case EOpBitShiftLeftAssign:
5117 case EOpBitShiftRightAssign:
5118 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5119 // check that the basic type is an integer type.
5120 isBitShift = true;
5121 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5122 {
5123 return false;
5124 }
5125 break;
5126 case EOpBitwiseAnd:
5127 case EOpBitwiseXor:
5128 case EOpBitwiseOr:
5129 case EOpBitwiseAndAssign:
5130 case EOpBitwiseXorAssign:
5131 case EOpBitwiseOrAssign:
5132 // It is enough to check the type of only one operand, since later it
5133 // is checked that the operand types match.
5134 if (!IsInteger(left->getBasicType()))
5135 {
5136 return false;
5137 }
5138 break;
5139 default:
5140 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005141 }
5142
5143 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5144 // So the basic type should usually match.
5145 if (!isBitShift && left->getBasicType() != right->getBasicType())
5146 {
5147 return false;
5148 }
5149
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005150 // Check that:
5151 // 1. Type sizes match exactly on ops that require that.
5152 // 2. Restrictions for structs that contain arrays or samplers are respected.
5153 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005154 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005155 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005156 case EOpAssign:
5157 case EOpInitialize:
5158 case EOpEqual:
5159 case EOpNotEqual:
5160 // ESSL 1.00 sections 5.7, 5.8, 5.9
5161 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5162 {
5163 error(loc, "undefined operation for structs containing arrays",
5164 GetOperatorString(op));
5165 return false;
5166 }
5167 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5168 // we interpret the spec so that this extends to structs containing samplers,
5169 // similarly to ESSL 1.00 spec.
5170 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5171 left->getType().isStructureContainingSamplers())
5172 {
5173 error(loc, "undefined operation for structs containing samplers",
5174 GetOperatorString(op));
5175 return false;
5176 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005177
Olli Etuahoe1805592017-01-02 16:41:20 +00005178 if ((left->getNominalSize() != right->getNominalSize()) ||
5179 (left->getSecondarySize() != right->getSecondarySize()))
5180 {
5181 error(loc, "dimension mismatch", GetOperatorString(op));
5182 return false;
5183 }
5184 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005185 case EOpLessThan:
5186 case EOpGreaterThan:
5187 case EOpLessThanEqual:
5188 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005189 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005190 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005191 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005192 return false;
5193 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005194 break;
5195 case EOpAdd:
5196 case EOpSub:
5197 case EOpDiv:
5198 case EOpIMod:
5199 case EOpBitShiftLeft:
5200 case EOpBitShiftRight:
5201 case EOpBitwiseAnd:
5202 case EOpBitwiseXor:
5203 case EOpBitwiseOr:
5204 case EOpAddAssign:
5205 case EOpSubAssign:
5206 case EOpDivAssign:
5207 case EOpIModAssign:
5208 case EOpBitShiftLeftAssign:
5209 case EOpBitShiftRightAssign:
5210 case EOpBitwiseAndAssign:
5211 case EOpBitwiseXorAssign:
5212 case EOpBitwiseOrAssign:
5213 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5214 {
5215 return false;
5216 }
5217
5218 // Are the sizes compatible?
5219 if (left->getNominalSize() != right->getNominalSize() ||
5220 left->getSecondarySize() != right->getSecondarySize())
5221 {
5222 // If the nominal sizes of operands do not match:
5223 // One of them must be a scalar.
5224 if (!left->isScalar() && !right->isScalar())
5225 return false;
5226
5227 // In the case of compound assignment other than multiply-assign,
5228 // the right side needs to be a scalar. Otherwise a vector/matrix
5229 // would be assigned to a scalar. A scalar can't be shifted by a
5230 // vector either.
5231 if (!right->isScalar() &&
5232 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5233 return false;
5234 }
5235 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005236 default:
5237 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005238 }
5239
Olli Etuahod6b14282015-03-17 14:31:35 +02005240 return true;
5241}
5242
Olli Etuaho1dded802016-08-18 18:13:13 +03005243bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5244 const TType &left,
5245 const TType &right)
5246{
5247 switch (op)
5248 {
5249 case EOpMul:
5250 case EOpMulAssign:
5251 return left.getNominalSize() == right.getNominalSize() &&
5252 left.getSecondarySize() == right.getSecondarySize();
5253 case EOpVectorTimesScalar:
5254 return true;
5255 case EOpVectorTimesScalarAssign:
5256 ASSERT(!left.isMatrix() && !right.isMatrix());
5257 return left.isVector() && !right.isVector();
5258 case EOpVectorTimesMatrix:
5259 return left.getNominalSize() == right.getRows();
5260 case EOpVectorTimesMatrixAssign:
5261 ASSERT(!left.isMatrix() && right.isMatrix());
5262 return left.isVector() && left.getNominalSize() == right.getRows() &&
5263 left.getNominalSize() == right.getCols();
5264 case EOpMatrixTimesVector:
5265 return left.getCols() == right.getNominalSize();
5266 case EOpMatrixTimesScalar:
5267 return true;
5268 case EOpMatrixTimesScalarAssign:
5269 ASSERT(left.isMatrix() && !right.isMatrix());
5270 return !right.isVector();
5271 case EOpMatrixTimesMatrix:
5272 return left.getCols() == right.getRows();
5273 case EOpMatrixTimesMatrixAssign:
5274 ASSERT(left.isMatrix() && right.isMatrix());
5275 // We need to check two things:
5276 // 1. The matrix multiplication step is valid.
5277 // 2. The result will have the same number of columns as the lvalue.
5278 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5279
5280 default:
5281 UNREACHABLE();
5282 return false;
5283 }
5284}
5285
Jamie Madillb98c3a82015-07-23 14:26:04 -04005286TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5287 TIntermTyped *left,
5288 TIntermTyped *right,
5289 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005290{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005291 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005292 return nullptr;
5293
Olli Etuahofc1806e2015-03-17 13:03:11 +02005294 switch (op)
5295 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005296 case EOpEqual:
5297 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005298 case EOpLessThan:
5299 case EOpGreaterThan:
5300 case EOpLessThanEqual:
5301 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005302 break;
5303 case EOpLogicalOr:
5304 case EOpLogicalXor:
5305 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005306 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5307 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005308 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005309 {
5310 return nullptr;
5311 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005312 // Basic types matching should have been already checked.
5313 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005314 break;
5315 case EOpAdd:
5316 case EOpSub:
5317 case EOpDiv:
5318 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005319 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5320 !right->getType().getStruct());
5321 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005322 {
5323 return nullptr;
5324 }
5325 break;
5326 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005327 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5328 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005329 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005330 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005331 {
5332 return nullptr;
5333 }
5334 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005335 default:
5336 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005337 }
5338
Olli Etuaho1dded802016-08-18 18:13:13 +03005339 if (op == EOpMul)
5340 {
5341 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5342 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5343 {
5344 return nullptr;
5345 }
5346 }
5347
Olli Etuaho3fdec912016-08-18 15:08:06 +03005348 TIntermBinary *node = new TIntermBinary(op, left, right);
5349 node->setLine(loc);
5350
Olli Etuaho3fdec912016-08-18 15:08:06 +03005351 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005352 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005353}
5354
Jamie Madillb98c3a82015-07-23 14:26:04 -04005355TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5356 TIntermTyped *left,
5357 TIntermTyped *right,
5358 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005359{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005360 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005361 if (node == 0)
5362 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005363 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5364 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005365 return left;
5366 }
5367 return node;
5368}
5369
Jamie Madillb98c3a82015-07-23 14:26:04 -04005370TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5371 TIntermTyped *left,
5372 TIntermTyped *right,
5373 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005374{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005375 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005376 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005377 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005378 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5379 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005380 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005381 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005382 }
5383 return node;
5384}
5385
Olli Etuaho13389b62016-10-16 11:48:18 +01005386TIntermBinary *TParseContext::createAssign(TOperator op,
5387 TIntermTyped *left,
5388 TIntermTyped *right,
5389 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005390{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005391 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005392 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005393 if (op == EOpMulAssign)
5394 {
5395 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5396 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5397 {
5398 return nullptr;
5399 }
5400 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005401 TIntermBinary *node = new TIntermBinary(op, left, right);
5402 node->setLine(loc);
5403
Olli Etuaho3fdec912016-08-18 15:08:06 +03005404 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005405 }
5406 return nullptr;
5407}
5408
Jamie Madillb98c3a82015-07-23 14:26:04 -04005409TIntermTyped *TParseContext::addAssign(TOperator op,
5410 TIntermTyped *left,
5411 TIntermTyped *right,
5412 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005413{
Olli Etuahocce89652017-06-19 16:04:09 +03005414 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005415 TIntermTyped *node = createAssign(op, left, right, loc);
5416 if (node == nullptr)
5417 {
5418 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005419 return left;
5420 }
5421 return node;
5422}
5423
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005424TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5425 TIntermTyped *right,
5426 const TSourceLoc &loc)
5427{
Corentin Wallez0d959252016-07-12 17:26:32 -04005428 // WebGL2 section 5.26, the following results in an error:
5429 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005430 if (mShaderSpec == SH_WEBGL2_SPEC &&
5431 (left->isArray() || left->getBasicType() == EbtVoid ||
5432 left->getType().isStructureContainingArrays() || right->isArray() ||
5433 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005434 {
5435 error(loc,
5436 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5437 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005438 }
5439
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005440 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5441 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5442 commaNode->getTypePointer()->setQualifier(resultQualifier);
5443 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005444}
5445
Olli Etuaho49300862015-02-20 14:54:49 +02005446TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5447{
5448 switch (op)
5449 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005450 case EOpContinue:
5451 if (mLoopNestingLevel <= 0)
5452 {
5453 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005454 }
5455 break;
5456 case EOpBreak:
5457 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5458 {
5459 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005460 }
5461 break;
5462 case EOpReturn:
5463 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5464 {
5465 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005466 }
5467 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005468 case EOpKill:
5469 if (mShaderType != GL_FRAGMENT_SHADER)
5470 {
5471 error(loc, "discard supported in fragment shaders only", "discard");
5472 }
5473 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005474 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005475 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005476 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005477 }
Olli Etuahocce89652017-06-19 16:04:09 +03005478 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005479}
5480
Jamie Madillb98c3a82015-07-23 14:26:04 -04005481TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005482 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005483 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005484{
Olli Etuahocce89652017-06-19 16:04:09 +03005485 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005486 {
Olli Etuahocce89652017-06-19 16:04:09 +03005487 ASSERT(op == EOpReturn);
5488 mFunctionReturnsValue = true;
5489 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5490 {
5491 error(loc, "void function cannot return a value", "return");
5492 }
5493 else if (*mCurrentFunctionType != expression->getType())
5494 {
5495 error(loc, "function return is not matching type:", "return");
5496 }
Olli Etuaho49300862015-02-20 14:54:49 +02005497 }
Olli Etuahocce89652017-06-19 16:04:09 +03005498 TIntermBranch *node = new TIntermBranch(op, expression);
5499 node->setLine(loc);
5500 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005501}
5502
Martin Radev84aa2dc2017-09-11 15:51:02 +03005503void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5504{
5505 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5506 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5507 bool isTextureGather = (name == "textureGather");
5508 bool isTextureGatherOffset = (name == "textureGatherOffset");
5509 if (isTextureGather || isTextureGatherOffset)
5510 {
5511 TIntermNode *componentNode = nullptr;
5512 TIntermSequence *arguments = functionCall->getSequence();
5513 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5514 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5515 ASSERT(sampler != nullptr);
5516 switch (sampler->getBasicType())
5517 {
5518 case EbtSampler2D:
5519 case EbtISampler2D:
5520 case EbtUSampler2D:
5521 case EbtSampler2DArray:
5522 case EbtISampler2DArray:
5523 case EbtUSampler2DArray:
5524 if ((isTextureGather && arguments->size() == 3u) ||
5525 (isTextureGatherOffset && arguments->size() == 4u))
5526 {
5527 componentNode = arguments->back();
5528 }
5529 break;
5530 case EbtSamplerCube:
5531 case EbtISamplerCube:
5532 case EbtUSamplerCube:
5533 ASSERT(!isTextureGatherOffset);
5534 if (arguments->size() == 3u)
5535 {
5536 componentNode = arguments->back();
5537 }
5538 break;
5539 case EbtSampler2DShadow:
5540 case EbtSampler2DArrayShadow:
5541 case EbtSamplerCubeShadow:
5542 break;
5543 default:
5544 UNREACHABLE();
5545 break;
5546 }
5547 if (componentNode)
5548 {
5549 const TIntermConstantUnion *componentConstantUnion =
5550 componentNode->getAsConstantUnion();
5551 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5552 {
5553 error(functionCall->getLine(), "Texture component must be a constant expression",
5554 name.c_str());
5555 }
5556 else
5557 {
5558 int component = componentConstantUnion->getIConst(0);
5559 if (component < 0 || component > 3)
5560 {
5561 error(functionCall->getLine(), "Component must be in the range [0;3]",
5562 name.c_str());
5563 }
5564 }
5565 }
5566 }
5567}
5568
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005569void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5570{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005571 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005572 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005573 TIntermNode *offset = nullptr;
5574 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005575 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005576 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5577 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5578 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005579 {
5580 offset = arguments->back();
5581 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005582 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005583 {
5584 // A bias parameter might follow the offset parameter.
5585 ASSERT(arguments->size() >= 3);
5586 offset = (*arguments)[2];
5587 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005588 else if (name == "textureGatherOffset")
5589 {
5590 ASSERT(arguments->size() >= 3u);
5591 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5592 ASSERT(sampler != nullptr);
5593 switch (sampler->getBasicType())
5594 {
5595 case EbtSampler2D:
5596 case EbtISampler2D:
5597 case EbtUSampler2D:
5598 case EbtSampler2DArray:
5599 case EbtISampler2DArray:
5600 case EbtUSampler2DArray:
5601 offset = (*arguments)[2];
5602 break;
5603 case EbtSampler2DShadow:
5604 case EbtSampler2DArrayShadow:
5605 offset = (*arguments)[3];
5606 break;
5607 default:
5608 UNREACHABLE();
5609 break;
5610 }
5611 useTextureGatherOffsetConstraints = true;
5612 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005613 if (offset != nullptr)
5614 {
5615 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5616 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5617 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005618 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005619 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005620 }
5621 else
5622 {
5623 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5624 size_t size = offsetConstantUnion->getType().getObjectSize();
5625 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005626 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5627 : mMinProgramTexelOffset;
5628 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5629 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005630 for (size_t i = 0u; i < size; ++i)
5631 {
5632 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005633 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005634 {
5635 std::stringstream tokenStream;
5636 tokenStream << offsetValue;
5637 std::string token = tokenStream.str();
5638 error(offset->getLine(), "Texture offset value out of valid range",
5639 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005640 }
5641 }
5642 }
5643 }
5644}
5645
Jiajia Qina3106c52017-11-03 09:39:39 +08005646void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5647{
5648 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5649 if (IsAtomicBuiltin(name))
5650 {
5651 TIntermSequence *arguments = functionCall->getSequence();
5652 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5653
5654 if (IsBufferOrSharedVariable(memNode))
5655 {
5656 return;
5657 }
5658
5659 while (memNode->getAsBinaryNode())
5660 {
5661 memNode = memNode->getAsBinaryNode()->getLeft();
5662 if (IsBufferOrSharedVariable(memNode))
5663 {
5664 return;
5665 }
5666 }
5667
5668 error(memNode->getLine(),
5669 "The value passed to the mem argument of an atomic memory function does not "
5670 "correspond to a buffer or shared variable.",
5671 functionCall->getFunctionSymbolInfo()->getName().c_str());
5672 }
5673}
5674
Martin Radev2cc85b32016-08-05 16:22:53 +03005675// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5676void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5677{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005678 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005679 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5680
5681 if (name.compare(0, 5, "image") == 0)
5682 {
5683 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005684 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005685
Olli Etuaho485eefd2017-02-14 17:40:06 +00005686 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005687
5688 if (name.compare(5, 5, "Store") == 0)
5689 {
5690 if (memoryQualifier.readonly)
5691 {
5692 error(imageNode->getLine(),
5693 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005694 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005695 }
5696 }
5697 else if (name.compare(5, 4, "Load") == 0)
5698 {
5699 if (memoryQualifier.writeonly)
5700 {
5701 error(imageNode->getLine(),
5702 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005703 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005704 }
5705 }
5706 }
5707}
5708
5709// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5710void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5711 const TFunction *functionDefinition,
5712 const TIntermAggregate *functionCall)
5713{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005714 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005715
5716 const TIntermSequence &arguments = *functionCall->getSequence();
5717
5718 ASSERT(functionDefinition->getParamCount() == arguments.size());
5719
5720 for (size_t i = 0; i < arguments.size(); ++i)
5721 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005722 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5723 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005724 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5725 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5726
5727 if (IsImage(functionArgumentType.getBasicType()))
5728 {
5729 const TMemoryQualifier &functionArgumentMemoryQualifier =
5730 functionArgumentType.getMemoryQualifier();
5731 const TMemoryQualifier &functionParameterMemoryQualifier =
5732 functionParameterType.getMemoryQualifier();
5733 if (functionArgumentMemoryQualifier.readonly &&
5734 !functionParameterMemoryQualifier.readonly)
5735 {
5736 error(functionCall->getLine(),
5737 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005738 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005739 }
5740
5741 if (functionArgumentMemoryQualifier.writeonly &&
5742 !functionParameterMemoryQualifier.writeonly)
5743 {
5744 error(functionCall->getLine(),
5745 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005746 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005747 }
Martin Radev049edfa2016-11-11 14:35:37 +02005748
5749 if (functionArgumentMemoryQualifier.coherent &&
5750 !functionParameterMemoryQualifier.coherent)
5751 {
5752 error(functionCall->getLine(),
5753 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005754 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005755 }
5756
5757 if (functionArgumentMemoryQualifier.volatileQualifier &&
5758 !functionParameterMemoryQualifier.volatileQualifier)
5759 {
5760 error(functionCall->getLine(),
5761 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005762 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005763 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005764 }
5765 }
5766}
5767
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005768TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005769{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005770 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005771}
5772
5773TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005774 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005775 TIntermNode *thisNode,
5776 const TSourceLoc &loc)
5777{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005778 if (thisNode != nullptr)
5779 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005780 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005781 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005782
5783 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005784 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005785 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005786 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005787 }
5788 else
5789 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005790 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005791 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5792 }
5793}
5794
5795TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5796 TIntermSequence *arguments,
5797 TIntermNode *thisNode,
5798 const TSourceLoc &loc)
5799{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005800 TIntermTyped *typedThis = thisNode->getAsTyped();
5801 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5802 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5803 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5804 // So accessing fnCall->getName() below is safe.
5805 if (fnCall->getName() != "length")
5806 {
5807 error(loc, "invalid method", fnCall->getName().c_str());
5808 }
5809 else if (!arguments->empty())
5810 {
5811 error(loc, "method takes no parameters", "length");
5812 }
5813 else if (typedThis == nullptr || !typedThis->isArray())
5814 {
5815 error(loc, "length can only be called on arrays", "length");
5816 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005817 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5818 mGeometryShaderInputPrimitiveType == EptUndefined)
5819 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005820 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005821 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5822 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005823 else
5824 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005825 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5826 node->setLine(loc);
5827 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005828 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005829 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005830}
5831
5832TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5833 TIntermSequence *arguments,
5834 const TSourceLoc &loc)
5835{
5836 // First find by unmangled name to check whether the function name has been
5837 // hidden by a variable name or struct typename.
5838 // If a function is found, check for one with a matching argument list.
5839 bool builtIn;
5840 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5841 if (symbol != nullptr && !symbol->isFunction())
5842 {
5843 error(loc, "function name expected", fnCall->getName().c_str());
5844 }
5845 else
5846 {
5847 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5848 mShaderVersion, &builtIn);
5849 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005850 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005851 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5852 }
5853 else
5854 {
5855 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005856 //
5857 // A declared function.
5858 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005859 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005860 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005861 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005862 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005863 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005864 if (builtIn && op != EOpNull)
5865 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005866 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005867 if (fnCandidate->getParamCount() == 1)
5868 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005869 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005870 TIntermNode *unaryParamNode = arguments->front();
5871 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005872 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005873 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005874 }
5875 else
5876 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005877 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005878 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005879 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005880
5881 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005882 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305883
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005884 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305885 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005886 // See if we can constant fold a built-in. Note that this may be possible
5887 // even if it is not const-qualified.
5888 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305889 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005890 else
5891 {
5892 return callNode;
5893 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005894 }
5895 }
5896 else
5897 {
5898 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005899 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005900
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005901 // If builtIn == false, the function is user defined - could be an overloaded
5902 // built-in as well.
5903 // if builtIn == true, it's a builtIn function with no op associated with it.
5904 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005905 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005906 {
Olli Etuahofe486322017-03-21 09:30:54 +00005907 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005908 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005909 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005910 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005911 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005912 }
5913 else
5914 {
Olli Etuahofe486322017-03-21 09:30:54 +00005915 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005916 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005917 }
5918
Jiajia Qinbc585152017-06-23 15:42:17 +08005919 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005920
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005921 callNode->setLine(loc);
5922
5923 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005924 }
5925 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005926 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005927
5928 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005929 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005930}
5931
Jamie Madillb98c3a82015-07-23 14:26:04 -04005932TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005933 TIntermTyped *trueExpression,
5934 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005935 const TSourceLoc &loc)
5936{
Olli Etuaho56229f12017-07-10 14:16:33 +03005937 if (!checkIsScalarBool(loc, cond))
5938 {
5939 return falseExpression;
5940 }
Olli Etuaho52901742015-04-15 13:42:45 +03005941
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005942 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005943 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005944 std::stringstream reasonStream;
5945 reasonStream << "mismatching ternary operator operand types '"
5946 << trueExpression->getCompleteString() << " and '"
5947 << falseExpression->getCompleteString() << "'";
5948 std::string reason = reasonStream.str();
5949 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005950 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005951 }
Olli Etuahode318b22016-10-25 16:18:25 +01005952 if (IsOpaqueType(trueExpression->getBasicType()))
5953 {
5954 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005955 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005956 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5957 // Note that structs containing opaque types don't need to be checked as structs are
5958 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005959 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005960 return falseExpression;
5961 }
5962
Jiajia Qinbc585152017-06-23 15:42:17 +08005963 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5964 falseExpression->getMemoryQualifier().writeonly)
5965 {
5966 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5967 return falseExpression;
5968 }
5969
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005970 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005971 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005972 // ESSL 3.00.6 section 5.7:
5973 // Ternary operator support is optional for arrays. No certainty that it works across all
5974 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5975 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005976 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005977 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005978 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005979 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005980 }
Olli Etuaho94050052017-05-08 14:17:44 +03005981 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5982 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005983 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005984 return falseExpression;
5985 }
5986
Corentin Wallez0d959252016-07-12 17:26:32 -04005987 // WebGL2 section 5.26, the following results in an error:
5988 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005989 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005990 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005991 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005992 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005993 }
5994
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005995 // Note that the node resulting from here can be a constant union without being qualified as
5996 // constant.
5997 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5998 node->setLine(loc);
5999
6000 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03006001}
Olli Etuaho49300862015-02-20 14:54:49 +02006002
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00006003//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006004// Parse an array of strings using yyparse.
6005//
6006// Returns 0 for success.
6007//
Jamie Madillb98c3a82015-07-23 14:26:04 -04006008int PaParseStrings(size_t count,
6009 const char *const string[],
6010 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05306011 TParseContext *context)
6012{
Yunchao He4f285442017-04-21 12:15:49 +08006013 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006014 return 1;
6015
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006016 if (glslang_initialize(context))
6017 return 1;
6018
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006019 int error = glslang_scan(count, string, length, context);
6020 if (!error)
6021 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006022
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006023 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006024
alokp@chromium.org6b495712012-06-29 00:06:58 +00006025 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006026}
Jamie Madill45bcc782016-11-07 13:58:48 -05006027
6028} // namespace sh