blob: 0514212667567489d835c8279fea3bc6c16245de [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 Etuaho54a29ff2017-11-28 17:35:20 +02001128 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
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 Etuaho54a29ff2017-11-28 17:35:20 +02001808 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001809 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001810 checkCanUseExtension(location, variable->extension());
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
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001899 node = new TIntermSymbol(variable->uniqueId(), variable->name(), 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 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001904 node = new TIntermSymbol(variable->uniqueId(), variable->name(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001905 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001906 ASSERT(node != nullptr);
1907 node->setLine(location);
1908 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001909}
1910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911// Initializers show up in several places in the grammar. Have one set of
1912// code to handle them here.
1913//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001914// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001915bool TParseContext::executeInitializer(const TSourceLoc &line,
1916 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001917 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001918 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001919 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920{
Olli Etuaho13389b62016-10-16 11:48:18 +01001921 ASSERT(initNode != nullptr);
1922 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923
Olli Etuaho2935c582015-04-08 14:32:06 +03001924 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001925 if (type.isUnsizedArray())
1926 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001927 // In case initializer is not an array or type has more dimensions than initializer, this
1928 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1929 // actually is an array or not. Having a non-array initializer for an unsized array will
1930 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001931 auto *arraySizes = initializer->getType().getArraySizes();
1932 type.sizeUnsizedArrays(arraySizes);
Olli Etuaho376f1b52015-04-13 13:23:41 +03001933 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001934 if (!declareVariable(line, identifier, type, &variable))
1935 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001936 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001937 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001938
Olli Etuahob0c645e2015-05-12 14:25:36 +03001939 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001940 if (symbolTable.atGlobalLevel() &&
1941 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001942 {
1943 // Error message does not completely match behavior with ESSL 1.00, but
1944 // we want to steer developers towards only using constant expressions.
1945 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001946 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001947 }
1948 if (globalInitWarning)
1949 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001950 warning(
1951 line,
1952 "global variable initializers should be constant expressions "
1953 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1954 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001955 }
1956
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001957 //
1958 // identifier must be of type constant, a global, or a temporary
1959 //
1960 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301961 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1962 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001963 error(line, " cannot initialize this type of qualifier ",
1964 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001965 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001966 }
1967 //
1968 // test for and propagate constant
1969 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001970
Arun Patole7e7e68d2015-05-22 12:02:25 +05301971 if (qualifier == EvqConst)
1972 {
1973 if (qualifier != initializer->getType().getQualifier())
1974 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001975 std::stringstream reasonStream;
1976 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1977 << "'";
1978 std::string reason = reasonStream.str();
1979 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001980 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001981 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001982 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301983 if (type != initializer->getType())
1984 {
1985 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001986 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001987 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001988 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001989 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001990
1991 // Save the constant folded value to the variable if possible. For example array
1992 // initializers are not folded, since that way copying the array literal to multiple places
1993 // in the shader is avoided.
1994 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1995 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301996 if (initializer->getAsConstantUnion())
1997 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001998 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001999 ASSERT(*initNode == nullptr);
2000 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302001 }
2002 else if (initializer->getAsSymbolNode())
2003 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002004 const TSymbol *symbol =
2005 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
2006 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002008 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002009 if (constArray)
2010 {
2011 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002012 ASSERT(*initNode == nullptr);
2013 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002014 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002015 }
2016 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002017
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002018 TIntermSymbol *intermSymbol =
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002019 new TIntermSymbol(variable->uniqueId(), variable->name(), variable->getType());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002020 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002021 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2022 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002023 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002024 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002025 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002026 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027
Olli Etuaho914b79a2017-06-19 16:03:19 +03002028 return true;
2029}
2030
2031TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2032 const TString &identifier,
2033 TIntermTyped *initializer,
2034 const TSourceLoc &loc)
2035{
2036 checkIsScalarBool(loc, pType);
2037 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002038 TType type(pType);
2039 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002040 {
2041 // The initializer is valid. The init condition needs to have a node - either the
2042 // initializer node, or a constant node in case the initialized variable is const and won't
2043 // be recorded in the AST.
2044 if (initNode == nullptr)
2045 {
2046 return initializer;
2047 }
2048 else
2049 {
2050 TIntermDeclaration *declaration = new TIntermDeclaration();
2051 declaration->appendDeclarator(initNode);
2052 return declaration;
2053 }
2054 }
2055 return nullptr;
2056}
2057
2058TIntermNode *TParseContext::addLoop(TLoopType type,
2059 TIntermNode *init,
2060 TIntermNode *cond,
2061 TIntermTyped *expr,
2062 TIntermNode *body,
2063 const TSourceLoc &line)
2064{
2065 TIntermNode *node = nullptr;
2066 TIntermTyped *typedCond = nullptr;
2067 if (cond)
2068 {
2069 typedCond = cond->getAsTyped();
2070 }
2071 if (cond == nullptr || typedCond)
2072 {
Olli Etuahocce89652017-06-19 16:04:09 +03002073 if (type == ELoopDoWhile)
2074 {
2075 checkIsScalarBool(line, typedCond);
2076 }
2077 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2078 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2079 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2080 !typedCond->isVector()));
2081
Olli Etuaho3ec75682017-07-05 17:02:55 +03002082 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002083 node->setLine(line);
2084 return node;
2085 }
2086
Olli Etuahocce89652017-06-19 16:04:09 +03002087 ASSERT(type != ELoopDoWhile);
2088
Olli Etuaho914b79a2017-06-19 16:03:19 +03002089 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2090 ASSERT(declaration);
2091 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2092 ASSERT(declarator->getLeft()->getAsSymbolNode());
2093
2094 // The condition is a declaration. In the AST representation we don't support declarations as
2095 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2096 // the loop.
2097 TIntermBlock *block = new TIntermBlock();
2098
2099 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2100 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2101 block->appendStatement(declareCondition);
2102
2103 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2104 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002105 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002106 block->appendStatement(loop);
2107 loop->setLine(line);
2108 block->setLine(line);
2109 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110}
2111
Olli Etuahocce89652017-06-19 16:04:09 +03002112TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2113 TIntermNodePair code,
2114 const TSourceLoc &loc)
2115{
Olli Etuaho56229f12017-07-10 14:16:33 +03002116 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002117
2118 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002119 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002120 {
2121 if (cond->getAsConstantUnion()->getBConst(0) == true)
2122 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002123 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002124 }
2125 else
2126 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002127 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002128 }
2129 }
2130
Olli Etuaho3ec75682017-07-05 17:02:55 +03002131 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002132 node->setLine(loc);
2133
2134 return node;
2135}
2136
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002137void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2138{
2139 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2140 typeSpecifier->getBasicType());
2141
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002142 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002143 {
2144 error(typeSpecifier->getLine(), "not supported", "first-class array");
2145 typeSpecifier->clearArrayness();
2146 }
2147}
2148
Martin Radev70866b82016-07-22 15:27:42 +03002149TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302150 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002151{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002152 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002153
Martin Radev70866b82016-07-22 15:27:42 +03002154 TPublicType returnType = typeSpecifier;
2155 returnType.qualifier = typeQualifier.qualifier;
2156 returnType.invariant = typeQualifier.invariant;
2157 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002158 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002159 returnType.precision = typeSpecifier.precision;
2160
2161 if (typeQualifier.precision != EbpUndefined)
2162 {
2163 returnType.precision = typeQualifier.precision;
2164 }
2165
Martin Radev4a9cd802016-09-01 16:51:51 +03002166 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2167 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002168
Martin Radev4a9cd802016-09-01 16:51:51 +03002169 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2170 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002171
Martin Radev4a9cd802016-09-01 16:51:51 +03002172 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002173
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002174 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002175 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002176 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002177 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002178 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002179 returnType.clearArrayness();
2180 }
2181
Martin Radev70866b82016-07-22 15:27:42 +03002182 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002183 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002184 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002185 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002186 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002187 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002188
Martin Radev70866b82016-07-22 15:27:42 +03002189 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002190 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002191 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002192 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002193 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002194 }
2195 }
2196 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002197 {
Martin Radev70866b82016-07-22 15:27:42 +03002198 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002199 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002200 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002201 }
Martin Radev70866b82016-07-22 15:27:42 +03002202 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2203 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002204 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002205 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2206 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002207 }
Martin Radev70866b82016-07-22 15:27:42 +03002208 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002209 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002210 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002211 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002212 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002213 }
2214
2215 return returnType;
2216}
2217
Olli Etuaho856c4972016-08-08 11:38:39 +03002218void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2219 const TPublicType &type,
2220 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002221{
2222 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002223 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002224 {
2225 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002226 }
2227
2228 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2229 switch (qualifier)
2230 {
2231 case EvqVertexIn:
2232 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002233 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002234 {
2235 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002236 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002237 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002238 return;
2239 case EvqFragmentOut:
2240 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002241 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002242 {
2243 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002244 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002245 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002246 return;
2247 default:
2248 break;
2249 }
2250
2251 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2252 // restrictions.
2253 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002254 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2255 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002256 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2257 {
2258 error(qualifierLocation, "must use 'flat' interpolation here",
2259 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002260 }
2261
Martin Radev4a9cd802016-09-01 16:51:51 +03002262 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002263 {
2264 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2265 // These restrictions are only implied by the ESSL 3.00 spec, but
2266 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002267 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002268 {
2269 error(qualifierLocation, "cannot be an array of structures",
2270 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002271 }
2272 if (type.isStructureContainingArrays())
2273 {
2274 error(qualifierLocation, "cannot be a structure containing an array",
2275 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002276 }
2277 if (type.isStructureContainingType(EbtStruct))
2278 {
2279 error(qualifierLocation, "cannot be a structure containing a structure",
2280 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002281 }
2282 if (type.isStructureContainingType(EbtBool))
2283 {
2284 error(qualifierLocation, "cannot be a structure containing a bool",
2285 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002286 }
2287 }
2288}
2289
Martin Radev2cc85b32016-08-05 16:22:53 +03002290void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2291{
2292 if (qualifier.getType() == QtStorage)
2293 {
2294 const TStorageQualifierWrapper &storageQualifier =
2295 static_cast<const TStorageQualifierWrapper &>(qualifier);
2296 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2297 !symbolTable.atGlobalLevel())
2298 {
2299 error(storageQualifier.getLine(),
2300 "Local variables can only use the const storage qualifier.",
2301 storageQualifier.getQualifierString().c_str());
2302 }
2303 }
2304}
2305
Olli Etuaho43364892017-02-13 16:00:12 +00002306void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002307 const TSourceLoc &location)
2308{
Jiajia Qinbc585152017-06-23 15:42:17 +08002309 const std::string reason(
2310 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2311 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002312 if (memoryQualifier.readonly)
2313 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002314 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002315 }
2316 if (memoryQualifier.writeonly)
2317 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002318 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002319 }
Martin Radev049edfa2016-11-11 14:35:37 +02002320 if (memoryQualifier.coherent)
2321 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002322 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002323 }
2324 if (memoryQualifier.restrictQualifier)
2325 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002326 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002327 }
2328 if (memoryQualifier.volatileQualifier)
2329 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002330 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002331 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002332}
2333
jchen104cdac9e2017-05-08 11:01:20 +08002334// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2335// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002336void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2337 const TSourceLoc &loc,
2338 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002339{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002340 if (!IsAtomicCounter(type->getBasicType()))
2341 {
2342 return;
2343 }
2344
2345 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2346 : kAtomicCounterSize;
2347 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2348 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002349 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002350 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002351 {
2352 offset = bindingState.appendSpan(size);
2353 }
2354 else
2355 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002356 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002357 }
2358 if (offset == -1)
2359 {
2360 error(loc, "Offset overlapping", "atomic counter");
2361 return;
2362 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002363 layoutQualifier.offset = offset;
2364 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002365}
2366
Olli Etuaho454c34c2017-10-25 16:35:56 +03002367void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2368 const char *token,
2369 TType *type)
2370{
2371 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2372 {
2373 if (type->isArray() && type->getOutermostArraySize() == 0u)
2374 {
2375 // Set size for the unsized geometry shader inputs if they are declared after a valid
2376 // input primitive declaration.
2377 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2378 {
2379 ASSERT(mGeometryShaderInputArraySize > 0u);
2380 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2381 }
2382 else
2383 {
2384 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2385 // An input can be declared without an array size if there is a previous layout
2386 // which specifies the size.
2387 error(location,
2388 "Missing a valid input primitive declaration before declaring an unsized "
2389 "array input",
2390 token);
2391 }
2392 }
2393 else if (type->isArray())
2394 {
2395 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2396 }
2397 else
2398 {
2399 error(location, "Geometry shader input variable must be declared as an array", token);
2400 }
2401 }
2402}
2403
Olli Etuaho13389b62016-10-16 11:48:18 +01002404TIntermDeclaration *TParseContext::parseSingleDeclaration(
2405 TPublicType &publicType,
2406 const TSourceLoc &identifierOrTypeLocation,
2407 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002408{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002409 TType type(publicType);
2410 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2411 mDirectiveHandler.pragma().stdgl.invariantAll)
2412 {
2413 TQualifier qualifier = type.getQualifier();
2414
2415 // The directive handler has already taken care of rejecting invalid uses of this pragma
2416 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2417 // affected variable declarations:
2418 //
2419 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2420 // elsewhere, in TranslatorGLSL.)
2421 //
2422 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2423 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2424 // the way this is currently implemented we have to enable this compiler option before
2425 // parsing the shader and determining the shading language version it uses. If this were
2426 // implemented as a post-pass, the workaround could be more targeted.
2427 //
2428 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2429 // the specification, but there are desktop OpenGL drivers that expect that this is the
2430 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2431 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2432 {
2433 type.setInvariant(true);
2434 }
2435 }
2436
Olli Etuaho454c34c2017-10-25 16:35:56 +03002437 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002438
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002439 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2440 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002441
Olli Etuahobab4c082015-04-24 16:38:49 +03002442 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002443 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002444
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002445 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002446 if (emptyDeclaration)
2447 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002448 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002449 // In most cases we don't need to create a symbol node for an empty declaration.
2450 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2451 if (type.getBasicType() == EbtStruct)
2452 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002453 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002454 }
jchen104cdac9e2017-05-08 11:01:20 +08002455 else if (IsAtomicCounter(publicType.getBasicType()))
2456 {
2457 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2458 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002459 }
2460 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002461 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002462 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002463
Olli Etuaho55bde912017-10-25 13:41:13 +03002464 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002465
Olli Etuaho55bc9052017-10-25 17:33:06 +03002466 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002467
Olli Etuaho2935c582015-04-08 14:32:06 +03002468 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002469 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002470
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002471 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002472 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002473 symbol = new TIntermSymbol(variable->uniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002474 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002475 }
2476
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002477 TIntermDeclaration *declaration = new TIntermDeclaration();
2478 declaration->setLine(identifierOrTypeLocation);
2479 if (symbol)
2480 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002481 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002482 declaration->appendDeclarator(symbol);
2483 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002484 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002485}
2486
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002487TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2488 TPublicType &elementType,
2489 const TSourceLoc &identifierLocation,
2490 const TString &identifier,
2491 const TSourceLoc &indexLocation,
2492 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002493{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002494 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002495
Olli Etuaho55bde912017-10-25 13:41:13 +03002496 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002497 identifierLocation);
2498
Olli Etuaho55bde912017-10-25 13:41:13 +03002499 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002500
Olli Etuaho55bde912017-10-25 13:41:13 +03002501 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002502
Olli Etuaho55bde912017-10-25 13:41:13 +03002503 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002504 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002505
Olli Etuaho454c34c2017-10-25 16:35:56 +03002506 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002507
2508 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2509
Olli Etuaho55bc9052017-10-25 17:33:06 +03002510 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002511
Olli Etuaho2935c582015-04-08 14:32:06 +03002512 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002513 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002514
Olli Etuaho13389b62016-10-16 11:48:18 +01002515 TIntermDeclaration *declaration = new TIntermDeclaration();
2516 declaration->setLine(identifierLocation);
2517
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002518 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002519 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002520 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, arrayType);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002521 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002522 declaration->appendDeclarator(symbol);
2523 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002524
Olli Etuaho13389b62016-10-16 11:48:18 +01002525 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002526}
2527
Olli Etuaho13389b62016-10-16 11:48:18 +01002528TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2529 const TSourceLoc &identifierLocation,
2530 const TString &identifier,
2531 const TSourceLoc &initLocation,
2532 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002533{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002534 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002535
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002536 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2537 identifierLocation);
2538
2539 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002540
Olli Etuaho13389b62016-10-16 11:48:18 +01002541 TIntermDeclaration *declaration = new TIntermDeclaration();
2542 declaration->setLine(identifierLocation);
2543
2544 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002545 TType type(publicType);
2546 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002547 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002548 if (initNode)
2549 {
2550 declaration->appendDeclarator(initNode);
2551 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002552 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002553 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002554}
2555
Olli Etuaho13389b62016-10-16 11:48:18 +01002556TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002557 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002558 const TSourceLoc &identifierLocation,
2559 const TString &identifier,
2560 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002561 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002562 const TSourceLoc &initLocation,
2563 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002564{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002565 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002566
Olli Etuaho55bde912017-10-25 13:41:13 +03002567 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002568 identifierLocation);
2569
Olli Etuaho55bde912017-10-25 13:41:13 +03002570 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002571
Olli Etuaho55bde912017-10-25 13:41:13 +03002572 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002573
Olli Etuaho55bde912017-10-25 13:41:13 +03002574 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002575 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002576
Olli Etuaho13389b62016-10-16 11:48:18 +01002577 TIntermDeclaration *declaration = new TIntermDeclaration();
2578 declaration->setLine(identifierLocation);
2579
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002580 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002581 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002582 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002583 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002584 if (initNode)
2585 {
2586 declaration->appendDeclarator(initNode);
2587 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002588 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002589
2590 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002591}
2592
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002593TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002594 const TTypeQualifierBuilder &typeQualifierBuilder,
2595 const TSourceLoc &identifierLoc,
2596 const TString *identifier,
2597 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002598{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002599 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002600
Martin Radev70866b82016-07-22 15:27:42 +03002601 if (!typeQualifier.invariant)
2602 {
2603 error(identifierLoc, "Expected invariant", identifier->c_str());
2604 return nullptr;
2605 }
2606 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2607 {
2608 return nullptr;
2609 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002610 if (!symbol)
2611 {
2612 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002613 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002614 }
Martin Radev70866b82016-07-22 15:27:42 +03002615 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002616 {
Martin Radev70866b82016-07-22 15:27:42 +03002617 error(identifierLoc, "invariant declaration specifies qualifier",
2618 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002619 }
Martin Radev70866b82016-07-22 15:27:42 +03002620 if (typeQualifier.precision != EbpUndefined)
2621 {
2622 error(identifierLoc, "invariant declaration specifies precision",
2623 getPrecisionString(typeQualifier.precision));
2624 }
2625 if (!typeQualifier.layoutQualifier.isEmpty())
2626 {
2627 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2628 }
2629
2630 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002631 if (!variable)
2632 {
2633 return nullptr;
2634 }
Martin Radev70866b82016-07-22 15:27:42 +03002635 const TType &type = variable->getType();
2636
2637 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2638 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002639 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002640
2641 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2642
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002643 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->uniqueId(), *identifier, type);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002644 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002645
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002646 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002647}
2648
Olli Etuaho13389b62016-10-16 11:48:18 +01002649void TParseContext::parseDeclarator(TPublicType &publicType,
2650 const TSourceLoc &identifierLocation,
2651 const TString &identifier,
2652 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002653{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002654 // If the declaration starting this declarator list was empty (example: int,), some checks were
2655 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002656 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002657 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002658 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2659 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002660 }
2661
Olli Etuaho856c4972016-08-08 11:38:39 +03002662 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002663
Olli Etuaho2935c582015-04-08 14:32:06 +03002664 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002665 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002666
2667 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2668
Olli Etuaho55bde912017-10-25 13:41:13 +03002669 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2670
Olli Etuaho55bc9052017-10-25 17:33:06 +03002671 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2672
Olli Etuaho43364892017-02-13 16:00:12 +00002673 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002674
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002675 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002676 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002677 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, type);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002678 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002679 declarationOut->appendDeclarator(symbol);
2680 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002681}
2682
Olli Etuaho55bde912017-10-25 13:41:13 +03002683void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002684 const TSourceLoc &identifierLocation,
2685 const TString &identifier,
2686 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002687 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002688 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002689{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002690 // If the declaration starting this declarator list was empty (example: int,), some checks were
2691 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002692 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002693 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002694 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002695 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002696 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002697
Olli Etuaho55bde912017-10-25 13:41:13 +03002698 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002699
Olli Etuaho55bde912017-10-25 13:41:13 +03002700 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002701 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002702 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002703 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002704
Olli Etuaho454c34c2017-10-25 16:35:56 +03002705 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2706
Olli Etuaho55bde912017-10-25 13:41:13 +03002707 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2708
Olli Etuaho55bc9052017-10-25 17:33:06 +03002709 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002710
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002711 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002712 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002713
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002714 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002715 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002716 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, arrayType);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002717 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 Etuaho54a29ff2017-11-28 17:35:20 +02003168 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003169
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 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003193 symbol = new TIntermSymbol(variable->uniqueId(), variable->name(),
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003194 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 Etuaho54a29ff2017-11-28 17:35:20 +02003292 error(location, "built-in functions cannot be redefined", (*function)->name().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 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003314 error(location, "function already has a body", (*function)->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003315 }
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 Etuaho54a29ff2017-11-28 17:35:20 +02003348 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003349 }
3350 }
3351
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003352 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
3353 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303354 {
Martin Radevda6254b2016-12-14 17:00:36 +02003355 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303356 // Therefore overloading or redefining builtin functions is an error.
3357 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003358 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303359 }
3360 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003361 {
3362 if (prevDec->getReturnType() != function->getReturnType())
3363 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003364 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003365 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003366 }
3367 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3368 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003369 if (prevDec->getParam(i).type->getQualifier() !=
3370 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003371 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003372 error(location,
3373 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003374 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003375 }
3376 }
3377 }
3378
3379 //
3380 // Check for previously declared variables using the same name.
3381 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003382 TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003383 if (prevSym)
3384 {
3385 if (!prevSym->isFunction())
3386 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003387 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003388 }
3389 }
3390 else
3391 {
3392 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003393 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003394 }
3395
3396 // We're at the inner scope level of the function's arguments and body statement.
3397 // Add the function prototype to the surrounding scope instead.
3398 symbolTable.getOuterLevel()->insert(function);
3399
Olli Etuaho78d13742017-01-18 13:06:10 +00003400 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003401 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003402 {
3403 if (function->getParamCount() > 0)
3404 {
3405 error(location, "function cannot take any parameter(s)", "main");
3406 }
3407 if (function->getReturnType().getBasicType() != EbtVoid)
3408 {
3409 error(location, "main function cannot return a value",
3410 function->getReturnType().getBasicString());
3411 }
3412 }
3413
Jamie Madill185fb402015-06-12 15:48:48 -04003414 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003415 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3416 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003417 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3418 //
3419 return function;
3420}
3421
Olli Etuaho9de84a52016-06-14 17:36:01 +03003422TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3423 const TString *name,
3424 const TSourceLoc &location)
3425{
3426 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3427 {
3428 error(location, "no qualifiers allowed for function return",
3429 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003430 }
3431 if (!type.layoutQualifier.isEmpty())
3432 {
3433 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003434 }
jchen10cc2a10e2017-05-03 14:05:12 +08003435 // make sure an opaque type is not involved as well...
3436 std::string reason(getBasicString(type.getBasicType()));
3437 reason += "s can't be function return values";
3438 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003439 if (mShaderVersion < 300)
3440 {
3441 // Array return values are forbidden, but there's also no valid syntax for declaring array
3442 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003443 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003444
3445 if (type.isStructureContainingArrays())
3446 {
3447 // ESSL 1.00.17 section 6.1 Function Definitions
3448 error(location, "structures containing arrays can't be function return values",
3449 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003450 }
3451 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003452
3453 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003454 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003455}
3456
Olli Etuahocce89652017-06-19 16:04:09 +03003457TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3458{
Olli Etuahocce89652017-06-19 16:04:09 +03003459 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003460 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003461}
3462
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003463TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003464{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003465 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003466 {
3467 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3468 "[]");
3469 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003470 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003471 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003472 error(publicType.getLine(), "constructor can't be a structure definition",
3473 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003474 }
3475
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003476 TType *type = new TType(publicType);
3477 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003478 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003479 error(publicType.getLine(), "cannot construct this type",
3480 getBasicString(publicType.getBasicType()));
3481 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003482 }
3483
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003484 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003485}
3486
Olli Etuaho55bde912017-10-25 13:41:13 +03003487void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3488 const char *errorMessage,
3489 const char *token,
3490 TType *arrayType)
3491{
3492 if (arrayType->isUnsizedArray())
3493 {
3494 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003495 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003496 }
3497}
3498
3499TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003500 const TString *name,
3501 const TSourceLoc &nameLoc)
3502{
Olli Etuaho55bde912017-10-25 13:41:13 +03003503 ASSERT(type);
3504 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3505 type);
3506 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003507 {
3508 error(nameLoc, "illegal use of type 'void'", name->c_str());
3509 }
3510 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003511 TParameter param = {name, type};
3512 return param;
3513}
3514
Olli Etuaho55bde912017-10-25 13:41:13 +03003515TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3516 const TString *name,
3517 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003518{
Olli Etuaho55bde912017-10-25 13:41:13 +03003519 TType *type = new TType(publicType);
3520 return parseParameterDeclarator(type, name, nameLoc);
3521}
3522
3523TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3524 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003525 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003526 const TSourceLoc &arrayLoc,
3527 TPublicType *elementType)
3528{
3529 checkArrayElementIsNotArray(arrayLoc, *elementType);
3530 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003531 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003532 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003533}
3534
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003535bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3536 TType type,
3537 const TSourceLoc &line)
3538{
3539 if (arguments->empty())
3540 {
3541 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3542 return false;
3543 }
3544 for (TIntermNode *arg : *arguments)
3545 {
3546 TIntermTyped *element = arg->getAsTyped();
3547 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003548 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3549 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003550 {
3551 error(line, "constructing from a non-dereferenced array", "constructor");
3552 return false;
3553 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003554 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003555 {
3556 if (dimensionalityFromElement == 1u)
3557 {
3558 error(line, "implicitly sized array of arrays constructor argument is not an array",
3559 "constructor");
3560 }
3561 else
3562 {
3563 error(line,
3564 "implicitly sized array of arrays constructor argument dimensionality is too "
3565 "low",
3566 "constructor");
3567 }
3568 return false;
3569 }
3570 }
3571 return true;
3572}
3573
Jamie Madillb98c3a82015-07-23 14:26:04 -04003574// This function is used to test for the correctness of the parameters passed to various constructor
3575// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003576//
Olli Etuaho856c4972016-08-08 11:38:39 +03003577// 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 +00003578//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003579TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003580 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303581 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003582{
Olli Etuaho856c4972016-08-08 11:38:39 +03003583 if (type.isUnsizedArray())
3584 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003585 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003586 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003587 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003588 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003589 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003590 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3591 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003592 if (type.getOutermostArraySize() == 0u)
3593 {
3594 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3595 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003596 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003597 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003598 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003599 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003600 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003601 }
3602 }
3603 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003604 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003605
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003606 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003607 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003608 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003609 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003610
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003611 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003612 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003613
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003614 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3615 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003616 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003617 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003618 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003619 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003620}
3621
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003622//
3623// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003624// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003625//
Olli Etuaho13389b62016-10-16 11:48:18 +01003626TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003627 const TTypeQualifierBuilder &typeQualifierBuilder,
3628 const TSourceLoc &nameLine,
3629 const TString &blockName,
3630 TFieldList *fieldList,
3631 const TString *instanceName,
3632 const TSourceLoc &instanceLine,
3633 TIntermTyped *arrayIndex,
3634 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003635{
Olli Etuaho856c4972016-08-08 11:38:39 +03003636 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003637
Olli Etuaho77ba4082016-12-16 12:01:18 +00003638 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003639
Jiajia Qinbc585152017-06-23 15:42:17 +08003640 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003641 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003642 error(typeQualifier.line,
3643 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3644 "3.10",
3645 getQualifierString(typeQualifier.qualifier));
3646 }
3647 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3648 {
3649 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003650 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003651 }
3652
Martin Radev70866b82016-07-22 15:27:42 +03003653 if (typeQualifier.invariant)
3654 {
3655 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3656 }
3657
Jiajia Qinbc585152017-06-23 15:42:17 +08003658 if (typeQualifier.qualifier != EvqBuffer)
3659 {
3660 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3661 }
Olli Etuaho43364892017-02-13 16:00:12 +00003662
jchen10af713a22017-04-19 09:10:56 +08003663 // add array index
3664 unsigned int arraySize = 0;
3665 if (arrayIndex != nullptr)
3666 {
3667 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3668 }
3669
3670 if (mShaderVersion < 310)
3671 {
3672 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3673 }
3674 else
3675 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003676 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3677 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003678 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003679
Andrei Volykhina5527072017-03-22 16:46:30 +03003680 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3681
Jamie Madill099c0f32013-06-20 11:55:52 -04003682 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003683 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003684 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3685 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003686
Jamie Madill099c0f32013-06-20 11:55:52 -04003687 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3688 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003689 if (typeQualifier.qualifier == EvqUniform)
3690 {
3691 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3692 }
3693 else if (typeQualifier.qualifier == EvqBuffer)
3694 {
3695 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3696 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003697 }
3698
Jamie Madill1566ef72013-06-20 11:55:54 -04003699 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3700 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003701 if (typeQualifier.qualifier == EvqUniform)
3702 {
3703 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3704 }
3705 else if (typeQualifier.qualifier == EvqBuffer)
3706 {
3707 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3708 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003709 }
3710
Olli Etuaho856c4972016-08-08 11:38:39 +03003711 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003712
Martin Radev2cc85b32016-08-05 16:22:53 +03003713 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3714
Olli Etuaho0f684632017-07-13 12:42:15 +03003715 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303716 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003717 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003718 }
3719
Jamie Madill98493dd2013-07-08 14:39:03 -04003720 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303721 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3722 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003723 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303724 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003725 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303726 {
jchen10cc2a10e2017-05-03 14:05:12 +08003727 std::string reason("unsupported type - ");
3728 reason += fieldType->getBasicString();
3729 reason += " types are not allowed in interface blocks";
3730 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003731 }
3732
Jamie Madill98493dd2013-07-08 14:39:03 -04003733 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003734 switch (qualifier)
3735 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003736 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003737 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003738 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003739 if (typeQualifier.qualifier == EvqBuffer)
3740 {
3741 error(field->line(), "invalid qualifier on shader storage block member",
3742 getQualifierString(qualifier));
3743 }
3744 break;
3745 case EvqBuffer:
3746 if (typeQualifier.qualifier == EvqUniform)
3747 {
3748 error(field->line(), "invalid qualifier on uniform block member",
3749 getQualifierString(qualifier));
3750 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003751 break;
3752 default:
3753 error(field->line(), "invalid qualifier on interface block member",
3754 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003755 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003756 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003757
Martin Radev70866b82016-07-22 15:27:42 +03003758 if (fieldType->isInvariant())
3759 {
3760 error(field->line(), "invalid qualifier on interface block member", "invariant");
3761 }
3762
Jamie Madilla5efff92013-06-06 11:56:47 -04003763 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003764 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003765 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003766 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003767
Jamie Madill98493dd2013-07-08 14:39:03 -04003768 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003769 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003770 error(field->line(), "invalid layout qualifier: cannot be used here",
3771 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003772 }
3773
Jamie Madill98493dd2013-07-08 14:39:03 -04003774 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003775 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003776 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003777 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003778 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003779 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003780 warning(field->line(),
3781 "extraneous layout qualifier: only has an effect on matrix types",
3782 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003783 }
3784
Jamie Madill98493dd2013-07-08 14:39:03 -04003785 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003786
Olli Etuahoebee5b32017-11-23 12:56:32 +02003787 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3788 typeQualifier.qualifier != EvqBuffer)
3789 {
3790 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3791 checkIsNotUnsizedArray(field->line(),
3792 "array members of interface blocks must specify a size",
3793 field->name().c_str(), field->type());
3794 }
3795
Jiajia Qinbc585152017-06-23 15:42:17 +08003796 if (typeQualifier.qualifier == EvqBuffer)
3797 {
3798 // set memory qualifiers
3799 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3800 // qualified with a memory qualifier, it is as if all of its members were declared with
3801 // the same memory qualifier.
3802 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3803 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3804 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3805 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3806 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3807 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3808 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3809 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3810 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3811 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3812 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003813 }
3814
Jamie Madillb98c3a82015-07-23 14:26:04 -04003815 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003816 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003817 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3818 if (arrayIndex != nullptr)
3819 {
3820 interfaceBlockType.makeArray(arraySize);
3821 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003822
3823 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003824 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003825
Jamie Madill98493dd2013-07-08 14:39:03 -04003826 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003827 {
3828 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003829 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3830 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003831 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303832 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003833
3834 // set parent pointer of the field variable
3835 fieldType->setInterfaceBlock(interfaceBlock);
3836
Olli Etuaho0f684632017-07-13 12:42:15 +03003837 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003838
Olli Etuaho0f684632017-07-13 12:42:15 +03003839 if (fieldVariable)
3840 {
3841 fieldVariable->setQualifier(typeQualifier.qualifier);
3842 }
3843 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303844 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003845 error(field->line(), "redefinition of an interface block member name",
3846 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003847 }
3848 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003849 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003850 }
3851 else
3852 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003853 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003854
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003855 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003856 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3857 if (instanceTypeDef)
3858 {
3859 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003860 symbolId = &instanceTypeDef->uniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003861 }
3862 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303863 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003864 error(instanceLine, "redefinition of an interface block instance name",
3865 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003866 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003867 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003868 }
3869
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003870 TIntermDeclaration *declaration = nullptr;
3871
3872 if (symbolId)
3873 {
3874 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3875 blockSymbol->setLine(typeQualifier.line);
3876 declaration = new TIntermDeclaration();
3877 declaration->appendDeclarator(blockSymbol);
3878 declaration->setLine(nameLine);
3879 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003880
3881 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003882 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003883}
3884
Olli Etuaho383b7912016-08-05 11:22:59 +03003885void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003886{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003887 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003888
3889 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003890 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303891 if (mStructNestingLevel > 1)
3892 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003893 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003894 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003895}
3896
3897void TParseContext::exitStructDeclaration()
3898{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003899 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003900}
3901
Olli Etuaho8a176262016-08-16 14:23:01 +03003902void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003903{
Jamie Madillacb4b812016-11-07 13:50:29 -05003904 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303905 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003906 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003907 }
3908
Arun Patole7e7e68d2015-05-22 12:02:25 +05303909 if (field.type()->getBasicType() != EbtStruct)
3910 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003911 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003912 }
3913
3914 // We're already inside a structure definition at this point, so add
3915 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303916 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3917 {
Jamie Madill41a49272014-03-18 16:10:13 -04003918 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003919 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3920 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003921 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003922 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003923 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003924 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003925}
3926
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003927//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003928// Parse an array index expression
3929//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003930TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3931 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303932 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003933{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003934 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3935 {
3936 if (baseExpression->getAsSymbolNode())
3937 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303938 error(location, " left of '[' is not of type array, matrix, or vector ",
3939 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003940 }
3941 else
3942 {
3943 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3944 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003945
Olli Etuaho3ec75682017-07-05 17:02:55 +03003946 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003947 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003948
Jiawei Shaod8105a02017-08-08 09:54:36 +08003949 if (baseExpression->getQualifier() == EvqPerVertexIn)
3950 {
3951 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3952 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3953 {
3954 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3955 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3956 }
3957 }
3958
Jamie Madill21c1e452014-12-29 11:33:41 -05003959 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3960
Olli Etuaho36b05142015-11-12 13:10:42 +02003961 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3962 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3963 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3964 // index is a constant expression.
3965 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3966 {
3967 if (baseExpression->isInterfaceBlock())
3968 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003969 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3970 switch (baseExpression->getQualifier())
3971 {
3972 case EvqPerVertexIn:
3973 break;
3974 case EvqUniform:
3975 case EvqBuffer:
3976 error(location,
3977 "array indexes for uniform block arrays and shader storage block arrays "
3978 "must be constant integral expressions",
3979 "[");
3980 break;
3981 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003982 // We can reach here only in error cases.
3983 ASSERT(mDiagnostics->numErrors() > 0);
3984 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003985 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003986 }
3987 else if (baseExpression->getQualifier() == EvqFragmentOut)
3988 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003989 error(location,
3990 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003991 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003992 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3993 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003994 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003995 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003996 }
3997
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003998 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003999 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004000 // If an out-of-range index is not qualified as constant, the behavior in the spec is
4001 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
4002 // constant fold expressions that are not constant expressions). The most compatible way to
4003 // handle this case is to report a warning instead of an error and force the index to be in
4004 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004005 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03004006 int index = 0;
4007 if (indexConstantUnion->getBasicType() == EbtInt)
4008 {
4009 index = indexConstantUnion->getIConst(0);
4010 }
4011 else if (indexConstantUnion->getBasicType() == EbtUInt)
4012 {
4013 index = static_cast<int>(indexConstantUnion->getUConst(0));
4014 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004015
4016 int safeIndex = -1;
4017
Olli Etuahoebee5b32017-11-23 12:56:32 +02004018 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004019 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004020 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4021 safeIndex = 0;
4022 }
4023
4024 if (!baseExpression->getType().isUnsizedArray())
4025 {
4026 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004027 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004028 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004029 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004030 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4031 {
4032 outOfRangeError(outOfRangeIndexIsError, location,
4033 "array index for gl_FragData must be zero when "
4034 "GL_EXT_draw_buffers is disabled",
4035 "[]");
4036 safeIndex = 0;
4037 }
4038 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004039 }
4040 // Only do generic out-of-range check if similar error hasn't already been reported.
4041 if (safeIndex < 0)
4042 {
4043 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004044 {
4045 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4046 baseExpression->getOutermostArraySize(),
4047 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004048 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004049 else if (baseExpression->isMatrix())
4050 {
4051 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4052 baseExpression->getType().getCols(),
4053 "matrix field selection out of range");
4054 }
4055 else
4056 {
4057 ASSERT(baseExpression->isVector());
4058 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4059 baseExpression->getType().getNominalSize(),
4060 "vector field selection out of range");
4061 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004062 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004063
Olli Etuahoebee5b32017-11-23 12:56:32 +02004064 ASSERT(safeIndex >= 0);
4065 // Data of constant unions can't be changed, because it may be shared with other
4066 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4067 // sanitized object.
4068 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4069 {
4070 TConstantUnion *safeConstantUnion = new TConstantUnion();
4071 safeConstantUnion->setIConst(safeIndex);
4072 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4073 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4074 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004075
Olli Etuahoebee5b32017-11-23 12:56:32 +02004076 TIntermBinary *node =
4077 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4078 node->setLine(location);
4079 return node->fold(mDiagnostics);
4080 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004081 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004082
4083 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4084 node->setLine(location);
4085 // Indirect indexing can never be constant folded.
4086 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004087}
4088
Olli Etuahoebee5b32017-11-23 12:56:32 +02004089int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4090 const TSourceLoc &location,
4091 int index,
4092 int arraySize,
4093 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004094{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004095 // Should not reach here with an unsized / runtime-sized array.
4096 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004097 // A negative index should already have been checked.
4098 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004099 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004100 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004101 std::stringstream reasonStream;
4102 reasonStream << reason << " '" << index << "'";
4103 std::string token = reasonStream.str();
4104 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004105 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004106 }
4107 return index;
4108}
4109
Jamie Madillb98c3a82015-07-23 14:26:04 -04004110TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4111 const TSourceLoc &dotLocation,
4112 const TString &fieldString,
4113 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004114{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004115 if (baseExpression->isArray())
4116 {
4117 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004118 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004119 }
4120
4121 if (baseExpression->isVector())
4122 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004123 TVector<int> fieldOffsets;
4124 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4125 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004126 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004127 fieldOffsets.resize(1);
4128 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004129 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004130 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4131 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004132
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004133 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004134 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004135 else if (baseExpression->getBasicType() == EbtStruct)
4136 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304137 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004138 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004139 {
4140 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004141 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004142 }
4143 else
4144 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004145 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004146 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004147 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004148 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004149 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004150 {
4151 fieldFound = true;
4152 break;
4153 }
4154 }
4155 if (fieldFound)
4156 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004157 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004158 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004159 TIntermBinary *node =
4160 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4161 node->setLine(dotLocation);
4162 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004163 }
4164 else
4165 {
4166 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004167 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004168 }
4169 }
4170 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004171 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004172 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304173 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004174 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004175 {
4176 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004177 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004178 }
4179 else
4180 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004181 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004182 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004183 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004184 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004185 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004186 {
4187 fieldFound = true;
4188 break;
4189 }
4190 }
4191 if (fieldFound)
4192 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004193 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004194 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004195 TIntermBinary *node =
4196 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4197 node->setLine(dotLocation);
4198 // Indexing interface blocks can never be constant folded.
4199 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004200 }
4201 else
4202 {
4203 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004204 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004205 }
4206 }
4207 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004208 else
4209 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004210 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004211 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004212 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304213 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004214 }
4215 else
4216 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304217 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004218 " field selection requires structure, vector, or interface block on left hand "
4219 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304220 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004221 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004222 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004223 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004224}
4225
Jamie Madillb98c3a82015-07-23 14:26:04 -04004226TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4227 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004228{
Jamie Madill2f294c92017-11-20 14:47:26 -05004229 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004230
4231 if (qualifierType == "shared")
4232 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004233 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004234 {
4235 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4236 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004237 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004238 }
4239 else if (qualifierType == "packed")
4240 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004241 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004242 {
4243 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4244 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004245 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004246 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004247 else if (qualifierType == "std430")
4248 {
4249 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4250 qualifier.blockStorage = EbsStd430;
4251 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004252 else if (qualifierType == "std140")
4253 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004254 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004255 }
4256 else if (qualifierType == "row_major")
4257 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004258 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004259 }
4260 else if (qualifierType == "column_major")
4261 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004262 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004263 }
4264 else if (qualifierType == "location")
4265 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004266 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4267 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004268 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004269 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004270 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004271 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4272 {
4273 qualifier.yuv = true;
4274 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004275 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004276 else if (qualifierType == "rgba32f")
4277 {
4278 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4279 qualifier.imageInternalFormat = EiifRGBA32F;
4280 }
4281 else if (qualifierType == "rgba16f")
4282 {
4283 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4284 qualifier.imageInternalFormat = EiifRGBA16F;
4285 }
4286 else if (qualifierType == "r32f")
4287 {
4288 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4289 qualifier.imageInternalFormat = EiifR32F;
4290 }
4291 else if (qualifierType == "rgba8")
4292 {
4293 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4294 qualifier.imageInternalFormat = EiifRGBA8;
4295 }
4296 else if (qualifierType == "rgba8_snorm")
4297 {
4298 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4299 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4300 }
4301 else if (qualifierType == "rgba32i")
4302 {
4303 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4304 qualifier.imageInternalFormat = EiifRGBA32I;
4305 }
4306 else if (qualifierType == "rgba16i")
4307 {
4308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4309 qualifier.imageInternalFormat = EiifRGBA16I;
4310 }
4311 else if (qualifierType == "rgba8i")
4312 {
4313 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4314 qualifier.imageInternalFormat = EiifRGBA8I;
4315 }
4316 else if (qualifierType == "r32i")
4317 {
4318 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4319 qualifier.imageInternalFormat = EiifR32I;
4320 }
4321 else if (qualifierType == "rgba32ui")
4322 {
4323 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4324 qualifier.imageInternalFormat = EiifRGBA32UI;
4325 }
4326 else if (qualifierType == "rgba16ui")
4327 {
4328 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4329 qualifier.imageInternalFormat = EiifRGBA16UI;
4330 }
4331 else if (qualifierType == "rgba8ui")
4332 {
4333 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4334 qualifier.imageInternalFormat = EiifRGBA8UI;
4335 }
4336 else if (qualifierType == "r32ui")
4337 {
4338 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4339 qualifier.imageInternalFormat = EiifR32UI;
4340 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004341 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4342 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004343 {
4344 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4345 qualifier.primitiveType = EptPoints;
4346 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004347 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4348 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004349 {
4350 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4351 qualifier.primitiveType = EptLines;
4352 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004353 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4354 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004355 {
4356 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4357 qualifier.primitiveType = EptLinesAdjacency;
4358 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004359 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4360 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004361 {
4362 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4363 qualifier.primitiveType = EptTriangles;
4364 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004365 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4366 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004367 {
4368 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4369 qualifier.primitiveType = EptTrianglesAdjacency;
4370 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004371 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4372 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004373 {
4374 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4375 qualifier.primitiveType = EptLineStrip;
4376 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004377 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4378 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004379 {
4380 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4381 qualifier.primitiveType = EptTriangleStrip;
4382 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004383
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004384 else
4385 {
4386 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004387 }
4388
Jamie Madilla5efff92013-06-06 11:56:47 -04004389 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004390}
4391
Martin Radev802abe02016-08-04 17:48:32 +03004392void TParseContext::parseLocalSize(const TString &qualifierType,
4393 const TSourceLoc &qualifierTypeLine,
4394 int intValue,
4395 const TSourceLoc &intValueLine,
4396 const std::string &intValueString,
4397 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004398 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004399{
Olli Etuaho856c4972016-08-08 11:38:39 +03004400 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004401 if (intValue < 1)
4402 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004403 std::stringstream reasonStream;
4404 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4405 std::string reason = reasonStream.str();
4406 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004407 }
4408 (*localSize)[index] = intValue;
4409}
4410
Olli Etuaho09b04a22016-12-15 13:30:26 +00004411void TParseContext::parseNumViews(int intValue,
4412 const TSourceLoc &intValueLine,
4413 const std::string &intValueString,
4414 int *numViews)
4415{
4416 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4417 // specification.
4418 if (intValue < 1)
4419 {
4420 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4421 }
4422 *numViews = intValue;
4423}
4424
Shaob5cc1192017-07-06 10:47:20 +08004425void TParseContext::parseInvocations(int intValue,
4426 const TSourceLoc &intValueLine,
4427 const std::string &intValueString,
4428 int *numInvocations)
4429{
4430 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4431 // it doesn't make sense to accept invocations <= 0.
4432 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4433 {
4434 error(intValueLine,
4435 "out of range: invocations must be in the range of [1, "
4436 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4437 intValueString.c_str());
4438 }
4439 else
4440 {
4441 *numInvocations = intValue;
4442 }
4443}
4444
4445void TParseContext::parseMaxVertices(int intValue,
4446 const TSourceLoc &intValueLine,
4447 const std::string &intValueString,
4448 int *maxVertices)
4449{
4450 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4451 // it doesn't make sense to accept max_vertices < 0.
4452 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4453 {
4454 error(
4455 intValueLine,
4456 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4457 intValueString.c_str());
4458 }
4459 else
4460 {
4461 *maxVertices = intValue;
4462 }
4463}
4464
Jamie Madillb98c3a82015-07-23 14:26:04 -04004465TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4466 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004467 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304468 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004469{
Jamie Madill2f294c92017-11-20 14:47:26 -05004470 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004471
Martin Radev802abe02016-08-04 17:48:32 +03004472 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004473
Martin Radev802abe02016-08-04 17:48:32 +03004474 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004475 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004476 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004477 if (intValue < 0)
4478 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004479 error(intValueLine, "out of range: location must be non-negative",
4480 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004481 }
4482 else
4483 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004484 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004485 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004486 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004487 }
Olli Etuaho43364892017-02-13 16:00:12 +00004488 else if (qualifierType == "binding")
4489 {
4490 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4491 if (intValue < 0)
4492 {
4493 error(intValueLine, "out of range: binding must be non-negative",
4494 intValueString.c_str());
4495 }
4496 else
4497 {
4498 qualifier.binding = intValue;
4499 }
4500 }
jchen104cdac9e2017-05-08 11:01:20 +08004501 else if (qualifierType == "offset")
4502 {
4503 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4504 if (intValue < 0)
4505 {
4506 error(intValueLine, "out of range: offset must be non-negative",
4507 intValueString.c_str());
4508 }
4509 else
4510 {
4511 qualifier.offset = intValue;
4512 }
4513 }
Martin Radev802abe02016-08-04 17:48:32 +03004514 else if (qualifierType == "local_size_x")
4515 {
4516 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4517 &qualifier.localSize);
4518 }
4519 else if (qualifierType == "local_size_y")
4520 {
4521 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4522 &qualifier.localSize);
4523 }
4524 else if (qualifierType == "local_size_z")
4525 {
4526 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4527 &qualifier.localSize);
4528 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004529 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004530 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004531 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4532 {
4533 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4534 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004535 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004536 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4537 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004538 {
4539 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4540 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004541 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4542 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004543 {
4544 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4545 }
4546
Martin Radev802abe02016-08-04 17:48:32 +03004547 else
4548 {
4549 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004550 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004551
Jamie Madilla5efff92013-06-06 11:56:47 -04004552 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004553}
4554
Olli Etuaho613b9592016-09-05 12:05:53 +03004555TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4556{
4557 return new TTypeQualifierBuilder(
4558 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4559 mShaderVersion);
4560}
4561
Olli Etuahocce89652017-06-19 16:04:09 +03004562TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4563 const TSourceLoc &loc)
4564{
4565 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4566 return new TStorageQualifierWrapper(qualifier, loc);
4567}
4568
4569TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4570{
4571 if (getShaderType() == GL_VERTEX_SHADER)
4572 {
4573 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4574 }
4575 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4576}
4577
4578TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4579{
4580 if (declaringFunction())
4581 {
4582 return new TStorageQualifierWrapper(EvqIn, loc);
4583 }
Shaob5cc1192017-07-06 10:47:20 +08004584
4585 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004586 {
Shaob5cc1192017-07-06 10:47:20 +08004587 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004588 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004589 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004590 {
4591 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4592 }
4593 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004594 }
Shaob5cc1192017-07-06 10:47:20 +08004595 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004596 {
Shaob5cc1192017-07-06 10:47:20 +08004597 if (mShaderVersion < 300)
4598 {
4599 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4600 }
4601 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004602 }
Shaob5cc1192017-07-06 10:47:20 +08004603 case GL_COMPUTE_SHADER:
4604 {
4605 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4606 }
4607 case GL_GEOMETRY_SHADER_OES:
4608 {
4609 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4610 }
4611 default:
4612 {
4613 UNREACHABLE();
4614 return new TStorageQualifierWrapper(EvqLast, loc);
4615 }
Olli Etuahocce89652017-06-19 16:04:09 +03004616 }
Olli Etuahocce89652017-06-19 16:04:09 +03004617}
4618
4619TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4620{
4621 if (declaringFunction())
4622 {
4623 return new TStorageQualifierWrapper(EvqOut, loc);
4624 }
Shaob5cc1192017-07-06 10:47:20 +08004625 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004626 {
Shaob5cc1192017-07-06 10:47:20 +08004627 case GL_VERTEX_SHADER:
4628 {
4629 if (mShaderVersion < 300)
4630 {
4631 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4632 }
4633 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4634 }
4635 case GL_FRAGMENT_SHADER:
4636 {
4637 if (mShaderVersion < 300)
4638 {
4639 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4640 }
4641 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4642 }
4643 case GL_COMPUTE_SHADER:
4644 {
4645 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4646 return new TStorageQualifierWrapper(EvqLast, loc);
4647 }
4648 case GL_GEOMETRY_SHADER_OES:
4649 {
4650 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4651 }
4652 default:
4653 {
4654 UNREACHABLE();
4655 return new TStorageQualifierWrapper(EvqLast, loc);
4656 }
Olli Etuahocce89652017-06-19 16:04:09 +03004657 }
Olli Etuahocce89652017-06-19 16:04:09 +03004658}
4659
4660TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4661{
4662 if (!declaringFunction())
4663 {
4664 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4665 }
4666 return new TStorageQualifierWrapper(EvqInOut, loc);
4667}
4668
Jamie Madillb98c3a82015-07-23 14:26:04 -04004669TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004670 TLayoutQualifier rightQualifier,
4671 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004672{
Martin Radevc28888b2016-07-22 15:27:42 +03004673 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004674 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004675}
4676
Olli Etuahocce89652017-06-19 16:04:09 +03004677TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4678{
4679 checkIsNotReserved(loc, *identifier);
4680 TType *type = new TType(EbtVoid, EbpUndefined);
4681 return new TField(type, identifier, loc);
4682}
4683
4684TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4685 const TSourceLoc &loc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004686 const TVector<unsigned int> &arraySizes,
Olli Etuahocce89652017-06-19 16:04:09 +03004687 const TSourceLoc &arraySizeLoc)
4688{
4689 checkIsNotReserved(loc, *identifier);
4690
4691 TType *type = new TType(EbtVoid, EbpUndefined);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004692 type->makeArrays(arraySizes);
Olli Etuahocce89652017-06-19 16:04:09 +03004693
4694 return new TField(type, identifier, loc);
4695}
4696
Olli Etuaho722bfb52017-10-26 17:00:11 +03004697void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4698 const TFieldList::const_iterator end,
4699 const TString &name,
4700 const TSourceLoc &location)
4701{
4702 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4703 {
4704 if ((*fieldIter)->name() == name)
4705 {
4706 error(location, "duplicate field name in structure", name.c_str());
4707 }
4708 }
4709}
4710
4711TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4712{
4713 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4714 ++fieldIter)
4715 {
4716 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4717 location);
4718 }
4719 return fields;
4720}
4721
Olli Etuaho4de340a2016-12-16 09:32:03 +00004722TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4723 const TFieldList *newlyAddedFields,
4724 const TSourceLoc &location)
4725{
4726 for (TField *field : *newlyAddedFields)
4727 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004728 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4729 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004730 processedFields->push_back(field);
4731 }
4732 return processedFields;
4733}
4734
Martin Radev70866b82016-07-22 15:27:42 +03004735TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4736 const TTypeQualifierBuilder &typeQualifierBuilder,
4737 TPublicType *typeSpecifier,
4738 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004739{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004740 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004741
Martin Radev70866b82016-07-22 15:27:42 +03004742 typeSpecifier->qualifier = typeQualifier.qualifier;
4743 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004744 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004745 typeSpecifier->invariant = typeQualifier.invariant;
4746 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304747 {
Martin Radev70866b82016-07-22 15:27:42 +03004748 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004749 }
Martin Radev70866b82016-07-22 15:27:42 +03004750 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004751}
4752
Jamie Madillb98c3a82015-07-23 14:26:04 -04004753TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004754 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004755{
Martin Radev4a9cd802016-09-01 16:51:51 +03004756 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4757 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004758
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004759 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4760 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004761
Martin Radev4a9cd802016-09-01 16:51:51 +03004762 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004763
Olli Etuaho55bde912017-10-25 13:41:13 +03004764 for (TField *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304765 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004766 // Don't allow arrays of arrays in ESSL < 3.10.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08004767 if (declarator->type()->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304768 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004769 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004770 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004771
Kai Ninomiya57ea5332017-11-22 14:04:48 -08004772 auto *declaratorArraySizes = declarator->type()->getArraySizes();
4773
Olli Etuaho55bde912017-10-25 13:41:13 +03004774 TType *type = declarator->type();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004775 *type = TType(typeSpecifier);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08004776 if (declaratorArraySizes != nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304777 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08004778 for (unsigned int arraySize : *declaratorArraySizes)
4779 {
4780 type->makeArray(arraySize);
4781 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004782 }
4783
Olli Etuaho55bde912017-10-25 13:41:13 +03004784 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *declarator);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004785 }
4786
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004787 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004788}
4789
Martin Radev4a9cd802016-09-01 16:51:51 +03004790TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4791 const TSourceLoc &nameLine,
4792 const TString *structName,
4793 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004794{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004795 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004796
Jamie Madill9b820842015-02-12 10:40:10 -05004797 // Store a bool in the struct if we're at global scope, to allow us to
4798 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004799 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004800
Jamie Madill98493dd2013-07-08 14:39:03 -04004801 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004802 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004803 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004804 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304805 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004806 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004807 }
4808 }
4809
4810 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004811 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004812 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004813 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004814 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004815 switch (qualifier)
4816 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004817 case EvqGlobal:
4818 case EvqTemporary:
4819 break;
4820 default:
4821 error(field.line(), "invalid qualifier on struct member",
4822 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004823 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004824 }
Martin Radev70866b82016-07-22 15:27:42 +03004825 if (field.type()->isInvariant())
4826 {
4827 error(field.line(), "invalid qualifier on struct member", "invariant");
4828 }
jchen104cdac9e2017-05-08 11:01:20 +08004829 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4830 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004831 {
4832 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4833 }
4834
Olli Etuahoebee5b32017-11-23 12:56:32 +02004835 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4836 field.name().c_str(), field.type());
4837
Olli Etuaho43364892017-02-13 16:00:12 +00004838 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4839
4840 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004841
4842 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004843 }
4844
Martin Radev4a9cd802016-09-01 16:51:51 +03004845 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004846 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004847 exitStructDeclaration();
4848
Martin Radev4a9cd802016-09-01 16:51:51 +03004849 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004850}
4851
Jamie Madillb98c3a82015-07-23 14:26:04 -04004852TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004853 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004854 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004855{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004856 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004857 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004858 init->isVector())
4859 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004860 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4861 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004862 return nullptr;
4863 }
4864
Olli Etuaho923ecef2017-10-11 12:01:38 +03004865 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004866 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004867 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004868 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004869 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004870 }
4871
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004872 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4873 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004874 return node;
4875}
4876
4877TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4878{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004879 if (mSwitchNestingLevel == 0)
4880 {
4881 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004882 return nullptr;
4883 }
4884 if (condition == nullptr)
4885 {
4886 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004887 return nullptr;
4888 }
4889 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004890 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004891 {
4892 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004893 }
4894 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004895 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4896 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4897 // fold in case labels.
4898 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004899 {
4900 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004901 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004902 TIntermCase *node = new TIntermCase(condition);
4903 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004904 return node;
4905}
4906
4907TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4908{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004909 if (mSwitchNestingLevel == 0)
4910 {
4911 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004912 return nullptr;
4913 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004914 TIntermCase *node = new TIntermCase(nullptr);
4915 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004916 return node;
4917}
4918
Jamie Madillb98c3a82015-07-23 14:26:04 -04004919TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4920 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004921 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004922{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004923 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004924
4925 switch (op)
4926 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004927 case EOpLogicalNot:
4928 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4929 child->isVector())
4930 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004931 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004932 return nullptr;
4933 }
4934 break;
4935 case EOpBitwiseNot:
4936 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4937 child->isMatrix() || child->isArray())
4938 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004939 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004940 return nullptr;
4941 }
4942 break;
4943 case EOpPostIncrement:
4944 case EOpPreIncrement:
4945 case EOpPostDecrement:
4946 case EOpPreDecrement:
4947 case EOpNegative:
4948 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004949 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4950 child->getBasicType() == EbtBool || child->isArray() ||
4951 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004952 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004953 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004954 return nullptr;
4955 }
4956 // Operators for built-ins are already type checked against their prototype.
4957 default:
4958 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004959 }
4960
Jiajia Qinbc585152017-06-23 15:42:17 +08004961 if (child->getMemoryQualifier().writeonly)
4962 {
4963 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4964 return nullptr;
4965 }
4966
Olli Etuahof119a262016-08-19 15:54:22 +03004967 TIntermUnary *node = new TIntermUnary(op, child);
4968 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004969
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004970 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004971}
4972
Olli Etuaho09b22472015-02-11 11:47:26 +02004973TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4974{
Olli Etuahocce89652017-06-19 16:04:09 +03004975 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004976 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004977 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004978 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004979 return child;
4980 }
4981 return node;
4982}
4983
Jamie Madillb98c3a82015-07-23 14:26:04 -04004984TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4985 TIntermTyped *child,
4986 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004987{
Olli Etuaho856c4972016-08-08 11:38:39 +03004988 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004989 return addUnaryMath(op, child, loc);
4990}
4991
Jamie Madillb98c3a82015-07-23 14:26:04 -04004992bool TParseContext::binaryOpCommonCheck(TOperator op,
4993 TIntermTyped *left,
4994 TIntermTyped *right,
4995 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004996{
jchen10b4cf5652017-05-05 18:51:17 +08004997 // Check opaque types are not allowed to be operands in expressions other than array indexing
4998 // and structure member selection.
4999 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
5000 {
5001 switch (op)
5002 {
5003 case EOpIndexDirect:
5004 case EOpIndexIndirect:
5005 break;
5006 case EOpIndexDirectStruct:
5007 UNREACHABLE();
5008
5009 default:
5010 error(loc, "Invalid operation for variables with an opaque type",
5011 GetOperatorString(op));
5012 return false;
5013 }
5014 }
jchen10cc2a10e2017-05-03 14:05:12 +08005015
Jiajia Qinbc585152017-06-23 15:42:17 +08005016 if (right->getMemoryQualifier().writeonly)
5017 {
5018 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5019 return false;
5020 }
5021
5022 if (left->getMemoryQualifier().writeonly)
5023 {
5024 switch (op)
5025 {
5026 case EOpAssign:
5027 case EOpInitialize:
5028 case EOpIndexDirect:
5029 case EOpIndexIndirect:
5030 case EOpIndexDirectStruct:
5031 case EOpIndexDirectInterfaceBlock:
5032 break;
5033 default:
5034 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5035 return false;
5036 }
5037 }
5038
Olli Etuaho244be012016-08-18 15:26:02 +03005039 if (left->getType().getStruct() || right->getType().getStruct())
5040 {
5041 switch (op)
5042 {
5043 case EOpIndexDirectStruct:
5044 ASSERT(left->getType().getStruct());
5045 break;
5046 case EOpEqual:
5047 case EOpNotEqual:
5048 case EOpAssign:
5049 case EOpInitialize:
5050 if (left->getType() != right->getType())
5051 {
5052 return false;
5053 }
5054 break;
5055 default:
5056 error(loc, "Invalid operation for structs", GetOperatorString(op));
5057 return false;
5058 }
5059 }
5060
Olli Etuaho94050052017-05-08 14:17:44 +03005061 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5062 {
5063 switch (op)
5064 {
5065 case EOpIndexDirectInterfaceBlock:
5066 ASSERT(left->getType().getInterfaceBlock());
5067 break;
5068 default:
5069 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5070 return false;
5071 }
5072 }
5073
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005074 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005075 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005076 error(loc, "array / non-array mismatch", GetOperatorString(op));
5077 return false;
5078 }
5079
5080 if (left->isArray())
5081 {
5082 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005083 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005084 {
5085 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5086 return false;
5087 }
5088
Olli Etuahoe79904c2015-03-18 16:56:42 +02005089 switch (op)
5090 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005091 case EOpEqual:
5092 case EOpNotEqual:
5093 case EOpAssign:
5094 case EOpInitialize:
5095 break;
5096 default:
5097 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5098 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005099 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005100 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005101 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005102 {
5103 error(loc, "array size mismatch", GetOperatorString(op));
5104 return false;
5105 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005106 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005107
5108 // Check ops which require integer / ivec parameters
5109 bool isBitShift = false;
5110 switch (op)
5111 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005112 case EOpBitShiftLeft:
5113 case EOpBitShiftRight:
5114 case EOpBitShiftLeftAssign:
5115 case EOpBitShiftRightAssign:
5116 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5117 // check that the basic type is an integer type.
5118 isBitShift = true;
5119 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5120 {
5121 return false;
5122 }
5123 break;
5124 case EOpBitwiseAnd:
5125 case EOpBitwiseXor:
5126 case EOpBitwiseOr:
5127 case EOpBitwiseAndAssign:
5128 case EOpBitwiseXorAssign:
5129 case EOpBitwiseOrAssign:
5130 // It is enough to check the type of only one operand, since later it
5131 // is checked that the operand types match.
5132 if (!IsInteger(left->getBasicType()))
5133 {
5134 return false;
5135 }
5136 break;
5137 default:
5138 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005139 }
5140
5141 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5142 // So the basic type should usually match.
5143 if (!isBitShift && left->getBasicType() != right->getBasicType())
5144 {
5145 return false;
5146 }
5147
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005148 // Check that:
5149 // 1. Type sizes match exactly on ops that require that.
5150 // 2. Restrictions for structs that contain arrays or samplers are respected.
5151 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005152 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005153 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005154 case EOpAssign:
5155 case EOpInitialize:
5156 case EOpEqual:
5157 case EOpNotEqual:
5158 // ESSL 1.00 sections 5.7, 5.8, 5.9
5159 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5160 {
5161 error(loc, "undefined operation for structs containing arrays",
5162 GetOperatorString(op));
5163 return false;
5164 }
5165 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5166 // we interpret the spec so that this extends to structs containing samplers,
5167 // similarly to ESSL 1.00 spec.
5168 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5169 left->getType().isStructureContainingSamplers())
5170 {
5171 error(loc, "undefined operation for structs containing samplers",
5172 GetOperatorString(op));
5173 return false;
5174 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005175
Olli Etuahoe1805592017-01-02 16:41:20 +00005176 if ((left->getNominalSize() != right->getNominalSize()) ||
5177 (left->getSecondarySize() != right->getSecondarySize()))
5178 {
5179 error(loc, "dimension mismatch", GetOperatorString(op));
5180 return false;
5181 }
5182 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005183 case EOpLessThan:
5184 case EOpGreaterThan:
5185 case EOpLessThanEqual:
5186 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005187 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005188 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005189 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005190 return false;
5191 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005192 break;
5193 case EOpAdd:
5194 case EOpSub:
5195 case EOpDiv:
5196 case EOpIMod:
5197 case EOpBitShiftLeft:
5198 case EOpBitShiftRight:
5199 case EOpBitwiseAnd:
5200 case EOpBitwiseXor:
5201 case EOpBitwiseOr:
5202 case EOpAddAssign:
5203 case EOpSubAssign:
5204 case EOpDivAssign:
5205 case EOpIModAssign:
5206 case EOpBitShiftLeftAssign:
5207 case EOpBitShiftRightAssign:
5208 case EOpBitwiseAndAssign:
5209 case EOpBitwiseXorAssign:
5210 case EOpBitwiseOrAssign:
5211 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5212 {
5213 return false;
5214 }
5215
5216 // Are the sizes compatible?
5217 if (left->getNominalSize() != right->getNominalSize() ||
5218 left->getSecondarySize() != right->getSecondarySize())
5219 {
5220 // If the nominal sizes of operands do not match:
5221 // One of them must be a scalar.
5222 if (!left->isScalar() && !right->isScalar())
5223 return false;
5224
5225 // In the case of compound assignment other than multiply-assign,
5226 // the right side needs to be a scalar. Otherwise a vector/matrix
5227 // would be assigned to a scalar. A scalar can't be shifted by a
5228 // vector either.
5229 if (!right->isScalar() &&
5230 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5231 return false;
5232 }
5233 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005234 default:
5235 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005236 }
5237
Olli Etuahod6b14282015-03-17 14:31:35 +02005238 return true;
5239}
5240
Olli Etuaho1dded802016-08-18 18:13:13 +03005241bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5242 const TType &left,
5243 const TType &right)
5244{
5245 switch (op)
5246 {
5247 case EOpMul:
5248 case EOpMulAssign:
5249 return left.getNominalSize() == right.getNominalSize() &&
5250 left.getSecondarySize() == right.getSecondarySize();
5251 case EOpVectorTimesScalar:
5252 return true;
5253 case EOpVectorTimesScalarAssign:
5254 ASSERT(!left.isMatrix() && !right.isMatrix());
5255 return left.isVector() && !right.isVector();
5256 case EOpVectorTimesMatrix:
5257 return left.getNominalSize() == right.getRows();
5258 case EOpVectorTimesMatrixAssign:
5259 ASSERT(!left.isMatrix() && right.isMatrix());
5260 return left.isVector() && left.getNominalSize() == right.getRows() &&
5261 left.getNominalSize() == right.getCols();
5262 case EOpMatrixTimesVector:
5263 return left.getCols() == right.getNominalSize();
5264 case EOpMatrixTimesScalar:
5265 return true;
5266 case EOpMatrixTimesScalarAssign:
5267 ASSERT(left.isMatrix() && !right.isMatrix());
5268 return !right.isVector();
5269 case EOpMatrixTimesMatrix:
5270 return left.getCols() == right.getRows();
5271 case EOpMatrixTimesMatrixAssign:
5272 ASSERT(left.isMatrix() && right.isMatrix());
5273 // We need to check two things:
5274 // 1. The matrix multiplication step is valid.
5275 // 2. The result will have the same number of columns as the lvalue.
5276 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5277
5278 default:
5279 UNREACHABLE();
5280 return false;
5281 }
5282}
5283
Jamie Madillb98c3a82015-07-23 14:26:04 -04005284TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5285 TIntermTyped *left,
5286 TIntermTyped *right,
5287 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005288{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005289 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005290 return nullptr;
5291
Olli Etuahofc1806e2015-03-17 13:03:11 +02005292 switch (op)
5293 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005294 case EOpEqual:
5295 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005296 case EOpLessThan:
5297 case EOpGreaterThan:
5298 case EOpLessThanEqual:
5299 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005300 break;
5301 case EOpLogicalOr:
5302 case EOpLogicalXor:
5303 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005304 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5305 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005306 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005307 {
5308 return nullptr;
5309 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005310 // Basic types matching should have been already checked.
5311 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005312 break;
5313 case EOpAdd:
5314 case EOpSub:
5315 case EOpDiv:
5316 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005317 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5318 !right->getType().getStruct());
5319 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005320 {
5321 return nullptr;
5322 }
5323 break;
5324 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005325 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5326 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005327 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005328 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005329 {
5330 return nullptr;
5331 }
5332 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005333 default:
5334 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005335 }
5336
Olli Etuaho1dded802016-08-18 18:13:13 +03005337 if (op == EOpMul)
5338 {
5339 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5340 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5341 {
5342 return nullptr;
5343 }
5344 }
5345
Olli Etuaho3fdec912016-08-18 15:08:06 +03005346 TIntermBinary *node = new TIntermBinary(op, left, right);
5347 node->setLine(loc);
5348
Olli Etuaho3fdec912016-08-18 15:08:06 +03005349 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005350 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005351}
5352
Jamie Madillb98c3a82015-07-23 14:26:04 -04005353TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5354 TIntermTyped *left,
5355 TIntermTyped *right,
5356 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005357{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005358 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005359 if (node == 0)
5360 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005361 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5362 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005363 return left;
5364 }
5365 return node;
5366}
5367
Jamie Madillb98c3a82015-07-23 14:26:04 -04005368TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5369 TIntermTyped *left,
5370 TIntermTyped *right,
5371 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005372{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005373 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005374 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005375 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005376 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5377 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005378 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005379 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005380 }
5381 return node;
5382}
5383
Olli Etuaho13389b62016-10-16 11:48:18 +01005384TIntermBinary *TParseContext::createAssign(TOperator op,
5385 TIntermTyped *left,
5386 TIntermTyped *right,
5387 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005388{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005389 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005390 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005391 if (op == EOpMulAssign)
5392 {
5393 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5394 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5395 {
5396 return nullptr;
5397 }
5398 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005399 TIntermBinary *node = new TIntermBinary(op, left, right);
5400 node->setLine(loc);
5401
Olli Etuaho3fdec912016-08-18 15:08:06 +03005402 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005403 }
5404 return nullptr;
5405}
5406
Jamie Madillb98c3a82015-07-23 14:26:04 -04005407TIntermTyped *TParseContext::addAssign(TOperator op,
5408 TIntermTyped *left,
5409 TIntermTyped *right,
5410 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005411{
Olli Etuahocce89652017-06-19 16:04:09 +03005412 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005413 TIntermTyped *node = createAssign(op, left, right, loc);
5414 if (node == nullptr)
5415 {
5416 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005417 return left;
5418 }
5419 return node;
5420}
5421
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005422TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5423 TIntermTyped *right,
5424 const TSourceLoc &loc)
5425{
Corentin Wallez0d959252016-07-12 17:26:32 -04005426 // WebGL2 section 5.26, the following results in an error:
5427 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005428 if (mShaderSpec == SH_WEBGL2_SPEC &&
5429 (left->isArray() || left->getBasicType() == EbtVoid ||
5430 left->getType().isStructureContainingArrays() || right->isArray() ||
5431 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005432 {
5433 error(loc,
5434 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5435 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005436 }
5437
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005438 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5439 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5440 commaNode->getTypePointer()->setQualifier(resultQualifier);
5441 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005442}
5443
Olli Etuaho49300862015-02-20 14:54:49 +02005444TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5445{
5446 switch (op)
5447 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005448 case EOpContinue:
5449 if (mLoopNestingLevel <= 0)
5450 {
5451 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005452 }
5453 break;
5454 case EOpBreak:
5455 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5456 {
5457 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005458 }
5459 break;
5460 case EOpReturn:
5461 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5462 {
5463 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005464 }
5465 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005466 case EOpKill:
5467 if (mShaderType != GL_FRAGMENT_SHADER)
5468 {
5469 error(loc, "discard supported in fragment shaders only", "discard");
5470 }
5471 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005472 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005473 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005474 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005475 }
Olli Etuahocce89652017-06-19 16:04:09 +03005476 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005477}
5478
Jamie Madillb98c3a82015-07-23 14:26:04 -04005479TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005480 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005481 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005482{
Olli Etuahocce89652017-06-19 16:04:09 +03005483 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005484 {
Olli Etuahocce89652017-06-19 16:04:09 +03005485 ASSERT(op == EOpReturn);
5486 mFunctionReturnsValue = true;
5487 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5488 {
5489 error(loc, "void function cannot return a value", "return");
5490 }
5491 else if (*mCurrentFunctionType != expression->getType())
5492 {
5493 error(loc, "function return is not matching type:", "return");
5494 }
Olli Etuaho49300862015-02-20 14:54:49 +02005495 }
Olli Etuahocce89652017-06-19 16:04:09 +03005496 TIntermBranch *node = new TIntermBranch(op, expression);
5497 node->setLine(loc);
5498 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005499}
5500
Martin Radev84aa2dc2017-09-11 15:51:02 +03005501void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5502{
5503 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5504 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5505 bool isTextureGather = (name == "textureGather");
5506 bool isTextureGatherOffset = (name == "textureGatherOffset");
5507 if (isTextureGather || isTextureGatherOffset)
5508 {
5509 TIntermNode *componentNode = nullptr;
5510 TIntermSequence *arguments = functionCall->getSequence();
5511 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5512 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5513 ASSERT(sampler != nullptr);
5514 switch (sampler->getBasicType())
5515 {
5516 case EbtSampler2D:
5517 case EbtISampler2D:
5518 case EbtUSampler2D:
5519 case EbtSampler2DArray:
5520 case EbtISampler2DArray:
5521 case EbtUSampler2DArray:
5522 if ((isTextureGather && arguments->size() == 3u) ||
5523 (isTextureGatherOffset && arguments->size() == 4u))
5524 {
5525 componentNode = arguments->back();
5526 }
5527 break;
5528 case EbtSamplerCube:
5529 case EbtISamplerCube:
5530 case EbtUSamplerCube:
5531 ASSERT(!isTextureGatherOffset);
5532 if (arguments->size() == 3u)
5533 {
5534 componentNode = arguments->back();
5535 }
5536 break;
5537 case EbtSampler2DShadow:
5538 case EbtSampler2DArrayShadow:
5539 case EbtSamplerCubeShadow:
5540 break;
5541 default:
5542 UNREACHABLE();
5543 break;
5544 }
5545 if (componentNode)
5546 {
5547 const TIntermConstantUnion *componentConstantUnion =
5548 componentNode->getAsConstantUnion();
5549 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5550 {
5551 error(functionCall->getLine(), "Texture component must be a constant expression",
5552 name.c_str());
5553 }
5554 else
5555 {
5556 int component = componentConstantUnion->getIConst(0);
5557 if (component < 0 || component > 3)
5558 {
5559 error(functionCall->getLine(), "Component must be in the range [0;3]",
5560 name.c_str());
5561 }
5562 }
5563 }
5564 }
5565}
5566
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005567void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5568{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005569 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005570 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005571 TIntermNode *offset = nullptr;
5572 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005573 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005574 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5575 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5576 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005577 {
5578 offset = arguments->back();
5579 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005580 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005581 {
5582 // A bias parameter might follow the offset parameter.
5583 ASSERT(arguments->size() >= 3);
5584 offset = (*arguments)[2];
5585 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005586 else if (name == "textureGatherOffset")
5587 {
5588 ASSERT(arguments->size() >= 3u);
5589 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5590 ASSERT(sampler != nullptr);
5591 switch (sampler->getBasicType())
5592 {
5593 case EbtSampler2D:
5594 case EbtISampler2D:
5595 case EbtUSampler2D:
5596 case EbtSampler2DArray:
5597 case EbtISampler2DArray:
5598 case EbtUSampler2DArray:
5599 offset = (*arguments)[2];
5600 break;
5601 case EbtSampler2DShadow:
5602 case EbtSampler2DArrayShadow:
5603 offset = (*arguments)[3];
5604 break;
5605 default:
5606 UNREACHABLE();
5607 break;
5608 }
5609 useTextureGatherOffsetConstraints = true;
5610 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005611 if (offset != nullptr)
5612 {
5613 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5614 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5615 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005616 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005617 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005618 }
5619 else
5620 {
5621 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5622 size_t size = offsetConstantUnion->getType().getObjectSize();
5623 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005624 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5625 : mMinProgramTexelOffset;
5626 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5627 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005628 for (size_t i = 0u; i < size; ++i)
5629 {
5630 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005631 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005632 {
5633 std::stringstream tokenStream;
5634 tokenStream << offsetValue;
5635 std::string token = tokenStream.str();
5636 error(offset->getLine(), "Texture offset value out of valid range",
5637 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005638 }
5639 }
5640 }
5641 }
5642}
5643
Jiajia Qina3106c52017-11-03 09:39:39 +08005644void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5645{
5646 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5647 if (IsAtomicBuiltin(name))
5648 {
5649 TIntermSequence *arguments = functionCall->getSequence();
5650 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5651
5652 if (IsBufferOrSharedVariable(memNode))
5653 {
5654 return;
5655 }
5656
5657 while (memNode->getAsBinaryNode())
5658 {
5659 memNode = memNode->getAsBinaryNode()->getLeft();
5660 if (IsBufferOrSharedVariable(memNode))
5661 {
5662 return;
5663 }
5664 }
5665
5666 error(memNode->getLine(),
5667 "The value passed to the mem argument of an atomic memory function does not "
5668 "correspond to a buffer or shared variable.",
5669 functionCall->getFunctionSymbolInfo()->getName().c_str());
5670 }
5671}
5672
Martin Radev2cc85b32016-08-05 16:22:53 +03005673// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5674void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5675{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005676 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005677 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5678
5679 if (name.compare(0, 5, "image") == 0)
5680 {
5681 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005682 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005683
Olli Etuaho485eefd2017-02-14 17:40:06 +00005684 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005685
5686 if (name.compare(5, 5, "Store") == 0)
5687 {
5688 if (memoryQualifier.readonly)
5689 {
5690 error(imageNode->getLine(),
5691 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005692 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005693 }
5694 }
5695 else if (name.compare(5, 4, "Load") == 0)
5696 {
5697 if (memoryQualifier.writeonly)
5698 {
5699 error(imageNode->getLine(),
5700 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005701 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005702 }
5703 }
5704 }
5705}
5706
5707// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5708void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5709 const TFunction *functionDefinition,
5710 const TIntermAggregate *functionCall)
5711{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005712 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005713
5714 const TIntermSequence &arguments = *functionCall->getSequence();
5715
5716 ASSERT(functionDefinition->getParamCount() == arguments.size());
5717
5718 for (size_t i = 0; i < arguments.size(); ++i)
5719 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005720 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5721 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005722 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5723 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5724
5725 if (IsImage(functionArgumentType.getBasicType()))
5726 {
5727 const TMemoryQualifier &functionArgumentMemoryQualifier =
5728 functionArgumentType.getMemoryQualifier();
5729 const TMemoryQualifier &functionParameterMemoryQualifier =
5730 functionParameterType.getMemoryQualifier();
5731 if (functionArgumentMemoryQualifier.readonly &&
5732 !functionParameterMemoryQualifier.readonly)
5733 {
5734 error(functionCall->getLine(),
5735 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005736 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005737 }
5738
5739 if (functionArgumentMemoryQualifier.writeonly &&
5740 !functionParameterMemoryQualifier.writeonly)
5741 {
5742 error(functionCall->getLine(),
5743 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005744 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005745 }
Martin Radev049edfa2016-11-11 14:35:37 +02005746
5747 if (functionArgumentMemoryQualifier.coherent &&
5748 !functionParameterMemoryQualifier.coherent)
5749 {
5750 error(functionCall->getLine(),
5751 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005752 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005753 }
5754
5755 if (functionArgumentMemoryQualifier.volatileQualifier &&
5756 !functionParameterMemoryQualifier.volatileQualifier)
5757 {
5758 error(functionCall->getLine(),
5759 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005760 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005761 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005762 }
5763 }
5764}
5765
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005766TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005767{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005768 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005769}
5770
5771TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005772 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005773 TIntermNode *thisNode,
5774 const TSourceLoc &loc)
5775{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005776 if (thisNode != nullptr)
5777 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005778 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005779 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005780
5781 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005782 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005783 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005784 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005785 }
5786 else
5787 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005788 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005789 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5790 }
5791}
5792
5793TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5794 TIntermSequence *arguments,
5795 TIntermNode *thisNode,
5796 const TSourceLoc &loc)
5797{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005798 TIntermTyped *typedThis = thisNode->getAsTyped();
5799 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5800 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5801 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5802 // So accessing fnCall->getName() below is safe.
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005803 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005804 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005805 error(loc, "invalid method", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005806 }
5807 else if (!arguments->empty())
5808 {
5809 error(loc, "method takes no parameters", "length");
5810 }
5811 else if (typedThis == nullptr || !typedThis->isArray())
5812 {
5813 error(loc, "length can only be called on arrays", "length");
5814 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005815 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5816 mGeometryShaderInputPrimitiveType == EptUndefined)
5817 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005818 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005819 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5820 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005821 else
5822 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005823 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5824 node->setLine(loc);
5825 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005826 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005827 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005828}
5829
5830TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5831 TIntermSequence *arguments,
5832 const TSourceLoc &loc)
5833{
5834 // First find by unmangled name to check whether the function name has been
5835 // hidden by a variable name or struct typename.
5836 // If a function is found, check for one with a matching argument list.
5837 bool builtIn;
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005838 const TSymbol *symbol = symbolTable.find(fnCall->name(), mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005839 if (symbol != nullptr && !symbol->isFunction())
5840 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005841 error(loc, "function name expected", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005842 }
5843 else
5844 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005845 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->name(), *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005846 mShaderVersion, &builtIn);
5847 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005848 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005849 error(loc, "no matching overloaded function found", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005850 }
5851 else
5852 {
5853 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005854 //
5855 // A declared function.
5856 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005857 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005858 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005859 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005860 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005861 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005862 if (builtIn && op != EOpNull)
5863 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005864 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005865 if (fnCandidate->getParamCount() == 1)
5866 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005867 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005868 TIntermNode *unaryParamNode = arguments->front();
5869 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005870 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005871 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005872 }
5873 else
5874 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005875 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005876 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005877 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005878
5879 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005880 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305881
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005882 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305883 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005884 // See if we can constant fold a built-in. Note that this may be possible
5885 // even if it is not const-qualified.
5886 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305887 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005888 else
5889 {
5890 return callNode;
5891 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005892 }
5893 }
5894 else
5895 {
5896 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005897 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005898
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005899 // If builtIn == false, the function is user defined - could be an overloaded
5900 // built-in as well.
5901 // if builtIn == true, it's a builtIn function with no op associated with it.
5902 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005903 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005904 {
Olli Etuahofe486322017-03-21 09:30:54 +00005905 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005906 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005907 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005908 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005909 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005910 }
5911 else
5912 {
Olli Etuahofe486322017-03-21 09:30:54 +00005913 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005914 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005915 }
5916
Jiajia Qinbc585152017-06-23 15:42:17 +08005917 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005918
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005919 callNode->setLine(loc);
5920
5921 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005922 }
5923 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005924 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005925
5926 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005927 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005928}
5929
Jamie Madillb98c3a82015-07-23 14:26:04 -04005930TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005931 TIntermTyped *trueExpression,
5932 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005933 const TSourceLoc &loc)
5934{
Olli Etuaho56229f12017-07-10 14:16:33 +03005935 if (!checkIsScalarBool(loc, cond))
5936 {
5937 return falseExpression;
5938 }
Olli Etuaho52901742015-04-15 13:42:45 +03005939
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005940 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005941 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005942 std::stringstream reasonStream;
5943 reasonStream << "mismatching ternary operator operand types '"
5944 << trueExpression->getCompleteString() << " and '"
5945 << falseExpression->getCompleteString() << "'";
5946 std::string reason = reasonStream.str();
5947 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005948 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005949 }
Olli Etuahode318b22016-10-25 16:18:25 +01005950 if (IsOpaqueType(trueExpression->getBasicType()))
5951 {
5952 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005953 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005954 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5955 // Note that structs containing opaque types don't need to be checked as structs are
5956 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005957 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005958 return falseExpression;
5959 }
5960
Jiajia Qinbc585152017-06-23 15:42:17 +08005961 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5962 falseExpression->getMemoryQualifier().writeonly)
5963 {
5964 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5965 return falseExpression;
5966 }
5967
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005968 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005969 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005970 // ESSL 3.00.6 section 5.7:
5971 // Ternary operator support is optional for arrays. No certainty that it works across all
5972 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5973 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005974 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005975 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005976 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005977 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005978 }
Olli Etuaho94050052017-05-08 14:17:44 +03005979 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5980 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005981 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005982 return falseExpression;
5983 }
5984
Corentin Wallez0d959252016-07-12 17:26:32 -04005985 // WebGL2 section 5.26, the following results in an error:
5986 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005987 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005988 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005989 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005990 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005991 }
5992
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005993 // Note that the node resulting from here can be a constant union without being qualified as
5994 // constant.
5995 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5996 node->setLine(loc);
5997
5998 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005999}
Olli Etuaho49300862015-02-20 14:54:49 +02006000
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00006001//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006002// Parse an array of strings using yyparse.
6003//
6004// Returns 0 for success.
6005//
Jamie Madillb98c3a82015-07-23 14:26:04 -04006006int PaParseStrings(size_t count,
6007 const char *const string[],
6008 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05306009 TParseContext *context)
6010{
Yunchao He4f285442017-04-21 12:15:49 +08006011 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006012 return 1;
6013
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006014 if (glslang_initialize(context))
6015 return 1;
6016
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006017 int error = glslang_scan(count, string, length, context);
6018 if (!error)
6019 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006020
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006021 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006022
alokp@chromium.org6b495712012-06-29 00:06:58 +00006023 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006024}
Jamie Madill45bcc782016-11-07 13:58:48 -05006025
6026} // namespace sh