blob: 67207ca45c3d794ebbb167711ef6784d0f5d3ecd [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.
1931 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001932 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001933 if (!declareVariable(line, identifier, type, &variable))
1934 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001935 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001936 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937
Olli Etuahob0c645e2015-05-12 14:25:36 +03001938 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001939 if (symbolTable.atGlobalLevel() &&
1940 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001941 {
1942 // Error message does not completely match behavior with ESSL 1.00, but
1943 // we want to steer developers towards only using constant expressions.
1944 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001945 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001946 }
1947 if (globalInitWarning)
1948 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001949 warning(
1950 line,
1951 "global variable initializers should be constant expressions "
1952 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1953 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001954 }
1955
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001956 //
1957 // identifier must be of type constant, a global, or a temporary
1958 //
1959 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301960 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1961 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001962 error(line, " cannot initialize this type of qualifier ",
1963 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001964 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001965 }
1966 //
1967 // test for and propagate constant
1968 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001969
Arun Patole7e7e68d2015-05-22 12:02:25 +05301970 if (qualifier == EvqConst)
1971 {
1972 if (qualifier != initializer->getType().getQualifier())
1973 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001974 std::stringstream reasonStream;
1975 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1976 << "'";
1977 std::string reason = reasonStream.str();
1978 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001979 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001980 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001981 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301982 if (type != initializer->getType())
1983 {
1984 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001985 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001986 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001987 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001988 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001989
1990 // Save the constant folded value to the variable if possible. For example array
1991 // initializers are not folded, since that way copying the array literal to multiple places
1992 // in the shader is avoided.
1993 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1994 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301995 if (initializer->getAsConstantUnion())
1996 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001997 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001998 ASSERT(*initNode == nullptr);
1999 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302000 }
2001 else if (initializer->getAsSymbolNode())
2002 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002003 const TSymbol *symbol =
2004 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
2005 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002007 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002008 if (constArray)
2009 {
2010 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002011 ASSERT(*initNode == nullptr);
2012 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002013 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002014 }
2015 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002016
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002017 TIntermSymbol *intermSymbol =
2018 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
2019 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002020 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2021 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002022 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002023 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002024 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002025 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026
Olli Etuaho914b79a2017-06-19 16:03:19 +03002027 return true;
2028}
2029
2030TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2031 const TString &identifier,
2032 TIntermTyped *initializer,
2033 const TSourceLoc &loc)
2034{
2035 checkIsScalarBool(loc, pType);
2036 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002037 TType type(pType);
2038 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002039 {
2040 // The initializer is valid. The init condition needs to have a node - either the
2041 // initializer node, or a constant node in case the initialized variable is const and won't
2042 // be recorded in the AST.
2043 if (initNode == nullptr)
2044 {
2045 return initializer;
2046 }
2047 else
2048 {
2049 TIntermDeclaration *declaration = new TIntermDeclaration();
2050 declaration->appendDeclarator(initNode);
2051 return declaration;
2052 }
2053 }
2054 return nullptr;
2055}
2056
2057TIntermNode *TParseContext::addLoop(TLoopType type,
2058 TIntermNode *init,
2059 TIntermNode *cond,
2060 TIntermTyped *expr,
2061 TIntermNode *body,
2062 const TSourceLoc &line)
2063{
2064 TIntermNode *node = nullptr;
2065 TIntermTyped *typedCond = nullptr;
2066 if (cond)
2067 {
2068 typedCond = cond->getAsTyped();
2069 }
2070 if (cond == nullptr || typedCond)
2071 {
Olli Etuahocce89652017-06-19 16:04:09 +03002072 if (type == ELoopDoWhile)
2073 {
2074 checkIsScalarBool(line, typedCond);
2075 }
2076 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2077 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2078 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2079 !typedCond->isVector()));
2080
Olli Etuaho3ec75682017-07-05 17:02:55 +03002081 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002082 node->setLine(line);
2083 return node;
2084 }
2085
Olli Etuahocce89652017-06-19 16:04:09 +03002086 ASSERT(type != ELoopDoWhile);
2087
Olli Etuaho914b79a2017-06-19 16:03:19 +03002088 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2089 ASSERT(declaration);
2090 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2091 ASSERT(declarator->getLeft()->getAsSymbolNode());
2092
2093 // The condition is a declaration. In the AST representation we don't support declarations as
2094 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2095 // the loop.
2096 TIntermBlock *block = new TIntermBlock();
2097
2098 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2099 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2100 block->appendStatement(declareCondition);
2101
2102 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2103 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002104 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002105 block->appendStatement(loop);
2106 loop->setLine(line);
2107 block->setLine(line);
2108 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109}
2110
Olli Etuahocce89652017-06-19 16:04:09 +03002111TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2112 TIntermNodePair code,
2113 const TSourceLoc &loc)
2114{
Olli Etuaho56229f12017-07-10 14:16:33 +03002115 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002116
2117 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002118 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002119 {
2120 if (cond->getAsConstantUnion()->getBConst(0) == true)
2121 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002122 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002123 }
2124 else
2125 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002126 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002127 }
2128 }
2129
Olli Etuaho3ec75682017-07-05 17:02:55 +03002130 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002131 node->setLine(loc);
2132
2133 return node;
2134}
2135
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002136void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2137{
2138 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2139 typeSpecifier->getBasicType());
2140
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002141 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002142 {
2143 error(typeSpecifier->getLine(), "not supported", "first-class array");
2144 typeSpecifier->clearArrayness();
2145 }
2146}
2147
Martin Radev70866b82016-07-22 15:27:42 +03002148TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302149 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002150{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002151 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002152
Martin Radev70866b82016-07-22 15:27:42 +03002153 TPublicType returnType = typeSpecifier;
2154 returnType.qualifier = typeQualifier.qualifier;
2155 returnType.invariant = typeQualifier.invariant;
2156 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002157 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002158 returnType.precision = typeSpecifier.precision;
2159
2160 if (typeQualifier.precision != EbpUndefined)
2161 {
2162 returnType.precision = typeQualifier.precision;
2163 }
2164
Martin Radev4a9cd802016-09-01 16:51:51 +03002165 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2166 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002167
Martin Radev4a9cd802016-09-01 16:51:51 +03002168 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2169 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002170
Martin Radev4a9cd802016-09-01 16:51:51 +03002171 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002172
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002173 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002174 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002175 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002176 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002177 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002178 returnType.clearArrayness();
2179 }
2180
Martin Radev70866b82016-07-22 15:27:42 +03002181 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002182 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002183 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002184 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002185 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002186 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002187
Martin Radev70866b82016-07-22 15:27:42 +03002188 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002189 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002190 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002191 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002192 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002193 }
2194 }
2195 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002196 {
Martin Radev70866b82016-07-22 15:27:42 +03002197 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002198 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002199 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002200 }
Martin Radev70866b82016-07-22 15:27:42 +03002201 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2202 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002203 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002204 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2205 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002206 }
Martin Radev70866b82016-07-22 15:27:42 +03002207 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002208 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002209 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002210 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002211 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002212 }
2213
2214 return returnType;
2215}
2216
Olli Etuaho856c4972016-08-08 11:38:39 +03002217void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2218 const TPublicType &type,
2219 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002220{
2221 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002222 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002223 {
2224 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002225 }
2226
2227 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2228 switch (qualifier)
2229 {
2230 case EvqVertexIn:
2231 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002232 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002233 {
2234 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002235 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002236 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002237 return;
2238 case EvqFragmentOut:
2239 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002240 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002241 {
2242 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002243 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002244 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002245 return;
2246 default:
2247 break;
2248 }
2249
2250 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2251 // restrictions.
2252 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002253 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2254 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002255 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2256 {
2257 error(qualifierLocation, "must use 'flat' interpolation here",
2258 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002259 }
2260
Martin Radev4a9cd802016-09-01 16:51:51 +03002261 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002262 {
2263 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2264 // These restrictions are only implied by the ESSL 3.00 spec, but
2265 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002266 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 {
2268 error(qualifierLocation, "cannot be an array of structures",
2269 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002270 }
2271 if (type.isStructureContainingArrays())
2272 {
2273 error(qualifierLocation, "cannot be a structure containing an array",
2274 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002275 }
2276 if (type.isStructureContainingType(EbtStruct))
2277 {
2278 error(qualifierLocation, "cannot be a structure containing a structure",
2279 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002280 }
2281 if (type.isStructureContainingType(EbtBool))
2282 {
2283 error(qualifierLocation, "cannot be a structure containing a bool",
2284 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002285 }
2286 }
2287}
2288
Martin Radev2cc85b32016-08-05 16:22:53 +03002289void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2290{
2291 if (qualifier.getType() == QtStorage)
2292 {
2293 const TStorageQualifierWrapper &storageQualifier =
2294 static_cast<const TStorageQualifierWrapper &>(qualifier);
2295 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2296 !symbolTable.atGlobalLevel())
2297 {
2298 error(storageQualifier.getLine(),
2299 "Local variables can only use the const storage qualifier.",
2300 storageQualifier.getQualifierString().c_str());
2301 }
2302 }
2303}
2304
Olli Etuaho43364892017-02-13 16:00:12 +00002305void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002306 const TSourceLoc &location)
2307{
Jiajia Qinbc585152017-06-23 15:42:17 +08002308 const std::string reason(
2309 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2310 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002311 if (memoryQualifier.readonly)
2312 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002313 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002314 }
2315 if (memoryQualifier.writeonly)
2316 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002317 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002318 }
Martin Radev049edfa2016-11-11 14:35:37 +02002319 if (memoryQualifier.coherent)
2320 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002321 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002322 }
2323 if (memoryQualifier.restrictQualifier)
2324 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002325 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002326 }
2327 if (memoryQualifier.volatileQualifier)
2328 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002329 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002330 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002331}
2332
jchen104cdac9e2017-05-08 11:01:20 +08002333// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2334// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002335void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2336 const TSourceLoc &loc,
2337 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002338{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002339 if (!IsAtomicCounter(type->getBasicType()))
2340 {
2341 return;
2342 }
2343
2344 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2345 : kAtomicCounterSize;
2346 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2347 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002348 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002349 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002350 {
2351 offset = bindingState.appendSpan(size);
2352 }
2353 else
2354 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002355 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002356 }
2357 if (offset == -1)
2358 {
2359 error(loc, "Offset overlapping", "atomic counter");
2360 return;
2361 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002362 layoutQualifier.offset = offset;
2363 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002364}
2365
Olli Etuaho454c34c2017-10-25 16:35:56 +03002366void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2367 const char *token,
2368 TType *type)
2369{
2370 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2371 {
2372 if (type->isArray() && type->getOutermostArraySize() == 0u)
2373 {
2374 // Set size for the unsized geometry shader inputs if they are declared after a valid
2375 // input primitive declaration.
2376 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2377 {
2378 ASSERT(mGeometryShaderInputArraySize > 0u);
2379 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2380 }
2381 else
2382 {
2383 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2384 // An input can be declared without an array size if there is a previous layout
2385 // which specifies the size.
2386 error(location,
2387 "Missing a valid input primitive declaration before declaring an unsized "
2388 "array input",
2389 token);
2390 }
2391 }
2392 else if (type->isArray())
2393 {
2394 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2395 }
2396 else
2397 {
2398 error(location, "Geometry shader input variable must be declared as an array", token);
2399 }
2400 }
2401}
2402
Olli Etuaho13389b62016-10-16 11:48:18 +01002403TIntermDeclaration *TParseContext::parseSingleDeclaration(
2404 TPublicType &publicType,
2405 const TSourceLoc &identifierOrTypeLocation,
2406 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002407{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002408 TType type(publicType);
2409 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2410 mDirectiveHandler.pragma().stdgl.invariantAll)
2411 {
2412 TQualifier qualifier = type.getQualifier();
2413
2414 // The directive handler has already taken care of rejecting invalid uses of this pragma
2415 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2416 // affected variable declarations:
2417 //
2418 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2419 // elsewhere, in TranslatorGLSL.)
2420 //
2421 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2422 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2423 // the way this is currently implemented we have to enable this compiler option before
2424 // parsing the shader and determining the shading language version it uses. If this were
2425 // implemented as a post-pass, the workaround could be more targeted.
2426 //
2427 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2428 // the specification, but there are desktop OpenGL drivers that expect that this is the
2429 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2430 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2431 {
2432 type.setInvariant(true);
2433 }
2434 }
2435
Olli Etuaho454c34c2017-10-25 16:35:56 +03002436 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002437
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002438 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2439 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002440
Olli Etuahobab4c082015-04-24 16:38:49 +03002441 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002442 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002443
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002444 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002445 if (emptyDeclaration)
2446 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002447 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002448 // In most cases we don't need to create a symbol node for an empty declaration.
2449 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2450 if (type.getBasicType() == EbtStruct)
2451 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002452 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002453 }
jchen104cdac9e2017-05-08 11:01:20 +08002454 else if (IsAtomicCounter(publicType.getBasicType()))
2455 {
2456 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2457 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002458 }
2459 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002460 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002461 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002462
Olli Etuaho55bde912017-10-25 13:41:13 +03002463 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002464
Olli Etuaho55bc9052017-10-25 17:33:06 +03002465 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002466
Olli Etuaho2935c582015-04-08 14:32:06 +03002467 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002468 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002469
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002470 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002471 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002472 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002473 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002474 }
2475
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002476 TIntermDeclaration *declaration = new TIntermDeclaration();
2477 declaration->setLine(identifierOrTypeLocation);
2478 if (symbol)
2479 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002480 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002481 declaration->appendDeclarator(symbol);
2482 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002483 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002484}
2485
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002486TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2487 TPublicType &elementType,
2488 const TSourceLoc &identifierLocation,
2489 const TString &identifier,
2490 const TSourceLoc &indexLocation,
2491 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002492{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002493 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002494
Olli Etuaho55bde912017-10-25 13:41:13 +03002495 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002496 identifierLocation);
2497
Olli Etuaho55bde912017-10-25 13:41:13 +03002498 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002499
Olli Etuaho55bde912017-10-25 13:41:13 +03002500 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002501
Olli Etuaho55bde912017-10-25 13:41:13 +03002502 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002503 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002504
Olli Etuaho454c34c2017-10-25 16:35:56 +03002505 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002506
2507 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2508
Olli Etuaho55bc9052017-10-25 17:33:06 +03002509 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002510
Olli Etuaho2935c582015-04-08 14:32:06 +03002511 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002512 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002513
Olli Etuaho13389b62016-10-16 11:48:18 +01002514 TIntermDeclaration *declaration = new TIntermDeclaration();
2515 declaration->setLine(identifierLocation);
2516
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002517 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002518 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002519 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2520 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002521 declaration->appendDeclarator(symbol);
2522 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002523
Olli Etuaho13389b62016-10-16 11:48:18 +01002524 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002525}
2526
Olli Etuaho13389b62016-10-16 11:48:18 +01002527TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2528 const TSourceLoc &identifierLocation,
2529 const TString &identifier,
2530 const TSourceLoc &initLocation,
2531 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002532{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002533 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002534
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002535 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2536 identifierLocation);
2537
2538 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002539
Olli Etuaho13389b62016-10-16 11:48:18 +01002540 TIntermDeclaration *declaration = new TIntermDeclaration();
2541 declaration->setLine(identifierLocation);
2542
2543 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002544 TType type(publicType);
2545 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002546 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002547 if (initNode)
2548 {
2549 declaration->appendDeclarator(initNode);
2550 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002551 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002552 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002553}
2554
Olli Etuaho13389b62016-10-16 11:48:18 +01002555TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002556 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002557 const TSourceLoc &identifierLocation,
2558 const TString &identifier,
2559 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002560 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002561 const TSourceLoc &initLocation,
2562 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002563{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002564 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565
Olli Etuaho55bde912017-10-25 13:41:13 +03002566 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002567 identifierLocation);
2568
Olli Etuaho55bde912017-10-25 13:41:13 +03002569 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002570
Olli Etuaho55bde912017-10-25 13:41:13 +03002571 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572
Olli Etuaho55bde912017-10-25 13:41:13 +03002573 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002574 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002575
Olli Etuaho13389b62016-10-16 11:48:18 +01002576 TIntermDeclaration *declaration = new TIntermDeclaration();
2577 declaration->setLine(identifierLocation);
2578
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002579 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002580 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002581 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002582 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002583 if (initNode)
2584 {
2585 declaration->appendDeclarator(initNode);
2586 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002587 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002588
2589 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002590}
2591
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002592TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002593 const TTypeQualifierBuilder &typeQualifierBuilder,
2594 const TSourceLoc &identifierLoc,
2595 const TString *identifier,
2596 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002597{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002598 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002599
Martin Radev70866b82016-07-22 15:27:42 +03002600 if (!typeQualifier.invariant)
2601 {
2602 error(identifierLoc, "Expected invariant", identifier->c_str());
2603 return nullptr;
2604 }
2605 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2606 {
2607 return nullptr;
2608 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002609 if (!symbol)
2610 {
2611 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002612 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002613 }
Martin Radev70866b82016-07-22 15:27:42 +03002614 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002615 {
Martin Radev70866b82016-07-22 15:27:42 +03002616 error(identifierLoc, "invariant declaration specifies qualifier",
2617 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002618 }
Martin Radev70866b82016-07-22 15:27:42 +03002619 if (typeQualifier.precision != EbpUndefined)
2620 {
2621 error(identifierLoc, "invariant declaration specifies precision",
2622 getPrecisionString(typeQualifier.precision));
2623 }
2624 if (!typeQualifier.layoutQualifier.isEmpty())
2625 {
2626 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2627 }
2628
2629 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002630 if (!variable)
2631 {
2632 return nullptr;
2633 }
Martin Radev70866b82016-07-22 15:27:42 +03002634 const TType &type = variable->getType();
2635
2636 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2637 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002638 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002639
2640 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2641
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002642 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2643 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002644
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002645 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002646}
2647
Olli Etuaho13389b62016-10-16 11:48:18 +01002648void TParseContext::parseDeclarator(TPublicType &publicType,
2649 const TSourceLoc &identifierLocation,
2650 const TString &identifier,
2651 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002652{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002653 // If the declaration starting this declarator list was empty (example: int,), some checks were
2654 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002655 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002656 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002657 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2658 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002659 }
2660
Olli Etuaho856c4972016-08-08 11:38:39 +03002661 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002662
Olli Etuaho2935c582015-04-08 14:32:06 +03002663 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002664 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002665
2666 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2667
Olli Etuaho55bde912017-10-25 13:41:13 +03002668 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2669
Olli Etuaho55bc9052017-10-25 17:33:06 +03002670 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2671
Olli Etuaho43364892017-02-13 16:00:12 +00002672 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002673
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002674 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002675 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002676 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2677 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002678 declarationOut->appendDeclarator(symbol);
2679 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002680}
2681
Olli Etuaho55bde912017-10-25 13:41:13 +03002682void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002683 const TSourceLoc &identifierLocation,
2684 const TString &identifier,
2685 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002686 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002687 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002688{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002689 // If the declaration starting this declarator list was empty (example: int,), some checks were
2690 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002691 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002692 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002693 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002694 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002695 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002696
Olli Etuaho55bde912017-10-25 13:41:13 +03002697 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002698
Olli Etuaho55bde912017-10-25 13:41:13 +03002699 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002700 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002701 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002702 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002703
Olli Etuaho454c34c2017-10-25 16:35:56 +03002704 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2705
Olli Etuaho55bde912017-10-25 13:41:13 +03002706 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2707
Olli Etuaho55bc9052017-10-25 17:33:06 +03002708 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002709
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002710 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002711 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002712
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002713 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002714 {
2715 TIntermSymbol *symbol =
2716 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2717 symbol->setLine(identifierLocation);
2718 declarationOut->appendDeclarator(symbol);
2719 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002720 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002721}
2722
Olli Etuaho13389b62016-10-16 11:48:18 +01002723void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2724 const TSourceLoc &identifierLocation,
2725 const TString &identifier,
2726 const TSourceLoc &initLocation,
2727 TIntermTyped *initializer,
2728 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002729{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002730 // If the declaration starting this declarator list was empty (example: int,), some checks were
2731 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002732 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002733 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002734 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2735 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002736 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002737
Olli Etuaho856c4972016-08-08 11:38:39 +03002738 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002739
Olli Etuaho13389b62016-10-16 11:48:18 +01002740 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002741 TType type(publicType);
2742 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002743 {
2744 //
2745 // build the intermediate representation
2746 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002747 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002748 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002749 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002750 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002751 }
2752}
2753
Olli Etuaho55bde912017-10-25 13:41:13 +03002754void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002755 const TSourceLoc &identifierLocation,
2756 const TString &identifier,
2757 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002758 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002759 const TSourceLoc &initLocation,
2760 TIntermTyped *initializer,
2761 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002763 // If the declaration starting this declarator list was empty (example: int,), some checks were
2764 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002765 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002766 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002767 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002768 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002769 }
2770
Olli Etuaho55bde912017-10-25 13:41:13 +03002771 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002772
Olli Etuaho55bde912017-10-25 13:41:13 +03002773 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002774
Olli Etuaho55bde912017-10-25 13:41:13 +03002775 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002776 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002777
2778 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002779 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002780 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002781 {
2782 if (initNode)
2783 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002784 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002785 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002786 }
2787}
2788
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002789TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2790{
2791 // It's simpler to parse an empty statement as a constant expression rather than having a
2792 // different type of node just for empty statements, that will be pruned from the AST anyway.
2793 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2794 node->setLine(location);
2795 return node;
2796}
2797
jchen104cdac9e2017-05-08 11:01:20 +08002798void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2799 const TSourceLoc &location)
2800{
2801 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2802 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2803 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2804 {
2805 error(location, "Requires both binding and offset", "layout");
2806 return;
2807 }
2808 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2809}
2810
Olli Etuahocce89652017-06-19 16:04:09 +03002811void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2812 const TPublicType &type,
2813 const TSourceLoc &loc)
2814{
2815 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2816 !getFragmentPrecisionHigh())
2817 {
2818 error(loc, "precision is not supported in fragment shader", "highp");
2819 }
2820
2821 if (!CanSetDefaultPrecisionOnType(type))
2822 {
2823 error(loc, "illegal type argument for default precision qualifier",
2824 getBasicString(type.getBasicType()));
2825 return;
2826 }
2827 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2828}
2829
Shaob5cc1192017-07-06 10:47:20 +08002830bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2831{
2832 switch (typeQualifier.layoutQualifier.primitiveType)
2833 {
2834 case EptLines:
2835 case EptLinesAdjacency:
2836 case EptTriangles:
2837 case EptTrianglesAdjacency:
2838 return typeQualifier.qualifier == EvqGeometryIn;
2839
2840 case EptLineStrip:
2841 case EptTriangleStrip:
2842 return typeQualifier.qualifier == EvqGeometryOut;
2843
2844 case EptPoints:
2845 return true;
2846
2847 default:
2848 UNREACHABLE();
2849 return false;
2850 }
2851}
2852
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002853void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2854 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002855{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002856 if (mGeometryShaderInputArraySize == 0u)
2857 {
2858 mGeometryShaderInputArraySize = inputArraySize;
2859 }
2860 else if (mGeometryShaderInputArraySize != inputArraySize)
2861 {
2862 error(line,
2863 "Array size or input primitive declaration doesn't match the size of earlier sized "
2864 "array inputs.",
2865 "layout");
2866 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002867}
2868
Shaob5cc1192017-07-06 10:47:20 +08002869bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2870{
2871 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2872
2873 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2874
2875 if (layoutQualifier.maxVertices != -1)
2876 {
2877 error(typeQualifier.line,
2878 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2879 return false;
2880 }
2881
2882 // Set mGeometryInputPrimitiveType if exists
2883 if (layoutQualifier.primitiveType != EptUndefined)
2884 {
2885 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2886 {
2887 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2888 return false;
2889 }
2890
2891 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2892 {
2893 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002894 setGeometryShaderInputArraySize(
2895 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2896 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002897 }
2898 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2899 {
2900 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2901 "layout");
2902 return false;
2903 }
2904 }
2905
2906 // Set mGeometryInvocations if exists
2907 if (layoutQualifier.invocations > 0)
2908 {
2909 if (mGeometryShaderInvocations == 0)
2910 {
2911 mGeometryShaderInvocations = layoutQualifier.invocations;
2912 }
2913 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2914 {
2915 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2916 "layout");
2917 return false;
2918 }
2919 }
2920
2921 return true;
2922}
2923
2924bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2925{
2926 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2927
2928 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2929
2930 if (layoutQualifier.invocations > 0)
2931 {
2932 error(typeQualifier.line,
2933 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2934 return false;
2935 }
2936
2937 // Set mGeometryOutputPrimitiveType if exists
2938 if (layoutQualifier.primitiveType != EptUndefined)
2939 {
2940 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2941 {
2942 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2943 return false;
2944 }
2945
2946 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2947 {
2948 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2949 }
2950 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2951 {
2952 error(typeQualifier.line,
2953 "primitive doesn't match earlier output primitive declaration", "layout");
2954 return false;
2955 }
2956 }
2957
2958 // Set mGeometryMaxVertices if exists
2959 if (layoutQualifier.maxVertices > -1)
2960 {
2961 if (mGeometryShaderMaxVertices == -1)
2962 {
2963 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2964 }
2965 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2966 {
2967 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2968 "layout");
2969 return false;
2970 }
2971 }
2972
2973 return true;
2974}
2975
Martin Radev70866b82016-07-22 15:27:42 +03002976void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002977{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002978 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002979 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002980
Martin Radev70866b82016-07-22 15:27:42 +03002981 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2982 typeQualifier.line);
2983
Jamie Madillc2128ff2016-07-04 10:26:17 -04002984 // It should never be the case, but some strange parser errors can send us here.
2985 if (layoutQualifier.isEmpty())
2986 {
2987 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002988 return;
2989 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002990
Martin Radev802abe02016-08-04 17:48:32 +03002991 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002992 {
Olli Etuaho43364892017-02-13 16:00:12 +00002993 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002994 return;
2995 }
2996
Olli Etuaho43364892017-02-13 16:00:12 +00002997 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2998
2999 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003000
3001 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3002
Andrei Volykhina5527072017-03-22 16:46:30 +03003003 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3004
jchen104cdac9e2017-05-08 11:01:20 +08003005 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3006
Qin Jiajiaca68d982017-09-18 16:41:56 +08003007 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3008 typeQualifier.qualifier);
3009
Martin Radev802abe02016-08-04 17:48:32 +03003010 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003011 {
Martin Radev802abe02016-08-04 17:48:32 +03003012 if (mComputeShaderLocalSizeDeclared &&
3013 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3014 {
3015 error(typeQualifier.line, "Work group size does not match the previous declaration",
3016 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003017 return;
3018 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003019
Martin Radev802abe02016-08-04 17:48:32 +03003020 if (mShaderVersion < 310)
3021 {
3022 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003023 return;
3024 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003025
Martin Radev4c4c8e72016-08-04 12:25:34 +03003026 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003027 {
3028 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003029 return;
3030 }
3031
3032 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3033 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3034
3035 const TConstantUnion *maxComputeWorkGroupSizeData =
3036 maxComputeWorkGroupSize->getConstPointer();
3037
3038 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3039 {
3040 if (layoutQualifier.localSize[i] != -1)
3041 {
3042 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3043 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3044 if (mComputeShaderLocalSize[i] < 1 ||
3045 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3046 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003047 std::stringstream reasonStream;
3048 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3049 << maxComputeWorkGroupSizeValue;
3050 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003051
Olli Etuaho4de340a2016-12-16 09:32:03 +00003052 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003053 return;
3054 }
3055 }
3056 }
3057
3058 mComputeShaderLocalSizeDeclared = true;
3059 }
Shaob5cc1192017-07-06 10:47:20 +08003060 else if (typeQualifier.qualifier == EvqGeometryIn)
3061 {
3062 if (mShaderVersion < 310)
3063 {
3064 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3065 return;
3066 }
3067
3068 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3069 {
3070 return;
3071 }
3072 }
3073 else if (typeQualifier.qualifier == EvqGeometryOut)
3074 {
3075 if (mShaderVersion < 310)
3076 {
3077 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3078 "layout");
3079 return;
3080 }
3081
3082 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3083 {
3084 return;
3085 }
3086 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003087 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3088 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003089 {
3090 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3091 // specification.
3092 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3093 {
3094 error(typeQualifier.line, "Number of views does not match the previous declaration",
3095 "layout");
3096 return;
3097 }
3098
3099 if (layoutQualifier.numViews == -1)
3100 {
3101 error(typeQualifier.line, "No num_views specified", "layout");
3102 return;
3103 }
3104
3105 if (layoutQualifier.numViews > mMaxNumViews)
3106 {
3107 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3108 "layout");
3109 return;
3110 }
3111
3112 mNumViews = layoutQualifier.numViews;
3113 }
Martin Radev802abe02016-08-04 17:48:32 +03003114 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003115 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003116 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003117 {
Martin Radev802abe02016-08-04 17:48:32 +03003118 return;
3119 }
3120
Jiajia Qinbc585152017-06-23 15:42:17 +08003121 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003122 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003123 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003124 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003125 return;
3126 }
3127
3128 if (mShaderVersion < 300)
3129 {
3130 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3131 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003132 return;
3133 }
3134
Olli Etuaho09b04a22016-12-15 13:30:26 +00003135 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003136
3137 if (layoutQualifier.matrixPacking != EmpUnspecified)
3138 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003139 if (typeQualifier.qualifier == EvqUniform)
3140 {
3141 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3142 }
3143 else if (typeQualifier.qualifier == EvqBuffer)
3144 {
3145 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3146 }
Martin Radev802abe02016-08-04 17:48:32 +03003147 }
3148
3149 if (layoutQualifier.blockStorage != EbsUnspecified)
3150 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003151 if (typeQualifier.qualifier == EvqUniform)
3152 {
3153 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3154 }
3155 else if (typeQualifier.qualifier == EvqBuffer)
3156 {
3157 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3158 }
Martin Radev802abe02016-08-04 17:48:32 +03003159 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003160 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003161}
3162
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003163TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3164 const TFunction &function,
3165 const TSourceLoc &location,
3166 bool insertParametersToSymbolTable)
3167{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003168 checkIsNotReserved(location, function.getName());
3169
Olli Etuahofe486322017-03-21 09:30:54 +00003170 TIntermFunctionPrototype *prototype =
3171 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003172 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3173 // point to the data that already exists in the symbol table.
3174 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3175 prototype->setLine(location);
3176
3177 for (size_t i = 0; i < function.getParamCount(); i++)
3178 {
3179 const TConstParameter &param = function.getParam(i);
3180
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003181 TIntermSymbol *symbol = nullptr;
3182
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003183 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3184 // be used for unused args).
3185 if (param.name != nullptr)
3186 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003187 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003188 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003189 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003190 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3191 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003192 {
3193 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3194 variable->getType());
3195 }
3196 else
3197 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003198 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003199 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003200 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003201 // Unsized type of a named parameter should have already been checked and sanitized.
3202 ASSERT(!param.type->isUnsizedArray());
3203 }
3204 else
3205 {
3206 if (param.type->isUnsizedArray())
3207 {
3208 error(location, "function parameter array must be sized at compile time", "[]");
3209 // We don't need to size the arrays since the parameter is unnamed and hence
3210 // inaccessible.
3211 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003212 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003213 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003214 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003215 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3216 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003217 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003218 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003219 symbol->setLine(location);
3220 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003221 }
3222 return prototype;
3223}
3224
Olli Etuaho16c745a2017-01-16 17:02:27 +00003225TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3226 const TFunction &parsedFunction,
3227 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003228{
Olli Etuaho476197f2016-10-11 13:59:08 +01003229 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3230 // first declaration. Either way the instance in the symbol table is used to track whether the
3231 // function is declared multiple times.
3232 TFunction *function = static_cast<TFunction *>(
3233 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3234 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003235 {
3236 // ESSL 1.00.17 section 4.2.7.
3237 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3238 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003239 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003240 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003241
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003242 TIntermFunctionPrototype *prototype =
3243 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003244
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003245 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003246
3247 if (!symbolTable.atGlobalLevel())
3248 {
3249 // ESSL 3.00.4 section 4.2.4.
3250 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003251 }
3252
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003253 return prototype;
3254}
3255
Olli Etuaho336b1472016-10-05 16:37:55 +01003256TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003257 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003258 TIntermBlock *functionBody,
3259 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003260{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003261 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003262 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3263 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003264 error(location, "function does not return a value:",
3265 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003266 }
3267
Olli Etuahof51fdd22016-10-03 10:03:40 +01003268 if (functionBody == nullptr)
3269 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003270 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003271 functionBody->setLine(location);
3272 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003273 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003274 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003275 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003276
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003277 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003278 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003279}
3280
Olli Etuaho476197f2016-10-11 13:59:08 +01003281void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3282 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003283 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003284{
Olli Etuaho476197f2016-10-11 13:59:08 +01003285 ASSERT(function);
3286 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003287 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003288 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003289
3290 if (builtIn)
3291 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003292 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003293 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003294 else
Jamie Madill185fb402015-06-12 15:48:48 -04003295 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003296 TFunction *prevDec = static_cast<TFunction *>(
3297 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3298
3299 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3300 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3301 // occurance.
3302 if (*function != prevDec)
3303 {
3304 // Swap the parameters of the previous declaration to the parameters of the function
3305 // definition (parameter names may differ).
3306 prevDec->swapParameters(**function);
3307
3308 // The function definition will share the same symbol as any previous declaration.
3309 *function = prevDec;
3310 }
3311
3312 if ((*function)->isDefined())
3313 {
3314 error(location, "function already has a body", (*function)->getName().c_str());
3315 }
3316
3317 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003318 }
Jamie Madill185fb402015-06-12 15:48:48 -04003319
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003320 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003321 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003322 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003323
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003324 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003325 setLoopNestingLevel(0);
3326}
3327
Jamie Madillb98c3a82015-07-23 14:26:04 -04003328TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003329{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003330 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003331 // We don't know at this point whether this is a function definition or a prototype.
3332 // The definition production code will check for redefinitions.
3333 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003334 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003335 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3336 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003337 //
3338 TFunction *prevDec =
3339 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303340
Olli Etuahod80f2942017-11-06 12:44:45 +02003341 for (size_t i = 0u; i < function->getParamCount(); ++i)
3342 {
3343 auto &param = function->getParam(i);
3344 if (param.type->isStructSpecifier())
3345 {
3346 // ESSL 3.00.6 section 12.10.
3347 error(location, "Function parameter type cannot be a structure definition",
Olli Etuaho7af63722017-11-13 15:03:40 +02003348 function->getName().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003349 }
3350 }
3351
Martin Radevda6254b2016-12-14 17:00:36 +02003352 if (getShaderVersion() >= 300 &&
3353 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3354 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303355 {
Martin Radevda6254b2016-12-14 17:00:36 +02003356 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303357 // Therefore overloading or redefining builtin functions is an error.
3358 error(location, "Name of a built-in function cannot be redeclared as function",
3359 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303360 }
3361 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003362 {
3363 if (prevDec->getReturnType() != function->getReturnType())
3364 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003365 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003366 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003367 }
3368 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3369 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003370 if (prevDec->getParam(i).type->getQualifier() !=
3371 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003372 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003373 error(location,
3374 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003375 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003376 }
3377 }
3378 }
3379
3380 //
3381 // Check for previously declared variables using the same name.
3382 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003383 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003384 if (prevSym)
3385 {
3386 if (!prevSym->isFunction())
3387 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003388 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003389 }
3390 }
3391 else
3392 {
3393 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003394 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003395 }
3396
3397 // We're at the inner scope level of the function's arguments and body statement.
3398 // Add the function prototype to the surrounding scope instead.
3399 symbolTable.getOuterLevel()->insert(function);
3400
Olli Etuaho78d13742017-01-18 13:06:10 +00003401 // Raise error message if main function takes any parameters or return anything other than void
3402 if (function->getName() == "main")
3403 {
3404 if (function->getParamCount() > 0)
3405 {
3406 error(location, "function cannot take any parameter(s)", "main");
3407 }
3408 if (function->getReturnType().getBasicType() != EbtVoid)
3409 {
3410 error(location, "main function cannot return a value",
3411 function->getReturnType().getBasicString());
3412 }
3413 }
3414
Jamie Madill185fb402015-06-12 15:48:48 -04003415 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003416 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3417 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003418 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3419 //
3420 return function;
3421}
3422
Olli Etuaho9de84a52016-06-14 17:36:01 +03003423TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3424 const TString *name,
3425 const TSourceLoc &location)
3426{
3427 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3428 {
3429 error(location, "no qualifiers allowed for function return",
3430 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003431 }
3432 if (!type.layoutQualifier.isEmpty())
3433 {
3434 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003435 }
jchen10cc2a10e2017-05-03 14:05:12 +08003436 // make sure an opaque type is not involved as well...
3437 std::string reason(getBasicString(type.getBasicType()));
3438 reason += "s can't be function return values";
3439 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003440 if (mShaderVersion < 300)
3441 {
3442 // Array return values are forbidden, but there's also no valid syntax for declaring array
3443 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003444 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003445
3446 if (type.isStructureContainingArrays())
3447 {
3448 // ESSL 1.00.17 section 6.1 Function Definitions
3449 error(location, "structures containing arrays can't be function return values",
3450 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003451 }
3452 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003453
3454 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003455 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003456}
3457
Olli Etuahocce89652017-06-19 16:04:09 +03003458TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3459{
Olli Etuahocce89652017-06-19 16:04:09 +03003460 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003461 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003462}
3463
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003464TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003465{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003466 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003467 {
3468 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3469 "[]");
3470 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003471 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003472 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003473 error(publicType.getLine(), "constructor can't be a structure definition",
3474 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003475 }
3476
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003477 TType *type = new TType(publicType);
3478 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003479 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003480 error(publicType.getLine(), "cannot construct this type",
3481 getBasicString(publicType.getBasicType()));
3482 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003483 }
3484
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003485 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003486}
3487
Olli Etuaho55bde912017-10-25 13:41:13 +03003488void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3489 const char *errorMessage,
3490 const char *token,
3491 TType *arrayType)
3492{
3493 if (arrayType->isUnsizedArray())
3494 {
3495 error(line, errorMessage, token);
3496 arrayType->sizeUnsizedArrays(TVector<unsigned int>());
3497 }
3498}
3499
3500TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003501 const TString *name,
3502 const TSourceLoc &nameLoc)
3503{
Olli Etuaho55bde912017-10-25 13:41:13 +03003504 ASSERT(type);
3505 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3506 type);
3507 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003508 {
3509 error(nameLoc, "illegal use of type 'void'", name->c_str());
3510 }
3511 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003512 TParameter param = {name, type};
3513 return param;
3514}
3515
Olli Etuaho55bde912017-10-25 13:41:13 +03003516TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3517 const TString *name,
3518 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003519{
Olli Etuaho55bde912017-10-25 13:41:13 +03003520 TType *type = new TType(publicType);
3521 return parseParameterDeclarator(type, name, nameLoc);
3522}
3523
3524TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3525 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003526 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003527 const TSourceLoc &arrayLoc,
3528 TPublicType *elementType)
3529{
3530 checkArrayElementIsNotArray(arrayLoc, *elementType);
3531 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003532 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003533 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003534}
3535
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003536bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3537 TType type,
3538 const TSourceLoc &line)
3539{
3540 if (arguments->empty())
3541 {
3542 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3543 return false;
3544 }
3545 for (TIntermNode *arg : *arguments)
3546 {
3547 TIntermTyped *element = arg->getAsTyped();
3548 ASSERT(element);
3549 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3550 if (dimensionalityFromElement > type.getArraySizes().size())
3551 {
3552 error(line, "constructing from a non-dereferenced array", "constructor");
3553 return false;
3554 }
3555 else if (dimensionalityFromElement < type.getArraySizes().size())
3556 {
3557 if (dimensionalityFromElement == 1u)
3558 {
3559 error(line, "implicitly sized array of arrays constructor argument is not an array",
3560 "constructor");
3561 }
3562 else
3563 {
3564 error(line,
3565 "implicitly sized array of arrays constructor argument dimensionality is too "
3566 "low",
3567 "constructor");
3568 }
3569 return false;
3570 }
3571 }
3572 return true;
3573}
3574
Jamie Madillb98c3a82015-07-23 14:26:04 -04003575// This function is used to test for the correctness of the parameters passed to various constructor
3576// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003577//
Olli Etuaho856c4972016-08-08 11:38:39 +03003578// Returns a node to add to the tree regardless of if an error was generated or not.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003579//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003580TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003581 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303582 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003583{
Olli Etuaho856c4972016-08-08 11:38:39 +03003584 if (type.isUnsizedArray())
3585 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003586 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003587 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003588 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003589 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003590 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003591 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3592 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003593 if (type.getOutermostArraySize() == 0u)
3594 {
3595 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3596 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003597 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3598 {
3599 if (type.getArraySizes()[i] == 0u)
3600 {
3601 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3602 }
3603 }
3604 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003605 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003606
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003607 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003608 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003609 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003610 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003611
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003612 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003613 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003614
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003615 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3616 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003617 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003618 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003619 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003620 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003621}
3622
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003623//
3624// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003625// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003626//
Olli Etuaho13389b62016-10-16 11:48:18 +01003627TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003628 const TTypeQualifierBuilder &typeQualifierBuilder,
3629 const TSourceLoc &nameLine,
3630 const TString &blockName,
3631 TFieldList *fieldList,
3632 const TString *instanceName,
3633 const TSourceLoc &instanceLine,
3634 TIntermTyped *arrayIndex,
3635 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003636{
Olli Etuaho856c4972016-08-08 11:38:39 +03003637 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003638
Olli Etuaho77ba4082016-12-16 12:01:18 +00003639 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003640
Jiajia Qinbc585152017-06-23 15:42:17 +08003641 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003642 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003643 error(typeQualifier.line,
3644 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3645 "3.10",
3646 getQualifierString(typeQualifier.qualifier));
3647 }
3648 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3649 {
3650 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003651 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003652 }
3653
Martin Radev70866b82016-07-22 15:27:42 +03003654 if (typeQualifier.invariant)
3655 {
3656 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3657 }
3658
Jiajia Qinbc585152017-06-23 15:42:17 +08003659 if (typeQualifier.qualifier != EvqBuffer)
3660 {
3661 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3662 }
Olli Etuaho43364892017-02-13 16:00:12 +00003663
jchen10af713a22017-04-19 09:10:56 +08003664 // add array index
3665 unsigned int arraySize = 0;
3666 if (arrayIndex != nullptr)
3667 {
3668 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3669 }
3670
3671 if (mShaderVersion < 310)
3672 {
3673 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3674 }
3675 else
3676 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003677 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3678 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003679 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003680
Andrei Volykhina5527072017-03-22 16:46:30 +03003681 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3682
Jamie Madill099c0f32013-06-20 11:55:52 -04003683 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003684 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003685 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3686 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003687
Jamie Madill099c0f32013-06-20 11:55:52 -04003688 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3689 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003690 if (typeQualifier.qualifier == EvqUniform)
3691 {
3692 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3693 }
3694 else if (typeQualifier.qualifier == EvqBuffer)
3695 {
3696 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3697 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003698 }
3699
Jamie Madill1566ef72013-06-20 11:55:54 -04003700 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3701 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003702 if (typeQualifier.qualifier == EvqUniform)
3703 {
3704 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3705 }
3706 else if (typeQualifier.qualifier == EvqBuffer)
3707 {
3708 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3709 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003710 }
3711
Olli Etuaho856c4972016-08-08 11:38:39 +03003712 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003713
Martin Radev2cc85b32016-08-05 16:22:53 +03003714 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3715
Olli Etuaho0f684632017-07-13 12:42:15 +03003716 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303717 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003718 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003719 }
3720
Jamie Madill98493dd2013-07-08 14:39:03 -04003721 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303722 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3723 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003724 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303725 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003726 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303727 {
jchen10cc2a10e2017-05-03 14:05:12 +08003728 std::string reason("unsupported type - ");
3729 reason += fieldType->getBasicString();
3730 reason += " types are not allowed in interface blocks";
3731 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003732 }
3733
Jamie Madill98493dd2013-07-08 14:39:03 -04003734 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003735 switch (qualifier)
3736 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003737 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003738 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003739 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003740 if (typeQualifier.qualifier == EvqBuffer)
3741 {
3742 error(field->line(), "invalid qualifier on shader storage block member",
3743 getQualifierString(qualifier));
3744 }
3745 break;
3746 case EvqBuffer:
3747 if (typeQualifier.qualifier == EvqUniform)
3748 {
3749 error(field->line(), "invalid qualifier on uniform block member",
3750 getQualifierString(qualifier));
3751 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003752 break;
3753 default:
3754 error(field->line(), "invalid qualifier on interface block member",
3755 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003756 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003757 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003758
Martin Radev70866b82016-07-22 15:27:42 +03003759 if (fieldType->isInvariant())
3760 {
3761 error(field->line(), "invalid qualifier on interface block member", "invariant");
3762 }
3763
Jamie Madilla5efff92013-06-06 11:56:47 -04003764 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003765 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003766 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003767 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003768
Jamie Madill98493dd2013-07-08 14:39:03 -04003769 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003770 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003771 error(field->line(), "invalid layout qualifier: cannot be used here",
3772 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003773 }
3774
Jamie Madill98493dd2013-07-08 14:39:03 -04003775 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003776 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003777 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003778 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003779 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003780 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003781 warning(field->line(),
3782 "extraneous layout qualifier: only has an effect on matrix types",
3783 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003784 }
3785
Jamie Madill98493dd2013-07-08 14:39:03 -04003786 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003787
Olli Etuahoebee5b32017-11-23 12:56:32 +02003788 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3789 typeQualifier.qualifier != EvqBuffer)
3790 {
3791 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3792 checkIsNotUnsizedArray(field->line(),
3793 "array members of interface blocks must specify a size",
3794 field->name().c_str(), field->type());
3795 }
3796
Jiajia Qinbc585152017-06-23 15:42:17 +08003797 if (typeQualifier.qualifier == EvqBuffer)
3798 {
3799 // set memory qualifiers
3800 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3801 // qualified with a memory qualifier, it is as if all of its members were declared with
3802 // the same memory qualifier.
3803 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3804 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3805 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3806 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3807 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3808 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3809 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3810 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3811 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3812 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3813 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003814 }
3815
Jamie Madillb98c3a82015-07-23 14:26:04 -04003816 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003817 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003818 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3819 if (arrayIndex != nullptr)
3820 {
3821 interfaceBlockType.makeArray(arraySize);
3822 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003823
3824 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003825 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003826
Jamie Madill98493dd2013-07-08 14:39:03 -04003827 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003828 {
3829 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003830 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3831 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003832 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303833 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003834
3835 // set parent pointer of the field variable
3836 fieldType->setInterfaceBlock(interfaceBlock);
3837
Olli Etuaho0f684632017-07-13 12:42:15 +03003838 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003839
Olli Etuaho0f684632017-07-13 12:42:15 +03003840 if (fieldVariable)
3841 {
3842 fieldVariable->setQualifier(typeQualifier.qualifier);
3843 }
3844 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303845 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003846 error(field->line(), "redefinition of an interface block member name",
3847 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003848 }
3849 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003850 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003851 }
3852 else
3853 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003854 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003855
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003856 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003857 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3858 if (instanceTypeDef)
3859 {
3860 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003861 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003862 }
3863 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303864 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003865 error(instanceLine, "redefinition of an interface block instance name",
3866 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003867 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003868 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003869 }
3870
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003871 TIntermDeclaration *declaration = nullptr;
3872
3873 if (symbolId)
3874 {
3875 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3876 blockSymbol->setLine(typeQualifier.line);
3877 declaration = new TIntermDeclaration();
3878 declaration->appendDeclarator(blockSymbol);
3879 declaration->setLine(nameLine);
3880 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003881
3882 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003883 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003884}
3885
Olli Etuaho383b7912016-08-05 11:22:59 +03003886void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003887{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003888 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003889
3890 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003891 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303892 if (mStructNestingLevel > 1)
3893 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003894 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003895 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003896}
3897
3898void TParseContext::exitStructDeclaration()
3899{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003900 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003901}
3902
Olli Etuaho8a176262016-08-16 14:23:01 +03003903void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003904{
Jamie Madillacb4b812016-11-07 13:50:29 -05003905 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303906 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003907 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003908 }
3909
Arun Patole7e7e68d2015-05-22 12:02:25 +05303910 if (field.type()->getBasicType() != EbtStruct)
3911 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003912 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003913 }
3914
3915 // We're already inside a structure definition at this point, so add
3916 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303917 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3918 {
Jamie Madill41a49272014-03-18 16:10:13 -04003919 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003920 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3921 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003922 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003923 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003924 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003925 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003926}
3927
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003928//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003929// Parse an array index expression
3930//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003931TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3932 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303933 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003934{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003935 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3936 {
3937 if (baseExpression->getAsSymbolNode())
3938 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303939 error(location, " left of '[' is not of type array, matrix, or vector ",
3940 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003941 }
3942 else
3943 {
3944 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3945 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003946
Olli Etuaho3ec75682017-07-05 17:02:55 +03003947 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003948 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003949
Jiawei Shaod8105a02017-08-08 09:54:36 +08003950 if (baseExpression->getQualifier() == EvqPerVertexIn)
3951 {
3952 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3953 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3954 {
3955 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3956 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3957 }
3958 }
3959
Jamie Madill21c1e452014-12-29 11:33:41 -05003960 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3961
Olli Etuaho36b05142015-11-12 13:10:42 +02003962 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3963 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3964 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3965 // index is a constant expression.
3966 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3967 {
3968 if (baseExpression->isInterfaceBlock())
3969 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003970 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3971 switch (baseExpression->getQualifier())
3972 {
3973 case EvqPerVertexIn:
3974 break;
3975 case EvqUniform:
3976 case EvqBuffer:
3977 error(location,
3978 "array indexes for uniform block arrays and shader storage block arrays "
3979 "must be constant integral expressions",
3980 "[");
3981 break;
3982 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003983 // We can reach here only in error cases.
3984 ASSERT(mDiagnostics->numErrors() > 0);
3985 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003986 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003987 }
3988 else if (baseExpression->getQualifier() == EvqFragmentOut)
3989 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003990 error(location,
3991 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003992 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003993 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3994 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003995 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003996 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003997 }
3998
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003999 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04004000 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004001 // If an out-of-range index is not qualified as constant, the behavior in the spec is
4002 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
4003 // constant fold expressions that are not constant expressions). The most compatible way to
4004 // handle this case is to report a warning instead of an error and force the index to be in
4005 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004006 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03004007 int index = 0;
4008 if (indexConstantUnion->getBasicType() == EbtInt)
4009 {
4010 index = indexConstantUnion->getIConst(0);
4011 }
4012 else if (indexConstantUnion->getBasicType() == EbtUInt)
4013 {
4014 index = static_cast<int>(indexConstantUnion->getUConst(0));
4015 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004016
4017 int safeIndex = -1;
4018
Olli Etuahoebee5b32017-11-23 12:56:32 +02004019 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004020 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004021 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4022 safeIndex = 0;
4023 }
4024
4025 if (!baseExpression->getType().isUnsizedArray())
4026 {
4027 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004028 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004029 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004030 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004031 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4032 {
4033 outOfRangeError(outOfRangeIndexIsError, location,
4034 "array index for gl_FragData must be zero when "
4035 "GL_EXT_draw_buffers is disabled",
4036 "[]");
4037 safeIndex = 0;
4038 }
4039 }
4040 // Only do generic out-of-range check if similar error hasn't already been reported.
4041 if (safeIndex < 0)
4042 {
4043 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4044 baseExpression->getOutermostArraySize(),
4045 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004046 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03004047 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004048 else if (baseExpression->isMatrix())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004049 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004050 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4051 baseExpression->getType().getCols(),
4052 "matrix field selection out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004053 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004054 else if (baseExpression->isVector())
4055 {
4056 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4057 baseExpression->getType().getNominalSize(),
4058 "vector field selection out of range");
4059 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004060
Olli Etuahoebee5b32017-11-23 12:56:32 +02004061 ASSERT(safeIndex >= 0);
4062 // Data of constant unions can't be changed, because it may be shared with other
4063 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4064 // sanitized object.
4065 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4066 {
4067 TConstantUnion *safeConstantUnion = new TConstantUnion();
4068 safeConstantUnion->setIConst(safeIndex);
4069 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4070 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4071 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004072
Olli Etuahoebee5b32017-11-23 12:56:32 +02004073 TIntermBinary *node =
4074 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4075 node->setLine(location);
4076 return node->fold(mDiagnostics);
4077 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004078 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004079
4080 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4081 node->setLine(location);
4082 // Indirect indexing can never be constant folded.
4083 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004084}
4085
Olli Etuahoebee5b32017-11-23 12:56:32 +02004086int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4087 const TSourceLoc &location,
4088 int index,
4089 int arraySize,
4090 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004091{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004092 // Should not reach here with an unsized / runtime-sized array.
4093 ASSERT(arraySize > 0);
4094 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004095 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004096 std::stringstream reasonStream;
4097 reasonStream << reason << " '" << index << "'";
4098 std::string token = reasonStream.str();
4099 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004100 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004101 }
4102 return index;
4103}
4104
Jamie Madillb98c3a82015-07-23 14:26:04 -04004105TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4106 const TSourceLoc &dotLocation,
4107 const TString &fieldString,
4108 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004109{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004110 if (baseExpression->isArray())
4111 {
4112 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004113 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004114 }
4115
4116 if (baseExpression->isVector())
4117 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004118 TVector<int> fieldOffsets;
4119 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4120 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004122 fieldOffsets.resize(1);
4123 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004125 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4126 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004127
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004128 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004129 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004130 else if (baseExpression->getBasicType() == EbtStruct)
4131 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304132 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004133 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004134 {
4135 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004136 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004137 }
4138 else
4139 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004140 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004141 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004142 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004143 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004144 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004145 {
4146 fieldFound = true;
4147 break;
4148 }
4149 }
4150 if (fieldFound)
4151 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004152 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004153 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004154 TIntermBinary *node =
4155 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4156 node->setLine(dotLocation);
4157 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004158 }
4159 else
4160 {
4161 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004162 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004163 }
4164 }
4165 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004166 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004167 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304168 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004169 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004170 {
4171 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004172 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004173 }
4174 else
4175 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004176 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004177 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004178 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004179 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004180 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004181 {
4182 fieldFound = true;
4183 break;
4184 }
4185 }
4186 if (fieldFound)
4187 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004188 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004189 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004190 TIntermBinary *node =
4191 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4192 node->setLine(dotLocation);
4193 // Indexing interface blocks can never be constant folded.
4194 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004195 }
4196 else
4197 {
4198 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004199 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004200 }
4201 }
4202 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004203 else
4204 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004205 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004206 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004207 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304208 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004209 }
4210 else
4211 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304212 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004213 " field selection requires structure, vector, or interface block on left hand "
4214 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304215 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004216 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004217 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004218 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004219}
4220
Jamie Madillb98c3a82015-07-23 14:26:04 -04004221TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4222 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004223{
Jamie Madill2f294c92017-11-20 14:47:26 -05004224 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004225
4226 if (qualifierType == "shared")
4227 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004228 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004229 {
4230 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4231 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004232 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004233 }
4234 else if (qualifierType == "packed")
4235 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004236 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004237 {
4238 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4239 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004240 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004241 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004242 else if (qualifierType == "std430")
4243 {
4244 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4245 qualifier.blockStorage = EbsStd430;
4246 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004247 else if (qualifierType == "std140")
4248 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004249 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004250 }
4251 else if (qualifierType == "row_major")
4252 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004253 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004254 }
4255 else if (qualifierType == "column_major")
4256 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004257 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004258 }
4259 else if (qualifierType == "location")
4260 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004261 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4262 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004263 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004264 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004265 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004266 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4267 {
4268 qualifier.yuv = true;
4269 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004270 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004271 else if (qualifierType == "rgba32f")
4272 {
4273 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4274 qualifier.imageInternalFormat = EiifRGBA32F;
4275 }
4276 else if (qualifierType == "rgba16f")
4277 {
4278 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4279 qualifier.imageInternalFormat = EiifRGBA16F;
4280 }
4281 else if (qualifierType == "r32f")
4282 {
4283 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4284 qualifier.imageInternalFormat = EiifR32F;
4285 }
4286 else if (qualifierType == "rgba8")
4287 {
4288 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4289 qualifier.imageInternalFormat = EiifRGBA8;
4290 }
4291 else if (qualifierType == "rgba8_snorm")
4292 {
4293 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4294 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4295 }
4296 else if (qualifierType == "rgba32i")
4297 {
4298 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4299 qualifier.imageInternalFormat = EiifRGBA32I;
4300 }
4301 else if (qualifierType == "rgba16i")
4302 {
4303 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4304 qualifier.imageInternalFormat = EiifRGBA16I;
4305 }
4306 else if (qualifierType == "rgba8i")
4307 {
4308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4309 qualifier.imageInternalFormat = EiifRGBA8I;
4310 }
4311 else if (qualifierType == "r32i")
4312 {
4313 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4314 qualifier.imageInternalFormat = EiifR32I;
4315 }
4316 else if (qualifierType == "rgba32ui")
4317 {
4318 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4319 qualifier.imageInternalFormat = EiifRGBA32UI;
4320 }
4321 else if (qualifierType == "rgba16ui")
4322 {
4323 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4324 qualifier.imageInternalFormat = EiifRGBA16UI;
4325 }
4326 else if (qualifierType == "rgba8ui")
4327 {
4328 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4329 qualifier.imageInternalFormat = EiifRGBA8UI;
4330 }
4331 else if (qualifierType == "r32ui")
4332 {
4333 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4334 qualifier.imageInternalFormat = EiifR32UI;
4335 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004336 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4337 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004338 {
4339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4340 qualifier.primitiveType = EptPoints;
4341 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004342 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4343 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004344 {
4345 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4346 qualifier.primitiveType = EptLines;
4347 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004348 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4349 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004350 {
4351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4352 qualifier.primitiveType = EptLinesAdjacency;
4353 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004354 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4355 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004356 {
4357 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4358 qualifier.primitiveType = EptTriangles;
4359 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004360 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4361 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004362 {
4363 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4364 qualifier.primitiveType = EptTrianglesAdjacency;
4365 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004366 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4367 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004368 {
4369 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4370 qualifier.primitiveType = EptLineStrip;
4371 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004372 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4373 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004374 {
4375 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4376 qualifier.primitiveType = EptTriangleStrip;
4377 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004378
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004379 else
4380 {
4381 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004382 }
4383
Jamie Madilla5efff92013-06-06 11:56:47 -04004384 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004385}
4386
Martin Radev802abe02016-08-04 17:48:32 +03004387void TParseContext::parseLocalSize(const TString &qualifierType,
4388 const TSourceLoc &qualifierTypeLine,
4389 int intValue,
4390 const TSourceLoc &intValueLine,
4391 const std::string &intValueString,
4392 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004393 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004394{
Olli Etuaho856c4972016-08-08 11:38:39 +03004395 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004396 if (intValue < 1)
4397 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004398 std::stringstream reasonStream;
4399 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4400 std::string reason = reasonStream.str();
4401 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004402 }
4403 (*localSize)[index] = intValue;
4404}
4405
Olli Etuaho09b04a22016-12-15 13:30:26 +00004406void TParseContext::parseNumViews(int intValue,
4407 const TSourceLoc &intValueLine,
4408 const std::string &intValueString,
4409 int *numViews)
4410{
4411 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4412 // specification.
4413 if (intValue < 1)
4414 {
4415 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4416 }
4417 *numViews = intValue;
4418}
4419
Shaob5cc1192017-07-06 10:47:20 +08004420void TParseContext::parseInvocations(int intValue,
4421 const TSourceLoc &intValueLine,
4422 const std::string &intValueString,
4423 int *numInvocations)
4424{
4425 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4426 // it doesn't make sense to accept invocations <= 0.
4427 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4428 {
4429 error(intValueLine,
4430 "out of range: invocations must be in the range of [1, "
4431 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4432 intValueString.c_str());
4433 }
4434 else
4435 {
4436 *numInvocations = intValue;
4437 }
4438}
4439
4440void TParseContext::parseMaxVertices(int intValue,
4441 const TSourceLoc &intValueLine,
4442 const std::string &intValueString,
4443 int *maxVertices)
4444{
4445 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4446 // it doesn't make sense to accept max_vertices < 0.
4447 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4448 {
4449 error(
4450 intValueLine,
4451 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4452 intValueString.c_str());
4453 }
4454 else
4455 {
4456 *maxVertices = intValue;
4457 }
4458}
4459
Jamie Madillb98c3a82015-07-23 14:26:04 -04004460TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4461 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004462 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304463 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004464{
Jamie Madill2f294c92017-11-20 14:47:26 -05004465 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004466
Martin Radev802abe02016-08-04 17:48:32 +03004467 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004468
Martin Radev802abe02016-08-04 17:48:32 +03004469 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004470 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004471 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004472 if (intValue < 0)
4473 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004474 error(intValueLine, "out of range: location must be non-negative",
4475 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004476 }
4477 else
4478 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004479 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004480 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004481 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004482 }
Olli Etuaho43364892017-02-13 16:00:12 +00004483 else if (qualifierType == "binding")
4484 {
4485 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4486 if (intValue < 0)
4487 {
4488 error(intValueLine, "out of range: binding must be non-negative",
4489 intValueString.c_str());
4490 }
4491 else
4492 {
4493 qualifier.binding = intValue;
4494 }
4495 }
jchen104cdac9e2017-05-08 11:01:20 +08004496 else if (qualifierType == "offset")
4497 {
4498 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4499 if (intValue < 0)
4500 {
4501 error(intValueLine, "out of range: offset must be non-negative",
4502 intValueString.c_str());
4503 }
4504 else
4505 {
4506 qualifier.offset = intValue;
4507 }
4508 }
Martin Radev802abe02016-08-04 17:48:32 +03004509 else if (qualifierType == "local_size_x")
4510 {
4511 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4512 &qualifier.localSize);
4513 }
4514 else if (qualifierType == "local_size_y")
4515 {
4516 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4517 &qualifier.localSize);
4518 }
4519 else if (qualifierType == "local_size_z")
4520 {
4521 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4522 &qualifier.localSize);
4523 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004524 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004525 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004526 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4527 {
4528 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4529 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004530 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004531 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4532 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004533 {
4534 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4535 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004536 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4537 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004538 {
4539 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4540 }
4541
Martin Radev802abe02016-08-04 17:48:32 +03004542 else
4543 {
4544 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004545 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004546
Jamie Madilla5efff92013-06-06 11:56:47 -04004547 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004548}
4549
Olli Etuaho613b9592016-09-05 12:05:53 +03004550TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4551{
4552 return new TTypeQualifierBuilder(
4553 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4554 mShaderVersion);
4555}
4556
Olli Etuahocce89652017-06-19 16:04:09 +03004557TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4558 const TSourceLoc &loc)
4559{
4560 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4561 return new TStorageQualifierWrapper(qualifier, loc);
4562}
4563
4564TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4565{
4566 if (getShaderType() == GL_VERTEX_SHADER)
4567 {
4568 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4569 }
4570 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4571}
4572
4573TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4574{
4575 if (declaringFunction())
4576 {
4577 return new TStorageQualifierWrapper(EvqIn, loc);
4578 }
Shaob5cc1192017-07-06 10:47:20 +08004579
4580 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004581 {
Shaob5cc1192017-07-06 10:47:20 +08004582 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004583 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004584 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004585 {
4586 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4587 }
4588 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004589 }
Shaob5cc1192017-07-06 10:47:20 +08004590 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004591 {
Shaob5cc1192017-07-06 10:47:20 +08004592 if (mShaderVersion < 300)
4593 {
4594 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4595 }
4596 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004597 }
Shaob5cc1192017-07-06 10:47:20 +08004598 case GL_COMPUTE_SHADER:
4599 {
4600 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4601 }
4602 case GL_GEOMETRY_SHADER_OES:
4603 {
4604 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4605 }
4606 default:
4607 {
4608 UNREACHABLE();
4609 return new TStorageQualifierWrapper(EvqLast, loc);
4610 }
Olli Etuahocce89652017-06-19 16:04:09 +03004611 }
Olli Etuahocce89652017-06-19 16:04:09 +03004612}
4613
4614TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4615{
4616 if (declaringFunction())
4617 {
4618 return new TStorageQualifierWrapper(EvqOut, loc);
4619 }
Shaob5cc1192017-07-06 10:47:20 +08004620 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004621 {
Shaob5cc1192017-07-06 10:47:20 +08004622 case GL_VERTEX_SHADER:
4623 {
4624 if (mShaderVersion < 300)
4625 {
4626 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4627 }
4628 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4629 }
4630 case GL_FRAGMENT_SHADER:
4631 {
4632 if (mShaderVersion < 300)
4633 {
4634 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4635 }
4636 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4637 }
4638 case GL_COMPUTE_SHADER:
4639 {
4640 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4641 return new TStorageQualifierWrapper(EvqLast, loc);
4642 }
4643 case GL_GEOMETRY_SHADER_OES:
4644 {
4645 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4646 }
4647 default:
4648 {
4649 UNREACHABLE();
4650 return new TStorageQualifierWrapper(EvqLast, loc);
4651 }
Olli Etuahocce89652017-06-19 16:04:09 +03004652 }
Olli Etuahocce89652017-06-19 16:04:09 +03004653}
4654
4655TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4656{
4657 if (!declaringFunction())
4658 {
4659 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4660 }
4661 return new TStorageQualifierWrapper(EvqInOut, loc);
4662}
4663
Jamie Madillb98c3a82015-07-23 14:26:04 -04004664TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004665 TLayoutQualifier rightQualifier,
4666 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004667{
Martin Radevc28888b2016-07-22 15:27:42 +03004668 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004669 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004670}
4671
Olli Etuahocce89652017-06-19 16:04:09 +03004672TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4673{
4674 checkIsNotReserved(loc, *identifier);
4675 TType *type = new TType(EbtVoid, EbpUndefined);
4676 return new TField(type, identifier, loc);
4677}
4678
4679TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4680 const TSourceLoc &loc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004681 const TVector<unsigned int> &arraySizes,
Olli Etuahocce89652017-06-19 16:04:09 +03004682 const TSourceLoc &arraySizeLoc)
4683{
4684 checkIsNotReserved(loc, *identifier);
4685
4686 TType *type = new TType(EbtVoid, EbpUndefined);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004687 type->makeArrays(arraySizes);
Olli Etuahocce89652017-06-19 16:04:09 +03004688
4689 return new TField(type, identifier, loc);
4690}
4691
Olli Etuaho722bfb52017-10-26 17:00:11 +03004692void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4693 const TFieldList::const_iterator end,
4694 const TString &name,
4695 const TSourceLoc &location)
4696{
4697 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4698 {
4699 if ((*fieldIter)->name() == name)
4700 {
4701 error(location, "duplicate field name in structure", name.c_str());
4702 }
4703 }
4704}
4705
4706TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4707{
4708 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4709 ++fieldIter)
4710 {
4711 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4712 location);
4713 }
4714 return fields;
4715}
4716
Olli Etuaho4de340a2016-12-16 09:32:03 +00004717TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4718 const TFieldList *newlyAddedFields,
4719 const TSourceLoc &location)
4720{
4721 for (TField *field : *newlyAddedFields)
4722 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004723 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4724 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004725 processedFields->push_back(field);
4726 }
4727 return processedFields;
4728}
4729
Martin Radev70866b82016-07-22 15:27:42 +03004730TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4731 const TTypeQualifierBuilder &typeQualifierBuilder,
4732 TPublicType *typeSpecifier,
4733 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004734{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004735 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004736
Martin Radev70866b82016-07-22 15:27:42 +03004737 typeSpecifier->qualifier = typeQualifier.qualifier;
4738 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004739 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004740 typeSpecifier->invariant = typeQualifier.invariant;
4741 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304742 {
Martin Radev70866b82016-07-22 15:27:42 +03004743 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004744 }
Martin Radev70866b82016-07-22 15:27:42 +03004745 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004746}
4747
Jamie Madillb98c3a82015-07-23 14:26:04 -04004748TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004749 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004750{
Martin Radev4a9cd802016-09-01 16:51:51 +03004751 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4752 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004753
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004754 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4755 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004756
Martin Radev4a9cd802016-09-01 16:51:51 +03004757 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004758
Olli Etuaho55bde912017-10-25 13:41:13 +03004759 for (TField *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304760 {
Olli Etuaho55bde912017-10-25 13:41:13 +03004761 auto declaratorArraySizes = declarator->type()->getArraySizes();
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004762 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004763 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304764 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004765 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004766 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004767
Olli Etuaho55bde912017-10-25 13:41:13 +03004768 TType *type = declarator->type();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004769 *type = TType(typeSpecifier);
4770 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304771 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004772 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004773 }
4774
Olli Etuaho55bde912017-10-25 13:41:13 +03004775 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *declarator);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004776 }
4777
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004778 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004779}
4780
Martin Radev4a9cd802016-09-01 16:51:51 +03004781TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4782 const TSourceLoc &nameLine,
4783 const TString *structName,
4784 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004785{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004786 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004787
Jamie Madill9b820842015-02-12 10:40:10 -05004788 // Store a bool in the struct if we're at global scope, to allow us to
4789 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004790 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004791
Jamie Madill98493dd2013-07-08 14:39:03 -04004792 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004793 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004794 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004795 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304796 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004797 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004798 }
4799 }
4800
4801 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004802 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004803 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004804 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004805 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004806 switch (qualifier)
4807 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004808 case EvqGlobal:
4809 case EvqTemporary:
4810 break;
4811 default:
4812 error(field.line(), "invalid qualifier on struct member",
4813 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004814 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004815 }
Martin Radev70866b82016-07-22 15:27:42 +03004816 if (field.type()->isInvariant())
4817 {
4818 error(field.line(), "invalid qualifier on struct member", "invariant");
4819 }
jchen104cdac9e2017-05-08 11:01:20 +08004820 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4821 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004822 {
4823 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4824 }
4825
Olli Etuahoebee5b32017-11-23 12:56:32 +02004826 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4827 field.name().c_str(), field.type());
4828
Olli Etuaho43364892017-02-13 16:00:12 +00004829 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4830
4831 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004832
4833 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004834 }
4835
Martin Radev4a9cd802016-09-01 16:51:51 +03004836 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004837 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004838 exitStructDeclaration();
4839
Martin Radev4a9cd802016-09-01 16:51:51 +03004840 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004841}
4842
Jamie Madillb98c3a82015-07-23 14:26:04 -04004843TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004844 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004845 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004846{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004847 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004848 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004849 init->isVector())
4850 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004851 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4852 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004853 return nullptr;
4854 }
4855
Olli Etuaho923ecef2017-10-11 12:01:38 +03004856 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004857 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004858 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004859 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004860 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004861 }
4862
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004863 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4864 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004865 return node;
4866}
4867
4868TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4869{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004870 if (mSwitchNestingLevel == 0)
4871 {
4872 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004873 return nullptr;
4874 }
4875 if (condition == nullptr)
4876 {
4877 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004878 return nullptr;
4879 }
4880 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004881 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004882 {
4883 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004884 }
4885 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004886 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4887 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4888 // fold in case labels.
4889 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004890 {
4891 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004892 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004893 TIntermCase *node = new TIntermCase(condition);
4894 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004895 return node;
4896}
4897
4898TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4899{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004900 if (mSwitchNestingLevel == 0)
4901 {
4902 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004903 return nullptr;
4904 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004905 TIntermCase *node = new TIntermCase(nullptr);
4906 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004907 return node;
4908}
4909
Jamie Madillb98c3a82015-07-23 14:26:04 -04004910TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4911 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004912 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004913{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004914 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004915
4916 switch (op)
4917 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004918 case EOpLogicalNot:
4919 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4920 child->isVector())
4921 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004922 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004923 return nullptr;
4924 }
4925 break;
4926 case EOpBitwiseNot:
4927 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4928 child->isMatrix() || child->isArray())
4929 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004930 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004931 return nullptr;
4932 }
4933 break;
4934 case EOpPostIncrement:
4935 case EOpPreIncrement:
4936 case EOpPostDecrement:
4937 case EOpPreDecrement:
4938 case EOpNegative:
4939 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004940 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4941 child->getBasicType() == EbtBool || child->isArray() ||
4942 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004943 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004944 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004945 return nullptr;
4946 }
4947 // Operators for built-ins are already type checked against their prototype.
4948 default:
4949 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004950 }
4951
Jiajia Qinbc585152017-06-23 15:42:17 +08004952 if (child->getMemoryQualifier().writeonly)
4953 {
4954 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4955 return nullptr;
4956 }
4957
Olli Etuahof119a262016-08-19 15:54:22 +03004958 TIntermUnary *node = new TIntermUnary(op, child);
4959 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004960
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004961 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004962}
4963
Olli Etuaho09b22472015-02-11 11:47:26 +02004964TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4965{
Olli Etuahocce89652017-06-19 16:04:09 +03004966 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004967 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004968 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004969 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004970 return child;
4971 }
4972 return node;
4973}
4974
Jamie Madillb98c3a82015-07-23 14:26:04 -04004975TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4976 TIntermTyped *child,
4977 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004978{
Olli Etuaho856c4972016-08-08 11:38:39 +03004979 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004980 return addUnaryMath(op, child, loc);
4981}
4982
Jamie Madillb98c3a82015-07-23 14:26:04 -04004983bool TParseContext::binaryOpCommonCheck(TOperator op,
4984 TIntermTyped *left,
4985 TIntermTyped *right,
4986 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004987{
jchen10b4cf5652017-05-05 18:51:17 +08004988 // Check opaque types are not allowed to be operands in expressions other than array indexing
4989 // and structure member selection.
4990 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4991 {
4992 switch (op)
4993 {
4994 case EOpIndexDirect:
4995 case EOpIndexIndirect:
4996 break;
4997 case EOpIndexDirectStruct:
4998 UNREACHABLE();
4999
5000 default:
5001 error(loc, "Invalid operation for variables with an opaque type",
5002 GetOperatorString(op));
5003 return false;
5004 }
5005 }
jchen10cc2a10e2017-05-03 14:05:12 +08005006
Jiajia Qinbc585152017-06-23 15:42:17 +08005007 if (right->getMemoryQualifier().writeonly)
5008 {
5009 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5010 return false;
5011 }
5012
5013 if (left->getMemoryQualifier().writeonly)
5014 {
5015 switch (op)
5016 {
5017 case EOpAssign:
5018 case EOpInitialize:
5019 case EOpIndexDirect:
5020 case EOpIndexIndirect:
5021 case EOpIndexDirectStruct:
5022 case EOpIndexDirectInterfaceBlock:
5023 break;
5024 default:
5025 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5026 return false;
5027 }
5028 }
5029
Olli Etuaho244be012016-08-18 15:26:02 +03005030 if (left->getType().getStruct() || right->getType().getStruct())
5031 {
5032 switch (op)
5033 {
5034 case EOpIndexDirectStruct:
5035 ASSERT(left->getType().getStruct());
5036 break;
5037 case EOpEqual:
5038 case EOpNotEqual:
5039 case EOpAssign:
5040 case EOpInitialize:
5041 if (left->getType() != right->getType())
5042 {
5043 return false;
5044 }
5045 break;
5046 default:
5047 error(loc, "Invalid operation for structs", GetOperatorString(op));
5048 return false;
5049 }
5050 }
5051
Olli Etuaho94050052017-05-08 14:17:44 +03005052 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5053 {
5054 switch (op)
5055 {
5056 case EOpIndexDirectInterfaceBlock:
5057 ASSERT(left->getType().getInterfaceBlock());
5058 break;
5059 default:
5060 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5061 return false;
5062 }
5063 }
5064
Olli Etuahod6b14282015-03-17 14:31:35 +02005065 if (left->isArray() || right->isArray())
5066 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005067 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005068 {
5069 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5070 return false;
5071 }
5072
5073 if (left->isArray() != right->isArray())
5074 {
5075 error(loc, "array / non-array mismatch", GetOperatorString(op));
5076 return false;
5077 }
5078
5079 switch (op)
5080 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005081 case EOpEqual:
5082 case EOpNotEqual:
5083 case EOpAssign:
5084 case EOpInitialize:
5085 break;
5086 default:
5087 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5088 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005089 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005090 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03005091 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005092 {
5093 error(loc, "array size mismatch", GetOperatorString(op));
5094 return false;
5095 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005096 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005097
5098 // Check ops which require integer / ivec parameters
5099 bool isBitShift = false;
5100 switch (op)
5101 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005102 case EOpBitShiftLeft:
5103 case EOpBitShiftRight:
5104 case EOpBitShiftLeftAssign:
5105 case EOpBitShiftRightAssign:
5106 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5107 // check that the basic type is an integer type.
5108 isBitShift = true;
5109 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5110 {
5111 return false;
5112 }
5113 break;
5114 case EOpBitwiseAnd:
5115 case EOpBitwiseXor:
5116 case EOpBitwiseOr:
5117 case EOpBitwiseAndAssign:
5118 case EOpBitwiseXorAssign:
5119 case EOpBitwiseOrAssign:
5120 // It is enough to check the type of only one operand, since later it
5121 // is checked that the operand types match.
5122 if (!IsInteger(left->getBasicType()))
5123 {
5124 return false;
5125 }
5126 break;
5127 default:
5128 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005129 }
5130
5131 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5132 // So the basic type should usually match.
5133 if (!isBitShift && left->getBasicType() != right->getBasicType())
5134 {
5135 return false;
5136 }
5137
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005138 // Check that:
5139 // 1. Type sizes match exactly on ops that require that.
5140 // 2. Restrictions for structs that contain arrays or samplers are respected.
5141 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005142 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005143 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005144 case EOpAssign:
5145 case EOpInitialize:
5146 case EOpEqual:
5147 case EOpNotEqual:
5148 // ESSL 1.00 sections 5.7, 5.8, 5.9
5149 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5150 {
5151 error(loc, "undefined operation for structs containing arrays",
5152 GetOperatorString(op));
5153 return false;
5154 }
5155 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5156 // we interpret the spec so that this extends to structs containing samplers,
5157 // similarly to ESSL 1.00 spec.
5158 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5159 left->getType().isStructureContainingSamplers())
5160 {
5161 error(loc, "undefined operation for structs containing samplers",
5162 GetOperatorString(op));
5163 return false;
5164 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005165
Olli Etuahoe1805592017-01-02 16:41:20 +00005166 if ((left->getNominalSize() != right->getNominalSize()) ||
5167 (left->getSecondarySize() != right->getSecondarySize()))
5168 {
5169 error(loc, "dimension mismatch", GetOperatorString(op));
5170 return false;
5171 }
5172 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005173 case EOpLessThan:
5174 case EOpGreaterThan:
5175 case EOpLessThanEqual:
5176 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005177 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005178 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005179 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005180 return false;
5181 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005182 break;
5183 case EOpAdd:
5184 case EOpSub:
5185 case EOpDiv:
5186 case EOpIMod:
5187 case EOpBitShiftLeft:
5188 case EOpBitShiftRight:
5189 case EOpBitwiseAnd:
5190 case EOpBitwiseXor:
5191 case EOpBitwiseOr:
5192 case EOpAddAssign:
5193 case EOpSubAssign:
5194 case EOpDivAssign:
5195 case EOpIModAssign:
5196 case EOpBitShiftLeftAssign:
5197 case EOpBitShiftRightAssign:
5198 case EOpBitwiseAndAssign:
5199 case EOpBitwiseXorAssign:
5200 case EOpBitwiseOrAssign:
5201 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5202 {
5203 return false;
5204 }
5205
5206 // Are the sizes compatible?
5207 if (left->getNominalSize() != right->getNominalSize() ||
5208 left->getSecondarySize() != right->getSecondarySize())
5209 {
5210 // If the nominal sizes of operands do not match:
5211 // One of them must be a scalar.
5212 if (!left->isScalar() && !right->isScalar())
5213 return false;
5214
5215 // In the case of compound assignment other than multiply-assign,
5216 // the right side needs to be a scalar. Otherwise a vector/matrix
5217 // would be assigned to a scalar. A scalar can't be shifted by a
5218 // vector either.
5219 if (!right->isScalar() &&
5220 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5221 return false;
5222 }
5223 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005224 default:
5225 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005226 }
5227
Olli Etuahod6b14282015-03-17 14:31:35 +02005228 return true;
5229}
5230
Olli Etuaho1dded802016-08-18 18:13:13 +03005231bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5232 const TType &left,
5233 const TType &right)
5234{
5235 switch (op)
5236 {
5237 case EOpMul:
5238 case EOpMulAssign:
5239 return left.getNominalSize() == right.getNominalSize() &&
5240 left.getSecondarySize() == right.getSecondarySize();
5241 case EOpVectorTimesScalar:
5242 return true;
5243 case EOpVectorTimesScalarAssign:
5244 ASSERT(!left.isMatrix() && !right.isMatrix());
5245 return left.isVector() && !right.isVector();
5246 case EOpVectorTimesMatrix:
5247 return left.getNominalSize() == right.getRows();
5248 case EOpVectorTimesMatrixAssign:
5249 ASSERT(!left.isMatrix() && right.isMatrix());
5250 return left.isVector() && left.getNominalSize() == right.getRows() &&
5251 left.getNominalSize() == right.getCols();
5252 case EOpMatrixTimesVector:
5253 return left.getCols() == right.getNominalSize();
5254 case EOpMatrixTimesScalar:
5255 return true;
5256 case EOpMatrixTimesScalarAssign:
5257 ASSERT(left.isMatrix() && !right.isMatrix());
5258 return !right.isVector();
5259 case EOpMatrixTimesMatrix:
5260 return left.getCols() == right.getRows();
5261 case EOpMatrixTimesMatrixAssign:
5262 ASSERT(left.isMatrix() && right.isMatrix());
5263 // We need to check two things:
5264 // 1. The matrix multiplication step is valid.
5265 // 2. The result will have the same number of columns as the lvalue.
5266 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5267
5268 default:
5269 UNREACHABLE();
5270 return false;
5271 }
5272}
5273
Jamie Madillb98c3a82015-07-23 14:26:04 -04005274TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5275 TIntermTyped *left,
5276 TIntermTyped *right,
5277 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005278{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005279 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005280 return nullptr;
5281
Olli Etuahofc1806e2015-03-17 13:03:11 +02005282 switch (op)
5283 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005284 case EOpEqual:
5285 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005286 case EOpLessThan:
5287 case EOpGreaterThan:
5288 case EOpLessThanEqual:
5289 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005290 break;
5291 case EOpLogicalOr:
5292 case EOpLogicalXor:
5293 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005294 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5295 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005296 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005297 {
5298 return nullptr;
5299 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005300 // Basic types matching should have been already checked.
5301 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005302 break;
5303 case EOpAdd:
5304 case EOpSub:
5305 case EOpDiv:
5306 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005307 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5308 !right->getType().getStruct());
5309 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005310 {
5311 return nullptr;
5312 }
5313 break;
5314 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005315 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5316 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005317 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005318 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005319 {
5320 return nullptr;
5321 }
5322 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005323 default:
5324 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005325 }
5326
Olli Etuaho1dded802016-08-18 18:13:13 +03005327 if (op == EOpMul)
5328 {
5329 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5330 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5331 {
5332 return nullptr;
5333 }
5334 }
5335
Olli Etuaho3fdec912016-08-18 15:08:06 +03005336 TIntermBinary *node = new TIntermBinary(op, left, right);
5337 node->setLine(loc);
5338
Olli Etuaho3fdec912016-08-18 15:08:06 +03005339 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005340 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005341}
5342
Jamie Madillb98c3a82015-07-23 14:26:04 -04005343TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5344 TIntermTyped *left,
5345 TIntermTyped *right,
5346 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005347{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005348 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005349 if (node == 0)
5350 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005351 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5352 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005353 return left;
5354 }
5355 return node;
5356}
5357
Jamie Madillb98c3a82015-07-23 14:26:04 -04005358TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5359 TIntermTyped *left,
5360 TIntermTyped *right,
5361 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005362{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005363 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005364 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005365 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005366 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5367 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005368 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005369 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005370 }
5371 return node;
5372}
5373
Olli Etuaho13389b62016-10-16 11:48:18 +01005374TIntermBinary *TParseContext::createAssign(TOperator op,
5375 TIntermTyped *left,
5376 TIntermTyped *right,
5377 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005378{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005379 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005380 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005381 if (op == EOpMulAssign)
5382 {
5383 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5384 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5385 {
5386 return nullptr;
5387 }
5388 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005389 TIntermBinary *node = new TIntermBinary(op, left, right);
5390 node->setLine(loc);
5391
Olli Etuaho3fdec912016-08-18 15:08:06 +03005392 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005393 }
5394 return nullptr;
5395}
5396
Jamie Madillb98c3a82015-07-23 14:26:04 -04005397TIntermTyped *TParseContext::addAssign(TOperator op,
5398 TIntermTyped *left,
5399 TIntermTyped *right,
5400 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005401{
Olli Etuahocce89652017-06-19 16:04:09 +03005402 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005403 TIntermTyped *node = createAssign(op, left, right, loc);
5404 if (node == nullptr)
5405 {
5406 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005407 return left;
5408 }
5409 return node;
5410}
5411
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005412TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5413 TIntermTyped *right,
5414 const TSourceLoc &loc)
5415{
Corentin Wallez0d959252016-07-12 17:26:32 -04005416 // WebGL2 section 5.26, the following results in an error:
5417 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005418 if (mShaderSpec == SH_WEBGL2_SPEC &&
5419 (left->isArray() || left->getBasicType() == EbtVoid ||
5420 left->getType().isStructureContainingArrays() || right->isArray() ||
5421 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005422 {
5423 error(loc,
5424 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5425 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005426 }
5427
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005428 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5429 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5430 commaNode->getTypePointer()->setQualifier(resultQualifier);
5431 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005432}
5433
Olli Etuaho49300862015-02-20 14:54:49 +02005434TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5435{
5436 switch (op)
5437 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005438 case EOpContinue:
5439 if (mLoopNestingLevel <= 0)
5440 {
5441 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005442 }
5443 break;
5444 case EOpBreak:
5445 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5446 {
5447 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005448 }
5449 break;
5450 case EOpReturn:
5451 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5452 {
5453 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005454 }
5455 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005456 case EOpKill:
5457 if (mShaderType != GL_FRAGMENT_SHADER)
5458 {
5459 error(loc, "discard supported in fragment shaders only", "discard");
5460 }
5461 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005462 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005463 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005464 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005465 }
Olli Etuahocce89652017-06-19 16:04:09 +03005466 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005467}
5468
Jamie Madillb98c3a82015-07-23 14:26:04 -04005469TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005470 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005471 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005472{
Olli Etuahocce89652017-06-19 16:04:09 +03005473 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005474 {
Olli Etuahocce89652017-06-19 16:04:09 +03005475 ASSERT(op == EOpReturn);
5476 mFunctionReturnsValue = true;
5477 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5478 {
5479 error(loc, "void function cannot return a value", "return");
5480 }
5481 else if (*mCurrentFunctionType != expression->getType())
5482 {
5483 error(loc, "function return is not matching type:", "return");
5484 }
Olli Etuaho49300862015-02-20 14:54:49 +02005485 }
Olli Etuahocce89652017-06-19 16:04:09 +03005486 TIntermBranch *node = new TIntermBranch(op, expression);
5487 node->setLine(loc);
5488 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005489}
5490
Martin Radev84aa2dc2017-09-11 15:51:02 +03005491void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5492{
5493 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5494 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5495 bool isTextureGather = (name == "textureGather");
5496 bool isTextureGatherOffset = (name == "textureGatherOffset");
5497 if (isTextureGather || isTextureGatherOffset)
5498 {
5499 TIntermNode *componentNode = nullptr;
5500 TIntermSequence *arguments = functionCall->getSequence();
5501 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5502 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5503 ASSERT(sampler != nullptr);
5504 switch (sampler->getBasicType())
5505 {
5506 case EbtSampler2D:
5507 case EbtISampler2D:
5508 case EbtUSampler2D:
5509 case EbtSampler2DArray:
5510 case EbtISampler2DArray:
5511 case EbtUSampler2DArray:
5512 if ((isTextureGather && arguments->size() == 3u) ||
5513 (isTextureGatherOffset && arguments->size() == 4u))
5514 {
5515 componentNode = arguments->back();
5516 }
5517 break;
5518 case EbtSamplerCube:
5519 case EbtISamplerCube:
5520 case EbtUSamplerCube:
5521 ASSERT(!isTextureGatherOffset);
5522 if (arguments->size() == 3u)
5523 {
5524 componentNode = arguments->back();
5525 }
5526 break;
5527 case EbtSampler2DShadow:
5528 case EbtSampler2DArrayShadow:
5529 case EbtSamplerCubeShadow:
5530 break;
5531 default:
5532 UNREACHABLE();
5533 break;
5534 }
5535 if (componentNode)
5536 {
5537 const TIntermConstantUnion *componentConstantUnion =
5538 componentNode->getAsConstantUnion();
5539 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5540 {
5541 error(functionCall->getLine(), "Texture component must be a constant expression",
5542 name.c_str());
5543 }
5544 else
5545 {
5546 int component = componentConstantUnion->getIConst(0);
5547 if (component < 0 || component > 3)
5548 {
5549 error(functionCall->getLine(), "Component must be in the range [0;3]",
5550 name.c_str());
5551 }
5552 }
5553 }
5554 }
5555}
5556
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005557void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5558{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005559 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005560 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005561 TIntermNode *offset = nullptr;
5562 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005563 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005564 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5565 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5566 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005567 {
5568 offset = arguments->back();
5569 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005570 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005571 {
5572 // A bias parameter might follow the offset parameter.
5573 ASSERT(arguments->size() >= 3);
5574 offset = (*arguments)[2];
5575 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005576 else if (name == "textureGatherOffset")
5577 {
5578 ASSERT(arguments->size() >= 3u);
5579 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5580 ASSERT(sampler != nullptr);
5581 switch (sampler->getBasicType())
5582 {
5583 case EbtSampler2D:
5584 case EbtISampler2D:
5585 case EbtUSampler2D:
5586 case EbtSampler2DArray:
5587 case EbtISampler2DArray:
5588 case EbtUSampler2DArray:
5589 offset = (*arguments)[2];
5590 break;
5591 case EbtSampler2DShadow:
5592 case EbtSampler2DArrayShadow:
5593 offset = (*arguments)[3];
5594 break;
5595 default:
5596 UNREACHABLE();
5597 break;
5598 }
5599 useTextureGatherOffsetConstraints = true;
5600 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005601 if (offset != nullptr)
5602 {
5603 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5604 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5605 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005606 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005607 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005608 }
5609 else
5610 {
5611 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5612 size_t size = offsetConstantUnion->getType().getObjectSize();
5613 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005614 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5615 : mMinProgramTexelOffset;
5616 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5617 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005618 for (size_t i = 0u; i < size; ++i)
5619 {
5620 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005621 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005622 {
5623 std::stringstream tokenStream;
5624 tokenStream << offsetValue;
5625 std::string token = tokenStream.str();
5626 error(offset->getLine(), "Texture offset value out of valid range",
5627 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005628 }
5629 }
5630 }
5631 }
5632}
5633
Jiajia Qina3106c52017-11-03 09:39:39 +08005634void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5635{
5636 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5637 if (IsAtomicBuiltin(name))
5638 {
5639 TIntermSequence *arguments = functionCall->getSequence();
5640 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5641
5642 if (IsBufferOrSharedVariable(memNode))
5643 {
5644 return;
5645 }
5646
5647 while (memNode->getAsBinaryNode())
5648 {
5649 memNode = memNode->getAsBinaryNode()->getLeft();
5650 if (IsBufferOrSharedVariable(memNode))
5651 {
5652 return;
5653 }
5654 }
5655
5656 error(memNode->getLine(),
5657 "The value passed to the mem argument of an atomic memory function does not "
5658 "correspond to a buffer or shared variable.",
5659 functionCall->getFunctionSymbolInfo()->getName().c_str());
5660 }
5661}
5662
Martin Radev2cc85b32016-08-05 16:22:53 +03005663// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5664void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5665{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005666 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005667 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5668
5669 if (name.compare(0, 5, "image") == 0)
5670 {
5671 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005672 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005673
Olli Etuaho485eefd2017-02-14 17:40:06 +00005674 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005675
5676 if (name.compare(5, 5, "Store") == 0)
5677 {
5678 if (memoryQualifier.readonly)
5679 {
5680 error(imageNode->getLine(),
5681 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005682 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005683 }
5684 }
5685 else if (name.compare(5, 4, "Load") == 0)
5686 {
5687 if (memoryQualifier.writeonly)
5688 {
5689 error(imageNode->getLine(),
5690 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005691 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005692 }
5693 }
5694 }
5695}
5696
5697// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5698void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5699 const TFunction *functionDefinition,
5700 const TIntermAggregate *functionCall)
5701{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005702 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005703
5704 const TIntermSequence &arguments = *functionCall->getSequence();
5705
5706 ASSERT(functionDefinition->getParamCount() == arguments.size());
5707
5708 for (size_t i = 0; i < arguments.size(); ++i)
5709 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005710 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5711 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005712 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5713 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5714
5715 if (IsImage(functionArgumentType.getBasicType()))
5716 {
5717 const TMemoryQualifier &functionArgumentMemoryQualifier =
5718 functionArgumentType.getMemoryQualifier();
5719 const TMemoryQualifier &functionParameterMemoryQualifier =
5720 functionParameterType.getMemoryQualifier();
5721 if (functionArgumentMemoryQualifier.readonly &&
5722 !functionParameterMemoryQualifier.readonly)
5723 {
5724 error(functionCall->getLine(),
5725 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005726 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005727 }
5728
5729 if (functionArgumentMemoryQualifier.writeonly &&
5730 !functionParameterMemoryQualifier.writeonly)
5731 {
5732 error(functionCall->getLine(),
5733 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005734 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005735 }
Martin Radev049edfa2016-11-11 14:35:37 +02005736
5737 if (functionArgumentMemoryQualifier.coherent &&
5738 !functionParameterMemoryQualifier.coherent)
5739 {
5740 error(functionCall->getLine(),
5741 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005742 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005743 }
5744
5745 if (functionArgumentMemoryQualifier.volatileQualifier &&
5746 !functionParameterMemoryQualifier.volatileQualifier)
5747 {
5748 error(functionCall->getLine(),
5749 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005750 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005751 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005752 }
5753 }
5754}
5755
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005756TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005757{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005758 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005759}
5760
5761TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005762 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005763 TIntermNode *thisNode,
5764 const TSourceLoc &loc)
5765{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005766 if (thisNode != nullptr)
5767 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005768 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005769 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005770
5771 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005772 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005773 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005774 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005775 }
5776 else
5777 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005778 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005779 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5780 }
5781}
5782
5783TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5784 TIntermSequence *arguments,
5785 TIntermNode *thisNode,
5786 const TSourceLoc &loc)
5787{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005788 TIntermTyped *typedThis = thisNode->getAsTyped();
5789 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5790 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5791 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5792 // So accessing fnCall->getName() below is safe.
5793 if (fnCall->getName() != "length")
5794 {
5795 error(loc, "invalid method", fnCall->getName().c_str());
5796 }
5797 else if (!arguments->empty())
5798 {
5799 error(loc, "method takes no parameters", "length");
5800 }
5801 else if (typedThis == nullptr || !typedThis->isArray())
5802 {
5803 error(loc, "length can only be called on arrays", "length");
5804 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005805 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5806 mGeometryShaderInputPrimitiveType == EptUndefined)
5807 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005808 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005809 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5810 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005811 else
5812 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005813 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5814 node->setLine(loc);
5815 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005816 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005817 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005818}
5819
5820TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5821 TIntermSequence *arguments,
5822 const TSourceLoc &loc)
5823{
5824 // First find by unmangled name to check whether the function name has been
5825 // hidden by a variable name or struct typename.
5826 // If a function is found, check for one with a matching argument list.
5827 bool builtIn;
5828 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5829 if (symbol != nullptr && !symbol->isFunction())
5830 {
5831 error(loc, "function name expected", fnCall->getName().c_str());
5832 }
5833 else
5834 {
5835 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5836 mShaderVersion, &builtIn);
5837 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005838 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005839 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5840 }
5841 else
5842 {
5843 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005844 //
5845 // A declared function.
5846 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005847 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005848 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005849 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005850 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005851 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005852 if (builtIn && op != EOpNull)
5853 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005854 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005855 if (fnCandidate->getParamCount() == 1)
5856 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005857 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005858 TIntermNode *unaryParamNode = arguments->front();
5859 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005860 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005861 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005862 }
5863 else
5864 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005865 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005866 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005867 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005868
5869 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005870 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305871
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005872 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305873 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005874 // See if we can constant fold a built-in. Note that this may be possible
5875 // even if it is not const-qualified.
5876 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305877 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005878 else
5879 {
5880 return callNode;
5881 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005882 }
5883 }
5884 else
5885 {
5886 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005887 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005888
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005889 // If builtIn == false, the function is user defined - could be an overloaded
5890 // built-in as well.
5891 // if builtIn == true, it's a builtIn function with no op associated with it.
5892 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005893 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005894 {
Olli Etuahofe486322017-03-21 09:30:54 +00005895 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005896 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005897 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005898 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005899 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005900 }
5901 else
5902 {
Olli Etuahofe486322017-03-21 09:30:54 +00005903 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005904 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005905 }
5906
Jiajia Qinbc585152017-06-23 15:42:17 +08005907 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005908
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005909 callNode->setLine(loc);
5910
5911 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005912 }
5913 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005914 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005915
5916 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005917 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005918}
5919
Jamie Madillb98c3a82015-07-23 14:26:04 -04005920TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005921 TIntermTyped *trueExpression,
5922 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005923 const TSourceLoc &loc)
5924{
Olli Etuaho56229f12017-07-10 14:16:33 +03005925 if (!checkIsScalarBool(loc, cond))
5926 {
5927 return falseExpression;
5928 }
Olli Etuaho52901742015-04-15 13:42:45 +03005929
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005930 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005931 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005932 std::stringstream reasonStream;
5933 reasonStream << "mismatching ternary operator operand types '"
5934 << trueExpression->getCompleteString() << " and '"
5935 << falseExpression->getCompleteString() << "'";
5936 std::string reason = reasonStream.str();
5937 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005938 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005939 }
Olli Etuahode318b22016-10-25 16:18:25 +01005940 if (IsOpaqueType(trueExpression->getBasicType()))
5941 {
5942 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005943 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005944 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5945 // Note that structs containing opaque types don't need to be checked as structs are
5946 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005947 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005948 return falseExpression;
5949 }
5950
Jiajia Qinbc585152017-06-23 15:42:17 +08005951 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5952 falseExpression->getMemoryQualifier().writeonly)
5953 {
5954 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5955 return falseExpression;
5956 }
5957
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005958 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005959 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005960 // ESSL 3.00.6 section 5.7:
5961 // Ternary operator support is optional for arrays. No certainty that it works across all
5962 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5963 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005964 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005965 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005966 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005967 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005968 }
Olli Etuaho94050052017-05-08 14:17:44 +03005969 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5970 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005971 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005972 return falseExpression;
5973 }
5974
Corentin Wallez0d959252016-07-12 17:26:32 -04005975 // WebGL2 section 5.26, the following results in an error:
5976 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005977 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005978 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005979 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005980 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005981 }
5982
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005983 // Note that the node resulting from here can be a constant union without being qualified as
5984 // constant.
5985 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5986 node->setLine(loc);
5987
5988 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005989}
Olli Etuaho49300862015-02-20 14:54:49 +02005990
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005991//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005992// Parse an array of strings using yyparse.
5993//
5994// Returns 0 for success.
5995//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005996int PaParseStrings(size_t count,
5997 const char *const string[],
5998 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305999 TParseContext *context)
6000{
Yunchao He4f285442017-04-21 12:15:49 +08006001 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006002 return 1;
6003
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006004 if (glslang_initialize(context))
6005 return 1;
6006
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006007 int error = glslang_scan(count, string, length, context);
6008 if (!error)
6009 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006010
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006011 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006012
alokp@chromium.org6b495712012-06-29 00:06:58 +00006013 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006014}
Jamie Madill45bcc782016-11-07 13:58:48 -05006015
6016} // namespace sh