blob: 523228a19fa096394c6e7cdeb260fe86c41757dc [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),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000221 mNumViews(-1),
222 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000223 mMaxImageUnits(resources.MaxImageUnits),
224 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000225 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800226 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800227 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800228 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800229 mDeclaringFunction(false),
230 mGeometryShaderInputPrimitiveType(EptUndefined),
231 mGeometryShaderOutputPrimitiveType(EptUndefined),
232 mGeometryShaderInvocations(0),
233 mGeometryShaderMaxVertices(-1),
234 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800235 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800236 mGeometryShaderInputArraySize(0u)
Jamie Madillacb4b812016-11-07 13:50:29 -0500237{
238 mComputeShaderLocalSize.fill(-1);
239}
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();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500740 if (argType.isArray())
741 {
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 Etuaho8a176262016-08-16 14:23:01 +03001031 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -04001032 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001033 error(line, "cannot declare arrays of arrays",
1034 TType(elementType).getCompleteString().c_str());
1035 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001036 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001037 return true;
1038}
1039
1040// Check if this qualified element type can be formed into an array. This is only called when array
1041// brackets are associated with an identifier in a declaration, like this:
1042// float a[2];
1043// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1044// are associated with the type, like this:
1045// float[2] a;
1046bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1047 const TPublicType &elementType)
1048{
1049 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1050 {
1051 return false;
1052 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001053 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1054 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1055 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001056 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001057 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001058 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001059 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001060 TType(elementType).getCompleteString().c_str());
1061 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001062 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001063 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064}
1065
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001066// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001067void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1068 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001069 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070{
Olli Etuaho3739d232015-04-08 12:23:44 +03001071 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001072 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001073 {
1074 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001075 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001076
1077 // Generate informative error messages for ESSL1.
1078 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001079 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001080 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301081 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001082 "structures containing arrays may not be declared constant since they cannot be "
1083 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301084 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001085 }
1086 else
1087 {
1088 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1089 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001090 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001091 // This will make the type sized if it isn't sized yet.
1092 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1093 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094}
1095
Olli Etuaho2935c582015-04-08 14:32:06 +03001096// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097// and update the symbol table.
1098//
Olli Etuaho2935c582015-04-08 14:32:06 +03001099// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001101bool TParseContext::declareVariable(const TSourceLoc &line,
1102 const TString &identifier,
1103 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001104 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105{
Olli Etuaho2935c582015-04-08 14:32:06 +03001106 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107
Olli Etuaho43364892017-02-13 16:00:12 +00001108 checkBindingIsValid(line, type);
1109
Olli Etuaho856c4972016-08-08 11:38:39 +03001110 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111
Olli Etuaho2935c582015-04-08 14:32:06 +03001112 // gl_LastFragData may be redeclared with a new precision qualifier
1113 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1114 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001115 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1116 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001117 if (type.isArrayOfArrays())
1118 {
1119 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1120 identifier.c_str());
1121 return false;
1122 }
1123 else if (static_cast<int>(type.getOutermostArraySize()) ==
1124 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001125 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001126 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001127 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001128 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001129 }
1130 }
1131 else
1132 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001133 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1134 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001135 return false;
1136 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001137 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001138
Olli Etuaho8a176262016-08-16 14:23:01 +03001139 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001140 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141
Olli Etuaho0f684632017-07-13 12:42:15 +03001142 (*variable) = symbolTable.declareVariable(&identifier, type);
1143 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001144 {
1145 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001146 return false;
1147 }
1148
Olli Etuaho8a176262016-08-16 14:23:01 +03001149 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001150 return false;
1151
1152 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001153}
1154
Martin Radev70866b82016-07-22 15:27:42 +03001155void TParseContext::checkIsParameterQualifierValid(
1156 const TSourceLoc &line,
1157 const TTypeQualifierBuilder &typeQualifierBuilder,
1158 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301159{
Olli Etuahocce89652017-06-19 16:04:09 +03001160 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001161 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001162
1163 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301164 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001165 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1166 }
1167
1168 if (!IsImage(type->getBasicType()))
1169 {
Olli Etuaho43364892017-02-13 16:00:12 +00001170 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001171 }
1172 else
1173 {
1174 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001175 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001176
Martin Radev70866b82016-07-22 15:27:42 +03001177 type->setQualifier(typeQualifier.qualifier);
1178
1179 if (typeQualifier.precision != EbpUndefined)
1180 {
1181 type->setPrecision(typeQualifier.precision);
1182 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183}
1184
Olli Etuaho703671e2017-11-08 17:47:18 +02001185template <size_t size>
1186bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1187 const std::array<TExtension, size> &extensions)
1188{
1189 ASSERT(!extensions.empty());
1190 const TExtensionBehavior &extBehavior = extensionBehavior();
1191
1192 bool canUseWithWarning = false;
1193 bool canUseWithoutWarning = false;
1194
1195 const char *errorMsgString = "";
1196 TExtension errorMsgExtension = TExtension::UNDEFINED;
1197
1198 for (TExtension extension : extensions)
1199 {
1200 auto extIter = extBehavior.find(extension);
1201 if (canUseWithWarning)
1202 {
1203 // We already have an extension that we can use, but with a warning.
1204 // See if we can use the alternative extension without a warning.
1205 if (extIter == extBehavior.end())
1206 {
1207 continue;
1208 }
1209 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1210 {
1211 canUseWithoutWarning = true;
1212 break;
1213 }
1214 continue;
1215 }
1216 if (extIter == extBehavior.end())
1217 {
1218 errorMsgString = "extension is not supported";
1219 errorMsgExtension = extension;
1220 }
1221 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1222 {
1223 errorMsgString = "extension is disabled";
1224 errorMsgExtension = extension;
1225 }
1226 else if (extIter->second == EBhWarn)
1227 {
1228 errorMsgExtension = extension;
1229 canUseWithWarning = true;
1230 }
1231 else
1232 {
1233 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1234 canUseWithoutWarning = true;
1235 break;
1236 }
1237 }
1238
1239 if (canUseWithoutWarning)
1240 {
1241 return true;
1242 }
1243 if (canUseWithWarning)
1244 {
1245 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1246 return true;
1247 }
1248 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1249 return false;
1250}
1251
1252template bool TParseContext::checkCanUseOneOfExtensions(
1253 const TSourceLoc &line,
1254 const std::array<TExtension, 1> &extensions);
1255template bool TParseContext::checkCanUseOneOfExtensions(
1256 const TSourceLoc &line,
1257 const std::array<TExtension, 2> &extensions);
1258template bool TParseContext::checkCanUseOneOfExtensions(
1259 const TSourceLoc &line,
1260 const std::array<TExtension, 3> &extensions);
1261
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001262bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001263{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001264 ASSERT(extension != TExtension::UNDEFINED);
Jiawei Shao0e883132017-10-26 09:53:50 +08001265 ASSERT(extension != TExtension::EXT_geometry_shader);
Olli Etuaho703671e2017-11-08 17:47:18 +02001266 if (extension == TExtension::OES_geometry_shader)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301267 {
Olli Etuaho703671e2017-11-08 17:47:18 +02001268 // OES_geometry_shader and EXT_geometry_shader are always interchangeable.
1269 constexpr std::array<TExtension, 2u> extensions{
1270 {TExtension::EXT_geometry_shader, TExtension::OES_geometry_shader}};
1271 return checkCanUseOneOfExtensions(line, extensions);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001272 }
Corentin Wallez1d33c212017-11-13 10:21:39 -08001273 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274}
1275
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001276// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1277// compile-time or link-time errors are the same whether or not the declaration is empty".
1278// This function implements all the checks that are done on qualifiers regardless of if the
1279// declaration is empty.
1280void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1281 const sh::TLayoutQualifier &layoutQualifier,
1282 const TSourceLoc &location)
1283{
1284 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1285 {
1286 error(location, "Shared memory declarations cannot have layout specified", "layout");
1287 }
1288
1289 if (layoutQualifier.matrixPacking != EmpUnspecified)
1290 {
1291 error(location, "layout qualifier only valid for interface blocks",
1292 getMatrixPackingString(layoutQualifier.matrixPacking));
1293 return;
1294 }
1295
1296 if (layoutQualifier.blockStorage != EbsUnspecified)
1297 {
1298 error(location, "layout qualifier only valid for interface blocks",
1299 getBlockStorageString(layoutQualifier.blockStorage));
1300 return;
1301 }
1302
1303 if (qualifier == EvqFragmentOut)
1304 {
1305 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1306 {
1307 error(location, "invalid layout qualifier combination", "yuv");
1308 return;
1309 }
1310 }
1311 else
1312 {
1313 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1314 }
1315
Olli Etuaho95468d12017-05-04 11:14:34 +03001316 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1317 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001318 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1319 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001320 {
1321 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1322 }
1323
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001324 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001325 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001326 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001327 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001328 // We're not checking whether the uniform location is in range here since that depends on
1329 // the type of the variable.
1330 // The type can only be fully determined for non-empty declarations.
1331 }
1332 if (!canHaveLocation)
1333 {
1334 checkLocationIsNotSpecified(location, layoutQualifier);
1335 }
1336}
1337
jchen104cdac9e2017-05-08 11:01:20 +08001338void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1339 const TSourceLoc &location)
1340{
1341 if (publicType.precision != EbpHigh)
1342 {
1343 error(location, "Can only be highp", "atomic counter");
1344 }
1345 // dEQP enforces compile error if location is specified. See uniform_location.test.
1346 if (publicType.layoutQualifier.location != -1)
1347 {
1348 error(location, "location must not be set for atomic_uint", "layout");
1349 }
1350 if (publicType.layoutQualifier.binding == -1)
1351 {
1352 error(location, "no binding specified", "atomic counter");
1353 }
1354}
1355
Olli Etuaho55bde912017-10-25 13:41:13 +03001356void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001357{
Olli Etuaho55bde912017-10-25 13:41:13 +03001358 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001359 {
1360 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1361 // error. It is assumed that this applies to empty declarations as well.
1362 error(location, "empty array declaration needs to specify a size", "");
1363 }
Martin Radevb8b01222016-11-20 23:25:53 +02001364}
1365
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001366// These checks are done for all declarations that are non-empty. They're done for non-empty
1367// declarations starting a declarator list, and declarators that follow an empty declaration.
1368void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1369 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001370{
Olli Etuahofa33d582015-04-09 14:33:12 +03001371 switch (publicType.qualifier)
1372 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001373 case EvqVaryingIn:
1374 case EvqVaryingOut:
1375 case EvqAttribute:
1376 case EvqVertexIn:
1377 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001378 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001379 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001380 {
1381 error(identifierLocation, "cannot be used with a structure",
1382 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001383 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001384 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001385 break;
1386 case EvqBuffer:
1387 if (publicType.getBasicType() != EbtInterfaceBlock)
1388 {
1389 error(identifierLocation,
1390 "cannot declare buffer variables at global scope(outside a block)",
1391 getQualifierString(publicType.qualifier));
1392 return;
1393 }
1394 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001395 default:
1396 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001397 }
jchen10cc2a10e2017-05-03 14:05:12 +08001398 std::string reason(getBasicString(publicType.getBasicType()));
1399 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001400 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001401 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001402 {
1403 return;
1404 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001405
Andrei Volykhina5527072017-03-22 16:46:30 +03001406 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1407 publicType.qualifier != EvqConst) &&
1408 publicType.getBasicType() == EbtYuvCscStandardEXT)
1409 {
1410 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1411 getQualifierString(publicType.qualifier));
1412 return;
1413 }
1414
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001415 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1416 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001417 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1418 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001419 TType type(publicType);
1420 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001421 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001422 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1423 publicType.layoutQualifier);
1424 }
1425 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001426
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001427 // check for layout qualifier issues
1428 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001429
Martin Radev2cc85b32016-08-05 16:22:53 +03001430 if (IsImage(publicType.getBasicType()))
1431 {
1432
1433 switch (layoutQualifier.imageInternalFormat)
1434 {
1435 case EiifRGBA32F:
1436 case EiifRGBA16F:
1437 case EiifR32F:
1438 case EiifRGBA8:
1439 case EiifRGBA8_SNORM:
1440 if (!IsFloatImage(publicType.getBasicType()))
1441 {
1442 error(identifierLocation,
1443 "internal image format requires a floating image type",
1444 getBasicString(publicType.getBasicType()));
1445 return;
1446 }
1447 break;
1448 case EiifRGBA32I:
1449 case EiifRGBA16I:
1450 case EiifRGBA8I:
1451 case EiifR32I:
1452 if (!IsIntegerImage(publicType.getBasicType()))
1453 {
1454 error(identifierLocation,
1455 "internal image format requires an integer image type",
1456 getBasicString(publicType.getBasicType()));
1457 return;
1458 }
1459 break;
1460 case EiifRGBA32UI:
1461 case EiifRGBA16UI:
1462 case EiifRGBA8UI:
1463 case EiifR32UI:
1464 if (!IsUnsignedImage(publicType.getBasicType()))
1465 {
1466 error(identifierLocation,
1467 "internal image format requires an unsigned image type",
1468 getBasicString(publicType.getBasicType()));
1469 return;
1470 }
1471 break;
1472 case EiifUnspecified:
1473 error(identifierLocation, "layout qualifier", "No image internal format specified");
1474 return;
1475 default:
1476 error(identifierLocation, "layout qualifier", "unrecognized token");
1477 return;
1478 }
1479
1480 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1481 switch (layoutQualifier.imageInternalFormat)
1482 {
1483 case EiifR32F:
1484 case EiifR32I:
1485 case EiifR32UI:
1486 break;
1487 default:
1488 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1489 {
1490 error(identifierLocation, "layout qualifier",
1491 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1492 "image variables must be qualified readonly and/or writeonly");
1493 return;
1494 }
1495 break;
1496 }
1497 }
1498 else
1499 {
Olli Etuaho43364892017-02-13 16:00:12 +00001500 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001501 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1502 }
jchen104cdac9e2017-05-08 11:01:20 +08001503
1504 if (IsAtomicCounter(publicType.getBasicType()))
1505 {
1506 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1507 }
1508 else
1509 {
1510 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1511 }
Olli Etuaho43364892017-02-13 16:00:12 +00001512}
Martin Radev2cc85b32016-08-05 16:22:53 +03001513
Olli Etuaho43364892017-02-13 16:00:12 +00001514void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1515{
1516 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001517 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1518 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1519 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1520 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1521 // when it comes to which shaders are accepted by the compiler.
1522 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001523 if (IsImage(type.getBasicType()))
1524 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001525 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1526 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001527 }
1528 else if (IsSampler(type.getBasicType()))
1529 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001530 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1531 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001532 }
jchen104cdac9e2017-05-08 11:01:20 +08001533 else if (IsAtomicCounter(type.getBasicType()))
1534 {
1535 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1536 }
Olli Etuaho43364892017-02-13 16:00:12 +00001537 else
1538 {
1539 ASSERT(!IsOpaqueType(type.getBasicType()));
1540 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001541 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001542}
1543
Olli Etuaho856c4972016-08-08 11:38:39 +03001544void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1545 const TString &layoutQualifierName,
1546 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001547{
1548
1549 if (mShaderVersion < versionRequired)
1550 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001551 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001552 }
1553}
1554
Olli Etuaho856c4972016-08-08 11:38:39 +03001555bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1556 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001557{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001558 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001559 for (size_t i = 0u; i < localSize.size(); ++i)
1560 {
1561 if (localSize[i] != -1)
1562 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001563 error(location,
1564 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1565 "global layout declaration",
1566 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001567 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001568 }
1569 }
1570
Olli Etuaho8a176262016-08-16 14:23:01 +03001571 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001572}
1573
Olli Etuaho43364892017-02-13 16:00:12 +00001574void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001575 TLayoutImageInternalFormat internalFormat)
1576{
1577 if (internalFormat != EiifUnspecified)
1578 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001579 error(location, "invalid layout qualifier: only valid when used with images",
1580 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001581 }
Olli Etuaho43364892017-02-13 16:00:12 +00001582}
1583
1584void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1585{
1586 if (binding != -1)
1587 {
1588 error(location,
1589 "invalid layout qualifier: only valid when used with opaque types or blocks",
1590 "binding");
1591 }
1592}
1593
jchen104cdac9e2017-05-08 11:01:20 +08001594void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1595{
1596 if (offset != -1)
1597 {
1598 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1599 "offset");
1600 }
1601}
1602
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001603void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1604 int binding,
1605 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001606{
1607 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001608 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001609 {
1610 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1611 }
1612}
1613
1614void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1615 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001616 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001617{
1618 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001619 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001620 {
1621 error(location, "sampler binding greater than maximum texture units", "binding");
1622 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001623}
1624
Jiajia Qinbc585152017-06-23 15:42:17 +08001625void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1626 const TQualifier &qualifier,
1627 int binding,
1628 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001629{
1630 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001631 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001632 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001633 if (binding + size > mMaxUniformBufferBindings)
1634 {
1635 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1636 "binding");
1637 }
1638 }
1639 else if (qualifier == EvqBuffer)
1640 {
1641 if (binding + size > mMaxShaderStorageBufferBindings)
1642 {
1643 error(location,
1644 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1645 "binding");
1646 }
jchen10af713a22017-04-19 09:10:56 +08001647 }
1648}
jchen104cdac9e2017-05-08 11:01:20 +08001649void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1650{
1651 if (binding >= mMaxAtomicCounterBindings)
1652 {
1653 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1654 "binding");
1655 }
1656}
jchen10af713a22017-04-19 09:10:56 +08001657
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001658void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1659 int objectLocationCount,
1660 const TLayoutQualifier &layoutQualifier)
1661{
1662 int loc = layoutQualifier.location;
1663 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1664 {
1665 error(location, "Uniform location out of range", "location");
1666 }
1667}
1668
Andrei Volykhina5527072017-03-22 16:46:30 +03001669void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1670{
1671 if (yuv != false)
1672 {
1673 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1674 }
1675}
1676
Jiajia Qinbc585152017-06-23 15:42:17 +08001677void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1678 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001679{
1680 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1681 {
1682 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001683 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1684 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1685 qual == EvqInOut || qual == EvqConstReadOnly))
1686 {
1687 if (argument->getMemoryQualifier().writeonly)
1688 {
1689 error(argument->getLine(),
1690 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1691 fnCall->getFunctionSymbolInfo()->getName().c_str());
1692 return;
1693 }
1694 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001695 if (qual == EvqOut || qual == EvqInOut)
1696 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001697 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001698 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001699 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001700 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001701 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001702 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001703 }
1704 }
1705 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001706}
1707
Martin Radev70866b82016-07-22 15:27:42 +03001708void TParseContext::checkInvariantVariableQualifier(bool invariant,
1709 const TQualifier qualifier,
1710 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001711{
Martin Radev70866b82016-07-22 15:27:42 +03001712 if (!invariant)
1713 return;
1714
1715 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001716 {
Martin Radev70866b82016-07-22 15:27:42 +03001717 // input variables in the fragment shader can be also qualified as invariant
1718 if (!sh::CanBeInvariantESSL1(qualifier))
1719 {
1720 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1721 }
1722 }
1723 else
1724 {
1725 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1726 {
1727 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1728 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001729 }
1730}
1731
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001732bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001733{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001734 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001735}
1736
Jamie Madillb98c3a82015-07-23 14:26:04 -04001737void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1738 const char *extName,
1739 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001740{
1741 pp::SourceLocation srcLoc;
1742 srcLoc.file = loc.first_file;
1743 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001744 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001745}
1746
Jamie Madillb98c3a82015-07-23 14:26:04 -04001747void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1748 const char *name,
1749 const char *value,
1750 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001751{
1752 pp::SourceLocation srcLoc;
1753 srcLoc.file = loc.first_file;
1754 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001755 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001756}
1757
Martin Radev4c4c8e72016-08-04 12:25:34 +03001758sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001759{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001760 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001761 for (size_t i = 0u; i < result.size(); ++i)
1762 {
1763 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1764 {
1765 result[i] = 1;
1766 }
1767 else
1768 {
1769 result[i] = mComputeShaderLocalSize[i];
1770 }
1771 }
1772 return result;
1773}
1774
Olli Etuaho56229f12017-07-10 14:16:33 +03001775TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1776 const TSourceLoc &line)
1777{
1778 TIntermConstantUnion *node = new TIntermConstantUnion(
1779 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1780 node->setLine(line);
1781 return node;
1782}
1783
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784/////////////////////////////////////////////////////////////////////////////////
1785//
1786// Non-Errors.
1787//
1788/////////////////////////////////////////////////////////////////////////////////
1789
Jamie Madill5c097022014-08-20 16:38:32 -04001790const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1791 const TString *name,
1792 const TSymbol *symbol)
1793{
Jamie Madill5c097022014-08-20 16:38:32 -04001794 if (!symbol)
1795 {
1796 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001797 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001798 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001799
1800 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001801 {
1802 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001803 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001804 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001805
1806 const TVariable *variable = static_cast<const TVariable *>(symbol);
1807
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001808 if (variable->getExtension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001809 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001810 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001811 }
1812
Olli Etuaho0f684632017-07-13 12:42:15 +03001813 // Reject shaders using both gl_FragData and gl_FragColor
1814 TQualifier qualifier = variable->getType().getQualifier();
1815 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001816 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001817 mUsesFragData = true;
1818 }
1819 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1820 {
1821 mUsesFragColor = true;
1822 }
1823 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1824 {
1825 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001826 }
1827
Olli Etuaho0f684632017-07-13 12:42:15 +03001828 // This validation is not quite correct - it's only an error to write to
1829 // both FragData and FragColor. For simplicity, and because users shouldn't
1830 // be rewarded for reading from undefined varaibles, return an error
1831 // if they are both referenced, rather than assigned.
1832 if (mUsesFragData && mUsesFragColor)
1833 {
1834 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1835 if (mUsesSecondaryOutputs)
1836 {
1837 errorMessage =
1838 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1839 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1840 }
1841 error(location, errorMessage, name->c_str());
1842 }
1843
1844 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1845 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1846 qualifier == EvqWorkGroupSize)
1847 {
1848 error(location,
1849 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1850 "gl_WorkGroupSize");
1851 }
Jamie Madill5c097022014-08-20 16:38:32 -04001852 return variable;
1853}
1854
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001855TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1856 const TString *name,
1857 const TSymbol *symbol)
1858{
1859 const TVariable *variable = getNamedVariable(location, name, symbol);
1860
Olli Etuaho0f684632017-07-13 12:42:15 +03001861 if (!variable)
1862 {
1863 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1864 node->setLine(location);
1865 return node;
1866 }
1867
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001868 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001869 TIntermTyped *node = nullptr;
1870
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001871 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001872 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001873 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001874 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001875 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001876 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001877 {
1878 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1879 // needs to be added to the AST as a constant and not as a symbol.
1880 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1881 TConstantUnion *constArray = new TConstantUnion[3];
1882 for (size_t i = 0; i < 3; ++i)
1883 {
1884 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1885 }
1886
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001887 ASSERT(variableType.getBasicType() == EbtUInt);
1888 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001889
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001890 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001891 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001892 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001893 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001894 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1895 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001896 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001897 ASSERT(mGeometryShaderInputArraySize > 0u);
1898
1899 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho55bde912017-10-25 13:41:13 +03001900 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001901 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001902 else
1903 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001904 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001905 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001906 ASSERT(node != nullptr);
1907 node->setLine(location);
1908 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001909}
1910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911// Initializers show up in several places in the grammar. Have one set of
1912// code to handle them here.
1913//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001914// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001915bool TParseContext::executeInitializer(const TSourceLoc &line,
1916 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001917 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001918 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001919 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920{
Olli Etuaho13389b62016-10-16 11:48:18 +01001921 ASSERT(initNode != nullptr);
1922 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923
Olli Etuaho2935c582015-04-08 14:32:06 +03001924 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001925 if (type.isUnsizedArray())
1926 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001927 // In case initializer is not an array or type has more dimensions than initializer, this
1928 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1929 // actually is an array or not. Having a non-array initializer for an unsized array will
1930 // result in an error later, so we don't generate an error message here.
1931 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001932 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001933 if (!declareVariable(line, identifier, type, &variable))
1934 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001935 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001936 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937
Olli Etuahob0c645e2015-05-12 14:25:36 +03001938 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001939 if (symbolTable.atGlobalLevel() &&
1940 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001941 {
1942 // Error message does not completely match behavior with ESSL 1.00, but
1943 // we want to steer developers towards only using constant expressions.
1944 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001945 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001946 }
1947 if (globalInitWarning)
1948 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001949 warning(
1950 line,
1951 "global variable initializers should be constant expressions "
1952 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1953 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001954 }
1955
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001956 //
1957 // identifier must be of type constant, a global, or a temporary
1958 //
1959 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301960 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1961 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001962 error(line, " cannot initialize this type of qualifier ",
1963 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001964 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001965 }
1966 //
1967 // test for and propagate constant
1968 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001969
Arun Patole7e7e68d2015-05-22 12:02:25 +05301970 if (qualifier == EvqConst)
1971 {
1972 if (qualifier != initializer->getType().getQualifier())
1973 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001974 std::stringstream reasonStream;
1975 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1976 << "'";
1977 std::string reason = reasonStream.str();
1978 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001979 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001980 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001981 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301982 if (type != initializer->getType())
1983 {
1984 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001985 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001986 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001987 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001988 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001989
1990 // Save the constant folded value to the variable if possible. For example array
1991 // initializers are not folded, since that way copying the array literal to multiple places
1992 // in the shader is avoided.
1993 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1994 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301995 if (initializer->getAsConstantUnion())
1996 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001997 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001998 ASSERT(*initNode == nullptr);
1999 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302000 }
2001 else if (initializer->getAsSymbolNode())
2002 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002003 const TSymbol *symbol =
2004 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
2005 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002007 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002008 if (constArray)
2009 {
2010 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002011 ASSERT(*initNode == nullptr);
2012 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002013 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002014 }
2015 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002016
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002017 TIntermSymbol *intermSymbol =
2018 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
2019 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002020 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2021 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002022 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002023 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002024 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002025 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026
Olli Etuaho914b79a2017-06-19 16:03:19 +03002027 return true;
2028}
2029
2030TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2031 const TString &identifier,
2032 TIntermTyped *initializer,
2033 const TSourceLoc &loc)
2034{
2035 checkIsScalarBool(loc, pType);
2036 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002037 TType type(pType);
2038 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002039 {
2040 // The initializer is valid. The init condition needs to have a node - either the
2041 // initializer node, or a constant node in case the initialized variable is const and won't
2042 // be recorded in the AST.
2043 if (initNode == nullptr)
2044 {
2045 return initializer;
2046 }
2047 else
2048 {
2049 TIntermDeclaration *declaration = new TIntermDeclaration();
2050 declaration->appendDeclarator(initNode);
2051 return declaration;
2052 }
2053 }
2054 return nullptr;
2055}
2056
2057TIntermNode *TParseContext::addLoop(TLoopType type,
2058 TIntermNode *init,
2059 TIntermNode *cond,
2060 TIntermTyped *expr,
2061 TIntermNode *body,
2062 const TSourceLoc &line)
2063{
2064 TIntermNode *node = nullptr;
2065 TIntermTyped *typedCond = nullptr;
2066 if (cond)
2067 {
2068 typedCond = cond->getAsTyped();
2069 }
2070 if (cond == nullptr || typedCond)
2071 {
Olli Etuahocce89652017-06-19 16:04:09 +03002072 if (type == ELoopDoWhile)
2073 {
2074 checkIsScalarBool(line, typedCond);
2075 }
2076 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2077 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2078 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2079 !typedCond->isVector()));
2080
Olli Etuaho3ec75682017-07-05 17:02:55 +03002081 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002082 node->setLine(line);
2083 return node;
2084 }
2085
Olli Etuahocce89652017-06-19 16:04:09 +03002086 ASSERT(type != ELoopDoWhile);
2087
Olli Etuaho914b79a2017-06-19 16:03:19 +03002088 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2089 ASSERT(declaration);
2090 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2091 ASSERT(declarator->getLeft()->getAsSymbolNode());
2092
2093 // The condition is a declaration. In the AST representation we don't support declarations as
2094 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2095 // the loop.
2096 TIntermBlock *block = new TIntermBlock();
2097
2098 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2099 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2100 block->appendStatement(declareCondition);
2101
2102 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2103 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002104 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002105 block->appendStatement(loop);
2106 loop->setLine(line);
2107 block->setLine(line);
2108 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109}
2110
Olli Etuahocce89652017-06-19 16:04:09 +03002111TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2112 TIntermNodePair code,
2113 const TSourceLoc &loc)
2114{
Olli Etuaho56229f12017-07-10 14:16:33 +03002115 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002116
2117 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002118 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002119 {
2120 if (cond->getAsConstantUnion()->getBConst(0) == true)
2121 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002122 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002123 }
2124 else
2125 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002126 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002127 }
2128 }
2129
Olli Etuaho3ec75682017-07-05 17:02:55 +03002130 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002131 node->setLine(loc);
2132
2133 return node;
2134}
2135
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002136void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2137{
2138 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2139 typeSpecifier->getBasicType());
2140
2141 if (mShaderVersion < 300 && typeSpecifier->array)
2142 {
2143 error(typeSpecifier->getLine(), "not supported", "first-class array");
2144 typeSpecifier->clearArrayness();
2145 }
2146}
2147
Martin Radev70866b82016-07-22 15:27:42 +03002148TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302149 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002150{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002151 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002152
Martin Radev70866b82016-07-22 15:27:42 +03002153 TPublicType returnType = typeSpecifier;
2154 returnType.qualifier = typeQualifier.qualifier;
2155 returnType.invariant = typeQualifier.invariant;
2156 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002157 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002158 returnType.precision = typeSpecifier.precision;
2159
2160 if (typeQualifier.precision != EbpUndefined)
2161 {
2162 returnType.precision = typeQualifier.precision;
2163 }
2164
Martin Radev4a9cd802016-09-01 16:51:51 +03002165 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2166 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002167
Martin Radev4a9cd802016-09-01 16:51:51 +03002168 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2169 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002170
Martin Radev4a9cd802016-09-01 16:51:51 +03002171 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002172
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002173 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002174 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002175 if (typeSpecifier.array)
2176 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002177 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002178 returnType.clearArrayness();
2179 }
2180
Martin Radev70866b82016-07-22 15:27:42 +03002181 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002182 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002183 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002184 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002185 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002186 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002187
Martin Radev70866b82016-07-22 15:27:42 +03002188 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002189 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002190 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002191 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002192 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002193 }
2194 }
2195 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002196 {
Martin Radev70866b82016-07-22 15:27:42 +03002197 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002198 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002199 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002200 }
Martin Radev70866b82016-07-22 15:27:42 +03002201 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2202 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002203 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002204 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2205 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002206 }
Martin Radev70866b82016-07-22 15:27:42 +03002207 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002208 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002209 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002210 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002211 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002212 }
2213
2214 return returnType;
2215}
2216
Olli Etuaho856c4972016-08-08 11:38:39 +03002217void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2218 const TPublicType &type,
2219 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002220{
2221 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002222 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002223 {
2224 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002225 }
2226
2227 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2228 switch (qualifier)
2229 {
2230 case EvqVertexIn:
2231 // ESSL 3.00 section 4.3.4
2232 if (type.array)
2233 {
2234 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002235 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002236 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002237 return;
2238 case EvqFragmentOut:
2239 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002240 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002241 {
2242 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002243 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002244 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002245 return;
2246 default:
2247 break;
2248 }
2249
2250 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2251 // restrictions.
2252 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002253 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2254 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002255 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2256 {
2257 error(qualifierLocation, "must use 'flat' interpolation here",
2258 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002259 }
2260
Martin Radev4a9cd802016-09-01 16:51:51 +03002261 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002262 {
2263 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2264 // These restrictions are only implied by the ESSL 3.00 spec, but
2265 // the ESSL 3.10 spec lists these restrictions explicitly.
2266 if (type.array)
2267 {
2268 error(qualifierLocation, "cannot be an array of structures",
2269 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002270 }
2271 if (type.isStructureContainingArrays())
2272 {
2273 error(qualifierLocation, "cannot be a structure containing an array",
2274 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002275 }
2276 if (type.isStructureContainingType(EbtStruct))
2277 {
2278 error(qualifierLocation, "cannot be a structure containing a structure",
2279 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002280 }
2281 if (type.isStructureContainingType(EbtBool))
2282 {
2283 error(qualifierLocation, "cannot be a structure containing a bool",
2284 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002285 }
2286 }
2287}
2288
Martin Radev2cc85b32016-08-05 16:22:53 +03002289void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2290{
2291 if (qualifier.getType() == QtStorage)
2292 {
2293 const TStorageQualifierWrapper &storageQualifier =
2294 static_cast<const TStorageQualifierWrapper &>(qualifier);
2295 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2296 !symbolTable.atGlobalLevel())
2297 {
2298 error(storageQualifier.getLine(),
2299 "Local variables can only use the const storage qualifier.",
2300 storageQualifier.getQualifierString().c_str());
2301 }
2302 }
2303}
2304
Olli Etuaho43364892017-02-13 16:00:12 +00002305void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002306 const TSourceLoc &location)
2307{
Jiajia Qinbc585152017-06-23 15:42:17 +08002308 const std::string reason(
2309 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2310 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002311 if (memoryQualifier.readonly)
2312 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002313 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002314 }
2315 if (memoryQualifier.writeonly)
2316 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002317 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002318 }
Martin Radev049edfa2016-11-11 14:35:37 +02002319 if (memoryQualifier.coherent)
2320 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002321 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002322 }
2323 if (memoryQualifier.restrictQualifier)
2324 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002325 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002326 }
2327 if (memoryQualifier.volatileQualifier)
2328 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002329 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002330 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002331}
2332
jchen104cdac9e2017-05-08 11:01:20 +08002333// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2334// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002335void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2336 const TSourceLoc &loc,
2337 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002338{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002339 if (!IsAtomicCounter(type->getBasicType()))
2340 {
2341 return;
2342 }
2343
2344 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2345 : kAtomicCounterSize;
2346 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2347 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002348 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002349 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002350 {
2351 offset = bindingState.appendSpan(size);
2352 }
2353 else
2354 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002355 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002356 }
2357 if (offset == -1)
2358 {
2359 error(loc, "Offset overlapping", "atomic counter");
2360 return;
2361 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002362 layoutQualifier.offset = offset;
2363 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002364}
2365
Olli Etuaho454c34c2017-10-25 16:35:56 +03002366void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2367 const char *token,
2368 TType *type)
2369{
2370 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2371 {
2372 if (type->isArray() && type->getOutermostArraySize() == 0u)
2373 {
2374 // Set size for the unsized geometry shader inputs if they are declared after a valid
2375 // input primitive declaration.
2376 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2377 {
2378 ASSERT(mGeometryShaderInputArraySize > 0u);
2379 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2380 }
2381 else
2382 {
2383 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2384 // An input can be declared without an array size if there is a previous layout
2385 // which specifies the size.
2386 error(location,
2387 "Missing a valid input primitive declaration before declaring an unsized "
2388 "array input",
2389 token);
2390 }
2391 }
2392 else if (type->isArray())
2393 {
2394 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2395 }
2396 else
2397 {
2398 error(location, "Geometry shader input variable must be declared as an array", token);
2399 }
2400 }
2401}
2402
Olli Etuaho13389b62016-10-16 11:48:18 +01002403TIntermDeclaration *TParseContext::parseSingleDeclaration(
2404 TPublicType &publicType,
2405 const TSourceLoc &identifierOrTypeLocation,
2406 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002407{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002408 TType type(publicType);
2409 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2410 mDirectiveHandler.pragma().stdgl.invariantAll)
2411 {
2412 TQualifier qualifier = type.getQualifier();
2413
2414 // The directive handler has already taken care of rejecting invalid uses of this pragma
2415 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2416 // affected variable declarations:
2417 //
2418 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2419 // elsewhere, in TranslatorGLSL.)
2420 //
2421 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2422 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2423 // the way this is currently implemented we have to enable this compiler option before
2424 // parsing the shader and determining the shading language version it uses. If this were
2425 // implemented as a post-pass, the workaround could be more targeted.
2426 //
2427 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2428 // the specification, but there are desktop OpenGL drivers that expect that this is the
2429 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2430 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2431 {
2432 type.setInvariant(true);
2433 }
2434 }
2435
Olli Etuaho454c34c2017-10-25 16:35:56 +03002436 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002437
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002438 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2439 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002440
Olli Etuahobab4c082015-04-24 16:38:49 +03002441 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002442 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002443
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002444 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002445 if (emptyDeclaration)
2446 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002447 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002448 // In most cases we don't need to create a symbol node for an empty declaration.
2449 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2450 if (type.getBasicType() == EbtStruct)
2451 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002452 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002453 }
jchen104cdac9e2017-05-08 11:01:20 +08002454 else if (IsAtomicCounter(publicType.getBasicType()))
2455 {
2456 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2457 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002458 }
2459 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002460 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002461 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002462
Olli Etuaho55bde912017-10-25 13:41:13 +03002463 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002464
Olli Etuaho55bc9052017-10-25 17:33:06 +03002465 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002466
Olli Etuaho2935c582015-04-08 14:32:06 +03002467 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002468 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002469
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002470 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002471 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002472 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002473 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002474 }
2475
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002476 TIntermDeclaration *declaration = new TIntermDeclaration();
2477 declaration->setLine(identifierOrTypeLocation);
2478 if (symbol)
2479 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002480 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002481 declaration->appendDeclarator(symbol);
2482 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002483 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002484}
2485
Olli Etuaho55bde912017-10-25 13:41:13 +03002486TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002487 const TSourceLoc &identifierLocation,
2488 const TString &identifier,
2489 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002490 unsigned int arraySize)
Jamie Madill60ed9812013-06-06 11:56:46 -04002491{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002492 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002493
Olli Etuaho55bde912017-10-25 13:41:13 +03002494 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002495 identifierLocation);
2496
Olli Etuaho55bde912017-10-25 13:41:13 +03002497 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002498
Olli Etuaho55bde912017-10-25 13:41:13 +03002499 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002500
Olli Etuaho55bde912017-10-25 13:41:13 +03002501 TType arrayType(elementType);
2502 arrayType.makeArray(arraySize);
Jamie Madill60ed9812013-06-06 11:56:46 -04002503
Olli Etuaho454c34c2017-10-25 16:35:56 +03002504 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002505
2506 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2507
Olli Etuaho55bc9052017-10-25 17:33:06 +03002508 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002509
Olli Etuaho2935c582015-04-08 14:32:06 +03002510 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002511 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002512
Olli Etuaho13389b62016-10-16 11:48:18 +01002513 TIntermDeclaration *declaration = new TIntermDeclaration();
2514 declaration->setLine(identifierLocation);
2515
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002516 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002517 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002518 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2519 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002520 declaration->appendDeclarator(symbol);
2521 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002522
Olli Etuaho13389b62016-10-16 11:48:18 +01002523 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002524}
2525
Olli Etuaho13389b62016-10-16 11:48:18 +01002526TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2527 const TSourceLoc &identifierLocation,
2528 const TString &identifier,
2529 const TSourceLoc &initLocation,
2530 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002531{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002532 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002533
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002534 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2535 identifierLocation);
2536
2537 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002538
Olli Etuaho13389b62016-10-16 11:48:18 +01002539 TIntermDeclaration *declaration = new TIntermDeclaration();
2540 declaration->setLine(identifierLocation);
2541
2542 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002543 TType type(publicType);
2544 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002545 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002546 if (initNode)
2547 {
2548 declaration->appendDeclarator(initNode);
2549 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002550 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002551 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002552}
2553
Olli Etuaho13389b62016-10-16 11:48:18 +01002554TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002555 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002556 const TSourceLoc &identifierLocation,
2557 const TString &identifier,
2558 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002559 unsigned int arraySize,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002560 const TSourceLoc &initLocation,
2561 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002562{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002563 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002564
Olli Etuaho55bde912017-10-25 13:41:13 +03002565 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002566 identifierLocation);
2567
Olli Etuaho55bde912017-10-25 13:41:13 +03002568 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002569
Olli Etuaho55bde912017-10-25 13:41:13 +03002570 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002571
Olli Etuaho55bde912017-10-25 13:41:13 +03002572 TType arrayType(elementType);
2573 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002574
Olli Etuaho13389b62016-10-16 11:48:18 +01002575 TIntermDeclaration *declaration = new TIntermDeclaration();
2576 declaration->setLine(identifierLocation);
2577
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002578 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002579 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002580 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002581 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002582 if (initNode)
2583 {
2584 declaration->appendDeclarator(initNode);
2585 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002586 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002587
2588 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002589}
2590
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002591TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002592 const TTypeQualifierBuilder &typeQualifierBuilder,
2593 const TSourceLoc &identifierLoc,
2594 const TString *identifier,
2595 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002596{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002597 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002598
Martin Radev70866b82016-07-22 15:27:42 +03002599 if (!typeQualifier.invariant)
2600 {
2601 error(identifierLoc, "Expected invariant", identifier->c_str());
2602 return nullptr;
2603 }
2604 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2605 {
2606 return nullptr;
2607 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002608 if (!symbol)
2609 {
2610 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002611 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002612 }
Martin Radev70866b82016-07-22 15:27:42 +03002613 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002614 {
Martin Radev70866b82016-07-22 15:27:42 +03002615 error(identifierLoc, "invariant declaration specifies qualifier",
2616 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002617 }
Martin Radev70866b82016-07-22 15:27:42 +03002618 if (typeQualifier.precision != EbpUndefined)
2619 {
2620 error(identifierLoc, "invariant declaration specifies precision",
2621 getPrecisionString(typeQualifier.precision));
2622 }
2623 if (!typeQualifier.layoutQualifier.isEmpty())
2624 {
2625 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2626 }
2627
2628 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002629 if (!variable)
2630 {
2631 return nullptr;
2632 }
Martin Radev70866b82016-07-22 15:27:42 +03002633 const TType &type = variable->getType();
2634
2635 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2636 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002637 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002638
2639 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2640
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002641 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2642 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002643
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002644 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002645}
2646
Olli Etuaho13389b62016-10-16 11:48:18 +01002647void TParseContext::parseDeclarator(TPublicType &publicType,
2648 const TSourceLoc &identifierLocation,
2649 const TString &identifier,
2650 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002651{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002652 // If the declaration starting this declarator list was empty (example: int,), some checks were
2653 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002654 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002655 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002656 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2657 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002658 }
2659
Olli Etuaho856c4972016-08-08 11:38:39 +03002660 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002661
Olli Etuaho2935c582015-04-08 14:32:06 +03002662 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002663 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002664
2665 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2666
Olli Etuaho55bde912017-10-25 13:41:13 +03002667 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2668
Olli Etuaho55bc9052017-10-25 17:33:06 +03002669 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2670
Olli Etuaho43364892017-02-13 16:00:12 +00002671 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002672
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002673 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002674 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002675 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2676 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002677 declarationOut->appendDeclarator(symbol);
2678 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002679}
2680
Olli Etuaho55bde912017-10-25 13:41:13 +03002681void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002682 const TSourceLoc &identifierLocation,
2683 const TString &identifier,
2684 const TSourceLoc &arrayLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002685 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002686 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002687{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002688 // If the declaration starting this declarator list was empty (example: int,), some checks were
2689 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002690 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002691 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002692 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002693 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002694 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002695
Olli Etuaho55bde912017-10-25 13:41:13 +03002696 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002697
Olli Etuaho55bde912017-10-25 13:41:13 +03002698 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002699 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002700 TType arrayType(elementType);
2701 arrayType.makeArray(arraySize);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002702
Olli Etuaho454c34c2017-10-25 16:35:56 +03002703 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2704
Olli Etuaho55bde912017-10-25 13:41:13 +03002705 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2706
Olli Etuaho55bc9052017-10-25 17:33:06 +03002707 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002708
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002709 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002710 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002711
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002712 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002713 {
2714 TIntermSymbol *symbol =
2715 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2716 symbol->setLine(identifierLocation);
2717 declarationOut->appendDeclarator(symbol);
2718 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002719 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002720}
2721
Olli Etuaho13389b62016-10-16 11:48:18 +01002722void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2723 const TSourceLoc &identifierLocation,
2724 const TString &identifier,
2725 const TSourceLoc &initLocation,
2726 TIntermTyped *initializer,
2727 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002728{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002729 // If the declaration starting this declarator list was empty (example: int,), some checks were
2730 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002731 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002732 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002733 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2734 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002735 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002736
Olli Etuaho856c4972016-08-08 11:38:39 +03002737 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002738
Olli Etuaho13389b62016-10-16 11:48:18 +01002739 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002740 TType type(publicType);
2741 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002742 {
2743 //
2744 // build the intermediate representation
2745 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002746 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002747 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002748 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002749 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002750 }
2751}
2752
Olli Etuaho55bde912017-10-25 13:41:13 +03002753void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002754 const TSourceLoc &identifierLocation,
2755 const TString &identifier,
2756 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002757 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002758 const TSourceLoc &initLocation,
2759 TIntermTyped *initializer,
2760 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002761{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002762 // If the declaration starting this declarator list was empty (example: int,), some checks were
2763 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002764 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002765 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002766 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002767 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002768 }
2769
Olli Etuaho55bde912017-10-25 13:41:13 +03002770 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002771
Olli Etuaho55bde912017-10-25 13:41:13 +03002772 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002773
Olli Etuaho55bde912017-10-25 13:41:13 +03002774 TType arrayType(elementType);
2775 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002776
2777 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002778 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002779 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002780 {
2781 if (initNode)
2782 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002783 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002784 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002785 }
2786}
2787
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002788TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2789{
2790 // It's simpler to parse an empty statement as a constant expression rather than having a
2791 // different type of node just for empty statements, that will be pruned from the AST anyway.
2792 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2793 node->setLine(location);
2794 return node;
2795}
2796
jchen104cdac9e2017-05-08 11:01:20 +08002797void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2798 const TSourceLoc &location)
2799{
2800 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2801 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2802 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2803 {
2804 error(location, "Requires both binding and offset", "layout");
2805 return;
2806 }
2807 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2808}
2809
Olli Etuahocce89652017-06-19 16:04:09 +03002810void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2811 const TPublicType &type,
2812 const TSourceLoc &loc)
2813{
2814 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2815 !getFragmentPrecisionHigh())
2816 {
2817 error(loc, "precision is not supported in fragment shader", "highp");
2818 }
2819
2820 if (!CanSetDefaultPrecisionOnType(type))
2821 {
2822 error(loc, "illegal type argument for default precision qualifier",
2823 getBasicString(type.getBasicType()));
2824 return;
2825 }
2826 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2827}
2828
Shaob5cc1192017-07-06 10:47:20 +08002829bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2830{
2831 switch (typeQualifier.layoutQualifier.primitiveType)
2832 {
2833 case EptLines:
2834 case EptLinesAdjacency:
2835 case EptTriangles:
2836 case EptTrianglesAdjacency:
2837 return typeQualifier.qualifier == EvqGeometryIn;
2838
2839 case EptLineStrip:
2840 case EptTriangleStrip:
2841 return typeQualifier.qualifier == EvqGeometryOut;
2842
2843 case EptPoints:
2844 return true;
2845
2846 default:
2847 UNREACHABLE();
2848 return false;
2849 }
2850}
2851
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002852void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2853 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002854{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002855 if (mGeometryShaderInputArraySize == 0u)
2856 {
2857 mGeometryShaderInputArraySize = inputArraySize;
2858 }
2859 else if (mGeometryShaderInputArraySize != inputArraySize)
2860 {
2861 error(line,
2862 "Array size or input primitive declaration doesn't match the size of earlier sized "
2863 "array inputs.",
2864 "layout");
2865 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002866}
2867
Shaob5cc1192017-07-06 10:47:20 +08002868bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2869{
2870 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2871
2872 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2873
2874 if (layoutQualifier.maxVertices != -1)
2875 {
2876 error(typeQualifier.line,
2877 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2878 return false;
2879 }
2880
2881 // Set mGeometryInputPrimitiveType if exists
2882 if (layoutQualifier.primitiveType != EptUndefined)
2883 {
2884 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2885 {
2886 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2887 return false;
2888 }
2889
2890 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2891 {
2892 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002893 setGeometryShaderInputArraySize(
2894 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2895 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002896 }
2897 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2898 {
2899 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2900 "layout");
2901 return false;
2902 }
2903 }
2904
2905 // Set mGeometryInvocations if exists
2906 if (layoutQualifier.invocations > 0)
2907 {
2908 if (mGeometryShaderInvocations == 0)
2909 {
2910 mGeometryShaderInvocations = layoutQualifier.invocations;
2911 }
2912 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2913 {
2914 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2915 "layout");
2916 return false;
2917 }
2918 }
2919
2920 return true;
2921}
2922
2923bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2924{
2925 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2926
2927 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2928
2929 if (layoutQualifier.invocations > 0)
2930 {
2931 error(typeQualifier.line,
2932 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2933 return false;
2934 }
2935
2936 // Set mGeometryOutputPrimitiveType if exists
2937 if (layoutQualifier.primitiveType != EptUndefined)
2938 {
2939 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2940 {
2941 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2942 return false;
2943 }
2944
2945 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2946 {
2947 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2948 }
2949 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2950 {
2951 error(typeQualifier.line,
2952 "primitive doesn't match earlier output primitive declaration", "layout");
2953 return false;
2954 }
2955 }
2956
2957 // Set mGeometryMaxVertices if exists
2958 if (layoutQualifier.maxVertices > -1)
2959 {
2960 if (mGeometryShaderMaxVertices == -1)
2961 {
2962 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2963 }
2964 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2965 {
2966 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2967 "layout");
2968 return false;
2969 }
2970 }
2971
2972 return true;
2973}
2974
Martin Radev70866b82016-07-22 15:27:42 +03002975void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002976{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002977 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002978 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002979
Martin Radev70866b82016-07-22 15:27:42 +03002980 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2981 typeQualifier.line);
2982
Jamie Madillc2128ff2016-07-04 10:26:17 -04002983 // It should never be the case, but some strange parser errors can send us here.
2984 if (layoutQualifier.isEmpty())
2985 {
2986 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002987 return;
2988 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002989
Martin Radev802abe02016-08-04 17:48:32 +03002990 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002991 {
Olli Etuaho43364892017-02-13 16:00:12 +00002992 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002993 return;
2994 }
2995
Olli Etuaho43364892017-02-13 16:00:12 +00002996 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2997
2998 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002999
3000 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3001
Andrei Volykhina5527072017-03-22 16:46:30 +03003002 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3003
jchen104cdac9e2017-05-08 11:01:20 +08003004 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3005
Qin Jiajiaca68d982017-09-18 16:41:56 +08003006 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3007 typeQualifier.qualifier);
3008
Martin Radev802abe02016-08-04 17:48:32 +03003009 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003010 {
Martin Radev802abe02016-08-04 17:48:32 +03003011 if (mComputeShaderLocalSizeDeclared &&
3012 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3013 {
3014 error(typeQualifier.line, "Work group size does not match the previous declaration",
3015 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003016 return;
3017 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003018
Martin Radev802abe02016-08-04 17:48:32 +03003019 if (mShaderVersion < 310)
3020 {
3021 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003022 return;
3023 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003024
Martin Radev4c4c8e72016-08-04 12:25:34 +03003025 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003026 {
3027 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003028 return;
3029 }
3030
3031 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3032 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3033
3034 const TConstantUnion *maxComputeWorkGroupSizeData =
3035 maxComputeWorkGroupSize->getConstPointer();
3036
3037 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3038 {
3039 if (layoutQualifier.localSize[i] != -1)
3040 {
3041 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3042 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3043 if (mComputeShaderLocalSize[i] < 1 ||
3044 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3045 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003046 std::stringstream reasonStream;
3047 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3048 << maxComputeWorkGroupSizeValue;
3049 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003050
Olli Etuaho4de340a2016-12-16 09:32:03 +00003051 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003052 return;
3053 }
3054 }
3055 }
3056
3057 mComputeShaderLocalSizeDeclared = true;
3058 }
Shaob5cc1192017-07-06 10:47:20 +08003059 else if (typeQualifier.qualifier == EvqGeometryIn)
3060 {
3061 if (mShaderVersion < 310)
3062 {
3063 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3064 return;
3065 }
3066
3067 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3068 {
3069 return;
3070 }
3071 }
3072 else if (typeQualifier.qualifier == EvqGeometryOut)
3073 {
3074 if (mShaderVersion < 310)
3075 {
3076 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3077 "layout");
3078 return;
3079 }
3080
3081 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3082 {
3083 return;
3084 }
3085 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003086 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3087 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003088 {
3089 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3090 // specification.
3091 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3092 {
3093 error(typeQualifier.line, "Number of views does not match the previous declaration",
3094 "layout");
3095 return;
3096 }
3097
3098 if (layoutQualifier.numViews == -1)
3099 {
3100 error(typeQualifier.line, "No num_views specified", "layout");
3101 return;
3102 }
3103
3104 if (layoutQualifier.numViews > mMaxNumViews)
3105 {
3106 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3107 "layout");
3108 return;
3109 }
3110
3111 mNumViews = layoutQualifier.numViews;
3112 }
Martin Radev802abe02016-08-04 17:48:32 +03003113 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003114 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003115 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003116 {
Martin Radev802abe02016-08-04 17:48:32 +03003117 return;
3118 }
3119
Jiajia Qinbc585152017-06-23 15:42:17 +08003120 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003121 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003122 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003123 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003124 return;
3125 }
3126
3127 if (mShaderVersion < 300)
3128 {
3129 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3130 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003131 return;
3132 }
3133
Olli Etuaho09b04a22016-12-15 13:30:26 +00003134 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003135
3136 if (layoutQualifier.matrixPacking != EmpUnspecified)
3137 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003138 if (typeQualifier.qualifier == EvqUniform)
3139 {
3140 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3141 }
3142 else if (typeQualifier.qualifier == EvqBuffer)
3143 {
3144 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3145 }
Martin Radev802abe02016-08-04 17:48:32 +03003146 }
3147
3148 if (layoutQualifier.blockStorage != EbsUnspecified)
3149 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003150 if (typeQualifier.qualifier == EvqUniform)
3151 {
3152 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3153 }
3154 else if (typeQualifier.qualifier == EvqBuffer)
3155 {
3156 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3157 }
Martin Radev802abe02016-08-04 17:48:32 +03003158 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003159 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003160}
3161
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003162TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3163 const TFunction &function,
3164 const TSourceLoc &location,
3165 bool insertParametersToSymbolTable)
3166{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003167 checkIsNotReserved(location, function.getName());
3168
Olli Etuahofe486322017-03-21 09:30:54 +00003169 TIntermFunctionPrototype *prototype =
3170 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003171 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3172 // point to the data that already exists in the symbol table.
3173 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3174 prototype->setLine(location);
3175
3176 for (size_t i = 0; i < function.getParamCount(); i++)
3177 {
3178 const TConstParameter &param = function.getParam(i);
3179
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003180 TIntermSymbol *symbol = nullptr;
3181
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003182 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3183 // be used for unused args).
3184 if (param.name != nullptr)
3185 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003186 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003187 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003188 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003189 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3190 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003191 {
3192 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3193 variable->getType());
3194 }
3195 else
3196 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003197 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003198 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003199 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003200 // Unsized type of a named parameter should have already been checked and sanitized.
3201 ASSERT(!param.type->isUnsizedArray());
3202 }
3203 else
3204 {
3205 if (param.type->isUnsizedArray())
3206 {
3207 error(location, "function parameter array must be sized at compile time", "[]");
3208 // We don't need to size the arrays since the parameter is unnamed and hence
3209 // inaccessible.
3210 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003211 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003212 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003213 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003214 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3215 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003216 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003217 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003218 symbol->setLine(location);
3219 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003220 }
3221 return prototype;
3222}
3223
Olli Etuaho16c745a2017-01-16 17:02:27 +00003224TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3225 const TFunction &parsedFunction,
3226 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003227{
Olli Etuaho476197f2016-10-11 13:59:08 +01003228 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3229 // first declaration. Either way the instance in the symbol table is used to track whether the
3230 // function is declared multiple times.
3231 TFunction *function = static_cast<TFunction *>(
3232 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3233 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003234 {
3235 // ESSL 1.00.17 section 4.2.7.
3236 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3237 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003238 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003239 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003240
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003241 TIntermFunctionPrototype *prototype =
3242 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003243
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003244 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003245
3246 if (!symbolTable.atGlobalLevel())
3247 {
3248 // ESSL 3.00.4 section 4.2.4.
3249 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003250 }
3251
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003252 return prototype;
3253}
3254
Olli Etuaho336b1472016-10-05 16:37:55 +01003255TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003256 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003257 TIntermBlock *functionBody,
3258 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003259{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003260 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003261 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3262 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003263 error(location, "function does not return a value:",
3264 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003265 }
3266
Olli Etuahof51fdd22016-10-03 10:03:40 +01003267 if (functionBody == nullptr)
3268 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003269 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003270 functionBody->setLine(location);
3271 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003272 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003273 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003274 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003275
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003276 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003277 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003278}
3279
Olli Etuaho476197f2016-10-11 13:59:08 +01003280void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3281 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003282 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003283{
Olli Etuaho476197f2016-10-11 13:59:08 +01003284 ASSERT(function);
3285 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003286 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003287 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003288
3289 if (builtIn)
3290 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003291 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003292 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003293 else
Jamie Madill185fb402015-06-12 15:48:48 -04003294 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003295 TFunction *prevDec = static_cast<TFunction *>(
3296 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3297
3298 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3299 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3300 // occurance.
3301 if (*function != prevDec)
3302 {
3303 // Swap the parameters of the previous declaration to the parameters of the function
3304 // definition (parameter names may differ).
3305 prevDec->swapParameters(**function);
3306
3307 // The function definition will share the same symbol as any previous declaration.
3308 *function = prevDec;
3309 }
3310
3311 if ((*function)->isDefined())
3312 {
3313 error(location, "function already has a body", (*function)->getName().c_str());
3314 }
3315
3316 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003317 }
Jamie Madill185fb402015-06-12 15:48:48 -04003318
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003319 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003320 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003321 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003322
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003323 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003324 setLoopNestingLevel(0);
3325}
3326
Jamie Madillb98c3a82015-07-23 14:26:04 -04003327TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003328{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003329 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003330 // We don't know at this point whether this is a function definition or a prototype.
3331 // The definition production code will check for redefinitions.
3332 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003333 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003334 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3335 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003336 //
3337 TFunction *prevDec =
3338 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303339
Olli Etuahod80f2942017-11-06 12:44:45 +02003340 for (size_t i = 0u; i < function->getParamCount(); ++i)
3341 {
3342 auto &param = function->getParam(i);
3343 if (param.type->isStructSpecifier())
3344 {
3345 // ESSL 3.00.6 section 12.10.
3346 error(location, "Function parameter type cannot be a structure definition",
Olli Etuaho7af63722017-11-13 15:03:40 +02003347 function->getName().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003348 }
3349 }
3350
Martin Radevda6254b2016-12-14 17:00:36 +02003351 if (getShaderVersion() >= 300 &&
3352 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3353 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",
3358 function->getName().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 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003382 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003383 if (prevSym)
3384 {
3385 if (!prevSym->isFunction())
3386 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003387 error(location, "redefinition of a function", function->getName().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
3401 if (function->getName() == "main")
3402 {
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 Etuaho77ba4082016-12-16 12:01:18 +00003443 ASSERT(type.arraySize == 0 || 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 Etuahocce89652017-06-19 16:04:09 +03003465 if (mShaderVersion < 300 && publicType.array)
3466 {
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);
3495 arrayType->sizeUnsizedArrays(TVector<unsigned int>());
3496 }
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,
3525 unsigned int arraySize,
3526 const TSourceLoc &arrayLoc,
3527 TPublicType *elementType)
3528{
3529 checkArrayElementIsNotArray(arrayLoc, *elementType);
3530 TType *arrayType = new TType(*elementType);
3531 arrayType->makeArray(arraySize);
3532 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);
3548 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3549 if (dimensionalityFromElement > type.getArraySizes().size())
3550 {
3551 error(line, "constructing from a non-dereferenced array", "constructor");
3552 return false;
3553 }
3554 else if (dimensionalityFromElement < type.getArraySizes().size())
3555 {
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 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003587 type.sizeUnsizedArrays(TVector<unsigned int>());
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 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003596 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3597 {
3598 if (type.getArraySizes()[i] == 0u)
3599 {
3600 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3601 }
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
3787 if (typeQualifier.qualifier == EvqBuffer)
3788 {
3789 // set memory qualifiers
3790 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3791 // qualified with a memory qualifier, it is as if all of its members were declared with
3792 // the same memory qualifier.
3793 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3794 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3795 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3796 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3797 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3798 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3799 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3800 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3801 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3802 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3803 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003804 }
3805
Jamie Madillb98c3a82015-07-23 14:26:04 -04003806 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003807 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003808 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3809 if (arrayIndex != nullptr)
3810 {
3811 interfaceBlockType.makeArray(arraySize);
3812 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003813
3814 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003815 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003816
Jamie Madill98493dd2013-07-08 14:39:03 -04003817 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003818 {
3819 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003820 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3821 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003822 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303823 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003824
3825 // set parent pointer of the field variable
3826 fieldType->setInterfaceBlock(interfaceBlock);
3827
Olli Etuaho0f684632017-07-13 12:42:15 +03003828 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003829
Olli Etuaho0f684632017-07-13 12:42:15 +03003830 if (fieldVariable)
3831 {
3832 fieldVariable->setQualifier(typeQualifier.qualifier);
3833 }
3834 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303835 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003836 error(field->line(), "redefinition of an interface block member name",
3837 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003838 }
3839 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003840 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003841 }
3842 else
3843 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003844 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003845
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003846 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003847 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3848 if (instanceTypeDef)
3849 {
3850 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003851 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003852 }
3853 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303854 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003855 error(instanceLine, "redefinition of an interface block instance name",
3856 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003857 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003858 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003859 }
3860
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003861 TIntermDeclaration *declaration = nullptr;
3862
3863 if (symbolId)
3864 {
3865 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3866 blockSymbol->setLine(typeQualifier.line);
3867 declaration = new TIntermDeclaration();
3868 declaration->appendDeclarator(blockSymbol);
3869 declaration->setLine(nameLine);
3870 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003871
3872 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003873 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003874}
3875
Olli Etuaho383b7912016-08-05 11:22:59 +03003876void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003877{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003878 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003879
3880 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003881 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303882 if (mStructNestingLevel > 1)
3883 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003884 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003885 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003886}
3887
3888void TParseContext::exitStructDeclaration()
3889{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003890 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003891}
3892
Olli Etuaho8a176262016-08-16 14:23:01 +03003893void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003894{
Jamie Madillacb4b812016-11-07 13:50:29 -05003895 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303896 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003897 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003898 }
3899
Arun Patole7e7e68d2015-05-22 12:02:25 +05303900 if (field.type()->getBasicType() != EbtStruct)
3901 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003902 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003903 }
3904
3905 // We're already inside a structure definition at this point, so add
3906 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303907 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3908 {
Jamie Madill41a49272014-03-18 16:10:13 -04003909 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003910 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3911 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003912 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003913 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003914 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003915 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003916}
3917
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003918//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003919// Parse an array index expression
3920//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003921TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3922 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303923 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003924{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003925 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3926 {
3927 if (baseExpression->getAsSymbolNode())
3928 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303929 error(location, " left of '[' is not of type array, matrix, or vector ",
3930 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003931 }
3932 else
3933 {
3934 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3935 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003936
Olli Etuaho3ec75682017-07-05 17:02:55 +03003937 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003938 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003939
Jiawei Shaod8105a02017-08-08 09:54:36 +08003940 if (baseExpression->getQualifier() == EvqPerVertexIn)
3941 {
3942 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3943 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3944 {
3945 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3946 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3947 }
3948 }
3949
Jamie Madill21c1e452014-12-29 11:33:41 -05003950 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3951
Olli Etuaho36b05142015-11-12 13:10:42 +02003952 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3953 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3954 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3955 // index is a constant expression.
3956 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3957 {
3958 if (baseExpression->isInterfaceBlock())
3959 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003960 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3961 switch (baseExpression->getQualifier())
3962 {
3963 case EvqPerVertexIn:
3964 break;
3965 case EvqUniform:
3966 case EvqBuffer:
3967 error(location,
3968 "array indexes for uniform block arrays and shader storage block arrays "
3969 "must be constant integral expressions",
3970 "[");
3971 break;
3972 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003973 // We can reach here only in error cases.
3974 ASSERT(mDiagnostics->numErrors() > 0);
3975 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003976 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003977 }
3978 else if (baseExpression->getQualifier() == EvqFragmentOut)
3979 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003980 error(location,
3981 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003982 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003983 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3984 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003985 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003986 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003987 }
3988
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003989 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003990 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003991 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3992 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3993 // constant fold expressions that are not constant expressions). The most compatible way to
3994 // handle this case is to report a warning instead of an error and force the index to be in
3995 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003996 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003997 int index = 0;
3998 if (indexConstantUnion->getBasicType() == EbtInt)
3999 {
4000 index = indexConstantUnion->getIConst(0);
4001 }
4002 else if (indexConstantUnion->getBasicType() == EbtUInt)
4003 {
4004 index = static_cast<int>(indexConstantUnion->getUConst(0));
4005 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004006
4007 int safeIndex = -1;
4008
4009 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04004010 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004011 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004012 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004013 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004014 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004015 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004016 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00004017 "GL_EXT_draw_buffers is disabled",
4018 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004019 safeIndex = 0;
4020 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03004021 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004022 // Only do generic out-of-range check if similar error hasn't already been reported.
4023 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004024 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004025 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004026 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00004027 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004028 }
4029 }
4030 else if (baseExpression->isMatrix())
4031 {
4032 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03004033 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00004034 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04004035 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004036 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04004037 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004038 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
4039 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00004040 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004041 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004042
4043 ASSERT(safeIndex >= 0);
4044 // Data of constant unions can't be changed, because it may be shared with other
4045 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4046 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03004047 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004048 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004049 TConstantUnion *safeConstantUnion = new TConstantUnion();
4050 safeConstantUnion->setIConst(safeIndex);
4051 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03004052 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004053 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004054
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004055 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4056 node->setLine(location);
4057 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004058 }
Jamie Madill7164cf42013-07-08 13:30:59 -04004059 else
4060 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004061 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4062 node->setLine(location);
4063 // Indirect indexing can never be constant folded.
4064 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04004065 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004066}
4067
Olli Etuaho90892fb2016-07-14 14:44:51 +03004068int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
4069 const TSourceLoc &location,
4070 int index,
4071 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00004072 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004073{
4074 if (index >= arraySize || index < 0)
4075 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004076 std::stringstream reasonStream;
4077 reasonStream << reason << " '" << index << "'";
4078 std::string token = reasonStream.str();
4079 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03004080 if (index < 0)
4081 {
4082 return 0;
4083 }
4084 else
4085 {
4086 return arraySize - 1;
4087 }
4088 }
4089 return index;
4090}
4091
Jamie Madillb98c3a82015-07-23 14:26:04 -04004092TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4093 const TSourceLoc &dotLocation,
4094 const TString &fieldString,
4095 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004096{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004097 if (baseExpression->isArray())
4098 {
4099 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004100 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004101 }
4102
4103 if (baseExpression->isVector())
4104 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004105 TVector<int> fieldOffsets;
4106 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4107 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004108 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004109 fieldOffsets.resize(1);
4110 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004111 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004112 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4113 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004114
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004115 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004116 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004117 else if (baseExpression->getBasicType() == EbtStruct)
4118 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304119 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004120 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121 {
4122 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004123 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124 }
4125 else
4126 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004127 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004128 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004129 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004130 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004131 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004132 {
4133 fieldFound = true;
4134 break;
4135 }
4136 }
4137 if (fieldFound)
4138 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004139 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004140 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004141 TIntermBinary *node =
4142 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4143 node->setLine(dotLocation);
4144 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004145 }
4146 else
4147 {
4148 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004149 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004150 }
4151 }
4152 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004153 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004154 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304155 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004156 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004157 {
4158 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004159 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004160 }
4161 else
4162 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004163 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004164 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004165 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004166 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004167 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004168 {
4169 fieldFound = true;
4170 break;
4171 }
4172 }
4173 if (fieldFound)
4174 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004175 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004176 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004177 TIntermBinary *node =
4178 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4179 node->setLine(dotLocation);
4180 // Indexing interface blocks can never be constant folded.
4181 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004182 }
4183 else
4184 {
4185 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004186 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004187 }
4188 }
4189 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004190 else
4191 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004192 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004193 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004194 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304195 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004196 }
4197 else
4198 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304199 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004200 " field selection requires structure, vector, or interface block on left hand "
4201 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304202 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004203 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004204 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004205 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004206}
4207
Jamie Madillb98c3a82015-07-23 14:26:04 -04004208TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4209 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004210{
Martin Radev802abe02016-08-04 17:48:32 +03004211 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004212
4213 if (qualifierType == "shared")
4214 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004215 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004216 {
4217 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4218 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004219 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004220 }
4221 else if (qualifierType == "packed")
4222 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004223 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004224 {
4225 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4226 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004227 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004228 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004229 else if (qualifierType == "std430")
4230 {
4231 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4232 qualifier.blockStorage = EbsStd430;
4233 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004234 else if (qualifierType == "std140")
4235 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004236 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004237 }
4238 else if (qualifierType == "row_major")
4239 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004240 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004241 }
4242 else if (qualifierType == "column_major")
4243 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004244 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004245 }
4246 else if (qualifierType == "location")
4247 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004248 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4249 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004250 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004251 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004252 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004253 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4254 {
4255 qualifier.yuv = true;
4256 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004257 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004258 else if (qualifierType == "rgba32f")
4259 {
4260 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4261 qualifier.imageInternalFormat = EiifRGBA32F;
4262 }
4263 else if (qualifierType == "rgba16f")
4264 {
4265 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4266 qualifier.imageInternalFormat = EiifRGBA16F;
4267 }
4268 else if (qualifierType == "r32f")
4269 {
4270 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4271 qualifier.imageInternalFormat = EiifR32F;
4272 }
4273 else if (qualifierType == "rgba8")
4274 {
4275 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4276 qualifier.imageInternalFormat = EiifRGBA8;
4277 }
4278 else if (qualifierType == "rgba8_snorm")
4279 {
4280 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4281 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4282 }
4283 else if (qualifierType == "rgba32i")
4284 {
4285 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4286 qualifier.imageInternalFormat = EiifRGBA32I;
4287 }
4288 else if (qualifierType == "rgba16i")
4289 {
4290 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4291 qualifier.imageInternalFormat = EiifRGBA16I;
4292 }
4293 else if (qualifierType == "rgba8i")
4294 {
4295 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4296 qualifier.imageInternalFormat = EiifRGBA8I;
4297 }
4298 else if (qualifierType == "r32i")
4299 {
4300 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4301 qualifier.imageInternalFormat = EiifR32I;
4302 }
4303 else if (qualifierType == "rgba32ui")
4304 {
4305 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4306 qualifier.imageInternalFormat = EiifRGBA32UI;
4307 }
4308 else if (qualifierType == "rgba16ui")
4309 {
4310 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4311 qualifier.imageInternalFormat = EiifRGBA16UI;
4312 }
4313 else if (qualifierType == "rgba8ui")
4314 {
4315 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4316 qualifier.imageInternalFormat = EiifRGBA8UI;
4317 }
4318 else if (qualifierType == "r32ui")
4319 {
4320 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4321 qualifier.imageInternalFormat = EiifR32UI;
4322 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004323 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4324 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004325 {
4326 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4327 qualifier.primitiveType = EptPoints;
4328 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004329 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4330 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004331 {
4332 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4333 qualifier.primitiveType = EptLines;
4334 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004335 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4336 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004337 {
4338 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4339 qualifier.primitiveType = EptLinesAdjacency;
4340 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004341 else if (qualifierType == "triangles" && 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 = EptTriangles;
4346 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004347 else if (qualifierType == "triangles_adjacency" && 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 = EptTrianglesAdjacency;
4352 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004353 else if (qualifierType == "line_strip" && 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 = EptLineStrip;
4358 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004359 else if (qualifierType == "triangle_strip" && 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 = EptTriangleStrip;
4364 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004365
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004366 else
4367 {
4368 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004369 }
4370
Jamie Madilla5efff92013-06-06 11:56:47 -04004371 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004372}
4373
Martin Radev802abe02016-08-04 17:48:32 +03004374void TParseContext::parseLocalSize(const TString &qualifierType,
4375 const TSourceLoc &qualifierTypeLine,
4376 int intValue,
4377 const TSourceLoc &intValueLine,
4378 const std::string &intValueString,
4379 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004380 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004381{
Olli Etuaho856c4972016-08-08 11:38:39 +03004382 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004383 if (intValue < 1)
4384 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004385 std::stringstream reasonStream;
4386 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4387 std::string reason = reasonStream.str();
4388 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004389 }
4390 (*localSize)[index] = intValue;
4391}
4392
Olli Etuaho09b04a22016-12-15 13:30:26 +00004393void TParseContext::parseNumViews(int intValue,
4394 const TSourceLoc &intValueLine,
4395 const std::string &intValueString,
4396 int *numViews)
4397{
4398 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4399 // specification.
4400 if (intValue < 1)
4401 {
4402 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4403 }
4404 *numViews = intValue;
4405}
4406
Shaob5cc1192017-07-06 10:47:20 +08004407void TParseContext::parseInvocations(int intValue,
4408 const TSourceLoc &intValueLine,
4409 const std::string &intValueString,
4410 int *numInvocations)
4411{
4412 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4413 // it doesn't make sense to accept invocations <= 0.
4414 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4415 {
4416 error(intValueLine,
4417 "out of range: invocations must be in the range of [1, "
4418 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4419 intValueString.c_str());
4420 }
4421 else
4422 {
4423 *numInvocations = intValue;
4424 }
4425}
4426
4427void TParseContext::parseMaxVertices(int intValue,
4428 const TSourceLoc &intValueLine,
4429 const std::string &intValueString,
4430 int *maxVertices)
4431{
4432 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4433 // it doesn't make sense to accept max_vertices < 0.
4434 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4435 {
4436 error(
4437 intValueLine,
4438 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4439 intValueString.c_str());
4440 }
4441 else
4442 {
4443 *maxVertices = intValue;
4444 }
4445}
4446
Jamie Madillb98c3a82015-07-23 14:26:04 -04004447TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4448 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004449 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304450 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004451{
Martin Radev802abe02016-08-04 17:48:32 +03004452 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004453
Martin Radev802abe02016-08-04 17:48:32 +03004454 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004455
Martin Radev802abe02016-08-04 17:48:32 +03004456 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004457 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004458 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004459 if (intValue < 0)
4460 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004461 error(intValueLine, "out of range: location must be non-negative",
4462 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004463 }
4464 else
4465 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004466 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004467 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004468 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004469 }
Olli Etuaho43364892017-02-13 16:00:12 +00004470 else if (qualifierType == "binding")
4471 {
4472 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4473 if (intValue < 0)
4474 {
4475 error(intValueLine, "out of range: binding must be non-negative",
4476 intValueString.c_str());
4477 }
4478 else
4479 {
4480 qualifier.binding = intValue;
4481 }
4482 }
jchen104cdac9e2017-05-08 11:01:20 +08004483 else if (qualifierType == "offset")
4484 {
4485 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4486 if (intValue < 0)
4487 {
4488 error(intValueLine, "out of range: offset must be non-negative",
4489 intValueString.c_str());
4490 }
4491 else
4492 {
4493 qualifier.offset = intValue;
4494 }
4495 }
Martin Radev802abe02016-08-04 17:48:32 +03004496 else if (qualifierType == "local_size_x")
4497 {
4498 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4499 &qualifier.localSize);
4500 }
4501 else if (qualifierType == "local_size_y")
4502 {
4503 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4504 &qualifier.localSize);
4505 }
4506 else if (qualifierType == "local_size_z")
4507 {
4508 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4509 &qualifier.localSize);
4510 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004511 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004512 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004513 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4514 {
4515 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4516 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004517 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004518 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4519 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004520 {
4521 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4522 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004523 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4524 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004525 {
4526 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4527 }
4528
Martin Radev802abe02016-08-04 17:48:32 +03004529 else
4530 {
4531 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004532 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004533
Jamie Madilla5efff92013-06-06 11:56:47 -04004534 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004535}
4536
Olli Etuaho613b9592016-09-05 12:05:53 +03004537TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4538{
4539 return new TTypeQualifierBuilder(
4540 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4541 mShaderVersion);
4542}
4543
Olli Etuahocce89652017-06-19 16:04:09 +03004544TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4545 const TSourceLoc &loc)
4546{
4547 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4548 return new TStorageQualifierWrapper(qualifier, loc);
4549}
4550
4551TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4552{
4553 if (getShaderType() == GL_VERTEX_SHADER)
4554 {
4555 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4556 }
4557 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4558}
4559
4560TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4561{
4562 if (declaringFunction())
4563 {
4564 return new TStorageQualifierWrapper(EvqIn, loc);
4565 }
Shaob5cc1192017-07-06 10:47:20 +08004566
4567 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004568 {
Shaob5cc1192017-07-06 10:47:20 +08004569 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004570 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004571 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004572 {
4573 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4574 }
4575 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004576 }
Shaob5cc1192017-07-06 10:47:20 +08004577 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004578 {
Shaob5cc1192017-07-06 10:47:20 +08004579 if (mShaderVersion < 300)
4580 {
4581 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4582 }
4583 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004584 }
Shaob5cc1192017-07-06 10:47:20 +08004585 case GL_COMPUTE_SHADER:
4586 {
4587 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4588 }
4589 case GL_GEOMETRY_SHADER_OES:
4590 {
4591 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4592 }
4593 default:
4594 {
4595 UNREACHABLE();
4596 return new TStorageQualifierWrapper(EvqLast, loc);
4597 }
Olli Etuahocce89652017-06-19 16:04:09 +03004598 }
Olli Etuahocce89652017-06-19 16:04:09 +03004599}
4600
4601TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4602{
4603 if (declaringFunction())
4604 {
4605 return new TStorageQualifierWrapper(EvqOut, loc);
4606 }
Shaob5cc1192017-07-06 10:47:20 +08004607 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004608 {
Shaob5cc1192017-07-06 10:47:20 +08004609 case GL_VERTEX_SHADER:
4610 {
4611 if (mShaderVersion < 300)
4612 {
4613 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4614 }
4615 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4616 }
4617 case GL_FRAGMENT_SHADER:
4618 {
4619 if (mShaderVersion < 300)
4620 {
4621 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4622 }
4623 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4624 }
4625 case GL_COMPUTE_SHADER:
4626 {
4627 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4628 return new TStorageQualifierWrapper(EvqLast, loc);
4629 }
4630 case GL_GEOMETRY_SHADER_OES:
4631 {
4632 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4633 }
4634 default:
4635 {
4636 UNREACHABLE();
4637 return new TStorageQualifierWrapper(EvqLast, loc);
4638 }
Olli Etuahocce89652017-06-19 16:04:09 +03004639 }
Olli Etuahocce89652017-06-19 16:04:09 +03004640}
4641
4642TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4643{
4644 if (!declaringFunction())
4645 {
4646 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4647 }
4648 return new TStorageQualifierWrapper(EvqInOut, loc);
4649}
4650
Jamie Madillb98c3a82015-07-23 14:26:04 -04004651TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004652 TLayoutQualifier rightQualifier,
4653 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004654{
Martin Radevc28888b2016-07-22 15:27:42 +03004655 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004656 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004657}
4658
Olli Etuahocce89652017-06-19 16:04:09 +03004659TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4660{
4661 checkIsNotReserved(loc, *identifier);
4662 TType *type = new TType(EbtVoid, EbpUndefined);
4663 return new TField(type, identifier, loc);
4664}
4665
4666TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4667 const TSourceLoc &loc,
Olli Etuaho4ddae352017-10-26 16:20:18 +03004668 unsigned int arraySize,
Olli Etuahocce89652017-06-19 16:04:09 +03004669 const TSourceLoc &arraySizeLoc)
4670{
4671 checkIsNotReserved(loc, *identifier);
4672
4673 TType *type = new TType(EbtVoid, EbpUndefined);
Olli Etuaho4ddae352017-10-26 16:20:18 +03004674 type->makeArray(arraySize);
Olli Etuahocce89652017-06-19 16:04:09 +03004675
4676 return new TField(type, identifier, loc);
4677}
4678
Olli Etuaho722bfb52017-10-26 17:00:11 +03004679void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4680 const TFieldList::const_iterator end,
4681 const TString &name,
4682 const TSourceLoc &location)
4683{
4684 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4685 {
4686 if ((*fieldIter)->name() == name)
4687 {
4688 error(location, "duplicate field name in structure", name.c_str());
4689 }
4690 }
4691}
4692
4693TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4694{
4695 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4696 ++fieldIter)
4697 {
4698 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4699 location);
4700 }
4701 return fields;
4702}
4703
Olli Etuaho4de340a2016-12-16 09:32:03 +00004704TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4705 const TFieldList *newlyAddedFields,
4706 const TSourceLoc &location)
4707{
4708 for (TField *field : *newlyAddedFields)
4709 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004710 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4711 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004712 processedFields->push_back(field);
4713 }
4714 return processedFields;
4715}
4716
Martin Radev70866b82016-07-22 15:27:42 +03004717TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4718 const TTypeQualifierBuilder &typeQualifierBuilder,
4719 TPublicType *typeSpecifier,
4720 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004721{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004722 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004723
Martin Radev70866b82016-07-22 15:27:42 +03004724 typeSpecifier->qualifier = typeQualifier.qualifier;
4725 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004726 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004727 typeSpecifier->invariant = typeQualifier.invariant;
4728 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304729 {
Martin Radev70866b82016-07-22 15:27:42 +03004730 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004731 }
Martin Radev70866b82016-07-22 15:27:42 +03004732 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004733}
4734
Jamie Madillb98c3a82015-07-23 14:26:04 -04004735TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004736 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004737{
Martin Radev4a9cd802016-09-01 16:51:51 +03004738 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4739 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004740
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004741 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4742 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004743
Martin Radev4a9cd802016-09-01 16:51:51 +03004744 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004745
Olli Etuaho55bde912017-10-25 13:41:13 +03004746 for (TField *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304747 {
Olli Etuaho55bde912017-10-25 13:41:13 +03004748 auto declaratorArraySizes = declarator->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004749 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004750 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304751 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004752 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004753 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004754
Olli Etuaho55bde912017-10-25 13:41:13 +03004755 TType *type = declarator->type();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004756 *type = TType(typeSpecifier);
4757 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304758 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004759 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004760 }
Olli Etuaho55bde912017-10-25 13:41:13 +03004761 checkIsNotUnsizedArray(typeSpecifier.getLine(),
4762 "array members of structs must specify a size",
4763 declarator->name().c_str(), type);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004764
Olli Etuaho55bde912017-10-25 13:41:13 +03004765 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *declarator);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004766 }
4767
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004768 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004769}
4770
Martin Radev4a9cd802016-09-01 16:51:51 +03004771TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4772 const TSourceLoc &nameLine,
4773 const TString *structName,
4774 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004775{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004776 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004777
Jamie Madill9b820842015-02-12 10:40:10 -05004778 // Store a bool in the struct if we're at global scope, to allow us to
4779 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004780 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004781
Jamie Madill98493dd2013-07-08 14:39:03 -04004782 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004783 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004784 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004785 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304786 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004787 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004788 }
4789 }
4790
4791 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004792 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004793 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004794 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004795 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004796 switch (qualifier)
4797 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004798 case EvqGlobal:
4799 case EvqTemporary:
4800 break;
4801 default:
4802 error(field.line(), "invalid qualifier on struct member",
4803 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004804 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004805 }
Martin Radev70866b82016-07-22 15:27:42 +03004806 if (field.type()->isInvariant())
4807 {
4808 error(field.line(), "invalid qualifier on struct member", "invariant");
4809 }
jchen104cdac9e2017-05-08 11:01:20 +08004810 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4811 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004812 {
4813 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4814 }
4815
Olli Etuaho43364892017-02-13 16:00:12 +00004816 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4817
4818 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004819
4820 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004821 }
4822
Martin Radev4a9cd802016-09-01 16:51:51 +03004823 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004824 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004825 exitStructDeclaration();
4826
Martin Radev4a9cd802016-09-01 16:51:51 +03004827 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004828}
4829
Jamie Madillb98c3a82015-07-23 14:26:04 -04004830TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004831 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004832 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004833{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004834 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004835 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004836 init->isVector())
4837 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004838 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4839 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004840 return nullptr;
4841 }
4842
Olli Etuaho923ecef2017-10-11 12:01:38 +03004843 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004844 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004845 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004846 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004847 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004848 }
4849
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004850 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4851 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004852 return node;
4853}
4854
4855TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4856{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004857 if (mSwitchNestingLevel == 0)
4858 {
4859 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004860 return nullptr;
4861 }
4862 if (condition == nullptr)
4863 {
4864 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004865 return nullptr;
4866 }
4867 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004868 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004869 {
4870 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004871 }
4872 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004873 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4874 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4875 // fold in case labels.
4876 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004877 {
4878 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004879 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004880 TIntermCase *node = new TIntermCase(condition);
4881 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004882 return node;
4883}
4884
4885TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4886{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004887 if (mSwitchNestingLevel == 0)
4888 {
4889 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004890 return nullptr;
4891 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004892 TIntermCase *node = new TIntermCase(nullptr);
4893 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004894 return node;
4895}
4896
Jamie Madillb98c3a82015-07-23 14:26:04 -04004897TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4898 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004899 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004900{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004901 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004902
4903 switch (op)
4904 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004905 case EOpLogicalNot:
4906 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4907 child->isVector())
4908 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004909 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004910 return nullptr;
4911 }
4912 break;
4913 case EOpBitwiseNot:
4914 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4915 child->isMatrix() || child->isArray())
4916 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004917 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004918 return nullptr;
4919 }
4920 break;
4921 case EOpPostIncrement:
4922 case EOpPreIncrement:
4923 case EOpPostDecrement:
4924 case EOpPreDecrement:
4925 case EOpNegative:
4926 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004927 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4928 child->getBasicType() == EbtBool || child->isArray() ||
4929 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004930 {
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 // Operators for built-ins are already type checked against their prototype.
4935 default:
4936 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004937 }
4938
Jiajia Qinbc585152017-06-23 15:42:17 +08004939 if (child->getMemoryQualifier().writeonly)
4940 {
4941 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4942 return nullptr;
4943 }
4944
Olli Etuahof119a262016-08-19 15:54:22 +03004945 TIntermUnary *node = new TIntermUnary(op, child);
4946 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004947
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004948 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004949}
4950
Olli Etuaho09b22472015-02-11 11:47:26 +02004951TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4952{
Olli Etuahocce89652017-06-19 16:04:09 +03004953 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004954 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004955 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004956 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004957 return child;
4958 }
4959 return node;
4960}
4961
Jamie Madillb98c3a82015-07-23 14:26:04 -04004962TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4963 TIntermTyped *child,
4964 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004965{
Olli Etuaho856c4972016-08-08 11:38:39 +03004966 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004967 return addUnaryMath(op, child, loc);
4968}
4969
Jamie Madillb98c3a82015-07-23 14:26:04 -04004970bool TParseContext::binaryOpCommonCheck(TOperator op,
4971 TIntermTyped *left,
4972 TIntermTyped *right,
4973 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004974{
jchen10b4cf5652017-05-05 18:51:17 +08004975 // Check opaque types are not allowed to be operands in expressions other than array indexing
4976 // and structure member selection.
4977 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4978 {
4979 switch (op)
4980 {
4981 case EOpIndexDirect:
4982 case EOpIndexIndirect:
4983 break;
4984 case EOpIndexDirectStruct:
4985 UNREACHABLE();
4986
4987 default:
4988 error(loc, "Invalid operation for variables with an opaque type",
4989 GetOperatorString(op));
4990 return false;
4991 }
4992 }
jchen10cc2a10e2017-05-03 14:05:12 +08004993
Jiajia Qinbc585152017-06-23 15:42:17 +08004994 if (right->getMemoryQualifier().writeonly)
4995 {
4996 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4997 return false;
4998 }
4999
5000 if (left->getMemoryQualifier().writeonly)
5001 {
5002 switch (op)
5003 {
5004 case EOpAssign:
5005 case EOpInitialize:
5006 case EOpIndexDirect:
5007 case EOpIndexIndirect:
5008 case EOpIndexDirectStruct:
5009 case EOpIndexDirectInterfaceBlock:
5010 break;
5011 default:
5012 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5013 return false;
5014 }
5015 }
5016
Olli Etuaho244be012016-08-18 15:26:02 +03005017 if (left->getType().getStruct() || right->getType().getStruct())
5018 {
5019 switch (op)
5020 {
5021 case EOpIndexDirectStruct:
5022 ASSERT(left->getType().getStruct());
5023 break;
5024 case EOpEqual:
5025 case EOpNotEqual:
5026 case EOpAssign:
5027 case EOpInitialize:
5028 if (left->getType() != right->getType())
5029 {
5030 return false;
5031 }
5032 break;
5033 default:
5034 error(loc, "Invalid operation for structs", GetOperatorString(op));
5035 return false;
5036 }
5037 }
5038
Olli Etuaho94050052017-05-08 14:17:44 +03005039 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5040 {
5041 switch (op)
5042 {
5043 case EOpIndexDirectInterfaceBlock:
5044 ASSERT(left->getType().getInterfaceBlock());
5045 break;
5046 default:
5047 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5048 return false;
5049 }
5050 }
5051
Olli Etuahod6b14282015-03-17 14:31:35 +02005052 if (left->isArray() || right->isArray())
5053 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005054 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005055 {
5056 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5057 return false;
5058 }
5059
5060 if (left->isArray() != right->isArray())
5061 {
5062 error(loc, "array / non-array mismatch", GetOperatorString(op));
5063 return false;
5064 }
5065
5066 switch (op)
5067 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005068 case EOpEqual:
5069 case EOpNotEqual:
5070 case EOpAssign:
5071 case EOpInitialize:
5072 break;
5073 default:
5074 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5075 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005076 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005077 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03005078 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005079 {
5080 error(loc, "array size mismatch", GetOperatorString(op));
5081 return false;
5082 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005083 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005084
5085 // Check ops which require integer / ivec parameters
5086 bool isBitShift = false;
5087 switch (op)
5088 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005089 case EOpBitShiftLeft:
5090 case EOpBitShiftRight:
5091 case EOpBitShiftLeftAssign:
5092 case EOpBitShiftRightAssign:
5093 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5094 // check that the basic type is an integer type.
5095 isBitShift = true;
5096 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5097 {
5098 return false;
5099 }
5100 break;
5101 case EOpBitwiseAnd:
5102 case EOpBitwiseXor:
5103 case EOpBitwiseOr:
5104 case EOpBitwiseAndAssign:
5105 case EOpBitwiseXorAssign:
5106 case EOpBitwiseOrAssign:
5107 // It is enough to check the type of only one operand, since later it
5108 // is checked that the operand types match.
5109 if (!IsInteger(left->getBasicType()))
5110 {
5111 return false;
5112 }
5113 break;
5114 default:
5115 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005116 }
5117
5118 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5119 // So the basic type should usually match.
5120 if (!isBitShift && left->getBasicType() != right->getBasicType())
5121 {
5122 return false;
5123 }
5124
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005125 // Check that:
5126 // 1. Type sizes match exactly on ops that require that.
5127 // 2. Restrictions for structs that contain arrays or samplers are respected.
5128 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005129 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005130 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005131 case EOpAssign:
5132 case EOpInitialize:
5133 case EOpEqual:
5134 case EOpNotEqual:
5135 // ESSL 1.00 sections 5.7, 5.8, 5.9
5136 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5137 {
5138 error(loc, "undefined operation for structs containing arrays",
5139 GetOperatorString(op));
5140 return false;
5141 }
5142 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5143 // we interpret the spec so that this extends to structs containing samplers,
5144 // similarly to ESSL 1.00 spec.
5145 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5146 left->getType().isStructureContainingSamplers())
5147 {
5148 error(loc, "undefined operation for structs containing samplers",
5149 GetOperatorString(op));
5150 return false;
5151 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005152
Olli Etuahoe1805592017-01-02 16:41:20 +00005153 if ((left->getNominalSize() != right->getNominalSize()) ||
5154 (left->getSecondarySize() != right->getSecondarySize()))
5155 {
5156 error(loc, "dimension mismatch", GetOperatorString(op));
5157 return false;
5158 }
5159 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005160 case EOpLessThan:
5161 case EOpGreaterThan:
5162 case EOpLessThanEqual:
5163 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005164 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005165 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005166 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005167 return false;
5168 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005169 break;
5170 case EOpAdd:
5171 case EOpSub:
5172 case EOpDiv:
5173 case EOpIMod:
5174 case EOpBitShiftLeft:
5175 case EOpBitShiftRight:
5176 case EOpBitwiseAnd:
5177 case EOpBitwiseXor:
5178 case EOpBitwiseOr:
5179 case EOpAddAssign:
5180 case EOpSubAssign:
5181 case EOpDivAssign:
5182 case EOpIModAssign:
5183 case EOpBitShiftLeftAssign:
5184 case EOpBitShiftRightAssign:
5185 case EOpBitwiseAndAssign:
5186 case EOpBitwiseXorAssign:
5187 case EOpBitwiseOrAssign:
5188 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5189 {
5190 return false;
5191 }
5192
5193 // Are the sizes compatible?
5194 if (left->getNominalSize() != right->getNominalSize() ||
5195 left->getSecondarySize() != right->getSecondarySize())
5196 {
5197 // If the nominal sizes of operands do not match:
5198 // One of them must be a scalar.
5199 if (!left->isScalar() && !right->isScalar())
5200 return false;
5201
5202 // In the case of compound assignment other than multiply-assign,
5203 // the right side needs to be a scalar. Otherwise a vector/matrix
5204 // would be assigned to a scalar. A scalar can't be shifted by a
5205 // vector either.
5206 if (!right->isScalar() &&
5207 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5208 return false;
5209 }
5210 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005211 default:
5212 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005213 }
5214
Olli Etuahod6b14282015-03-17 14:31:35 +02005215 return true;
5216}
5217
Olli Etuaho1dded802016-08-18 18:13:13 +03005218bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5219 const TType &left,
5220 const TType &right)
5221{
5222 switch (op)
5223 {
5224 case EOpMul:
5225 case EOpMulAssign:
5226 return left.getNominalSize() == right.getNominalSize() &&
5227 left.getSecondarySize() == right.getSecondarySize();
5228 case EOpVectorTimesScalar:
5229 return true;
5230 case EOpVectorTimesScalarAssign:
5231 ASSERT(!left.isMatrix() && !right.isMatrix());
5232 return left.isVector() && !right.isVector();
5233 case EOpVectorTimesMatrix:
5234 return left.getNominalSize() == right.getRows();
5235 case EOpVectorTimesMatrixAssign:
5236 ASSERT(!left.isMatrix() && right.isMatrix());
5237 return left.isVector() && left.getNominalSize() == right.getRows() &&
5238 left.getNominalSize() == right.getCols();
5239 case EOpMatrixTimesVector:
5240 return left.getCols() == right.getNominalSize();
5241 case EOpMatrixTimesScalar:
5242 return true;
5243 case EOpMatrixTimesScalarAssign:
5244 ASSERT(left.isMatrix() && !right.isMatrix());
5245 return !right.isVector();
5246 case EOpMatrixTimesMatrix:
5247 return left.getCols() == right.getRows();
5248 case EOpMatrixTimesMatrixAssign:
5249 ASSERT(left.isMatrix() && right.isMatrix());
5250 // We need to check two things:
5251 // 1. The matrix multiplication step is valid.
5252 // 2. The result will have the same number of columns as the lvalue.
5253 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5254
5255 default:
5256 UNREACHABLE();
5257 return false;
5258 }
5259}
5260
Jamie Madillb98c3a82015-07-23 14:26:04 -04005261TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5262 TIntermTyped *left,
5263 TIntermTyped *right,
5264 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005265{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005266 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005267 return nullptr;
5268
Olli Etuahofc1806e2015-03-17 13:03:11 +02005269 switch (op)
5270 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005271 case EOpEqual:
5272 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005273 case EOpLessThan:
5274 case EOpGreaterThan:
5275 case EOpLessThanEqual:
5276 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005277 break;
5278 case EOpLogicalOr:
5279 case EOpLogicalXor:
5280 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005281 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5282 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005283 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005284 {
5285 return nullptr;
5286 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005287 // Basic types matching should have been already checked.
5288 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005289 break;
5290 case EOpAdd:
5291 case EOpSub:
5292 case EOpDiv:
5293 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005294 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5295 !right->getType().getStruct());
5296 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005297 {
5298 return nullptr;
5299 }
5300 break;
5301 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005302 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5303 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005304 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005305 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005306 {
5307 return nullptr;
5308 }
5309 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005310 default:
5311 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005312 }
5313
Olli Etuaho1dded802016-08-18 18:13:13 +03005314 if (op == EOpMul)
5315 {
5316 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5317 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5318 {
5319 return nullptr;
5320 }
5321 }
5322
Olli Etuaho3fdec912016-08-18 15:08:06 +03005323 TIntermBinary *node = new TIntermBinary(op, left, right);
5324 node->setLine(loc);
5325
Olli Etuaho3fdec912016-08-18 15:08:06 +03005326 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005327 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005328}
5329
Jamie Madillb98c3a82015-07-23 14:26:04 -04005330TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5331 TIntermTyped *left,
5332 TIntermTyped *right,
5333 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005334{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005335 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005336 if (node == 0)
5337 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005338 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5339 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005340 return left;
5341 }
5342 return node;
5343}
5344
Jamie Madillb98c3a82015-07-23 14:26:04 -04005345TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5346 TIntermTyped *left,
5347 TIntermTyped *right,
5348 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005349{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005350 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005351 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005352 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005353 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5354 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005355 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005356 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005357 }
5358 return node;
5359}
5360
Olli Etuaho13389b62016-10-16 11:48:18 +01005361TIntermBinary *TParseContext::createAssign(TOperator op,
5362 TIntermTyped *left,
5363 TIntermTyped *right,
5364 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005365{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005366 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005367 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005368 if (op == EOpMulAssign)
5369 {
5370 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5371 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5372 {
5373 return nullptr;
5374 }
5375 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005376 TIntermBinary *node = new TIntermBinary(op, left, right);
5377 node->setLine(loc);
5378
Olli Etuaho3fdec912016-08-18 15:08:06 +03005379 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005380 }
5381 return nullptr;
5382}
5383
Jamie Madillb98c3a82015-07-23 14:26:04 -04005384TIntermTyped *TParseContext::addAssign(TOperator op,
5385 TIntermTyped *left,
5386 TIntermTyped *right,
5387 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005388{
Olli Etuahocce89652017-06-19 16:04:09 +03005389 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005390 TIntermTyped *node = createAssign(op, left, right, loc);
5391 if (node == nullptr)
5392 {
5393 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005394 return left;
5395 }
5396 return node;
5397}
5398
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005399TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5400 TIntermTyped *right,
5401 const TSourceLoc &loc)
5402{
Corentin Wallez0d959252016-07-12 17:26:32 -04005403 // WebGL2 section 5.26, the following results in an error:
5404 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005405 if (mShaderSpec == SH_WEBGL2_SPEC &&
5406 (left->isArray() || left->getBasicType() == EbtVoid ||
5407 left->getType().isStructureContainingArrays() || right->isArray() ||
5408 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005409 {
5410 error(loc,
5411 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5412 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005413 }
5414
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005415 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5416 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5417 commaNode->getTypePointer()->setQualifier(resultQualifier);
5418 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005419}
5420
Olli Etuaho49300862015-02-20 14:54:49 +02005421TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5422{
5423 switch (op)
5424 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005425 case EOpContinue:
5426 if (mLoopNestingLevel <= 0)
5427 {
5428 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005429 }
5430 break;
5431 case EOpBreak:
5432 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5433 {
5434 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005435 }
5436 break;
5437 case EOpReturn:
5438 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5439 {
5440 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005441 }
5442 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005443 case EOpKill:
5444 if (mShaderType != GL_FRAGMENT_SHADER)
5445 {
5446 error(loc, "discard supported in fragment shaders only", "discard");
5447 }
5448 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005449 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005450 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005451 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005452 }
Olli Etuahocce89652017-06-19 16:04:09 +03005453 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005454}
5455
Jamie Madillb98c3a82015-07-23 14:26:04 -04005456TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005457 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005458 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005459{
Olli Etuahocce89652017-06-19 16:04:09 +03005460 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005461 {
Olli Etuahocce89652017-06-19 16:04:09 +03005462 ASSERT(op == EOpReturn);
5463 mFunctionReturnsValue = true;
5464 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5465 {
5466 error(loc, "void function cannot return a value", "return");
5467 }
5468 else if (*mCurrentFunctionType != expression->getType())
5469 {
5470 error(loc, "function return is not matching type:", "return");
5471 }
Olli Etuaho49300862015-02-20 14:54:49 +02005472 }
Olli Etuahocce89652017-06-19 16:04:09 +03005473 TIntermBranch *node = new TIntermBranch(op, expression);
5474 node->setLine(loc);
5475 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005476}
5477
Martin Radev84aa2dc2017-09-11 15:51:02 +03005478void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5479{
5480 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5481 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5482 bool isTextureGather = (name == "textureGather");
5483 bool isTextureGatherOffset = (name == "textureGatherOffset");
5484 if (isTextureGather || isTextureGatherOffset)
5485 {
5486 TIntermNode *componentNode = nullptr;
5487 TIntermSequence *arguments = functionCall->getSequence();
5488 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5489 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5490 ASSERT(sampler != nullptr);
5491 switch (sampler->getBasicType())
5492 {
5493 case EbtSampler2D:
5494 case EbtISampler2D:
5495 case EbtUSampler2D:
5496 case EbtSampler2DArray:
5497 case EbtISampler2DArray:
5498 case EbtUSampler2DArray:
5499 if ((isTextureGather && arguments->size() == 3u) ||
5500 (isTextureGatherOffset && arguments->size() == 4u))
5501 {
5502 componentNode = arguments->back();
5503 }
5504 break;
5505 case EbtSamplerCube:
5506 case EbtISamplerCube:
5507 case EbtUSamplerCube:
5508 ASSERT(!isTextureGatherOffset);
5509 if (arguments->size() == 3u)
5510 {
5511 componentNode = arguments->back();
5512 }
5513 break;
5514 case EbtSampler2DShadow:
5515 case EbtSampler2DArrayShadow:
5516 case EbtSamplerCubeShadow:
5517 break;
5518 default:
5519 UNREACHABLE();
5520 break;
5521 }
5522 if (componentNode)
5523 {
5524 const TIntermConstantUnion *componentConstantUnion =
5525 componentNode->getAsConstantUnion();
5526 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5527 {
5528 error(functionCall->getLine(), "Texture component must be a constant expression",
5529 name.c_str());
5530 }
5531 else
5532 {
5533 int component = componentConstantUnion->getIConst(0);
5534 if (component < 0 || component > 3)
5535 {
5536 error(functionCall->getLine(), "Component must be in the range [0;3]",
5537 name.c_str());
5538 }
5539 }
5540 }
5541 }
5542}
5543
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005544void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5545{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005546 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005547 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005548 TIntermNode *offset = nullptr;
5549 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005550 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005551 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5552 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5553 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005554 {
5555 offset = arguments->back();
5556 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005557 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005558 {
5559 // A bias parameter might follow the offset parameter.
5560 ASSERT(arguments->size() >= 3);
5561 offset = (*arguments)[2];
5562 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005563 else if (name == "textureGatherOffset")
5564 {
5565 ASSERT(arguments->size() >= 3u);
5566 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5567 ASSERT(sampler != nullptr);
5568 switch (sampler->getBasicType())
5569 {
5570 case EbtSampler2D:
5571 case EbtISampler2D:
5572 case EbtUSampler2D:
5573 case EbtSampler2DArray:
5574 case EbtISampler2DArray:
5575 case EbtUSampler2DArray:
5576 offset = (*arguments)[2];
5577 break;
5578 case EbtSampler2DShadow:
5579 case EbtSampler2DArrayShadow:
5580 offset = (*arguments)[3];
5581 break;
5582 default:
5583 UNREACHABLE();
5584 break;
5585 }
5586 useTextureGatherOffsetConstraints = true;
5587 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005588 if (offset != nullptr)
5589 {
5590 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5591 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5592 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005593 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005594 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005595 }
5596 else
5597 {
5598 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5599 size_t size = offsetConstantUnion->getType().getObjectSize();
5600 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005601 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5602 : mMinProgramTexelOffset;
5603 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5604 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005605 for (size_t i = 0u; i < size; ++i)
5606 {
5607 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005608 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005609 {
5610 std::stringstream tokenStream;
5611 tokenStream << offsetValue;
5612 std::string token = tokenStream.str();
5613 error(offset->getLine(), "Texture offset value out of valid range",
5614 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005615 }
5616 }
5617 }
5618 }
5619}
5620
Jiajia Qina3106c52017-11-03 09:39:39 +08005621void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5622{
5623 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5624 if (IsAtomicBuiltin(name))
5625 {
5626 TIntermSequence *arguments = functionCall->getSequence();
5627 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5628
5629 if (IsBufferOrSharedVariable(memNode))
5630 {
5631 return;
5632 }
5633
5634 while (memNode->getAsBinaryNode())
5635 {
5636 memNode = memNode->getAsBinaryNode()->getLeft();
5637 if (IsBufferOrSharedVariable(memNode))
5638 {
5639 return;
5640 }
5641 }
5642
5643 error(memNode->getLine(),
5644 "The value passed to the mem argument of an atomic memory function does not "
5645 "correspond to a buffer or shared variable.",
5646 functionCall->getFunctionSymbolInfo()->getName().c_str());
5647 }
5648}
5649
Martin Radev2cc85b32016-08-05 16:22:53 +03005650// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5651void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5652{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005653 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005654 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5655
5656 if (name.compare(0, 5, "image") == 0)
5657 {
5658 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005659 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005660
Olli Etuaho485eefd2017-02-14 17:40:06 +00005661 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005662
5663 if (name.compare(5, 5, "Store") == 0)
5664 {
5665 if (memoryQualifier.readonly)
5666 {
5667 error(imageNode->getLine(),
5668 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005669 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005670 }
5671 }
5672 else if (name.compare(5, 4, "Load") == 0)
5673 {
5674 if (memoryQualifier.writeonly)
5675 {
5676 error(imageNode->getLine(),
5677 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005678 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005679 }
5680 }
5681 }
5682}
5683
5684// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5685void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5686 const TFunction *functionDefinition,
5687 const TIntermAggregate *functionCall)
5688{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005689 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005690
5691 const TIntermSequence &arguments = *functionCall->getSequence();
5692
5693 ASSERT(functionDefinition->getParamCount() == arguments.size());
5694
5695 for (size_t i = 0; i < arguments.size(); ++i)
5696 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005697 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5698 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005699 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5700 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5701
5702 if (IsImage(functionArgumentType.getBasicType()))
5703 {
5704 const TMemoryQualifier &functionArgumentMemoryQualifier =
5705 functionArgumentType.getMemoryQualifier();
5706 const TMemoryQualifier &functionParameterMemoryQualifier =
5707 functionParameterType.getMemoryQualifier();
5708 if (functionArgumentMemoryQualifier.readonly &&
5709 !functionParameterMemoryQualifier.readonly)
5710 {
5711 error(functionCall->getLine(),
5712 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005713 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005714 }
5715
5716 if (functionArgumentMemoryQualifier.writeonly &&
5717 !functionParameterMemoryQualifier.writeonly)
5718 {
5719 error(functionCall->getLine(),
5720 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005721 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005722 }
Martin Radev049edfa2016-11-11 14:35:37 +02005723
5724 if (functionArgumentMemoryQualifier.coherent &&
5725 !functionParameterMemoryQualifier.coherent)
5726 {
5727 error(functionCall->getLine(),
5728 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005729 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005730 }
5731
5732 if (functionArgumentMemoryQualifier.volatileQualifier &&
5733 !functionParameterMemoryQualifier.volatileQualifier)
5734 {
5735 error(functionCall->getLine(),
5736 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005737 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005738 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005739 }
5740 }
5741}
5742
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005743TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005744{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005745 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005746}
5747
5748TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005749 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005750 TIntermNode *thisNode,
5751 const TSourceLoc &loc)
5752{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005753 if (thisNode != nullptr)
5754 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005755 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005756 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005757
5758 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005759 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005760 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005761 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005762 }
5763 else
5764 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005765 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005766 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5767 }
5768}
5769
5770TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5771 TIntermSequence *arguments,
5772 TIntermNode *thisNode,
5773 const TSourceLoc &loc)
5774{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005775 TIntermTyped *typedThis = thisNode->getAsTyped();
5776 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5777 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5778 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5779 // So accessing fnCall->getName() below is safe.
5780 if (fnCall->getName() != "length")
5781 {
5782 error(loc, "invalid method", fnCall->getName().c_str());
5783 }
5784 else if (!arguments->empty())
5785 {
5786 error(loc, "method takes no parameters", "length");
5787 }
5788 else if (typedThis == nullptr || !typedThis->isArray())
5789 {
5790 error(loc, "length can only be called on arrays", "length");
5791 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005792 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5793 mGeometryShaderInputPrimitiveType == EptUndefined)
5794 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005795 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005796 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5797 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005798 else
5799 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005800 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5801 node->setLine(loc);
5802 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005803 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005804 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005805}
5806
5807TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5808 TIntermSequence *arguments,
5809 const TSourceLoc &loc)
5810{
5811 // First find by unmangled name to check whether the function name has been
5812 // hidden by a variable name or struct typename.
5813 // If a function is found, check for one with a matching argument list.
5814 bool builtIn;
5815 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5816 if (symbol != nullptr && !symbol->isFunction())
5817 {
5818 error(loc, "function name expected", fnCall->getName().c_str());
5819 }
5820 else
5821 {
5822 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5823 mShaderVersion, &builtIn);
5824 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005825 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005826 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5827 }
5828 else
5829 {
5830 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005831 //
5832 // A declared function.
5833 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005834 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005835 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005836 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005837 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005838 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005839 if (builtIn && op != EOpNull)
5840 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005841 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005842 if (fnCandidate->getParamCount() == 1)
5843 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005844 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005845 TIntermNode *unaryParamNode = arguments->front();
5846 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005847 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005848 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005849 }
5850 else
5851 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005852 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005853 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005854 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005855
5856 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005857 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305858
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005859 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305860 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005861 // See if we can constant fold a built-in. Note that this may be possible
5862 // even if it is not const-qualified.
5863 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305864 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005865 else
5866 {
5867 return callNode;
5868 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005869 }
5870 }
5871 else
5872 {
5873 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005874 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005875
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005876 // If builtIn == false, the function is user defined - could be an overloaded
5877 // built-in as well.
5878 // if builtIn == true, it's a builtIn function with no op associated with it.
5879 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005880 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005881 {
Olli Etuahofe486322017-03-21 09:30:54 +00005882 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005883 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005884 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005885 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005886 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005887 }
5888 else
5889 {
Olli Etuahofe486322017-03-21 09:30:54 +00005890 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005891 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005892 }
5893
Jiajia Qinbc585152017-06-23 15:42:17 +08005894 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005895
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005896 callNode->setLine(loc);
5897
5898 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005899 }
5900 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005901 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005902
5903 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005904 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005905}
5906
Jamie Madillb98c3a82015-07-23 14:26:04 -04005907TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005908 TIntermTyped *trueExpression,
5909 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005910 const TSourceLoc &loc)
5911{
Olli Etuaho56229f12017-07-10 14:16:33 +03005912 if (!checkIsScalarBool(loc, cond))
5913 {
5914 return falseExpression;
5915 }
Olli Etuaho52901742015-04-15 13:42:45 +03005916
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005917 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005918 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005919 std::stringstream reasonStream;
5920 reasonStream << "mismatching ternary operator operand types '"
5921 << trueExpression->getCompleteString() << " and '"
5922 << falseExpression->getCompleteString() << "'";
5923 std::string reason = reasonStream.str();
5924 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005925 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005926 }
Olli Etuahode318b22016-10-25 16:18:25 +01005927 if (IsOpaqueType(trueExpression->getBasicType()))
5928 {
5929 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005930 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005931 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5932 // Note that structs containing opaque types don't need to be checked as structs are
5933 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005934 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005935 return falseExpression;
5936 }
5937
Jiajia Qinbc585152017-06-23 15:42:17 +08005938 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5939 falseExpression->getMemoryQualifier().writeonly)
5940 {
5941 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5942 return falseExpression;
5943 }
5944
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005945 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005946 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005947 // ESSL 3.00.6 section 5.7:
5948 // Ternary operator support is optional for arrays. No certainty that it works across all
5949 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5950 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005951 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005952 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005953 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005954 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005955 }
Olli Etuaho94050052017-05-08 14:17:44 +03005956 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5957 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005958 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005959 return falseExpression;
5960 }
5961
Corentin Wallez0d959252016-07-12 17:26:32 -04005962 // WebGL2 section 5.26, the following results in an error:
5963 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005964 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005965 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005966 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005967 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005968 }
5969
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005970 // Note that the node resulting from here can be a constant union without being qualified as
5971 // constant.
5972 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5973 node->setLine(loc);
5974
5975 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005976}
Olli Etuaho49300862015-02-20 14:54:49 +02005977
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005978//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005979// Parse an array of strings using yyparse.
5980//
5981// Returns 0 for success.
5982//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005983int PaParseStrings(size_t count,
5984 const char *const string[],
5985 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305986 TParseContext *context)
5987{
Yunchao He4f285442017-04-21 12:15:49 +08005988 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005989 return 1;
5990
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005991 if (glslang_initialize(context))
5992 return 1;
5993
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005994 int error = glslang_scan(count, string, length, context);
5995 if (!error)
5996 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005997
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005998 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005999
alokp@chromium.org6b495712012-06-29 00:06:58 +00006000 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006001}
Jamie Madill45bcc782016-11-07 13:58:48 -05006002
6003} // namespace sh