blob: 7417ab74703a0bfa5598b5d6aedfcd29bd89c36d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
jchen104cdac9e2017-05-08 11:01:20 +080012#include "common/mathutil.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080017#include "compiler/translator/ValidateSwitch.h"
18#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030019#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000020
Jamie Madill45bcc782016-11-07 13:58:48 -050021namespace sh
22{
23
alokp@chromium.org8b851c62012-06-15 16:25:11 +000024///////////////////////////////////////////////////////////////////////
25//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026// Sub- vector and matrix fields
27//
28////////////////////////////////////////////////////////////////////////
29
Martin Radev2cc85b32016-08-05 16:22:53 +030030namespace
31{
32
33const int kWebGLMaxStructNesting = 4;
34
Jiajia Qina3106c52017-11-03 09:39:39 +080035const std::array<const char *, 8> kAtomicBuiltin = {{"atomicAdd", "atomicMin", "atomicMax",
36 "atomicAnd", "atomicOr", "atomicXor",
37 "atomicExchange", "atomicCompSwap"}};
38
39bool IsAtomicBuiltin(const TString &name)
40{
41 for (size_t i = 0; i < kAtomicBuiltin.size(); ++i)
42 {
43 if (name.compare(kAtomicBuiltin[i]) == 0)
44 {
45 return true;
46 }
47 }
48 return false;
49}
50
Olli Etuaho0f684632017-07-13 12:42:15 +030051bool ContainsSampler(const TStructure *structType);
52
Martin Radev2cc85b32016-08-05 16:22:53 +030053bool ContainsSampler(const TType &type)
54{
55 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030056 {
Martin Radev2cc85b32016-08-05 16:22:53 +030057 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030058 }
jchen10cc2a10e2017-05-03 14:05:12 +080059 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030060 {
Olli Etuaho0f684632017-07-13 12:42:15 +030061 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030062 }
63
64 return false;
65}
66
Olli Etuaho0f684632017-07-13 12:42:15 +030067bool ContainsSampler(const TStructure *structType)
68{
69 for (const auto &field : structType->fields())
70 {
71 if (ContainsSampler(*field->type()))
72 return true;
73 }
74 return false;
75}
76
Olli Etuaho485eefd2017-02-14 17:40:06 +000077// Get a token from an image argument to use as an error message token.
78const char *GetImageArgumentToken(TIntermTyped *imageNode)
79{
80 ASSERT(IsImage(imageNode->getBasicType()));
81 while (imageNode->getAsBinaryNode() &&
82 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
83 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
84 {
85 imageNode = imageNode->getAsBinaryNode()->getLeft();
86 }
87 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
88 if (imageSymbol)
89 {
90 return imageSymbol->getSymbol().c_str();
91 }
92 return "image";
93}
94
Olli Etuahocce89652017-06-19 16:04:09 +030095bool CanSetDefaultPrecisionOnType(const TPublicType &type)
96{
97 if (!SupportsPrecision(type.getBasicType()))
98 {
99 return false;
100 }
101 if (type.getBasicType() == EbtUInt)
102 {
103 // ESSL 3.00.4 section 4.5.4
104 return false;
105 }
106 if (type.isAggregate())
107 {
108 // Not allowed to set for aggregate types
109 return false;
110 }
111 return true;
112}
113
Jiawei Shaod8105a02017-08-08 09:54:36 +0800114// Map input primitive types to input array sizes in a geometry shader.
115GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
116{
117 switch (primitiveType)
118 {
119 case EptPoints:
120 return 1u;
121 case EptLines:
122 return 2u;
123 case EptTriangles:
124 return 3u;
125 case EptLinesAdjacency:
126 return 4u;
127 case EptTrianglesAdjacency:
128 return 6u;
129 default:
130 UNREACHABLE();
131 return 0u;
132 }
133}
134
Jiajia Qina3106c52017-11-03 09:39:39 +0800135bool IsBufferOrSharedVariable(TIntermTyped *var)
136{
137 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
138 var->getQualifier() == EvqShared)
139 {
140 return true;
141 }
142 return false;
143}
144
Martin Radev2cc85b32016-08-05 16:22:53 +0300145} // namespace
146
jchen104cdac9e2017-05-08 11:01:20 +0800147// This tracks each binding point's current default offset for inheritance of subsequent
148// variables using the same binding, and keeps offsets unique and non overlapping.
149// See GLSL ES 3.1, section 4.4.6.
150class TParseContext::AtomicCounterBindingState
151{
152 public:
153 AtomicCounterBindingState() : mDefaultOffset(0) {}
154 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
155 // newly inserted span.
156 int insertSpan(int start, size_t length)
157 {
158 gl::RangeI newSpan(start, start + static_cast<int>(length));
159 for (const auto &span : mSpans)
160 {
161 if (newSpan.intersects(span))
162 {
163 return -1;
164 }
165 }
166 mSpans.push_back(newSpan);
167 mDefaultOffset = newSpan.high();
168 return start;
169 }
170 // Inserts a new span starting from the default offset.
171 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
172 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
173
174 private:
175 int mDefaultOffset;
176 std::vector<gl::RangeI> mSpans;
177};
178
Jamie Madillacb4b812016-11-07 13:50:29 -0500179TParseContext::TParseContext(TSymbolTable &symt,
180 TExtensionBehavior &ext,
181 sh::GLenum type,
182 ShShaderSpec spec,
183 ShCompileOptions options,
184 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000185 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500186 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300187 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300188 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500189 mShaderType(type),
190 mShaderSpec(spec),
191 mCompileOptions(options),
192 mShaderVersion(100),
193 mTreeRoot(nullptr),
194 mLoopNestingLevel(0),
195 mStructNestingLevel(0),
196 mSwitchNestingLevel(0),
197 mCurrentFunctionType(nullptr),
198 mFunctionReturnsValue(false),
199 mChecksPrecisionErrors(checksPrecErrors),
200 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800201 mDefaultUniformMatrixPacking(EmpColumnMajor),
202 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
203 mDefaultBufferMatrixPacking(EmpColumnMajor),
204 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000205 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500206 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000207 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500208 mShaderVersion,
209 mShaderType,
210 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000211 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500212 mScanner(nullptr),
213 mUsesFragData(false),
214 mUsesFragColor(false),
215 mUsesSecondaryOutputs(false),
216 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
217 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300218 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
219 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500220 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500221 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000222 mNumViews(-1),
223 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000224 mMaxImageUnits(resources.MaxImageUnits),
225 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000226 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800227 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800228 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800229 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800230 mDeclaringFunction(false),
231 mGeometryShaderInputPrimitiveType(EptUndefined),
232 mGeometryShaderOutputPrimitiveType(EptUndefined),
233 mGeometryShaderInvocations(0),
234 mGeometryShaderMaxVertices(-1),
235 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800236 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800237 mGeometryShaderInputArraySize(0u)
Jamie Madillacb4b812016-11-07 13:50:29 -0500238{
Jamie Madillacb4b812016-11-07 13:50:29 -0500239}
240
jchen104cdac9e2017-05-08 11:01:20 +0800241TParseContext::~TParseContext()
242{
243}
244
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300245bool TParseContext::parseVectorFields(const TSourceLoc &line,
246 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400247 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300248 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300250 ASSERT(fieldOffsets);
251 size_t fieldCount = compString.size();
252 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530253 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000254 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000255 return false;
256 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300257 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 enum
260 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000261 exyzw,
262 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000263 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000264 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300266 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530267 {
268 switch (compString[i])
269 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400270 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300271 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400272 fieldSet[i] = exyzw;
273 break;
274 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300275 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400276 fieldSet[i] = ergba;
277 break;
278 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300279 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400280 fieldSet[i] = estpq;
281 break;
282 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300283 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400284 fieldSet[i] = exyzw;
285 break;
286 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300287 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400288 fieldSet[i] = ergba;
289 break;
290 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300291 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400292 fieldSet[i] = estpq;
293 break;
294 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300295 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400296 fieldSet[i] = exyzw;
297 break;
298 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300299 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400300 fieldSet[i] = ergba;
301 break;
302 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300303 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400304 fieldSet[i] = estpq;
305 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530306
Jamie Madillb98c3a82015-07-23 14:26:04 -0400307 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300308 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400309 fieldSet[i] = exyzw;
310 break;
311 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300312 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400313 fieldSet[i] = ergba;
314 break;
315 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300316 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400317 fieldSet[i] = estpq;
318 break;
319 default:
320 error(line, "illegal vector field selection", compString.c_str());
321 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000322 }
323 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300325 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530326 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300327 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530328 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400329 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 return false;
331 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332
Arun Patole7e7e68d2015-05-22 12:02:25 +0530333 if (i > 0)
334 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400335 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530336 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400337 error(line, "illegal - vector component fields not from the same set",
338 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000339 return false;
340 }
341 }
342 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000344 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345}
346
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347///////////////////////////////////////////////////////////////////////
348//
349// Errors
350//
351////////////////////////////////////////////////////////////////////////
352
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353//
354// Used by flex/bison to output all syntax and parsing errors.
355//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000356void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000358 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359}
360
Olli Etuaho4de340a2016-12-16 09:32:03 +0000361void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530362{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000363 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000364}
365
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200366void TParseContext::outOfRangeError(bool isError,
367 const TSourceLoc &loc,
368 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000369 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200370{
371 if (isError)
372 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000373 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200374 }
375 else
376 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000377 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200378 }
379}
380
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381//
382// Same error message for all places assignments don't work.
383//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530384void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000386 std::stringstream reasonStream;
387 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
388 std::string reason = reasonStream.str();
389 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390}
391
392//
393// Same error message for all places unary operations don't work.
394//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530395void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000397 std::stringstream reasonStream;
398 reasonStream << "wrong operand type - no operation '" << op
399 << "' exists that takes an operand of type " << operand
400 << " (or there is no acceptable conversion)";
401 std::string reason = reasonStream.str();
402 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403}
404
405//
406// Same error message for all binary operations don't work.
407//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400408void TParseContext::binaryOpError(const TSourceLoc &line,
409 const char *op,
410 TString left,
411 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000413 std::stringstream reasonStream;
414 reasonStream << "wrong operand types - no operation '" << op
415 << "' exists that takes a left-hand operand of type '" << left
416 << "' and a right operand of type '" << right
417 << "' (or there is no acceptable conversion)";
418 std::string reason = reasonStream.str();
419 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
Olli Etuaho856c4972016-08-08 11:38:39 +0300422void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
423 TPrecision precision,
424 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530425{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400426 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300427 return;
Martin Radev70866b82016-07-22 15:27:42 +0300428
429 if (precision != EbpUndefined && !SupportsPrecision(type))
430 {
431 error(line, "illegal type for precision qualifier", getBasicString(type));
432 }
433
Olli Etuaho183d7e22015-11-20 15:59:09 +0200434 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530435 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200436 switch (type)
437 {
438 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400439 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300440 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200441 case EbtInt:
442 case EbtUInt:
443 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400444 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300445 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200446 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800447 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200448 {
jchen10cc2a10e2017-05-03 14:05:12 +0800449 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300450 return;
451 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200452 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000453 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000454}
455
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456// Both test and if necessary, spit out an error, to see if the node is really
457// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300458bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500460 TIntermSymbol *symNode = node->getAsSymbolNode();
461 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100462 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
463
464 if (swizzleNode)
465 {
466 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
467 if (ok && swizzleNode->hasDuplicateOffsets())
468 {
469 error(line, " l-value of swizzle cannot have duplicate components", op);
470 return false;
471 }
472 return ok;
473 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474
Arun Patole7e7e68d2015-05-22 12:02:25 +0530475 if (binaryNode)
476 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400477 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530478 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400479 case EOpIndexDirect:
480 case EOpIndexIndirect:
481 case EOpIndexDirectStruct:
482 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300483 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400484 default:
485 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000486 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000487 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300488 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000489 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490
jchen10cc2a10e2017-05-03 14:05:12 +0800491 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530492 switch (node->getQualifier())
493 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400494 case EvqConst:
495 message = "can't modify a const";
496 break;
497 case EvqConstReadOnly:
498 message = "can't modify a const";
499 break;
500 case EvqAttribute:
501 message = "can't modify an attribute";
502 break;
503 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400504 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800505 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800506 case EvqFlatIn:
507 case EvqSmoothIn:
508 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400509 message = "can't modify an input";
510 break;
511 case EvqUniform:
512 message = "can't modify a uniform";
513 break;
514 case EvqVaryingIn:
515 message = "can't modify a varying";
516 break;
517 case EvqFragCoord:
518 message = "can't modify gl_FragCoord";
519 break;
520 case EvqFrontFacing:
521 message = "can't modify gl_FrontFacing";
522 break;
523 case EvqPointCoord:
524 message = "can't modify gl_PointCoord";
525 break;
Martin Radevb0883602016-08-04 17:48:58 +0300526 case EvqNumWorkGroups:
527 message = "can't modify gl_NumWorkGroups";
528 break;
529 case EvqWorkGroupSize:
530 message = "can't modify gl_WorkGroupSize";
531 break;
532 case EvqWorkGroupID:
533 message = "can't modify gl_WorkGroupID";
534 break;
535 case EvqLocalInvocationID:
536 message = "can't modify gl_LocalInvocationID";
537 break;
538 case EvqGlobalInvocationID:
539 message = "can't modify gl_GlobalInvocationID";
540 break;
541 case EvqLocalInvocationIndex:
542 message = "can't modify gl_LocalInvocationIndex";
543 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300544 case EvqViewIDOVR:
545 message = "can't modify gl_ViewID_OVR";
546 break;
Martin Radev802abe02016-08-04 17:48:32 +0300547 case EvqComputeIn:
548 message = "can't modify work group size variable";
549 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800550 case EvqPerVertexIn:
551 message = "can't modify any member in gl_in";
552 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800553 case EvqPrimitiveIDIn:
554 message = "can't modify gl_PrimitiveIDIn";
555 break;
556 case EvqInvocationID:
557 message = "can't modify gl_InvocationID";
558 break;
559 case EvqPrimitiveID:
560 if (mShaderType == GL_FRAGMENT_SHADER)
561 {
562 message = "can't modify gl_PrimitiveID in a fragment shader";
563 }
564 break;
565 case EvqLayer:
566 if (mShaderType == GL_FRAGMENT_SHADER)
567 {
568 message = "can't modify gl_Layer in a fragment shader";
569 }
570 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400571 default:
572 //
573 // Type that can't be written to?
574 //
575 if (node->getBasicType() == EbtVoid)
576 {
577 message = "can't modify void";
578 }
jchen10cc2a10e2017-05-03 14:05:12 +0800579 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400580 {
jchen10cc2a10e2017-05-03 14:05:12 +0800581 message = "can't modify a variable with type ";
582 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300583 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800584 else if (node->getMemoryQualifier().readonly)
585 {
586 message = "can't modify a readonly variable";
587 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000588 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000589
jchen10cc2a10e2017-05-03 14:05:12 +0800590 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530591 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000592 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000593
Olli Etuaho8a176262016-08-16 14:23:01 +0300594 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000597 //
598 // Everything else is okay, no error.
599 //
jchen10cc2a10e2017-05-03 14:05:12 +0800600 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300601 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000603 //
604 // If we get here, we have an error and a message.
605 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530606 if (symNode)
607 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000608 const char *symbol = symNode->getSymbol().c_str();
609 std::stringstream reasonStream;
610 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
611 std::string reason = reasonStream.str();
612 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000613 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530614 else
615 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000616 std::stringstream reasonStream;
617 reasonStream << "l-value required (" << message << ")";
618 std::string reason = reasonStream.str();
619 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000620 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000621
Olli Etuaho8a176262016-08-16 14:23:01 +0300622 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623}
624
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625// Both test, and if necessary spit out an error, to see if the node is really
626// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300627void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628{
Olli Etuaho383b7912016-08-05 11:22:59 +0300629 if (node->getQualifier() != EvqConst)
630 {
631 error(node->getLine(), "constant expression required", "");
632 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000633}
634
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635// Both test, and if necessary spit out an error, to see if the node is really
636// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300637void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000638{
Olli Etuaho383b7912016-08-05 11:22:59 +0300639 if (!node->isScalarInt())
640 {
641 error(node->getLine(), "integer expression required", token);
642 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643}
644
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645// Both test, and if necessary spit out an error, to see if we are currently
646// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800647bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648{
Olli Etuaho856c4972016-08-08 11:38:39 +0300649 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300650 {
651 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800652 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300653 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800654 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000655}
656
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300657// ESSL 3.00.5 sections 3.8 and 3.9.
658// If it starts "gl_" or contains two consecutive underscores, it's reserved.
659// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300660bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530662 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300663 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530664 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300665 error(line, reservedErrMsg, "gl_");
666 return false;
667 }
668 if (sh::IsWebGLBasedSpec(mShaderSpec))
669 {
670 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530671 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300672 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300673 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000674 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300675 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530676 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300677 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300678 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000679 }
680 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300681 if (identifier.find("__") != TString::npos)
682 {
683 error(line,
684 "identifiers containing two consecutive underscores (__) are reserved as "
685 "possible future keywords",
686 identifier.c_str());
687 return false;
688 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300689 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690}
691
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300692// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300693bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800694 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300695 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800697 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530698 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200699 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300700 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000701 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200702
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300703 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530704 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300705 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200706 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300707 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200708 {
jchen10cc2a10e2017-05-03 14:05:12 +0800709 std::string reason("cannot convert a variable with type ");
710 reason += getBasicString(argTyped->getBasicType());
711 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300712 return false;
713 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800714 else if (argTyped->getMemoryQualifier().writeonly)
715 {
716 error(line, "cannot convert a variable with writeonly", "constructor");
717 return false;
718 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200719 if (argTyped->getBasicType() == EbtVoid)
720 {
721 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300722 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200723 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725
Olli Etuaho856c4972016-08-08 11:38:39 +0300726 if (type.isArray())
727 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300728 // The size of an unsized constructor should already have been determined.
729 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300730 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300731 {
732 error(line, "array constructor needs one argument per array element", "constructor");
733 return false;
734 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300735 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
736 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800737 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300738 {
739 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300740 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500741 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300742 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500743 return false;
744 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300745 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300746 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000747 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300748 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300749 }
750 }
751 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300752 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300753 {
754 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300755 if (fields.size() != arguments->size())
756 {
757 error(line,
758 "Number of constructor parameters does not match the number of structure fields",
759 "constructor");
760 return false;
761 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300762
763 for (size_t i = 0; i < fields.size(); i++)
764 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800765 if (i >= arguments->size() ||
766 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300767 {
768 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000769 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300770 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300771 }
772 }
773 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300774 else
775 {
776 // We're constructing a scalar, vector, or matrix.
777
778 // Note: It's okay to have too many components available, but not okay to have unused
779 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
780 // there is an extra argument, so 'overFull' will become true.
781
782 size_t size = 0;
783 bool full = false;
784 bool overFull = false;
785 bool matrixArg = false;
786 for (TIntermNode *arg : *arguments)
787 {
788 const TIntermTyped *argTyped = arg->getAsTyped();
789 ASSERT(argTyped != nullptr);
790
Olli Etuaho487b63a2017-05-23 15:55:09 +0300791 if (argTyped->getBasicType() == EbtStruct)
792 {
793 error(line, "a struct cannot be used as a constructor argument for this type",
794 "constructor");
795 return false;
796 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300797 if (argTyped->getType().isArray())
798 {
799 error(line, "constructing from a non-dereferenced array", "constructor");
800 return false;
801 }
802 if (argTyped->getType().isMatrix())
803 {
804 matrixArg = true;
805 }
806
807 size += argTyped->getType().getObjectSize();
808 if (full)
809 {
810 overFull = true;
811 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300812 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300813 {
814 full = true;
815 }
816 }
817
818 if (type.isMatrix() && matrixArg)
819 {
820 if (arguments->size() != 1)
821 {
822 error(line, "constructing matrix from matrix can only take one argument",
823 "constructor");
824 return false;
825 }
826 }
827 else
828 {
829 if (size != 1 && size < type.getObjectSize())
830 {
831 error(line, "not enough data provided for construction", "constructor");
832 return false;
833 }
834 if (overFull)
835 {
836 error(line, "too many arguments", "constructor");
837 return false;
838 }
839 }
840 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300841
Olli Etuaho8a176262016-08-16 14:23:01 +0300842 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843}
844
Jamie Madillb98c3a82015-07-23 14:26:04 -0400845// This function checks to see if a void variable has been declared and raise an error message for
846// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847//
848// returns true in case of an error
849//
Olli Etuaho856c4972016-08-08 11:38:39 +0300850bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400851 const TString &identifier,
852 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300854 if (type == EbtVoid)
855 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000856 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300857 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300858 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859
Olli Etuaho8a176262016-08-16 14:23:01 +0300860 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
Jamie Madillb98c3a82015-07-23 14:26:04 -0400863// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300864// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300865bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300867 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530868 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000869 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300870 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530871 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300872 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873}
874
Jamie Madillb98c3a82015-07-23 14:26:04 -0400875// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300876// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300877void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878{
Martin Radev4a9cd802016-09-01 16:51:51 +0300879 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530880 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000881 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530882 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883}
884
jchen10cc2a10e2017-05-03 14:05:12 +0800885bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
886 const TTypeSpecifierNonArray &pType,
887 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530889 if (pType.type == EbtStruct)
890 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300891 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530892 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000893 std::stringstream reasonStream;
894 reasonStream << reason << " (structure contains a sampler)";
895 std::string reasonStr = reasonStream.str();
896 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300897 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000898 }
jchen10cc2a10e2017-05-03 14:05:12 +0800899 // only samplers need to be checked from structs, since other opaque types can't be struct
900 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300901 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530902 }
jchen10cc2a10e2017-05-03 14:05:12 +0800903 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530904 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000905 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300906 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000907 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908
Olli Etuaho8a176262016-08-16 14:23:01 +0300909 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910}
911
Olli Etuaho856c4972016-08-08 11:38:39 +0300912void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
913 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400914{
915 if (pType.layoutQualifier.location != -1)
916 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400917 error(line, "location must only be specified for a single input or output variable",
918 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400919 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400920}
921
Olli Etuaho856c4972016-08-08 11:38:39 +0300922void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
923 const TLayoutQualifier &layoutQualifier)
924{
925 if (layoutQualifier.location != -1)
926 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000927 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
928 if (mShaderVersion >= 310)
929 {
930 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800931 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000932 }
933 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300934 }
935}
936
Qin Jiajiaca68d982017-09-18 16:41:56 +0800937void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
938 const TLayoutBlockStorage &blockStorage,
939 const TQualifier &qualifier)
940{
941 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
942 {
943 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
944 }
945}
946
Martin Radev2cc85b32016-08-05 16:22:53 +0300947void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
948 TQualifier qualifier,
949 const TType &type)
950{
Martin Radev2cc85b32016-08-05 16:22:53 +0300951 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800952 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530953 {
jchen10cc2a10e2017-05-03 14:05:12 +0800954 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000955 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956}
957
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300959unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530961 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000962
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200963 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
964 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
965 // fold as array size.
966 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000967 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000968 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300969 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000970 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
Olli Etuaho856c4972016-08-08 11:38:39 +0300972 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400973
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000974 if (constant->getBasicType() == EbtUInt)
975 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300976 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000977 }
978 else
979 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300980 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000981
Olli Etuaho856c4972016-08-08 11:38:39 +0300982 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000983 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400984 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300985 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000986 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400987
Olli Etuaho856c4972016-08-08 11:38:39 +0300988 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400989 }
990
Olli Etuaho856c4972016-08-08 11:38:39 +0300991 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400992 {
993 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300994 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400995 }
996
997 // The size of arrays is restricted here to prevent issues further down the
998 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
999 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1000 const unsigned int sizeLimit = 65536;
1001
Olli Etuaho856c4972016-08-08 11:38:39 +03001002 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001003 {
1004 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001005 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001006 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001007
1008 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009}
1010
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001012bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1013 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014{
Olli Etuaho8a176262016-08-16 14:23:01 +03001015 if ((elementQualifier.qualifier == EvqAttribute) ||
1016 (elementQualifier.qualifier == EvqVertexIn) ||
1017 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001018 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001019 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001020 TType(elementQualifier).getQualifierString());
1021 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001022 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
Olli Etuaho8a176262016-08-16 14:23:01 +03001024 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025}
1026
Olli Etuaho8a176262016-08-16 14:23:01 +03001027// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001028bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1029 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001031 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001032 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001033 error(line, "cannot declare arrays of arrays",
1034 TType(elementType).getCompleteString().c_str());
1035 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001036 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001037 return true;
1038}
1039
1040// Check if this qualified element type can be formed into an array. This is only called when array
1041// brackets are associated with an identifier in a declaration, like this:
1042// float a[2];
1043// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1044// are associated with the type, like this:
1045// float[2] a;
1046bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1047 const TPublicType &elementType)
1048{
1049 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1050 {
1051 return false;
1052 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001053 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1054 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1055 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001056 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001057 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001058 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001059 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001060 TType(elementType).getCompleteString().c_str());
1061 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001062 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001063 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064}
1065
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001066// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001067void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1068 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001069 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070{
Olli Etuaho3739d232015-04-08 12:23:44 +03001071 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001072 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001073 {
1074 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001075 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001076
1077 // Generate informative error messages for ESSL1.
1078 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001079 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001080 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301081 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001082 "structures containing arrays may not be declared constant since they cannot be "
1083 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301084 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001085 }
1086 else
1087 {
1088 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1089 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001090 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001091 // This will make the type sized if it isn't sized yet.
1092 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1093 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094}
1095
Olli Etuaho2935c582015-04-08 14:32:06 +03001096// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097// and update the symbol table.
1098//
Olli Etuaho2935c582015-04-08 14:32:06 +03001099// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001101bool TParseContext::declareVariable(const TSourceLoc &line,
1102 const TString &identifier,
1103 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001104 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105{
Olli Etuaho2935c582015-04-08 14:32:06 +03001106 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107
Olli Etuaho43364892017-02-13 16:00:12 +00001108 checkBindingIsValid(line, type);
1109
Olli Etuaho856c4972016-08-08 11:38:39 +03001110 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111
Olli Etuaho2935c582015-04-08 14:32:06 +03001112 // gl_LastFragData may be redeclared with a new precision qualifier
1113 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1114 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001115 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1116 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001117 if (type.isArrayOfArrays())
1118 {
1119 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1120 identifier.c_str());
1121 return false;
1122 }
1123 else if (static_cast<int>(type.getOutermostArraySize()) ==
1124 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001125 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001126 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001127 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001128 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001129 }
1130 }
1131 else
1132 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001133 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1134 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001135 return false;
1136 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001137 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001138
Olli Etuaho8a176262016-08-16 14:23:01 +03001139 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001140 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141
Olli Etuaho0f684632017-07-13 12:42:15 +03001142 (*variable) = symbolTable.declareVariable(&identifier, type);
1143 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001144 {
1145 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001146 return false;
1147 }
1148
Olli Etuaho8a176262016-08-16 14:23:01 +03001149 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001150 return false;
1151
1152 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001153}
1154
Martin Radev70866b82016-07-22 15:27:42 +03001155void TParseContext::checkIsParameterQualifierValid(
1156 const TSourceLoc &line,
1157 const TTypeQualifierBuilder &typeQualifierBuilder,
1158 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301159{
Olli Etuahocce89652017-06-19 16:04:09 +03001160 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001161 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001162
1163 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301164 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001165 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1166 }
1167
1168 if (!IsImage(type->getBasicType()))
1169 {
Olli Etuaho43364892017-02-13 16:00:12 +00001170 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001171 }
1172 else
1173 {
1174 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001175 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001176
Martin Radev70866b82016-07-22 15:27:42 +03001177 type->setQualifier(typeQualifier.qualifier);
1178
1179 if (typeQualifier.precision != EbpUndefined)
1180 {
1181 type->setPrecision(typeQualifier.precision);
1182 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183}
1184
Olli Etuaho703671e2017-11-08 17:47:18 +02001185template <size_t size>
1186bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1187 const std::array<TExtension, size> &extensions)
1188{
1189 ASSERT(!extensions.empty());
1190 const TExtensionBehavior &extBehavior = extensionBehavior();
1191
1192 bool canUseWithWarning = false;
1193 bool canUseWithoutWarning = false;
1194
1195 const char *errorMsgString = "";
1196 TExtension errorMsgExtension = TExtension::UNDEFINED;
1197
1198 for (TExtension extension : extensions)
1199 {
1200 auto extIter = extBehavior.find(extension);
1201 if (canUseWithWarning)
1202 {
1203 // We already have an extension that we can use, but with a warning.
1204 // See if we can use the alternative extension without a warning.
1205 if (extIter == extBehavior.end())
1206 {
1207 continue;
1208 }
1209 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1210 {
1211 canUseWithoutWarning = true;
1212 break;
1213 }
1214 continue;
1215 }
1216 if (extIter == extBehavior.end())
1217 {
1218 errorMsgString = "extension is not supported";
1219 errorMsgExtension = extension;
1220 }
1221 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1222 {
1223 errorMsgString = "extension is disabled";
1224 errorMsgExtension = extension;
1225 }
1226 else if (extIter->second == EBhWarn)
1227 {
1228 errorMsgExtension = extension;
1229 canUseWithWarning = true;
1230 }
1231 else
1232 {
1233 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1234 canUseWithoutWarning = true;
1235 break;
1236 }
1237 }
1238
1239 if (canUseWithoutWarning)
1240 {
1241 return true;
1242 }
1243 if (canUseWithWarning)
1244 {
1245 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1246 return true;
1247 }
1248 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1249 return false;
1250}
1251
1252template bool TParseContext::checkCanUseOneOfExtensions(
1253 const TSourceLoc &line,
1254 const std::array<TExtension, 1> &extensions);
1255template bool TParseContext::checkCanUseOneOfExtensions(
1256 const TSourceLoc &line,
1257 const std::array<TExtension, 2> &extensions);
1258template bool TParseContext::checkCanUseOneOfExtensions(
1259 const TSourceLoc &line,
1260 const std::array<TExtension, 3> &extensions);
1261
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001262bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001263{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001264 ASSERT(extension != TExtension::UNDEFINED);
Jiawei Shao0e883132017-10-26 09:53:50 +08001265 ASSERT(extension != TExtension::EXT_geometry_shader);
Olli Etuaho703671e2017-11-08 17:47:18 +02001266 if (extension == TExtension::OES_geometry_shader)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301267 {
Olli Etuaho703671e2017-11-08 17:47:18 +02001268 // OES_geometry_shader and EXT_geometry_shader are always interchangeable.
1269 constexpr std::array<TExtension, 2u> extensions{
1270 {TExtension::EXT_geometry_shader, TExtension::OES_geometry_shader}};
1271 return checkCanUseOneOfExtensions(line, extensions);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001272 }
Corentin Wallez1d33c212017-11-13 10:21:39 -08001273 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274}
1275
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001276// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1277// compile-time or link-time errors are the same whether or not the declaration is empty".
1278// This function implements all the checks that are done on qualifiers regardless of if the
1279// declaration is empty.
1280void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1281 const sh::TLayoutQualifier &layoutQualifier,
1282 const TSourceLoc &location)
1283{
1284 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1285 {
1286 error(location, "Shared memory declarations cannot have layout specified", "layout");
1287 }
1288
1289 if (layoutQualifier.matrixPacking != EmpUnspecified)
1290 {
1291 error(location, "layout qualifier only valid for interface blocks",
1292 getMatrixPackingString(layoutQualifier.matrixPacking));
1293 return;
1294 }
1295
1296 if (layoutQualifier.blockStorage != EbsUnspecified)
1297 {
1298 error(location, "layout qualifier only valid for interface blocks",
1299 getBlockStorageString(layoutQualifier.blockStorage));
1300 return;
1301 }
1302
1303 if (qualifier == EvqFragmentOut)
1304 {
1305 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1306 {
1307 error(location, "invalid layout qualifier combination", "yuv");
1308 return;
1309 }
1310 }
1311 else
1312 {
1313 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1314 }
1315
Olli Etuaho95468d12017-05-04 11:14:34 +03001316 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1317 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001318 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1319 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001320 {
1321 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1322 }
1323
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001324 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001325 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001326 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001327 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001328 // We're not checking whether the uniform location is in range here since that depends on
1329 // the type of the variable.
1330 // The type can only be fully determined for non-empty declarations.
1331 }
1332 if (!canHaveLocation)
1333 {
1334 checkLocationIsNotSpecified(location, layoutQualifier);
1335 }
1336}
1337
jchen104cdac9e2017-05-08 11:01:20 +08001338void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1339 const TSourceLoc &location)
1340{
1341 if (publicType.precision != EbpHigh)
1342 {
1343 error(location, "Can only be highp", "atomic counter");
1344 }
1345 // dEQP enforces compile error if location is specified. See uniform_location.test.
1346 if (publicType.layoutQualifier.location != -1)
1347 {
1348 error(location, "location must not be set for atomic_uint", "layout");
1349 }
1350 if (publicType.layoutQualifier.binding == -1)
1351 {
1352 error(location, "no binding specified", "atomic counter");
1353 }
1354}
1355
Olli Etuaho55bde912017-10-25 13:41:13 +03001356void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001357{
Olli Etuaho55bde912017-10-25 13:41:13 +03001358 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001359 {
1360 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1361 // error. It is assumed that this applies to empty declarations as well.
1362 error(location, "empty array declaration needs to specify a size", "");
1363 }
Martin Radevb8b01222016-11-20 23:25:53 +02001364}
1365
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001366// These checks are done for all declarations that are non-empty. They're done for non-empty
1367// declarations starting a declarator list, and declarators that follow an empty declaration.
1368void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1369 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001370{
Olli Etuahofa33d582015-04-09 14:33:12 +03001371 switch (publicType.qualifier)
1372 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001373 case EvqVaryingIn:
1374 case EvqVaryingOut:
1375 case EvqAttribute:
1376 case EvqVertexIn:
1377 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001378 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001379 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001380 {
1381 error(identifierLocation, "cannot be used with a structure",
1382 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001383 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001384 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001385 break;
1386 case EvqBuffer:
1387 if (publicType.getBasicType() != EbtInterfaceBlock)
1388 {
1389 error(identifierLocation,
1390 "cannot declare buffer variables at global scope(outside a block)",
1391 getQualifierString(publicType.qualifier));
1392 return;
1393 }
1394 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001395 default:
1396 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001397 }
jchen10cc2a10e2017-05-03 14:05:12 +08001398 std::string reason(getBasicString(publicType.getBasicType()));
1399 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001400 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001401 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001402 {
1403 return;
1404 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001405
Andrei Volykhina5527072017-03-22 16:46:30 +03001406 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1407 publicType.qualifier != EvqConst) &&
1408 publicType.getBasicType() == EbtYuvCscStandardEXT)
1409 {
1410 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1411 getQualifierString(publicType.qualifier));
1412 return;
1413 }
1414
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001415 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1416 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001417 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1418 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001419 TType type(publicType);
1420 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001421 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001422 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1423 publicType.layoutQualifier);
1424 }
1425 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001426
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001427 // check for layout qualifier issues
1428 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001429
Martin Radev2cc85b32016-08-05 16:22:53 +03001430 if (IsImage(publicType.getBasicType()))
1431 {
1432
1433 switch (layoutQualifier.imageInternalFormat)
1434 {
1435 case EiifRGBA32F:
1436 case EiifRGBA16F:
1437 case EiifR32F:
1438 case EiifRGBA8:
1439 case EiifRGBA8_SNORM:
1440 if (!IsFloatImage(publicType.getBasicType()))
1441 {
1442 error(identifierLocation,
1443 "internal image format requires a floating image type",
1444 getBasicString(publicType.getBasicType()));
1445 return;
1446 }
1447 break;
1448 case EiifRGBA32I:
1449 case EiifRGBA16I:
1450 case EiifRGBA8I:
1451 case EiifR32I:
1452 if (!IsIntegerImage(publicType.getBasicType()))
1453 {
1454 error(identifierLocation,
1455 "internal image format requires an integer image type",
1456 getBasicString(publicType.getBasicType()));
1457 return;
1458 }
1459 break;
1460 case EiifRGBA32UI:
1461 case EiifRGBA16UI:
1462 case EiifRGBA8UI:
1463 case EiifR32UI:
1464 if (!IsUnsignedImage(publicType.getBasicType()))
1465 {
1466 error(identifierLocation,
1467 "internal image format requires an unsigned image type",
1468 getBasicString(publicType.getBasicType()));
1469 return;
1470 }
1471 break;
1472 case EiifUnspecified:
1473 error(identifierLocation, "layout qualifier", "No image internal format specified");
1474 return;
1475 default:
1476 error(identifierLocation, "layout qualifier", "unrecognized token");
1477 return;
1478 }
1479
1480 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1481 switch (layoutQualifier.imageInternalFormat)
1482 {
1483 case EiifR32F:
1484 case EiifR32I:
1485 case EiifR32UI:
1486 break;
1487 default:
1488 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1489 {
1490 error(identifierLocation, "layout qualifier",
1491 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1492 "image variables must be qualified readonly and/or writeonly");
1493 return;
1494 }
1495 break;
1496 }
1497 }
1498 else
1499 {
Olli Etuaho43364892017-02-13 16:00:12 +00001500 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001501 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1502 }
jchen104cdac9e2017-05-08 11:01:20 +08001503
1504 if (IsAtomicCounter(publicType.getBasicType()))
1505 {
1506 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1507 }
1508 else
1509 {
1510 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1511 }
Olli Etuaho43364892017-02-13 16:00:12 +00001512}
Martin Radev2cc85b32016-08-05 16:22:53 +03001513
Olli Etuaho43364892017-02-13 16:00:12 +00001514void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1515{
1516 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001517 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1518 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1519 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1520 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1521 // when it comes to which shaders are accepted by the compiler.
1522 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001523 if (IsImage(type.getBasicType()))
1524 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001525 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1526 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001527 }
1528 else if (IsSampler(type.getBasicType()))
1529 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001530 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1531 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001532 }
jchen104cdac9e2017-05-08 11:01:20 +08001533 else if (IsAtomicCounter(type.getBasicType()))
1534 {
1535 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1536 }
Olli Etuaho43364892017-02-13 16:00:12 +00001537 else
1538 {
1539 ASSERT(!IsOpaqueType(type.getBasicType()));
1540 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001541 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001542}
1543
Olli Etuaho856c4972016-08-08 11:38:39 +03001544void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1545 const TString &layoutQualifierName,
1546 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001547{
1548
1549 if (mShaderVersion < versionRequired)
1550 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001551 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001552 }
1553}
1554
Olli Etuaho856c4972016-08-08 11:38:39 +03001555bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1556 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001557{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001558 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001559 for (size_t i = 0u; i < localSize.size(); ++i)
1560 {
1561 if (localSize[i] != -1)
1562 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001563 error(location,
1564 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1565 "global layout declaration",
1566 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001567 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001568 }
1569 }
1570
Olli Etuaho8a176262016-08-16 14:23:01 +03001571 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001572}
1573
Olli Etuaho43364892017-02-13 16:00:12 +00001574void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001575 TLayoutImageInternalFormat internalFormat)
1576{
1577 if (internalFormat != EiifUnspecified)
1578 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001579 error(location, "invalid layout qualifier: only valid when used with images",
1580 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001581 }
Olli Etuaho43364892017-02-13 16:00:12 +00001582}
1583
1584void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1585{
1586 if (binding != -1)
1587 {
1588 error(location,
1589 "invalid layout qualifier: only valid when used with opaque types or blocks",
1590 "binding");
1591 }
1592}
1593
jchen104cdac9e2017-05-08 11:01:20 +08001594void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1595{
1596 if (offset != -1)
1597 {
1598 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1599 "offset");
1600 }
1601}
1602
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001603void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1604 int binding,
1605 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001606{
1607 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001608 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001609 {
1610 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1611 }
1612}
1613
1614void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1615 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001616 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001617{
1618 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001619 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001620 {
1621 error(location, "sampler binding greater than maximum texture units", "binding");
1622 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001623}
1624
Jiajia Qinbc585152017-06-23 15:42:17 +08001625void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1626 const TQualifier &qualifier,
1627 int binding,
1628 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001629{
1630 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001631 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001632 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001633 if (binding + size > mMaxUniformBufferBindings)
1634 {
1635 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1636 "binding");
1637 }
1638 }
1639 else if (qualifier == EvqBuffer)
1640 {
1641 if (binding + size > mMaxShaderStorageBufferBindings)
1642 {
1643 error(location,
1644 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1645 "binding");
1646 }
jchen10af713a22017-04-19 09:10:56 +08001647 }
1648}
jchen104cdac9e2017-05-08 11:01:20 +08001649void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1650{
1651 if (binding >= mMaxAtomicCounterBindings)
1652 {
1653 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1654 "binding");
1655 }
1656}
jchen10af713a22017-04-19 09:10:56 +08001657
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001658void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1659 int objectLocationCount,
1660 const TLayoutQualifier &layoutQualifier)
1661{
1662 int loc = layoutQualifier.location;
1663 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1664 {
1665 error(location, "Uniform location out of range", "location");
1666 }
1667}
1668
Andrei Volykhina5527072017-03-22 16:46:30 +03001669void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1670{
1671 if (yuv != false)
1672 {
1673 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1674 }
1675}
1676
Jiajia Qinbc585152017-06-23 15:42:17 +08001677void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1678 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001679{
1680 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1681 {
1682 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001683 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1684 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1685 qual == EvqInOut || qual == EvqConstReadOnly))
1686 {
1687 if (argument->getMemoryQualifier().writeonly)
1688 {
1689 error(argument->getLine(),
1690 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1691 fnCall->getFunctionSymbolInfo()->getName().c_str());
1692 return;
1693 }
1694 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001695 if (qual == EvqOut || qual == EvqInOut)
1696 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001697 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001698 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001699 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001700 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001701 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001702 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001703 }
1704 }
1705 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001706}
1707
Martin Radev70866b82016-07-22 15:27:42 +03001708void TParseContext::checkInvariantVariableQualifier(bool invariant,
1709 const TQualifier qualifier,
1710 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001711{
Martin Radev70866b82016-07-22 15:27:42 +03001712 if (!invariant)
1713 return;
1714
1715 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001716 {
Martin Radev70866b82016-07-22 15:27:42 +03001717 // input variables in the fragment shader can be also qualified as invariant
1718 if (!sh::CanBeInvariantESSL1(qualifier))
1719 {
1720 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1721 }
1722 }
1723 else
1724 {
1725 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1726 {
1727 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1728 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001729 }
1730}
1731
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001732bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001733{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001734 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001735}
1736
Jamie Madillb98c3a82015-07-23 14:26:04 -04001737void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1738 const char *extName,
1739 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001740{
1741 pp::SourceLocation srcLoc;
1742 srcLoc.file = loc.first_file;
1743 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001744 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001745}
1746
Jamie Madillb98c3a82015-07-23 14:26:04 -04001747void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1748 const char *name,
1749 const char *value,
1750 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001751{
1752 pp::SourceLocation srcLoc;
1753 srcLoc.file = loc.first_file;
1754 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001755 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001756}
1757
Martin Radev4c4c8e72016-08-04 12:25:34 +03001758sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001759{
Jamie Madill2f294c92017-11-20 14:47:26 -05001760 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001761 for (size_t i = 0u; i < result.size(); ++i)
1762 {
1763 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1764 {
1765 result[i] = 1;
1766 }
1767 else
1768 {
1769 result[i] = mComputeShaderLocalSize[i];
1770 }
1771 }
1772 return result;
1773}
1774
Olli Etuaho56229f12017-07-10 14:16:33 +03001775TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1776 const TSourceLoc &line)
1777{
1778 TIntermConstantUnion *node = new TIntermConstantUnion(
1779 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1780 node->setLine(line);
1781 return node;
1782}
1783
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784/////////////////////////////////////////////////////////////////////////////////
1785//
1786// Non-Errors.
1787//
1788/////////////////////////////////////////////////////////////////////////////////
1789
Jamie Madill5c097022014-08-20 16:38:32 -04001790const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1791 const TString *name,
1792 const TSymbol *symbol)
1793{
Jamie Madill5c097022014-08-20 16:38:32 -04001794 if (!symbol)
1795 {
1796 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001797 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001798 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001799
1800 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001801 {
1802 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001803 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001804 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001805
1806 const TVariable *variable = static_cast<const TVariable *>(symbol);
1807
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001808 if (variable->getExtension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001809 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001810 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001811 }
1812
Olli Etuaho0f684632017-07-13 12:42:15 +03001813 // Reject shaders using both gl_FragData and gl_FragColor
1814 TQualifier qualifier = variable->getType().getQualifier();
1815 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001816 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001817 mUsesFragData = true;
1818 }
1819 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1820 {
1821 mUsesFragColor = true;
1822 }
1823 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1824 {
1825 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001826 }
1827
Olli Etuaho0f684632017-07-13 12:42:15 +03001828 // This validation is not quite correct - it's only an error to write to
1829 // both FragData and FragColor. For simplicity, and because users shouldn't
1830 // be rewarded for reading from undefined varaibles, return an error
1831 // if they are both referenced, rather than assigned.
1832 if (mUsesFragData && mUsesFragColor)
1833 {
1834 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1835 if (mUsesSecondaryOutputs)
1836 {
1837 errorMessage =
1838 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1839 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1840 }
1841 error(location, errorMessage, name->c_str());
1842 }
1843
1844 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1845 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1846 qualifier == EvqWorkGroupSize)
1847 {
1848 error(location,
1849 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1850 "gl_WorkGroupSize");
1851 }
Jamie Madill5c097022014-08-20 16:38:32 -04001852 return variable;
1853}
1854
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001855TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1856 const TString *name,
1857 const TSymbol *symbol)
1858{
1859 const TVariable *variable = getNamedVariable(location, name, symbol);
1860
Olli Etuaho0f684632017-07-13 12:42:15 +03001861 if (!variable)
1862 {
1863 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1864 node->setLine(location);
1865 return node;
1866 }
1867
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001868 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001869 TIntermTyped *node = nullptr;
1870
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001871 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001872 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001873 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001874 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001875 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001876 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001877 {
1878 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1879 // needs to be added to the AST as a constant and not as a symbol.
1880 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1881 TConstantUnion *constArray = new TConstantUnion[3];
1882 for (size_t i = 0; i < 3; ++i)
1883 {
1884 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1885 }
1886
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001887 ASSERT(variableType.getBasicType() == EbtUInt);
1888 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001889
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001890 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001891 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001892 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001893 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001894 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1895 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001896 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001897 ASSERT(mGeometryShaderInputArraySize > 0u);
1898
1899 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho55bde912017-10-25 13:41:13 +03001900 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001901 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001902 else
1903 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001904 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001905 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001906 ASSERT(node != nullptr);
1907 node->setLine(location);
1908 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001909}
1910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911// Initializers show up in several places in the grammar. Have one set of
1912// code to handle them here.
1913//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001914// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001915bool TParseContext::executeInitializer(const TSourceLoc &line,
1916 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001917 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001918 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001919 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920{
Olli Etuaho13389b62016-10-16 11:48:18 +01001921 ASSERT(initNode != nullptr);
1922 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923
Olli Etuaho2935c582015-04-08 14:32:06 +03001924 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001925 if (type.isUnsizedArray())
1926 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001927 // In case initializer is not an array or type has more dimensions than initializer, this
1928 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1929 // actually is an array or not. Having a non-array initializer for an unsized array will
1930 // result in an error later, so we don't generate an error message here.
1931 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001932 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001933 if (!declareVariable(line, identifier, type, &variable))
1934 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001935 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001936 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937
Olli Etuahob0c645e2015-05-12 14:25:36 +03001938 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001939 if (symbolTable.atGlobalLevel() &&
1940 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001941 {
1942 // Error message does not completely match behavior with ESSL 1.00, but
1943 // we want to steer developers towards only using constant expressions.
1944 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001945 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001946 }
1947 if (globalInitWarning)
1948 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001949 warning(
1950 line,
1951 "global variable initializers should be constant expressions "
1952 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1953 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001954 }
1955
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001956 //
1957 // identifier must be of type constant, a global, or a temporary
1958 //
1959 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301960 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1961 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001962 error(line, " cannot initialize this type of qualifier ",
1963 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001964 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001965 }
1966 //
1967 // test for and propagate constant
1968 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001969
Arun Patole7e7e68d2015-05-22 12:02:25 +05301970 if (qualifier == EvqConst)
1971 {
1972 if (qualifier != initializer->getType().getQualifier())
1973 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001974 std::stringstream reasonStream;
1975 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1976 << "'";
1977 std::string reason = reasonStream.str();
1978 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001979 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001980 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001981 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301982 if (type != initializer->getType())
1983 {
1984 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001985 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001986 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001987 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001988 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001989
1990 // Save the constant folded value to the variable if possible. For example array
1991 // initializers are not folded, since that way copying the array literal to multiple places
1992 // in the shader is avoided.
1993 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1994 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301995 if (initializer->getAsConstantUnion())
1996 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001997 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001998 ASSERT(*initNode == nullptr);
1999 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302000 }
2001 else if (initializer->getAsSymbolNode())
2002 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002003 const TSymbol *symbol =
2004 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
2005 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002007 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002008 if (constArray)
2009 {
2010 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002011 ASSERT(*initNode == nullptr);
2012 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002013 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002014 }
2015 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002016
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002017 TIntermSymbol *intermSymbol =
2018 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
2019 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002020 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2021 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002022 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002023 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002024 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002025 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026
Olli Etuaho914b79a2017-06-19 16:03:19 +03002027 return true;
2028}
2029
2030TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2031 const TString &identifier,
2032 TIntermTyped *initializer,
2033 const TSourceLoc &loc)
2034{
2035 checkIsScalarBool(loc, pType);
2036 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002037 TType type(pType);
2038 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002039 {
2040 // The initializer is valid. The init condition needs to have a node - either the
2041 // initializer node, or a constant node in case the initialized variable is const and won't
2042 // be recorded in the AST.
2043 if (initNode == nullptr)
2044 {
2045 return initializer;
2046 }
2047 else
2048 {
2049 TIntermDeclaration *declaration = new TIntermDeclaration();
2050 declaration->appendDeclarator(initNode);
2051 return declaration;
2052 }
2053 }
2054 return nullptr;
2055}
2056
2057TIntermNode *TParseContext::addLoop(TLoopType type,
2058 TIntermNode *init,
2059 TIntermNode *cond,
2060 TIntermTyped *expr,
2061 TIntermNode *body,
2062 const TSourceLoc &line)
2063{
2064 TIntermNode *node = nullptr;
2065 TIntermTyped *typedCond = nullptr;
2066 if (cond)
2067 {
2068 typedCond = cond->getAsTyped();
2069 }
2070 if (cond == nullptr || typedCond)
2071 {
Olli Etuahocce89652017-06-19 16:04:09 +03002072 if (type == ELoopDoWhile)
2073 {
2074 checkIsScalarBool(line, typedCond);
2075 }
2076 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2077 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2078 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2079 !typedCond->isVector()));
2080
Olli Etuaho3ec75682017-07-05 17:02:55 +03002081 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002082 node->setLine(line);
2083 return node;
2084 }
2085
Olli Etuahocce89652017-06-19 16:04:09 +03002086 ASSERT(type != ELoopDoWhile);
2087
Olli Etuaho914b79a2017-06-19 16:03:19 +03002088 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2089 ASSERT(declaration);
2090 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2091 ASSERT(declarator->getLeft()->getAsSymbolNode());
2092
2093 // The condition is a declaration. In the AST representation we don't support declarations as
2094 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2095 // the loop.
2096 TIntermBlock *block = new TIntermBlock();
2097
2098 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2099 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2100 block->appendStatement(declareCondition);
2101
2102 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2103 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002104 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002105 block->appendStatement(loop);
2106 loop->setLine(line);
2107 block->setLine(line);
2108 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109}
2110
Olli Etuahocce89652017-06-19 16:04:09 +03002111TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2112 TIntermNodePair code,
2113 const TSourceLoc &loc)
2114{
Olli Etuaho56229f12017-07-10 14:16:33 +03002115 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002116
2117 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002118 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002119 {
2120 if (cond->getAsConstantUnion()->getBConst(0) == true)
2121 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002122 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002123 }
2124 else
2125 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002126 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002127 }
2128 }
2129
Olli Etuaho3ec75682017-07-05 17:02:55 +03002130 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002131 node->setLine(loc);
2132
2133 return node;
2134}
2135
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002136void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2137{
2138 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2139 typeSpecifier->getBasicType());
2140
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002141 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002142 {
2143 error(typeSpecifier->getLine(), "not supported", "first-class array");
2144 typeSpecifier->clearArrayness();
2145 }
2146}
2147
Martin Radev70866b82016-07-22 15:27:42 +03002148TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302149 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002150{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002151 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002152
Martin Radev70866b82016-07-22 15:27:42 +03002153 TPublicType returnType = typeSpecifier;
2154 returnType.qualifier = typeQualifier.qualifier;
2155 returnType.invariant = typeQualifier.invariant;
2156 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002157 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002158 returnType.precision = typeSpecifier.precision;
2159
2160 if (typeQualifier.precision != EbpUndefined)
2161 {
2162 returnType.precision = typeQualifier.precision;
2163 }
2164
Martin Radev4a9cd802016-09-01 16:51:51 +03002165 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2166 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002167
Martin Radev4a9cd802016-09-01 16:51:51 +03002168 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2169 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002170
Martin Radev4a9cd802016-09-01 16:51:51 +03002171 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002172
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002173 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002174 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002175 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002176 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002177 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002178 returnType.clearArrayness();
2179 }
2180
Martin Radev70866b82016-07-22 15:27:42 +03002181 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002182 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002183 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002184 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002185 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002186 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002187
Martin Radev70866b82016-07-22 15:27:42 +03002188 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002189 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002190 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002191 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002192 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002193 }
2194 }
2195 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002196 {
Martin Radev70866b82016-07-22 15:27:42 +03002197 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002198 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002199 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002200 }
Martin Radev70866b82016-07-22 15:27:42 +03002201 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2202 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002203 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002204 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2205 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002206 }
Martin Radev70866b82016-07-22 15:27:42 +03002207 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002208 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002209 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002210 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002211 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002212 }
2213
2214 return returnType;
2215}
2216
Olli Etuaho856c4972016-08-08 11:38:39 +03002217void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2218 const TPublicType &type,
2219 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002220{
2221 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002222 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002223 {
2224 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002225 }
2226
2227 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2228 switch (qualifier)
2229 {
2230 case EvqVertexIn:
2231 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002232 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002233 {
2234 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002235 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002236 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002237 return;
2238 case EvqFragmentOut:
2239 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002240 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002241 {
2242 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002243 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002244 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002245 return;
2246 default:
2247 break;
2248 }
2249
2250 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2251 // restrictions.
2252 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002253 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2254 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002255 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2256 {
2257 error(qualifierLocation, "must use 'flat' interpolation here",
2258 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002259 }
2260
Martin Radev4a9cd802016-09-01 16:51:51 +03002261 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002262 {
2263 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2264 // These restrictions are only implied by the ESSL 3.00 spec, but
2265 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002266 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 {
2268 error(qualifierLocation, "cannot be an array of structures",
2269 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002270 }
2271 if (type.isStructureContainingArrays())
2272 {
2273 error(qualifierLocation, "cannot be a structure containing an array",
2274 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002275 }
2276 if (type.isStructureContainingType(EbtStruct))
2277 {
2278 error(qualifierLocation, "cannot be a structure containing a structure",
2279 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002280 }
2281 if (type.isStructureContainingType(EbtBool))
2282 {
2283 error(qualifierLocation, "cannot be a structure containing a bool",
2284 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002285 }
2286 }
2287}
2288
Martin Radev2cc85b32016-08-05 16:22:53 +03002289void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2290{
2291 if (qualifier.getType() == QtStorage)
2292 {
2293 const TStorageQualifierWrapper &storageQualifier =
2294 static_cast<const TStorageQualifierWrapper &>(qualifier);
2295 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2296 !symbolTable.atGlobalLevel())
2297 {
2298 error(storageQualifier.getLine(),
2299 "Local variables can only use the const storage qualifier.",
2300 storageQualifier.getQualifierString().c_str());
2301 }
2302 }
2303}
2304
Olli Etuaho43364892017-02-13 16:00:12 +00002305void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002306 const TSourceLoc &location)
2307{
Jiajia Qinbc585152017-06-23 15:42:17 +08002308 const std::string reason(
2309 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2310 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002311 if (memoryQualifier.readonly)
2312 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002313 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002314 }
2315 if (memoryQualifier.writeonly)
2316 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002317 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002318 }
Martin Radev049edfa2016-11-11 14:35:37 +02002319 if (memoryQualifier.coherent)
2320 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002321 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002322 }
2323 if (memoryQualifier.restrictQualifier)
2324 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002325 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002326 }
2327 if (memoryQualifier.volatileQualifier)
2328 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002329 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002330 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002331}
2332
jchen104cdac9e2017-05-08 11:01:20 +08002333// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2334// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002335void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2336 const TSourceLoc &loc,
2337 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002338{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002339 if (!IsAtomicCounter(type->getBasicType()))
2340 {
2341 return;
2342 }
2343
2344 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2345 : kAtomicCounterSize;
2346 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2347 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002348 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002349 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002350 {
2351 offset = bindingState.appendSpan(size);
2352 }
2353 else
2354 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002355 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002356 }
2357 if (offset == -1)
2358 {
2359 error(loc, "Offset overlapping", "atomic counter");
2360 return;
2361 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002362 layoutQualifier.offset = offset;
2363 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002364}
2365
Olli Etuaho454c34c2017-10-25 16:35:56 +03002366void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2367 const char *token,
2368 TType *type)
2369{
2370 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2371 {
2372 if (type->isArray() && type->getOutermostArraySize() == 0u)
2373 {
2374 // Set size for the unsized geometry shader inputs if they are declared after a valid
2375 // input primitive declaration.
2376 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2377 {
2378 ASSERT(mGeometryShaderInputArraySize > 0u);
2379 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2380 }
2381 else
2382 {
2383 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2384 // An input can be declared without an array size if there is a previous layout
2385 // which specifies the size.
2386 error(location,
2387 "Missing a valid input primitive declaration before declaring an unsized "
2388 "array input",
2389 token);
2390 }
2391 }
2392 else if (type->isArray())
2393 {
2394 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2395 }
2396 else
2397 {
2398 error(location, "Geometry shader input variable must be declared as an array", token);
2399 }
2400 }
2401}
2402
Olli Etuaho13389b62016-10-16 11:48:18 +01002403TIntermDeclaration *TParseContext::parseSingleDeclaration(
2404 TPublicType &publicType,
2405 const TSourceLoc &identifierOrTypeLocation,
2406 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002407{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002408 TType type(publicType);
2409 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2410 mDirectiveHandler.pragma().stdgl.invariantAll)
2411 {
2412 TQualifier qualifier = type.getQualifier();
2413
2414 // The directive handler has already taken care of rejecting invalid uses of this pragma
2415 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2416 // affected variable declarations:
2417 //
2418 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2419 // elsewhere, in TranslatorGLSL.)
2420 //
2421 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2422 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2423 // the way this is currently implemented we have to enable this compiler option before
2424 // parsing the shader and determining the shading language version it uses. If this were
2425 // implemented as a post-pass, the workaround could be more targeted.
2426 //
2427 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2428 // the specification, but there are desktop OpenGL drivers that expect that this is the
2429 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2430 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2431 {
2432 type.setInvariant(true);
2433 }
2434 }
2435
Olli Etuaho454c34c2017-10-25 16:35:56 +03002436 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002437
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002438 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2439 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002440
Olli Etuahobab4c082015-04-24 16:38:49 +03002441 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002442 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002443
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002444 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002445 if (emptyDeclaration)
2446 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002447 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002448 // In most cases we don't need to create a symbol node for an empty declaration.
2449 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2450 if (type.getBasicType() == EbtStruct)
2451 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002452 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002453 }
jchen104cdac9e2017-05-08 11:01:20 +08002454 else if (IsAtomicCounter(publicType.getBasicType()))
2455 {
2456 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2457 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002458 }
2459 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002460 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002461 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002462
Olli Etuaho55bde912017-10-25 13:41:13 +03002463 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002464
Olli Etuaho55bc9052017-10-25 17:33:06 +03002465 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002466
Olli Etuaho2935c582015-04-08 14:32:06 +03002467 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002468 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002469
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002470 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002471 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002472 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002473 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002474 }
2475
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002476 TIntermDeclaration *declaration = new TIntermDeclaration();
2477 declaration->setLine(identifierOrTypeLocation);
2478 if (symbol)
2479 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002480 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002481 declaration->appendDeclarator(symbol);
2482 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002483 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002484}
2485
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002486TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2487 TPublicType &elementType,
2488 const TSourceLoc &identifierLocation,
2489 const TString &identifier,
2490 const TSourceLoc &indexLocation,
2491 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002492{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002493 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002494
Olli Etuaho55bde912017-10-25 13:41:13 +03002495 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002496 identifierLocation);
2497
Olli Etuaho55bde912017-10-25 13:41:13 +03002498 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002499
Olli Etuaho55bde912017-10-25 13:41:13 +03002500 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002501
Olli Etuaho55bde912017-10-25 13:41:13 +03002502 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002503 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002504
Olli Etuaho454c34c2017-10-25 16:35:56 +03002505 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002506
2507 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2508
Olli Etuaho55bc9052017-10-25 17:33:06 +03002509 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002510
Olli Etuaho2935c582015-04-08 14:32:06 +03002511 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002512 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002513
Olli Etuaho13389b62016-10-16 11:48:18 +01002514 TIntermDeclaration *declaration = new TIntermDeclaration();
2515 declaration->setLine(identifierLocation);
2516
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002517 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002518 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002519 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2520 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002521 declaration->appendDeclarator(symbol);
2522 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002523
Olli Etuaho13389b62016-10-16 11:48:18 +01002524 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002525}
2526
Olli Etuaho13389b62016-10-16 11:48:18 +01002527TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2528 const TSourceLoc &identifierLocation,
2529 const TString &identifier,
2530 const TSourceLoc &initLocation,
2531 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002532{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002533 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002534
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002535 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2536 identifierLocation);
2537
2538 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002539
Olli Etuaho13389b62016-10-16 11:48:18 +01002540 TIntermDeclaration *declaration = new TIntermDeclaration();
2541 declaration->setLine(identifierLocation);
2542
2543 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002544 TType type(publicType);
2545 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002546 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002547 if (initNode)
2548 {
2549 declaration->appendDeclarator(initNode);
2550 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002551 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002552 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002553}
2554
Olli Etuaho13389b62016-10-16 11:48:18 +01002555TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002556 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002557 const TSourceLoc &identifierLocation,
2558 const TString &identifier,
2559 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002560 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002561 const TSourceLoc &initLocation,
2562 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002563{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002564 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565
Olli Etuaho55bde912017-10-25 13:41:13 +03002566 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002567 identifierLocation);
2568
Olli Etuaho55bde912017-10-25 13:41:13 +03002569 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002570
Olli Etuaho55bde912017-10-25 13:41:13 +03002571 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572
Olli Etuaho55bde912017-10-25 13:41:13 +03002573 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002574 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002575
Olli Etuaho13389b62016-10-16 11:48:18 +01002576 TIntermDeclaration *declaration = new TIntermDeclaration();
2577 declaration->setLine(identifierLocation);
2578
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002579 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002580 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002581 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002582 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002583 if (initNode)
2584 {
2585 declaration->appendDeclarator(initNode);
2586 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002587 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002588
2589 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002590}
2591
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002592TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002593 const TTypeQualifierBuilder &typeQualifierBuilder,
2594 const TSourceLoc &identifierLoc,
2595 const TString *identifier,
2596 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002597{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002598 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002599
Martin Radev70866b82016-07-22 15:27:42 +03002600 if (!typeQualifier.invariant)
2601 {
2602 error(identifierLoc, "Expected invariant", identifier->c_str());
2603 return nullptr;
2604 }
2605 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2606 {
2607 return nullptr;
2608 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002609 if (!symbol)
2610 {
2611 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002612 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002613 }
Martin Radev70866b82016-07-22 15:27:42 +03002614 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002615 {
Martin Radev70866b82016-07-22 15:27:42 +03002616 error(identifierLoc, "invariant declaration specifies qualifier",
2617 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002618 }
Martin Radev70866b82016-07-22 15:27:42 +03002619 if (typeQualifier.precision != EbpUndefined)
2620 {
2621 error(identifierLoc, "invariant declaration specifies precision",
2622 getPrecisionString(typeQualifier.precision));
2623 }
2624 if (!typeQualifier.layoutQualifier.isEmpty())
2625 {
2626 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2627 }
2628
2629 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002630 if (!variable)
2631 {
2632 return nullptr;
2633 }
Martin Radev70866b82016-07-22 15:27:42 +03002634 const TType &type = variable->getType();
2635
2636 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2637 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002638 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002639
2640 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2641
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002642 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2643 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002644
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002645 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002646}
2647
Olli Etuaho13389b62016-10-16 11:48:18 +01002648void TParseContext::parseDeclarator(TPublicType &publicType,
2649 const TSourceLoc &identifierLocation,
2650 const TString &identifier,
2651 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002652{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002653 // If the declaration starting this declarator list was empty (example: int,), some checks were
2654 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002655 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002656 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002657 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2658 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002659 }
2660
Olli Etuaho856c4972016-08-08 11:38:39 +03002661 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002662
Olli Etuaho2935c582015-04-08 14:32:06 +03002663 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002664 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002665
2666 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2667
Olli Etuaho55bde912017-10-25 13:41:13 +03002668 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2669
Olli Etuaho55bc9052017-10-25 17:33:06 +03002670 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2671
Olli Etuaho43364892017-02-13 16:00:12 +00002672 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002673
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002674 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002675 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002676 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2677 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002678 declarationOut->appendDeclarator(symbol);
2679 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002680}
2681
Olli Etuaho55bde912017-10-25 13:41:13 +03002682void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002683 const TSourceLoc &identifierLocation,
2684 const TString &identifier,
2685 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002686 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002687 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002688{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002689 // If the declaration starting this declarator list was empty (example: int,), some checks were
2690 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002691 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002692 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002693 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002694 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002695 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002696
Olli Etuaho55bde912017-10-25 13:41:13 +03002697 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002698
Olli Etuaho55bde912017-10-25 13:41:13 +03002699 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002700 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002701 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002702 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002703
Olli Etuaho454c34c2017-10-25 16:35:56 +03002704 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2705
Olli Etuaho55bde912017-10-25 13:41:13 +03002706 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2707
Olli Etuaho55bc9052017-10-25 17:33:06 +03002708 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002709
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002710 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002711 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002712
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002713 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002714 {
2715 TIntermSymbol *symbol =
2716 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2717 symbol->setLine(identifierLocation);
2718 declarationOut->appendDeclarator(symbol);
2719 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002720 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002721}
2722
Olli Etuaho13389b62016-10-16 11:48:18 +01002723void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2724 const TSourceLoc &identifierLocation,
2725 const TString &identifier,
2726 const TSourceLoc &initLocation,
2727 TIntermTyped *initializer,
2728 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002729{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002730 // If the declaration starting this declarator list was empty (example: int,), some checks were
2731 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002732 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002733 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002734 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2735 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002736 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002737
Olli Etuaho856c4972016-08-08 11:38:39 +03002738 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002739
Olli Etuaho13389b62016-10-16 11:48:18 +01002740 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002741 TType type(publicType);
2742 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002743 {
2744 //
2745 // build the intermediate representation
2746 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002747 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002748 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002749 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002750 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002751 }
2752}
2753
Olli Etuaho55bde912017-10-25 13:41:13 +03002754void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002755 const TSourceLoc &identifierLocation,
2756 const TString &identifier,
2757 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002758 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002759 const TSourceLoc &initLocation,
2760 TIntermTyped *initializer,
2761 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002763 // If the declaration starting this declarator list was empty (example: int,), some checks were
2764 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002765 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002766 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002767 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002768 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002769 }
2770
Olli Etuaho55bde912017-10-25 13:41:13 +03002771 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002772
Olli Etuaho55bde912017-10-25 13:41:13 +03002773 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002774
Olli Etuaho55bde912017-10-25 13:41:13 +03002775 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002776 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002777
2778 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002779 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002780 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002781 {
2782 if (initNode)
2783 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002784 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002785 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002786 }
2787}
2788
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002789TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2790{
2791 // It's simpler to parse an empty statement as a constant expression rather than having a
2792 // different type of node just for empty statements, that will be pruned from the AST anyway.
2793 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2794 node->setLine(location);
2795 return node;
2796}
2797
jchen104cdac9e2017-05-08 11:01:20 +08002798void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2799 const TSourceLoc &location)
2800{
2801 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2802 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2803 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2804 {
2805 error(location, "Requires both binding and offset", "layout");
2806 return;
2807 }
2808 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2809}
2810
Olli Etuahocce89652017-06-19 16:04:09 +03002811void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2812 const TPublicType &type,
2813 const TSourceLoc &loc)
2814{
2815 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2816 !getFragmentPrecisionHigh())
2817 {
2818 error(loc, "precision is not supported in fragment shader", "highp");
2819 }
2820
2821 if (!CanSetDefaultPrecisionOnType(type))
2822 {
2823 error(loc, "illegal type argument for default precision qualifier",
2824 getBasicString(type.getBasicType()));
2825 return;
2826 }
2827 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2828}
2829
Shaob5cc1192017-07-06 10:47:20 +08002830bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2831{
2832 switch (typeQualifier.layoutQualifier.primitiveType)
2833 {
2834 case EptLines:
2835 case EptLinesAdjacency:
2836 case EptTriangles:
2837 case EptTrianglesAdjacency:
2838 return typeQualifier.qualifier == EvqGeometryIn;
2839
2840 case EptLineStrip:
2841 case EptTriangleStrip:
2842 return typeQualifier.qualifier == EvqGeometryOut;
2843
2844 case EptPoints:
2845 return true;
2846
2847 default:
2848 UNREACHABLE();
2849 return false;
2850 }
2851}
2852
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002853void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2854 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002855{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002856 if (mGeometryShaderInputArraySize == 0u)
2857 {
2858 mGeometryShaderInputArraySize = inputArraySize;
2859 }
2860 else if (mGeometryShaderInputArraySize != inputArraySize)
2861 {
2862 error(line,
2863 "Array size or input primitive declaration doesn't match the size of earlier sized "
2864 "array inputs.",
2865 "layout");
2866 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002867}
2868
Shaob5cc1192017-07-06 10:47:20 +08002869bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2870{
2871 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2872
2873 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2874
2875 if (layoutQualifier.maxVertices != -1)
2876 {
2877 error(typeQualifier.line,
2878 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2879 return false;
2880 }
2881
2882 // Set mGeometryInputPrimitiveType if exists
2883 if (layoutQualifier.primitiveType != EptUndefined)
2884 {
2885 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2886 {
2887 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2888 return false;
2889 }
2890
2891 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2892 {
2893 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002894 setGeometryShaderInputArraySize(
2895 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2896 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002897 }
2898 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2899 {
2900 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2901 "layout");
2902 return false;
2903 }
2904 }
2905
2906 // Set mGeometryInvocations if exists
2907 if (layoutQualifier.invocations > 0)
2908 {
2909 if (mGeometryShaderInvocations == 0)
2910 {
2911 mGeometryShaderInvocations = layoutQualifier.invocations;
2912 }
2913 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2914 {
2915 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2916 "layout");
2917 return false;
2918 }
2919 }
2920
2921 return true;
2922}
2923
2924bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2925{
2926 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2927
2928 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2929
2930 if (layoutQualifier.invocations > 0)
2931 {
2932 error(typeQualifier.line,
2933 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2934 return false;
2935 }
2936
2937 // Set mGeometryOutputPrimitiveType if exists
2938 if (layoutQualifier.primitiveType != EptUndefined)
2939 {
2940 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2941 {
2942 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2943 return false;
2944 }
2945
2946 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2947 {
2948 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2949 }
2950 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2951 {
2952 error(typeQualifier.line,
2953 "primitive doesn't match earlier output primitive declaration", "layout");
2954 return false;
2955 }
2956 }
2957
2958 // Set mGeometryMaxVertices if exists
2959 if (layoutQualifier.maxVertices > -1)
2960 {
2961 if (mGeometryShaderMaxVertices == -1)
2962 {
2963 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2964 }
2965 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2966 {
2967 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2968 "layout");
2969 return false;
2970 }
2971 }
2972
2973 return true;
2974}
2975
Martin Radev70866b82016-07-22 15:27:42 +03002976void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002977{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002978 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002979 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002980
Martin Radev70866b82016-07-22 15:27:42 +03002981 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2982 typeQualifier.line);
2983
Jamie Madillc2128ff2016-07-04 10:26:17 -04002984 // It should never be the case, but some strange parser errors can send us here.
2985 if (layoutQualifier.isEmpty())
2986 {
2987 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002988 return;
2989 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002990
Martin Radev802abe02016-08-04 17:48:32 +03002991 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002992 {
Olli Etuaho43364892017-02-13 16:00:12 +00002993 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002994 return;
2995 }
2996
Olli Etuaho43364892017-02-13 16:00:12 +00002997 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2998
2999 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003000
3001 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3002
Andrei Volykhina5527072017-03-22 16:46:30 +03003003 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3004
jchen104cdac9e2017-05-08 11:01:20 +08003005 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3006
Qin Jiajiaca68d982017-09-18 16:41:56 +08003007 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3008 typeQualifier.qualifier);
3009
Martin Radev802abe02016-08-04 17:48:32 +03003010 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003011 {
Martin Radev802abe02016-08-04 17:48:32 +03003012 if (mComputeShaderLocalSizeDeclared &&
3013 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3014 {
3015 error(typeQualifier.line, "Work group size does not match the previous declaration",
3016 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003017 return;
3018 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003019
Martin Radev802abe02016-08-04 17:48:32 +03003020 if (mShaderVersion < 310)
3021 {
3022 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003023 return;
3024 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003025
Martin Radev4c4c8e72016-08-04 12:25:34 +03003026 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003027 {
3028 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003029 return;
3030 }
3031
3032 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3033 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3034
3035 const TConstantUnion *maxComputeWorkGroupSizeData =
3036 maxComputeWorkGroupSize->getConstPointer();
3037
3038 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3039 {
3040 if (layoutQualifier.localSize[i] != -1)
3041 {
3042 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3043 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3044 if (mComputeShaderLocalSize[i] < 1 ||
3045 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3046 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003047 std::stringstream reasonStream;
3048 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3049 << maxComputeWorkGroupSizeValue;
3050 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003051
Olli Etuaho4de340a2016-12-16 09:32:03 +00003052 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003053 return;
3054 }
3055 }
3056 }
3057
3058 mComputeShaderLocalSizeDeclared = true;
3059 }
Shaob5cc1192017-07-06 10:47:20 +08003060 else if (typeQualifier.qualifier == EvqGeometryIn)
3061 {
3062 if (mShaderVersion < 310)
3063 {
3064 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3065 return;
3066 }
3067
3068 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3069 {
3070 return;
3071 }
3072 }
3073 else if (typeQualifier.qualifier == EvqGeometryOut)
3074 {
3075 if (mShaderVersion < 310)
3076 {
3077 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3078 "layout");
3079 return;
3080 }
3081
3082 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3083 {
3084 return;
3085 }
3086 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003087 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3088 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003089 {
3090 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3091 // specification.
3092 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3093 {
3094 error(typeQualifier.line, "Number of views does not match the previous declaration",
3095 "layout");
3096 return;
3097 }
3098
3099 if (layoutQualifier.numViews == -1)
3100 {
3101 error(typeQualifier.line, "No num_views specified", "layout");
3102 return;
3103 }
3104
3105 if (layoutQualifier.numViews > mMaxNumViews)
3106 {
3107 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3108 "layout");
3109 return;
3110 }
3111
3112 mNumViews = layoutQualifier.numViews;
3113 }
Martin Radev802abe02016-08-04 17:48:32 +03003114 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003115 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003116 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003117 {
Martin Radev802abe02016-08-04 17:48:32 +03003118 return;
3119 }
3120
Jiajia Qinbc585152017-06-23 15:42:17 +08003121 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003122 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003123 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003124 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003125 return;
3126 }
3127
3128 if (mShaderVersion < 300)
3129 {
3130 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3131 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003132 return;
3133 }
3134
Olli Etuaho09b04a22016-12-15 13:30:26 +00003135 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003136
3137 if (layoutQualifier.matrixPacking != EmpUnspecified)
3138 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003139 if (typeQualifier.qualifier == EvqUniform)
3140 {
3141 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3142 }
3143 else if (typeQualifier.qualifier == EvqBuffer)
3144 {
3145 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3146 }
Martin Radev802abe02016-08-04 17:48:32 +03003147 }
3148
3149 if (layoutQualifier.blockStorage != EbsUnspecified)
3150 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003151 if (typeQualifier.qualifier == EvqUniform)
3152 {
3153 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3154 }
3155 else if (typeQualifier.qualifier == EvqBuffer)
3156 {
3157 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3158 }
Martin Radev802abe02016-08-04 17:48:32 +03003159 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003160 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003161}
3162
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003163TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3164 const TFunction &function,
3165 const TSourceLoc &location,
3166 bool insertParametersToSymbolTable)
3167{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003168 checkIsNotReserved(location, function.getName());
3169
Olli Etuahofe486322017-03-21 09:30:54 +00003170 TIntermFunctionPrototype *prototype =
3171 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003172 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3173 // point to the data that already exists in the symbol table.
3174 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3175 prototype->setLine(location);
3176
3177 for (size_t i = 0; i < function.getParamCount(); i++)
3178 {
3179 const TConstParameter &param = function.getParam(i);
3180
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003181 TIntermSymbol *symbol = nullptr;
3182
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003183 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3184 // be used for unused args).
3185 if (param.name != nullptr)
3186 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003187 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003188 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003189 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003190 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3191 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003192 {
3193 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3194 variable->getType());
3195 }
3196 else
3197 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003198 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003199 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003200 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003201 // Unsized type of a named parameter should have already been checked and sanitized.
3202 ASSERT(!param.type->isUnsizedArray());
3203 }
3204 else
3205 {
3206 if (param.type->isUnsizedArray())
3207 {
3208 error(location, "function parameter array must be sized at compile time", "[]");
3209 // We don't need to size the arrays since the parameter is unnamed and hence
3210 // inaccessible.
3211 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003212 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003213 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003214 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003215 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3216 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003217 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003218 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003219 symbol->setLine(location);
3220 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003221 }
3222 return prototype;
3223}
3224
Olli Etuaho16c745a2017-01-16 17:02:27 +00003225TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3226 const TFunction &parsedFunction,
3227 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003228{
Olli Etuaho476197f2016-10-11 13:59:08 +01003229 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3230 // first declaration. Either way the instance in the symbol table is used to track whether the
3231 // function is declared multiple times.
3232 TFunction *function = static_cast<TFunction *>(
3233 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3234 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003235 {
3236 // ESSL 1.00.17 section 4.2.7.
3237 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3238 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003239 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003240 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003241
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003242 TIntermFunctionPrototype *prototype =
3243 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003244
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003245 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003246
3247 if (!symbolTable.atGlobalLevel())
3248 {
3249 // ESSL 3.00.4 section 4.2.4.
3250 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003251 }
3252
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003253 return prototype;
3254}
3255
Olli Etuaho336b1472016-10-05 16:37:55 +01003256TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003257 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003258 TIntermBlock *functionBody,
3259 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003260{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003261 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003262 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3263 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003264 error(location, "function does not return a value:",
3265 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003266 }
3267
Olli Etuahof51fdd22016-10-03 10:03:40 +01003268 if (functionBody == nullptr)
3269 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003270 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003271 functionBody->setLine(location);
3272 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003273 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003274 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003275 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003276
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003277 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003278 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003279}
3280
Olli Etuaho476197f2016-10-11 13:59:08 +01003281void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3282 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003283 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003284{
Olli Etuaho476197f2016-10-11 13:59:08 +01003285 ASSERT(function);
3286 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003287 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003288 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003289
3290 if (builtIn)
3291 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003292 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003293 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003294 else
Jamie Madill185fb402015-06-12 15:48:48 -04003295 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003296 TFunction *prevDec = static_cast<TFunction *>(
3297 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3298
3299 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3300 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3301 // occurance.
3302 if (*function != prevDec)
3303 {
3304 // Swap the parameters of the previous declaration to the parameters of the function
3305 // definition (parameter names may differ).
3306 prevDec->swapParameters(**function);
3307
3308 // The function definition will share the same symbol as any previous declaration.
3309 *function = prevDec;
3310 }
3311
3312 if ((*function)->isDefined())
3313 {
3314 error(location, "function already has a body", (*function)->getName().c_str());
3315 }
3316
3317 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003318 }
Jamie Madill185fb402015-06-12 15:48:48 -04003319
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003320 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003321 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003322 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003323
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003324 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003325 setLoopNestingLevel(0);
3326}
3327
Jamie Madillb98c3a82015-07-23 14:26:04 -04003328TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003329{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003330 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003331 // We don't know at this point whether this is a function definition or a prototype.
3332 // The definition production code will check for redefinitions.
3333 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003334 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003335 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3336 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003337 //
3338 TFunction *prevDec =
3339 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303340
Olli Etuahod80f2942017-11-06 12:44:45 +02003341 for (size_t i = 0u; i < function->getParamCount(); ++i)
3342 {
3343 auto &param = function->getParam(i);
3344 if (param.type->isStructSpecifier())
3345 {
3346 // ESSL 3.00.6 section 12.10.
3347 error(location, "Function parameter type cannot be a structure definition",
Olli Etuaho7af63722017-11-13 15:03:40 +02003348 function->getName().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003349 }
3350 }
3351
Martin Radevda6254b2016-12-14 17:00:36 +02003352 if (getShaderVersion() >= 300 &&
3353 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3354 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303355 {
Martin Radevda6254b2016-12-14 17:00:36 +02003356 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303357 // Therefore overloading or redefining builtin functions is an error.
3358 error(location, "Name of a built-in function cannot be redeclared as function",
3359 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303360 }
3361 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003362 {
3363 if (prevDec->getReturnType() != function->getReturnType())
3364 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003365 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003366 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003367 }
3368 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3369 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003370 if (prevDec->getParam(i).type->getQualifier() !=
3371 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003372 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003373 error(location,
3374 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003375 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003376 }
3377 }
3378 }
3379
3380 //
3381 // Check for previously declared variables using the same name.
3382 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003383 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003384 if (prevSym)
3385 {
3386 if (!prevSym->isFunction())
3387 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003388 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003389 }
3390 }
3391 else
3392 {
3393 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003394 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003395 }
3396
3397 // We're at the inner scope level of the function's arguments and body statement.
3398 // Add the function prototype to the surrounding scope instead.
3399 symbolTable.getOuterLevel()->insert(function);
3400
Olli Etuaho78d13742017-01-18 13:06:10 +00003401 // Raise error message if main function takes any parameters or return anything other than void
3402 if (function->getName() == "main")
3403 {
3404 if (function->getParamCount() > 0)
3405 {
3406 error(location, "function cannot take any parameter(s)", "main");
3407 }
3408 if (function->getReturnType().getBasicType() != EbtVoid)
3409 {
3410 error(location, "main function cannot return a value",
3411 function->getReturnType().getBasicString());
3412 }
3413 }
3414
Jamie Madill185fb402015-06-12 15:48:48 -04003415 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003416 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3417 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003418 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3419 //
3420 return function;
3421}
3422
Olli Etuaho9de84a52016-06-14 17:36:01 +03003423TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3424 const TString *name,
3425 const TSourceLoc &location)
3426{
3427 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3428 {
3429 error(location, "no qualifiers allowed for function return",
3430 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003431 }
3432 if (!type.layoutQualifier.isEmpty())
3433 {
3434 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003435 }
jchen10cc2a10e2017-05-03 14:05:12 +08003436 // make sure an opaque type is not involved as well...
3437 std::string reason(getBasicString(type.getBasicType()));
3438 reason += "s can't be function return values";
3439 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003440 if (mShaderVersion < 300)
3441 {
3442 // Array return values are forbidden, but there's also no valid syntax for declaring array
3443 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003444 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003445
3446 if (type.isStructureContainingArrays())
3447 {
3448 // ESSL 1.00.17 section 6.1 Function Definitions
3449 error(location, "structures containing arrays can't be function return values",
3450 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003451 }
3452 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003453
3454 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003455 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003456}
3457
Olli Etuahocce89652017-06-19 16:04:09 +03003458TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3459{
Olli Etuahocce89652017-06-19 16:04:09 +03003460 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003461 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003462}
3463
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003464TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003465{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003466 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003467 {
3468 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3469 "[]");
3470 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003471 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003472 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003473 error(publicType.getLine(), "constructor can't be a structure definition",
3474 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003475 }
3476
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003477 TType *type = new TType(publicType);
3478 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003479 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003480 error(publicType.getLine(), "cannot construct this type",
3481 getBasicString(publicType.getBasicType()));
3482 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003483 }
3484
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003485 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003486}
3487
Olli Etuaho55bde912017-10-25 13:41:13 +03003488void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3489 const char *errorMessage,
3490 const char *token,
3491 TType *arrayType)
3492{
3493 if (arrayType->isUnsizedArray())
3494 {
3495 error(line, errorMessage, token);
3496 arrayType->sizeUnsizedArrays(TVector<unsigned int>());
3497 }
3498}
3499
3500TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003501 const TString *name,
3502 const TSourceLoc &nameLoc)
3503{
Olli Etuaho55bde912017-10-25 13:41:13 +03003504 ASSERT(type);
3505 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3506 type);
3507 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003508 {
3509 error(nameLoc, "illegal use of type 'void'", name->c_str());
3510 }
3511 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003512 TParameter param = {name, type};
3513 return param;
3514}
3515
Olli Etuaho55bde912017-10-25 13:41:13 +03003516TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3517 const TString *name,
3518 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003519{
Olli Etuaho55bde912017-10-25 13:41:13 +03003520 TType *type = new TType(publicType);
3521 return parseParameterDeclarator(type, name, nameLoc);
3522}
3523
3524TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3525 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003526 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003527 const TSourceLoc &arrayLoc,
3528 TPublicType *elementType)
3529{
3530 checkArrayElementIsNotArray(arrayLoc, *elementType);
3531 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003532 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003533 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003534}
3535
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003536bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3537 TType type,
3538 const TSourceLoc &line)
3539{
3540 if (arguments->empty())
3541 {
3542 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3543 return false;
3544 }
3545 for (TIntermNode *arg : *arguments)
3546 {
3547 TIntermTyped *element = arg->getAsTyped();
3548 ASSERT(element);
3549 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3550 if (dimensionalityFromElement > type.getArraySizes().size())
3551 {
3552 error(line, "constructing from a non-dereferenced array", "constructor");
3553 return false;
3554 }
3555 else if (dimensionalityFromElement < type.getArraySizes().size())
3556 {
3557 if (dimensionalityFromElement == 1u)
3558 {
3559 error(line, "implicitly sized array of arrays constructor argument is not an array",
3560 "constructor");
3561 }
3562 else
3563 {
3564 error(line,
3565 "implicitly sized array of arrays constructor argument dimensionality is too "
3566 "low",
3567 "constructor");
3568 }
3569 return false;
3570 }
3571 }
3572 return true;
3573}
3574
Jamie Madillb98c3a82015-07-23 14:26:04 -04003575// This function is used to test for the correctness of the parameters passed to various constructor
3576// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003577//
Olli Etuaho856c4972016-08-08 11:38:39 +03003578// Returns a node to add to the tree regardless of if an error was generated or not.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003579//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003580TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003581 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303582 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003583{
Olli Etuaho856c4972016-08-08 11:38:39 +03003584 if (type.isUnsizedArray())
3585 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003586 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003587 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003588 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003589 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003590 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003591 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3592 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003593 if (type.getOutermostArraySize() == 0u)
3594 {
3595 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3596 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003597 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3598 {
3599 if (type.getArraySizes()[i] == 0u)
3600 {
3601 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3602 }
3603 }
3604 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003605 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003606
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003607 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003608 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003609 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003610 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003611
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003612 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003613 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003614
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003615 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3616 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003617 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003618 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003619 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003620 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003621}
3622
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003623//
3624// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003625// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003626//
Olli Etuaho13389b62016-10-16 11:48:18 +01003627TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003628 const TTypeQualifierBuilder &typeQualifierBuilder,
3629 const TSourceLoc &nameLine,
3630 const TString &blockName,
3631 TFieldList *fieldList,
3632 const TString *instanceName,
3633 const TSourceLoc &instanceLine,
3634 TIntermTyped *arrayIndex,
3635 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003636{
Olli Etuaho856c4972016-08-08 11:38:39 +03003637 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003638
Olli Etuaho77ba4082016-12-16 12:01:18 +00003639 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003640
Jiajia Qinbc585152017-06-23 15:42:17 +08003641 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003642 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003643 error(typeQualifier.line,
3644 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3645 "3.10",
3646 getQualifierString(typeQualifier.qualifier));
3647 }
3648 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3649 {
3650 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003651 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003652 }
3653
Martin Radev70866b82016-07-22 15:27:42 +03003654 if (typeQualifier.invariant)
3655 {
3656 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3657 }
3658
Jiajia Qinbc585152017-06-23 15:42:17 +08003659 if (typeQualifier.qualifier != EvqBuffer)
3660 {
3661 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3662 }
Olli Etuaho43364892017-02-13 16:00:12 +00003663
jchen10af713a22017-04-19 09:10:56 +08003664 // add array index
3665 unsigned int arraySize = 0;
3666 if (arrayIndex != nullptr)
3667 {
3668 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3669 }
3670
3671 if (mShaderVersion < 310)
3672 {
3673 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3674 }
3675 else
3676 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003677 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3678 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003679 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003680
Andrei Volykhina5527072017-03-22 16:46:30 +03003681 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3682
Jamie Madill099c0f32013-06-20 11:55:52 -04003683 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003684 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003685 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3686 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003687
Jamie Madill099c0f32013-06-20 11:55:52 -04003688 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3689 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003690 if (typeQualifier.qualifier == EvqUniform)
3691 {
3692 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3693 }
3694 else if (typeQualifier.qualifier == EvqBuffer)
3695 {
3696 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3697 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003698 }
3699
Jamie Madill1566ef72013-06-20 11:55:54 -04003700 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3701 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003702 if (typeQualifier.qualifier == EvqUniform)
3703 {
3704 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3705 }
3706 else if (typeQualifier.qualifier == EvqBuffer)
3707 {
3708 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3709 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003710 }
3711
Olli Etuaho856c4972016-08-08 11:38:39 +03003712 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003713
Martin Radev2cc85b32016-08-05 16:22:53 +03003714 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3715
Olli Etuaho0f684632017-07-13 12:42:15 +03003716 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303717 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003718 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003719 }
3720
Jamie Madill98493dd2013-07-08 14:39:03 -04003721 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303722 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3723 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003724 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303725 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003726 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303727 {
jchen10cc2a10e2017-05-03 14:05:12 +08003728 std::string reason("unsupported type - ");
3729 reason += fieldType->getBasicString();
3730 reason += " types are not allowed in interface blocks";
3731 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003732 }
3733
Jamie Madill98493dd2013-07-08 14:39:03 -04003734 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003735 switch (qualifier)
3736 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003737 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003738 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003739 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003740 if (typeQualifier.qualifier == EvqBuffer)
3741 {
3742 error(field->line(), "invalid qualifier on shader storage block member",
3743 getQualifierString(qualifier));
3744 }
3745 break;
3746 case EvqBuffer:
3747 if (typeQualifier.qualifier == EvqUniform)
3748 {
3749 error(field->line(), "invalid qualifier on uniform block member",
3750 getQualifierString(qualifier));
3751 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003752 break;
3753 default:
3754 error(field->line(), "invalid qualifier on interface block member",
3755 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003756 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003757 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003758
Martin Radev70866b82016-07-22 15:27:42 +03003759 if (fieldType->isInvariant())
3760 {
3761 error(field->line(), "invalid qualifier on interface block member", "invariant");
3762 }
3763
Jamie Madilla5efff92013-06-06 11:56:47 -04003764 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003765 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003766 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003767 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003768
Jamie Madill98493dd2013-07-08 14:39:03 -04003769 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003770 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003771 error(field->line(), "invalid layout qualifier: cannot be used here",
3772 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003773 }
3774
Jamie Madill98493dd2013-07-08 14:39:03 -04003775 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003776 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003777 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003778 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003779 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003780 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003781 warning(field->line(),
3782 "extraneous layout qualifier: only has an effect on matrix types",
3783 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003784 }
3785
Jamie Madill98493dd2013-07-08 14:39:03 -04003786 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003787
3788 if (typeQualifier.qualifier == EvqBuffer)
3789 {
3790 // set memory qualifiers
3791 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3792 // qualified with a memory qualifier, it is as if all of its members were declared with
3793 // the same memory qualifier.
3794 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3795 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3796 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3797 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3798 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3799 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3800 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3801 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3802 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3803 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3804 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003805 }
3806
Jamie Madillb98c3a82015-07-23 14:26:04 -04003807 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003808 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003809 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3810 if (arrayIndex != nullptr)
3811 {
3812 interfaceBlockType.makeArray(arraySize);
3813 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003814
3815 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003816 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003817
Jamie Madill98493dd2013-07-08 14:39:03 -04003818 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003819 {
3820 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003821 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3822 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003823 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303824 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003825
3826 // set parent pointer of the field variable
3827 fieldType->setInterfaceBlock(interfaceBlock);
3828
Olli Etuaho0f684632017-07-13 12:42:15 +03003829 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003830
Olli Etuaho0f684632017-07-13 12:42:15 +03003831 if (fieldVariable)
3832 {
3833 fieldVariable->setQualifier(typeQualifier.qualifier);
3834 }
3835 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303836 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003837 error(field->line(), "redefinition of an interface block member name",
3838 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003839 }
3840 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003841 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003842 }
3843 else
3844 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003845 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003846
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003847 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003848 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3849 if (instanceTypeDef)
3850 {
3851 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003852 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003853 }
3854 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303855 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003856 error(instanceLine, "redefinition of an interface block instance name",
3857 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003858 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003859 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003860 }
3861
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003862 TIntermDeclaration *declaration = nullptr;
3863
3864 if (symbolId)
3865 {
3866 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3867 blockSymbol->setLine(typeQualifier.line);
3868 declaration = new TIntermDeclaration();
3869 declaration->appendDeclarator(blockSymbol);
3870 declaration->setLine(nameLine);
3871 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003872
3873 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003874 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003875}
3876
Olli Etuaho383b7912016-08-05 11:22:59 +03003877void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003878{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003879 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003880
3881 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003882 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303883 if (mStructNestingLevel > 1)
3884 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003885 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003886 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003887}
3888
3889void TParseContext::exitStructDeclaration()
3890{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003891 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003892}
3893
Olli Etuaho8a176262016-08-16 14:23:01 +03003894void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003895{
Jamie Madillacb4b812016-11-07 13:50:29 -05003896 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303897 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003898 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003899 }
3900
Arun Patole7e7e68d2015-05-22 12:02:25 +05303901 if (field.type()->getBasicType() != EbtStruct)
3902 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003903 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003904 }
3905
3906 // We're already inside a structure definition at this point, so add
3907 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303908 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3909 {
Jamie Madill41a49272014-03-18 16:10:13 -04003910 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003911 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3912 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003913 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003914 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003915 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003916 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003917}
3918
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003919//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003920// Parse an array index expression
3921//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003922TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3923 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303924 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003925{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003926 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3927 {
3928 if (baseExpression->getAsSymbolNode())
3929 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303930 error(location, " left of '[' is not of type array, matrix, or vector ",
3931 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003932 }
3933 else
3934 {
3935 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3936 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003937
Olli Etuaho3ec75682017-07-05 17:02:55 +03003938 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003939 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003940
Jiawei Shaod8105a02017-08-08 09:54:36 +08003941 if (baseExpression->getQualifier() == EvqPerVertexIn)
3942 {
3943 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3944 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3945 {
3946 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3947 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3948 }
3949 }
3950
Jamie Madill21c1e452014-12-29 11:33:41 -05003951 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3952
Olli Etuaho36b05142015-11-12 13:10:42 +02003953 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3954 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3955 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3956 // index is a constant expression.
3957 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3958 {
3959 if (baseExpression->isInterfaceBlock())
3960 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003961 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3962 switch (baseExpression->getQualifier())
3963 {
3964 case EvqPerVertexIn:
3965 break;
3966 case EvqUniform:
3967 case EvqBuffer:
3968 error(location,
3969 "array indexes for uniform block arrays and shader storage block arrays "
3970 "must be constant integral expressions",
3971 "[");
3972 break;
3973 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003974 // We can reach here only in error cases.
3975 ASSERT(mDiagnostics->numErrors() > 0);
3976 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003977 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003978 }
3979 else if (baseExpression->getQualifier() == EvqFragmentOut)
3980 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003981 error(location,
3982 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003983 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003984 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3985 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003986 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003987 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003988 }
3989
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003990 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003991 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003992 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3993 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3994 // constant fold expressions that are not constant expressions). The most compatible way to
3995 // handle this case is to report a warning instead of an error and force the index to be in
3996 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003997 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003998 int index = 0;
3999 if (indexConstantUnion->getBasicType() == EbtInt)
4000 {
4001 index = indexConstantUnion->getIConst(0);
4002 }
4003 else if (indexConstantUnion->getBasicType() == EbtUInt)
4004 {
4005 index = static_cast<int>(indexConstantUnion->getUConst(0));
4006 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004007
4008 int safeIndex = -1;
4009
4010 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04004011 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004012 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004013 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004014 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004015 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004016 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004017 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00004018 "GL_EXT_draw_buffers is disabled",
4019 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004020 safeIndex = 0;
4021 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03004022 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004023 // Only do generic out-of-range check if similar error hasn't already been reported.
4024 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004025 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004026 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004027 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00004028 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004029 }
4030 }
4031 else if (baseExpression->isMatrix())
4032 {
4033 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03004034 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00004035 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04004036 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004037 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04004038 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004039 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
4040 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00004041 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004042 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004043
4044 ASSERT(safeIndex >= 0);
4045 // Data of constant unions can't be changed, because it may be shared with other
4046 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4047 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03004048 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004049 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004050 TConstantUnion *safeConstantUnion = new TConstantUnion();
4051 safeConstantUnion->setIConst(safeIndex);
4052 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03004053 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004054 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004055
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004056 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4057 node->setLine(location);
4058 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004059 }
Jamie Madill7164cf42013-07-08 13:30:59 -04004060 else
4061 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004062 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4063 node->setLine(location);
4064 // Indirect indexing can never be constant folded.
4065 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04004066 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004067}
4068
Olli Etuaho90892fb2016-07-14 14:44:51 +03004069int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
4070 const TSourceLoc &location,
4071 int index,
4072 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00004073 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004074{
4075 if (index >= arraySize || index < 0)
4076 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004077 std::stringstream reasonStream;
4078 reasonStream << reason << " '" << index << "'";
4079 std::string token = reasonStream.str();
4080 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03004081 if (index < 0)
4082 {
4083 return 0;
4084 }
4085 else
4086 {
4087 return arraySize - 1;
4088 }
4089 }
4090 return index;
4091}
4092
Jamie Madillb98c3a82015-07-23 14:26:04 -04004093TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4094 const TSourceLoc &dotLocation,
4095 const TString &fieldString,
4096 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004097{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004098 if (baseExpression->isArray())
4099 {
4100 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004101 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004102 }
4103
4104 if (baseExpression->isVector())
4105 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004106 TVector<int> fieldOffsets;
4107 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4108 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004109 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004110 fieldOffsets.resize(1);
4111 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004112 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004113 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4114 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004115
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004116 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004117 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004118 else if (baseExpression->getBasicType() == EbtStruct)
4119 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304120 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004121 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004122 {
4123 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004124 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004125 }
4126 else
4127 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004128 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004129 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004130 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004131 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004132 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004133 {
4134 fieldFound = true;
4135 break;
4136 }
4137 }
4138 if (fieldFound)
4139 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004140 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004141 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004142 TIntermBinary *node =
4143 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4144 node->setLine(dotLocation);
4145 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004146 }
4147 else
4148 {
4149 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004150 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004151 }
4152 }
4153 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004154 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004155 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304156 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004157 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004158 {
4159 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004160 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004161 }
4162 else
4163 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004164 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004165 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004166 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004167 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004168 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004169 {
4170 fieldFound = true;
4171 break;
4172 }
4173 }
4174 if (fieldFound)
4175 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004176 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004177 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004178 TIntermBinary *node =
4179 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4180 node->setLine(dotLocation);
4181 // Indexing interface blocks can never be constant folded.
4182 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004183 }
4184 else
4185 {
4186 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004187 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004188 }
4189 }
4190 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004191 else
4192 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004193 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004194 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004195 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304196 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004197 }
4198 else
4199 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304200 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004201 " field selection requires structure, vector, or interface block on left hand "
4202 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304203 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004204 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004205 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004206 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004207}
4208
Jamie Madillb98c3a82015-07-23 14:26:04 -04004209TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4210 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004211{
Jamie Madill2f294c92017-11-20 14:47:26 -05004212 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004213
4214 if (qualifierType == "shared")
4215 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004216 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004217 {
4218 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4219 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004220 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004221 }
4222 else if (qualifierType == "packed")
4223 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004224 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004225 {
4226 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4227 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004228 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004229 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004230 else if (qualifierType == "std430")
4231 {
4232 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4233 qualifier.blockStorage = EbsStd430;
4234 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004235 else if (qualifierType == "std140")
4236 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004237 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004238 }
4239 else if (qualifierType == "row_major")
4240 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004241 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004242 }
4243 else if (qualifierType == "column_major")
4244 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004245 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004246 }
4247 else if (qualifierType == "location")
4248 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004249 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4250 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004251 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004252 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004253 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004254 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4255 {
4256 qualifier.yuv = true;
4257 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004258 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004259 else if (qualifierType == "rgba32f")
4260 {
4261 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4262 qualifier.imageInternalFormat = EiifRGBA32F;
4263 }
4264 else if (qualifierType == "rgba16f")
4265 {
4266 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4267 qualifier.imageInternalFormat = EiifRGBA16F;
4268 }
4269 else if (qualifierType == "r32f")
4270 {
4271 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4272 qualifier.imageInternalFormat = EiifR32F;
4273 }
4274 else if (qualifierType == "rgba8")
4275 {
4276 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4277 qualifier.imageInternalFormat = EiifRGBA8;
4278 }
4279 else if (qualifierType == "rgba8_snorm")
4280 {
4281 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4282 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4283 }
4284 else if (qualifierType == "rgba32i")
4285 {
4286 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4287 qualifier.imageInternalFormat = EiifRGBA32I;
4288 }
4289 else if (qualifierType == "rgba16i")
4290 {
4291 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4292 qualifier.imageInternalFormat = EiifRGBA16I;
4293 }
4294 else if (qualifierType == "rgba8i")
4295 {
4296 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4297 qualifier.imageInternalFormat = EiifRGBA8I;
4298 }
4299 else if (qualifierType == "r32i")
4300 {
4301 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4302 qualifier.imageInternalFormat = EiifR32I;
4303 }
4304 else if (qualifierType == "rgba32ui")
4305 {
4306 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4307 qualifier.imageInternalFormat = EiifRGBA32UI;
4308 }
4309 else if (qualifierType == "rgba16ui")
4310 {
4311 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4312 qualifier.imageInternalFormat = EiifRGBA16UI;
4313 }
4314 else if (qualifierType == "rgba8ui")
4315 {
4316 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4317 qualifier.imageInternalFormat = EiifRGBA8UI;
4318 }
4319 else if (qualifierType == "r32ui")
4320 {
4321 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4322 qualifier.imageInternalFormat = EiifR32UI;
4323 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004324 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4325 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004326 {
4327 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4328 qualifier.primitiveType = EptPoints;
4329 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004330 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4331 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004332 {
4333 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4334 qualifier.primitiveType = EptLines;
4335 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004336 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4337 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004338 {
4339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4340 qualifier.primitiveType = EptLinesAdjacency;
4341 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004342 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4343 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004344 {
4345 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4346 qualifier.primitiveType = EptTriangles;
4347 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004348 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4349 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004350 {
4351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4352 qualifier.primitiveType = EptTrianglesAdjacency;
4353 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004354 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4355 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004356 {
4357 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4358 qualifier.primitiveType = EptLineStrip;
4359 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004360 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4361 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004362 {
4363 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4364 qualifier.primitiveType = EptTriangleStrip;
4365 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004366
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004367 else
4368 {
4369 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004370 }
4371
Jamie Madilla5efff92013-06-06 11:56:47 -04004372 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004373}
4374
Martin Radev802abe02016-08-04 17:48:32 +03004375void TParseContext::parseLocalSize(const TString &qualifierType,
4376 const TSourceLoc &qualifierTypeLine,
4377 int intValue,
4378 const TSourceLoc &intValueLine,
4379 const std::string &intValueString,
4380 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004381 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004382{
Olli Etuaho856c4972016-08-08 11:38:39 +03004383 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004384 if (intValue < 1)
4385 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004386 std::stringstream reasonStream;
4387 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4388 std::string reason = reasonStream.str();
4389 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004390 }
4391 (*localSize)[index] = intValue;
4392}
4393
Olli Etuaho09b04a22016-12-15 13:30:26 +00004394void TParseContext::parseNumViews(int intValue,
4395 const TSourceLoc &intValueLine,
4396 const std::string &intValueString,
4397 int *numViews)
4398{
4399 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4400 // specification.
4401 if (intValue < 1)
4402 {
4403 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4404 }
4405 *numViews = intValue;
4406}
4407
Shaob5cc1192017-07-06 10:47:20 +08004408void TParseContext::parseInvocations(int intValue,
4409 const TSourceLoc &intValueLine,
4410 const std::string &intValueString,
4411 int *numInvocations)
4412{
4413 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4414 // it doesn't make sense to accept invocations <= 0.
4415 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4416 {
4417 error(intValueLine,
4418 "out of range: invocations must be in the range of [1, "
4419 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4420 intValueString.c_str());
4421 }
4422 else
4423 {
4424 *numInvocations = intValue;
4425 }
4426}
4427
4428void TParseContext::parseMaxVertices(int intValue,
4429 const TSourceLoc &intValueLine,
4430 const std::string &intValueString,
4431 int *maxVertices)
4432{
4433 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4434 // it doesn't make sense to accept max_vertices < 0.
4435 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4436 {
4437 error(
4438 intValueLine,
4439 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4440 intValueString.c_str());
4441 }
4442 else
4443 {
4444 *maxVertices = intValue;
4445 }
4446}
4447
Jamie Madillb98c3a82015-07-23 14:26:04 -04004448TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4449 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004450 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304451 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004452{
Jamie Madill2f294c92017-11-20 14:47:26 -05004453 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004454
Martin Radev802abe02016-08-04 17:48:32 +03004455 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004456
Martin Radev802abe02016-08-04 17:48:32 +03004457 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004458 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004459 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004460 if (intValue < 0)
4461 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004462 error(intValueLine, "out of range: location must be non-negative",
4463 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004464 }
4465 else
4466 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004467 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004468 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004469 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004470 }
Olli Etuaho43364892017-02-13 16:00:12 +00004471 else if (qualifierType == "binding")
4472 {
4473 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4474 if (intValue < 0)
4475 {
4476 error(intValueLine, "out of range: binding must be non-negative",
4477 intValueString.c_str());
4478 }
4479 else
4480 {
4481 qualifier.binding = intValue;
4482 }
4483 }
jchen104cdac9e2017-05-08 11:01:20 +08004484 else if (qualifierType == "offset")
4485 {
4486 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4487 if (intValue < 0)
4488 {
4489 error(intValueLine, "out of range: offset must be non-negative",
4490 intValueString.c_str());
4491 }
4492 else
4493 {
4494 qualifier.offset = intValue;
4495 }
4496 }
Martin Radev802abe02016-08-04 17:48:32 +03004497 else if (qualifierType == "local_size_x")
4498 {
4499 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4500 &qualifier.localSize);
4501 }
4502 else if (qualifierType == "local_size_y")
4503 {
4504 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4505 &qualifier.localSize);
4506 }
4507 else if (qualifierType == "local_size_z")
4508 {
4509 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4510 &qualifier.localSize);
4511 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004512 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004513 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004514 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4515 {
4516 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4517 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004518 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004519 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4520 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004521 {
4522 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4523 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004524 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4525 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004526 {
4527 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4528 }
4529
Martin Radev802abe02016-08-04 17:48:32 +03004530 else
4531 {
4532 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004533 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004534
Jamie Madilla5efff92013-06-06 11:56:47 -04004535 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004536}
4537
Olli Etuaho613b9592016-09-05 12:05:53 +03004538TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4539{
4540 return new TTypeQualifierBuilder(
4541 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4542 mShaderVersion);
4543}
4544
Olli Etuahocce89652017-06-19 16:04:09 +03004545TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4546 const TSourceLoc &loc)
4547{
4548 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4549 return new TStorageQualifierWrapper(qualifier, loc);
4550}
4551
4552TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4553{
4554 if (getShaderType() == GL_VERTEX_SHADER)
4555 {
4556 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4557 }
4558 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4559}
4560
4561TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4562{
4563 if (declaringFunction())
4564 {
4565 return new TStorageQualifierWrapper(EvqIn, loc);
4566 }
Shaob5cc1192017-07-06 10:47:20 +08004567
4568 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004569 {
Shaob5cc1192017-07-06 10:47:20 +08004570 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004571 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004572 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004573 {
4574 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4575 }
4576 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004577 }
Shaob5cc1192017-07-06 10:47:20 +08004578 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004579 {
Shaob5cc1192017-07-06 10:47:20 +08004580 if (mShaderVersion < 300)
4581 {
4582 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4583 }
4584 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004585 }
Shaob5cc1192017-07-06 10:47:20 +08004586 case GL_COMPUTE_SHADER:
4587 {
4588 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4589 }
4590 case GL_GEOMETRY_SHADER_OES:
4591 {
4592 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4593 }
4594 default:
4595 {
4596 UNREACHABLE();
4597 return new TStorageQualifierWrapper(EvqLast, loc);
4598 }
Olli Etuahocce89652017-06-19 16:04:09 +03004599 }
Olli Etuahocce89652017-06-19 16:04:09 +03004600}
4601
4602TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4603{
4604 if (declaringFunction())
4605 {
4606 return new TStorageQualifierWrapper(EvqOut, loc);
4607 }
Shaob5cc1192017-07-06 10:47:20 +08004608 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004609 {
Shaob5cc1192017-07-06 10:47:20 +08004610 case GL_VERTEX_SHADER:
4611 {
4612 if (mShaderVersion < 300)
4613 {
4614 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4615 }
4616 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4617 }
4618 case GL_FRAGMENT_SHADER:
4619 {
4620 if (mShaderVersion < 300)
4621 {
4622 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4623 }
4624 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4625 }
4626 case GL_COMPUTE_SHADER:
4627 {
4628 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4629 return new TStorageQualifierWrapper(EvqLast, loc);
4630 }
4631 case GL_GEOMETRY_SHADER_OES:
4632 {
4633 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4634 }
4635 default:
4636 {
4637 UNREACHABLE();
4638 return new TStorageQualifierWrapper(EvqLast, loc);
4639 }
Olli Etuahocce89652017-06-19 16:04:09 +03004640 }
Olli Etuahocce89652017-06-19 16:04:09 +03004641}
4642
4643TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4644{
4645 if (!declaringFunction())
4646 {
4647 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4648 }
4649 return new TStorageQualifierWrapper(EvqInOut, loc);
4650}
4651
Jamie Madillb98c3a82015-07-23 14:26:04 -04004652TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004653 TLayoutQualifier rightQualifier,
4654 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004655{
Martin Radevc28888b2016-07-22 15:27:42 +03004656 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004657 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004658}
4659
Olli Etuahocce89652017-06-19 16:04:09 +03004660TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4661{
4662 checkIsNotReserved(loc, *identifier);
4663 TType *type = new TType(EbtVoid, EbpUndefined);
4664 return new TField(type, identifier, loc);
4665}
4666
4667TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4668 const TSourceLoc &loc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004669 const TVector<unsigned int> &arraySizes,
Olli Etuahocce89652017-06-19 16:04:09 +03004670 const TSourceLoc &arraySizeLoc)
4671{
4672 checkIsNotReserved(loc, *identifier);
4673
4674 TType *type = new TType(EbtVoid, EbpUndefined);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004675 type->makeArrays(arraySizes);
Olli Etuahocce89652017-06-19 16:04:09 +03004676
4677 return new TField(type, identifier, loc);
4678}
4679
Olli Etuaho722bfb52017-10-26 17:00:11 +03004680void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4681 const TFieldList::const_iterator end,
4682 const TString &name,
4683 const TSourceLoc &location)
4684{
4685 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4686 {
4687 if ((*fieldIter)->name() == name)
4688 {
4689 error(location, "duplicate field name in structure", name.c_str());
4690 }
4691 }
4692}
4693
4694TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4695{
4696 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4697 ++fieldIter)
4698 {
4699 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4700 location);
4701 }
4702 return fields;
4703}
4704
Olli Etuaho4de340a2016-12-16 09:32:03 +00004705TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4706 const TFieldList *newlyAddedFields,
4707 const TSourceLoc &location)
4708{
4709 for (TField *field : *newlyAddedFields)
4710 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004711 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4712 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004713 processedFields->push_back(field);
4714 }
4715 return processedFields;
4716}
4717
Martin Radev70866b82016-07-22 15:27:42 +03004718TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4719 const TTypeQualifierBuilder &typeQualifierBuilder,
4720 TPublicType *typeSpecifier,
4721 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004722{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004723 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004724
Martin Radev70866b82016-07-22 15:27:42 +03004725 typeSpecifier->qualifier = typeQualifier.qualifier;
4726 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004727 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004728 typeSpecifier->invariant = typeQualifier.invariant;
4729 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304730 {
Martin Radev70866b82016-07-22 15:27:42 +03004731 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004732 }
Martin Radev70866b82016-07-22 15:27:42 +03004733 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004734}
4735
Jamie Madillb98c3a82015-07-23 14:26:04 -04004736TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004737 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004738{
Martin Radev4a9cd802016-09-01 16:51:51 +03004739 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4740 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004741
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004742 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4743 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004744
Martin Radev4a9cd802016-09-01 16:51:51 +03004745 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004746
Olli Etuaho55bde912017-10-25 13:41:13 +03004747 for (TField *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304748 {
Olli Etuaho55bde912017-10-25 13:41:13 +03004749 auto declaratorArraySizes = declarator->type()->getArraySizes();
Olli Etuaho7881cfd2017-08-23 18:00:21 +03004750 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004751 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304752 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004753 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004754 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004755
Olli Etuaho55bde912017-10-25 13:41:13 +03004756 TType *type = declarator->type();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004757 *type = TType(typeSpecifier);
4758 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304759 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004760 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004761 }
Olli Etuaho55bde912017-10-25 13:41:13 +03004762 checkIsNotUnsizedArray(typeSpecifier.getLine(),
4763 "array members of structs must specify a size",
4764 declarator->name().c_str(), type);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004765
Olli Etuaho55bde912017-10-25 13:41:13 +03004766 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *declarator);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004767 }
4768
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004769 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004770}
4771
Martin Radev4a9cd802016-09-01 16:51:51 +03004772TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4773 const TSourceLoc &nameLine,
4774 const TString *structName,
4775 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004776{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004777 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004778
Jamie Madill9b820842015-02-12 10:40:10 -05004779 // Store a bool in the struct if we're at global scope, to allow us to
4780 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004781 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004782
Jamie Madill98493dd2013-07-08 14:39:03 -04004783 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004784 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004785 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004786 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304787 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004788 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004789 }
4790 }
4791
4792 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004793 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004794 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004795 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004796 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004797 switch (qualifier)
4798 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004799 case EvqGlobal:
4800 case EvqTemporary:
4801 break;
4802 default:
4803 error(field.line(), "invalid qualifier on struct member",
4804 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004805 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004806 }
Martin Radev70866b82016-07-22 15:27:42 +03004807 if (field.type()->isInvariant())
4808 {
4809 error(field.line(), "invalid qualifier on struct member", "invariant");
4810 }
jchen104cdac9e2017-05-08 11:01:20 +08004811 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4812 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004813 {
4814 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4815 }
4816
Olli Etuaho43364892017-02-13 16:00:12 +00004817 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4818
4819 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004820
4821 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004822 }
4823
Martin Radev4a9cd802016-09-01 16:51:51 +03004824 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004825 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004826 exitStructDeclaration();
4827
Martin Radev4a9cd802016-09-01 16:51:51 +03004828 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004829}
4830
Jamie Madillb98c3a82015-07-23 14:26:04 -04004831TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004832 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004833 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004834{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004835 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004836 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004837 init->isVector())
4838 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004839 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4840 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004841 return nullptr;
4842 }
4843
Olli Etuaho923ecef2017-10-11 12:01:38 +03004844 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004845 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004846 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004847 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004848 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004849 }
4850
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004851 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4852 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004853 return node;
4854}
4855
4856TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4857{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004858 if (mSwitchNestingLevel == 0)
4859 {
4860 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004861 return nullptr;
4862 }
4863 if (condition == nullptr)
4864 {
4865 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004866 return nullptr;
4867 }
4868 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004869 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004870 {
4871 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004872 }
4873 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004874 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4875 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4876 // fold in case labels.
4877 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004878 {
4879 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004880 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004881 TIntermCase *node = new TIntermCase(condition);
4882 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004883 return node;
4884}
4885
4886TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4887{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004888 if (mSwitchNestingLevel == 0)
4889 {
4890 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004891 return nullptr;
4892 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004893 TIntermCase *node = new TIntermCase(nullptr);
4894 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004895 return node;
4896}
4897
Jamie Madillb98c3a82015-07-23 14:26:04 -04004898TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4899 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004900 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004901{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004902 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004903
4904 switch (op)
4905 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004906 case EOpLogicalNot:
4907 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4908 child->isVector())
4909 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004910 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004911 return nullptr;
4912 }
4913 break;
4914 case EOpBitwiseNot:
4915 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4916 child->isMatrix() || child->isArray())
4917 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004918 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004919 return nullptr;
4920 }
4921 break;
4922 case EOpPostIncrement:
4923 case EOpPreIncrement:
4924 case EOpPostDecrement:
4925 case EOpPreDecrement:
4926 case EOpNegative:
4927 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004928 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4929 child->getBasicType() == EbtBool || child->isArray() ||
4930 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004931 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004932 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004933 return nullptr;
4934 }
4935 // Operators for built-ins are already type checked against their prototype.
4936 default:
4937 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004938 }
4939
Jiajia Qinbc585152017-06-23 15:42:17 +08004940 if (child->getMemoryQualifier().writeonly)
4941 {
4942 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4943 return nullptr;
4944 }
4945
Olli Etuahof119a262016-08-19 15:54:22 +03004946 TIntermUnary *node = new TIntermUnary(op, child);
4947 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004948
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004949 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004950}
4951
Olli Etuaho09b22472015-02-11 11:47:26 +02004952TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4953{
Olli Etuahocce89652017-06-19 16:04:09 +03004954 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004955 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004956 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004957 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004958 return child;
4959 }
4960 return node;
4961}
4962
Jamie Madillb98c3a82015-07-23 14:26:04 -04004963TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4964 TIntermTyped *child,
4965 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004966{
Olli Etuaho856c4972016-08-08 11:38:39 +03004967 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004968 return addUnaryMath(op, child, loc);
4969}
4970
Jamie Madillb98c3a82015-07-23 14:26:04 -04004971bool TParseContext::binaryOpCommonCheck(TOperator op,
4972 TIntermTyped *left,
4973 TIntermTyped *right,
4974 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004975{
jchen10b4cf5652017-05-05 18:51:17 +08004976 // Check opaque types are not allowed to be operands in expressions other than array indexing
4977 // and structure member selection.
4978 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4979 {
4980 switch (op)
4981 {
4982 case EOpIndexDirect:
4983 case EOpIndexIndirect:
4984 break;
4985 case EOpIndexDirectStruct:
4986 UNREACHABLE();
4987
4988 default:
4989 error(loc, "Invalid operation for variables with an opaque type",
4990 GetOperatorString(op));
4991 return false;
4992 }
4993 }
jchen10cc2a10e2017-05-03 14:05:12 +08004994
Jiajia Qinbc585152017-06-23 15:42:17 +08004995 if (right->getMemoryQualifier().writeonly)
4996 {
4997 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4998 return false;
4999 }
5000
5001 if (left->getMemoryQualifier().writeonly)
5002 {
5003 switch (op)
5004 {
5005 case EOpAssign:
5006 case EOpInitialize:
5007 case EOpIndexDirect:
5008 case EOpIndexIndirect:
5009 case EOpIndexDirectStruct:
5010 case EOpIndexDirectInterfaceBlock:
5011 break;
5012 default:
5013 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5014 return false;
5015 }
5016 }
5017
Olli Etuaho244be012016-08-18 15:26:02 +03005018 if (left->getType().getStruct() || right->getType().getStruct())
5019 {
5020 switch (op)
5021 {
5022 case EOpIndexDirectStruct:
5023 ASSERT(left->getType().getStruct());
5024 break;
5025 case EOpEqual:
5026 case EOpNotEqual:
5027 case EOpAssign:
5028 case EOpInitialize:
5029 if (left->getType() != right->getType())
5030 {
5031 return false;
5032 }
5033 break;
5034 default:
5035 error(loc, "Invalid operation for structs", GetOperatorString(op));
5036 return false;
5037 }
5038 }
5039
Olli Etuaho94050052017-05-08 14:17:44 +03005040 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5041 {
5042 switch (op)
5043 {
5044 case EOpIndexDirectInterfaceBlock:
5045 ASSERT(left->getType().getInterfaceBlock());
5046 break;
5047 default:
5048 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5049 return false;
5050 }
5051 }
5052
Olli Etuahod6b14282015-03-17 14:31:35 +02005053 if (left->isArray() || right->isArray())
5054 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005055 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005056 {
5057 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5058 return false;
5059 }
5060
5061 if (left->isArray() != right->isArray())
5062 {
5063 error(loc, "array / non-array mismatch", GetOperatorString(op));
5064 return false;
5065 }
5066
5067 switch (op)
5068 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005069 case EOpEqual:
5070 case EOpNotEqual:
5071 case EOpAssign:
5072 case EOpInitialize:
5073 break;
5074 default:
5075 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5076 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005077 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005078 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03005079 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005080 {
5081 error(loc, "array size mismatch", GetOperatorString(op));
5082 return false;
5083 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005084 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005085
5086 // Check ops which require integer / ivec parameters
5087 bool isBitShift = false;
5088 switch (op)
5089 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005090 case EOpBitShiftLeft:
5091 case EOpBitShiftRight:
5092 case EOpBitShiftLeftAssign:
5093 case EOpBitShiftRightAssign:
5094 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5095 // check that the basic type is an integer type.
5096 isBitShift = true;
5097 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5098 {
5099 return false;
5100 }
5101 break;
5102 case EOpBitwiseAnd:
5103 case EOpBitwiseXor:
5104 case EOpBitwiseOr:
5105 case EOpBitwiseAndAssign:
5106 case EOpBitwiseXorAssign:
5107 case EOpBitwiseOrAssign:
5108 // It is enough to check the type of only one operand, since later it
5109 // is checked that the operand types match.
5110 if (!IsInteger(left->getBasicType()))
5111 {
5112 return false;
5113 }
5114 break;
5115 default:
5116 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005117 }
5118
5119 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5120 // So the basic type should usually match.
5121 if (!isBitShift && left->getBasicType() != right->getBasicType())
5122 {
5123 return false;
5124 }
5125
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005126 // Check that:
5127 // 1. Type sizes match exactly on ops that require that.
5128 // 2. Restrictions for structs that contain arrays or samplers are respected.
5129 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005130 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005131 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005132 case EOpAssign:
5133 case EOpInitialize:
5134 case EOpEqual:
5135 case EOpNotEqual:
5136 // ESSL 1.00 sections 5.7, 5.8, 5.9
5137 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5138 {
5139 error(loc, "undefined operation for structs containing arrays",
5140 GetOperatorString(op));
5141 return false;
5142 }
5143 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5144 // we interpret the spec so that this extends to structs containing samplers,
5145 // similarly to ESSL 1.00 spec.
5146 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5147 left->getType().isStructureContainingSamplers())
5148 {
5149 error(loc, "undefined operation for structs containing samplers",
5150 GetOperatorString(op));
5151 return false;
5152 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005153
Olli Etuahoe1805592017-01-02 16:41:20 +00005154 if ((left->getNominalSize() != right->getNominalSize()) ||
5155 (left->getSecondarySize() != right->getSecondarySize()))
5156 {
5157 error(loc, "dimension mismatch", GetOperatorString(op));
5158 return false;
5159 }
5160 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005161 case EOpLessThan:
5162 case EOpGreaterThan:
5163 case EOpLessThanEqual:
5164 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005165 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005166 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005167 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005168 return false;
5169 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005170 break;
5171 case EOpAdd:
5172 case EOpSub:
5173 case EOpDiv:
5174 case EOpIMod:
5175 case EOpBitShiftLeft:
5176 case EOpBitShiftRight:
5177 case EOpBitwiseAnd:
5178 case EOpBitwiseXor:
5179 case EOpBitwiseOr:
5180 case EOpAddAssign:
5181 case EOpSubAssign:
5182 case EOpDivAssign:
5183 case EOpIModAssign:
5184 case EOpBitShiftLeftAssign:
5185 case EOpBitShiftRightAssign:
5186 case EOpBitwiseAndAssign:
5187 case EOpBitwiseXorAssign:
5188 case EOpBitwiseOrAssign:
5189 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5190 {
5191 return false;
5192 }
5193
5194 // Are the sizes compatible?
5195 if (left->getNominalSize() != right->getNominalSize() ||
5196 left->getSecondarySize() != right->getSecondarySize())
5197 {
5198 // If the nominal sizes of operands do not match:
5199 // One of them must be a scalar.
5200 if (!left->isScalar() && !right->isScalar())
5201 return false;
5202
5203 // In the case of compound assignment other than multiply-assign,
5204 // the right side needs to be a scalar. Otherwise a vector/matrix
5205 // would be assigned to a scalar. A scalar can't be shifted by a
5206 // vector either.
5207 if (!right->isScalar() &&
5208 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5209 return false;
5210 }
5211 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005212 default:
5213 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005214 }
5215
Olli Etuahod6b14282015-03-17 14:31:35 +02005216 return true;
5217}
5218
Olli Etuaho1dded802016-08-18 18:13:13 +03005219bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5220 const TType &left,
5221 const TType &right)
5222{
5223 switch (op)
5224 {
5225 case EOpMul:
5226 case EOpMulAssign:
5227 return left.getNominalSize() == right.getNominalSize() &&
5228 left.getSecondarySize() == right.getSecondarySize();
5229 case EOpVectorTimesScalar:
5230 return true;
5231 case EOpVectorTimesScalarAssign:
5232 ASSERT(!left.isMatrix() && !right.isMatrix());
5233 return left.isVector() && !right.isVector();
5234 case EOpVectorTimesMatrix:
5235 return left.getNominalSize() == right.getRows();
5236 case EOpVectorTimesMatrixAssign:
5237 ASSERT(!left.isMatrix() && right.isMatrix());
5238 return left.isVector() && left.getNominalSize() == right.getRows() &&
5239 left.getNominalSize() == right.getCols();
5240 case EOpMatrixTimesVector:
5241 return left.getCols() == right.getNominalSize();
5242 case EOpMatrixTimesScalar:
5243 return true;
5244 case EOpMatrixTimesScalarAssign:
5245 ASSERT(left.isMatrix() && !right.isMatrix());
5246 return !right.isVector();
5247 case EOpMatrixTimesMatrix:
5248 return left.getCols() == right.getRows();
5249 case EOpMatrixTimesMatrixAssign:
5250 ASSERT(left.isMatrix() && right.isMatrix());
5251 // We need to check two things:
5252 // 1. The matrix multiplication step is valid.
5253 // 2. The result will have the same number of columns as the lvalue.
5254 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5255
5256 default:
5257 UNREACHABLE();
5258 return false;
5259 }
5260}
5261
Jamie Madillb98c3a82015-07-23 14:26:04 -04005262TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5263 TIntermTyped *left,
5264 TIntermTyped *right,
5265 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005266{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005267 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005268 return nullptr;
5269
Olli Etuahofc1806e2015-03-17 13:03:11 +02005270 switch (op)
5271 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005272 case EOpEqual:
5273 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005274 case EOpLessThan:
5275 case EOpGreaterThan:
5276 case EOpLessThanEqual:
5277 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005278 break;
5279 case EOpLogicalOr:
5280 case EOpLogicalXor:
5281 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005282 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5283 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005284 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005285 {
5286 return nullptr;
5287 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005288 // Basic types matching should have been already checked.
5289 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005290 break;
5291 case EOpAdd:
5292 case EOpSub:
5293 case EOpDiv:
5294 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005295 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5296 !right->getType().getStruct());
5297 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005298 {
5299 return nullptr;
5300 }
5301 break;
5302 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005303 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5304 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005305 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005306 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005307 {
5308 return nullptr;
5309 }
5310 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005311 default:
5312 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005313 }
5314
Olli Etuaho1dded802016-08-18 18:13:13 +03005315 if (op == EOpMul)
5316 {
5317 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5318 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5319 {
5320 return nullptr;
5321 }
5322 }
5323
Olli Etuaho3fdec912016-08-18 15:08:06 +03005324 TIntermBinary *node = new TIntermBinary(op, left, right);
5325 node->setLine(loc);
5326
Olli Etuaho3fdec912016-08-18 15:08:06 +03005327 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005328 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005329}
5330
Jamie Madillb98c3a82015-07-23 14:26:04 -04005331TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5332 TIntermTyped *left,
5333 TIntermTyped *right,
5334 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005335{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005336 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005337 if (node == 0)
5338 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005339 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5340 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005341 return left;
5342 }
5343 return node;
5344}
5345
Jamie Madillb98c3a82015-07-23 14:26:04 -04005346TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5347 TIntermTyped *left,
5348 TIntermTyped *right,
5349 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005350{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005351 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005352 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005353 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005354 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5355 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005356 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005357 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005358 }
5359 return node;
5360}
5361
Olli Etuaho13389b62016-10-16 11:48:18 +01005362TIntermBinary *TParseContext::createAssign(TOperator op,
5363 TIntermTyped *left,
5364 TIntermTyped *right,
5365 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005366{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005367 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005368 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005369 if (op == EOpMulAssign)
5370 {
5371 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5372 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5373 {
5374 return nullptr;
5375 }
5376 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005377 TIntermBinary *node = new TIntermBinary(op, left, right);
5378 node->setLine(loc);
5379
Olli Etuaho3fdec912016-08-18 15:08:06 +03005380 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005381 }
5382 return nullptr;
5383}
5384
Jamie Madillb98c3a82015-07-23 14:26:04 -04005385TIntermTyped *TParseContext::addAssign(TOperator op,
5386 TIntermTyped *left,
5387 TIntermTyped *right,
5388 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005389{
Olli Etuahocce89652017-06-19 16:04:09 +03005390 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005391 TIntermTyped *node = createAssign(op, left, right, loc);
5392 if (node == nullptr)
5393 {
5394 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005395 return left;
5396 }
5397 return node;
5398}
5399
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005400TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5401 TIntermTyped *right,
5402 const TSourceLoc &loc)
5403{
Corentin Wallez0d959252016-07-12 17:26:32 -04005404 // WebGL2 section 5.26, the following results in an error:
5405 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005406 if (mShaderSpec == SH_WEBGL2_SPEC &&
5407 (left->isArray() || left->getBasicType() == EbtVoid ||
5408 left->getType().isStructureContainingArrays() || right->isArray() ||
5409 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005410 {
5411 error(loc,
5412 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5413 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005414 }
5415
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005416 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5417 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5418 commaNode->getTypePointer()->setQualifier(resultQualifier);
5419 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005420}
5421
Olli Etuaho49300862015-02-20 14:54:49 +02005422TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5423{
5424 switch (op)
5425 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005426 case EOpContinue:
5427 if (mLoopNestingLevel <= 0)
5428 {
5429 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005430 }
5431 break;
5432 case EOpBreak:
5433 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5434 {
5435 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005436 }
5437 break;
5438 case EOpReturn:
5439 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5440 {
5441 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005442 }
5443 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005444 case EOpKill:
5445 if (mShaderType != GL_FRAGMENT_SHADER)
5446 {
5447 error(loc, "discard supported in fragment shaders only", "discard");
5448 }
5449 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005450 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005451 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005452 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005453 }
Olli Etuahocce89652017-06-19 16:04:09 +03005454 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005455}
5456
Jamie Madillb98c3a82015-07-23 14:26:04 -04005457TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005458 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005459 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005460{
Olli Etuahocce89652017-06-19 16:04:09 +03005461 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005462 {
Olli Etuahocce89652017-06-19 16:04:09 +03005463 ASSERT(op == EOpReturn);
5464 mFunctionReturnsValue = true;
5465 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5466 {
5467 error(loc, "void function cannot return a value", "return");
5468 }
5469 else if (*mCurrentFunctionType != expression->getType())
5470 {
5471 error(loc, "function return is not matching type:", "return");
5472 }
Olli Etuaho49300862015-02-20 14:54:49 +02005473 }
Olli Etuahocce89652017-06-19 16:04:09 +03005474 TIntermBranch *node = new TIntermBranch(op, expression);
5475 node->setLine(loc);
5476 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005477}
5478
Martin Radev84aa2dc2017-09-11 15:51:02 +03005479void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5480{
5481 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5482 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5483 bool isTextureGather = (name == "textureGather");
5484 bool isTextureGatherOffset = (name == "textureGatherOffset");
5485 if (isTextureGather || isTextureGatherOffset)
5486 {
5487 TIntermNode *componentNode = nullptr;
5488 TIntermSequence *arguments = functionCall->getSequence();
5489 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5490 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5491 ASSERT(sampler != nullptr);
5492 switch (sampler->getBasicType())
5493 {
5494 case EbtSampler2D:
5495 case EbtISampler2D:
5496 case EbtUSampler2D:
5497 case EbtSampler2DArray:
5498 case EbtISampler2DArray:
5499 case EbtUSampler2DArray:
5500 if ((isTextureGather && arguments->size() == 3u) ||
5501 (isTextureGatherOffset && arguments->size() == 4u))
5502 {
5503 componentNode = arguments->back();
5504 }
5505 break;
5506 case EbtSamplerCube:
5507 case EbtISamplerCube:
5508 case EbtUSamplerCube:
5509 ASSERT(!isTextureGatherOffset);
5510 if (arguments->size() == 3u)
5511 {
5512 componentNode = arguments->back();
5513 }
5514 break;
5515 case EbtSampler2DShadow:
5516 case EbtSampler2DArrayShadow:
5517 case EbtSamplerCubeShadow:
5518 break;
5519 default:
5520 UNREACHABLE();
5521 break;
5522 }
5523 if (componentNode)
5524 {
5525 const TIntermConstantUnion *componentConstantUnion =
5526 componentNode->getAsConstantUnion();
5527 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5528 {
5529 error(functionCall->getLine(), "Texture component must be a constant expression",
5530 name.c_str());
5531 }
5532 else
5533 {
5534 int component = componentConstantUnion->getIConst(0);
5535 if (component < 0 || component > 3)
5536 {
5537 error(functionCall->getLine(), "Component must be in the range [0;3]",
5538 name.c_str());
5539 }
5540 }
5541 }
5542 }
5543}
5544
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005545void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5546{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005547 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005548 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005549 TIntermNode *offset = nullptr;
5550 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005551 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005552 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5553 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5554 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005555 {
5556 offset = arguments->back();
5557 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005558 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005559 {
5560 // A bias parameter might follow the offset parameter.
5561 ASSERT(arguments->size() >= 3);
5562 offset = (*arguments)[2];
5563 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005564 else if (name == "textureGatherOffset")
5565 {
5566 ASSERT(arguments->size() >= 3u);
5567 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5568 ASSERT(sampler != nullptr);
5569 switch (sampler->getBasicType())
5570 {
5571 case EbtSampler2D:
5572 case EbtISampler2D:
5573 case EbtUSampler2D:
5574 case EbtSampler2DArray:
5575 case EbtISampler2DArray:
5576 case EbtUSampler2DArray:
5577 offset = (*arguments)[2];
5578 break;
5579 case EbtSampler2DShadow:
5580 case EbtSampler2DArrayShadow:
5581 offset = (*arguments)[3];
5582 break;
5583 default:
5584 UNREACHABLE();
5585 break;
5586 }
5587 useTextureGatherOffsetConstraints = true;
5588 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005589 if (offset != nullptr)
5590 {
5591 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5592 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5593 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005594 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005595 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005596 }
5597 else
5598 {
5599 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5600 size_t size = offsetConstantUnion->getType().getObjectSize();
5601 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005602 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5603 : mMinProgramTexelOffset;
5604 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5605 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005606 for (size_t i = 0u; i < size; ++i)
5607 {
5608 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005609 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005610 {
5611 std::stringstream tokenStream;
5612 tokenStream << offsetValue;
5613 std::string token = tokenStream.str();
5614 error(offset->getLine(), "Texture offset value out of valid range",
5615 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005616 }
5617 }
5618 }
5619 }
5620}
5621
Jiajia Qina3106c52017-11-03 09:39:39 +08005622void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5623{
5624 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5625 if (IsAtomicBuiltin(name))
5626 {
5627 TIntermSequence *arguments = functionCall->getSequence();
5628 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5629
5630 if (IsBufferOrSharedVariable(memNode))
5631 {
5632 return;
5633 }
5634
5635 while (memNode->getAsBinaryNode())
5636 {
5637 memNode = memNode->getAsBinaryNode()->getLeft();
5638 if (IsBufferOrSharedVariable(memNode))
5639 {
5640 return;
5641 }
5642 }
5643
5644 error(memNode->getLine(),
5645 "The value passed to the mem argument of an atomic memory function does not "
5646 "correspond to a buffer or shared variable.",
5647 functionCall->getFunctionSymbolInfo()->getName().c_str());
5648 }
5649}
5650
Martin Radev2cc85b32016-08-05 16:22:53 +03005651// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5652void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5653{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005654 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005655 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5656
5657 if (name.compare(0, 5, "image") == 0)
5658 {
5659 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005660 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005661
Olli Etuaho485eefd2017-02-14 17:40:06 +00005662 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005663
5664 if (name.compare(5, 5, "Store") == 0)
5665 {
5666 if (memoryQualifier.readonly)
5667 {
5668 error(imageNode->getLine(),
5669 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005670 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005671 }
5672 }
5673 else if (name.compare(5, 4, "Load") == 0)
5674 {
5675 if (memoryQualifier.writeonly)
5676 {
5677 error(imageNode->getLine(),
5678 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005679 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005680 }
5681 }
5682 }
5683}
5684
5685// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5686void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5687 const TFunction *functionDefinition,
5688 const TIntermAggregate *functionCall)
5689{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005690 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005691
5692 const TIntermSequence &arguments = *functionCall->getSequence();
5693
5694 ASSERT(functionDefinition->getParamCount() == arguments.size());
5695
5696 for (size_t i = 0; i < arguments.size(); ++i)
5697 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005698 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5699 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005700 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5701 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5702
5703 if (IsImage(functionArgumentType.getBasicType()))
5704 {
5705 const TMemoryQualifier &functionArgumentMemoryQualifier =
5706 functionArgumentType.getMemoryQualifier();
5707 const TMemoryQualifier &functionParameterMemoryQualifier =
5708 functionParameterType.getMemoryQualifier();
5709 if (functionArgumentMemoryQualifier.readonly &&
5710 !functionParameterMemoryQualifier.readonly)
5711 {
5712 error(functionCall->getLine(),
5713 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005714 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005715 }
5716
5717 if (functionArgumentMemoryQualifier.writeonly &&
5718 !functionParameterMemoryQualifier.writeonly)
5719 {
5720 error(functionCall->getLine(),
5721 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005722 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005723 }
Martin Radev049edfa2016-11-11 14:35:37 +02005724
5725 if (functionArgumentMemoryQualifier.coherent &&
5726 !functionParameterMemoryQualifier.coherent)
5727 {
5728 error(functionCall->getLine(),
5729 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005730 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005731 }
5732
5733 if (functionArgumentMemoryQualifier.volatileQualifier &&
5734 !functionParameterMemoryQualifier.volatileQualifier)
5735 {
5736 error(functionCall->getLine(),
5737 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005738 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005739 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005740 }
5741 }
5742}
5743
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005744TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005745{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005746 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005747}
5748
5749TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005750 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005751 TIntermNode *thisNode,
5752 const TSourceLoc &loc)
5753{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005754 if (thisNode != nullptr)
5755 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005756 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005757 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005758
5759 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005760 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005761 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005762 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005763 }
5764 else
5765 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005766 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005767 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5768 }
5769}
5770
5771TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5772 TIntermSequence *arguments,
5773 TIntermNode *thisNode,
5774 const TSourceLoc &loc)
5775{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005776 TIntermTyped *typedThis = thisNode->getAsTyped();
5777 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5778 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5779 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5780 // So accessing fnCall->getName() below is safe.
5781 if (fnCall->getName() != "length")
5782 {
5783 error(loc, "invalid method", fnCall->getName().c_str());
5784 }
5785 else if (!arguments->empty())
5786 {
5787 error(loc, "method takes no parameters", "length");
5788 }
5789 else if (typedThis == nullptr || !typedThis->isArray())
5790 {
5791 error(loc, "length can only be called on arrays", "length");
5792 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005793 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5794 mGeometryShaderInputPrimitiveType == EptUndefined)
5795 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005796 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005797 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5798 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005799 else
5800 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005801 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5802 node->setLine(loc);
5803 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005804 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005805 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005806}
5807
5808TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5809 TIntermSequence *arguments,
5810 const TSourceLoc &loc)
5811{
5812 // First find by unmangled name to check whether the function name has been
5813 // hidden by a variable name or struct typename.
5814 // If a function is found, check for one with a matching argument list.
5815 bool builtIn;
5816 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5817 if (symbol != nullptr && !symbol->isFunction())
5818 {
5819 error(loc, "function name expected", fnCall->getName().c_str());
5820 }
5821 else
5822 {
5823 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5824 mShaderVersion, &builtIn);
5825 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005826 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005827 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5828 }
5829 else
5830 {
5831 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005832 //
5833 // A declared function.
5834 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005835 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005836 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005837 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005838 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005839 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005840 if (builtIn && op != EOpNull)
5841 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005842 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005843 if (fnCandidate->getParamCount() == 1)
5844 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005845 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005846 TIntermNode *unaryParamNode = arguments->front();
5847 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005848 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005849 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005850 }
5851 else
5852 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005853 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005854 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005855 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005856
5857 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005858 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305859
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005860 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305861 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005862 // See if we can constant fold a built-in. Note that this may be possible
5863 // even if it is not const-qualified.
5864 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305865 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005866 else
5867 {
5868 return callNode;
5869 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005870 }
5871 }
5872 else
5873 {
5874 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005875 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005876
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005877 // If builtIn == false, the function is user defined - could be an overloaded
5878 // built-in as well.
5879 // if builtIn == true, it's a builtIn function with no op associated with it.
5880 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005881 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005882 {
Olli Etuahofe486322017-03-21 09:30:54 +00005883 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005884 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005885 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005886 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005887 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005888 }
5889 else
5890 {
Olli Etuahofe486322017-03-21 09:30:54 +00005891 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005892 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005893 }
5894
Jiajia Qinbc585152017-06-23 15:42:17 +08005895 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005896
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005897 callNode->setLine(loc);
5898
5899 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005900 }
5901 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005902 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005903
5904 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005905 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005906}
5907
Jamie Madillb98c3a82015-07-23 14:26:04 -04005908TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005909 TIntermTyped *trueExpression,
5910 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005911 const TSourceLoc &loc)
5912{
Olli Etuaho56229f12017-07-10 14:16:33 +03005913 if (!checkIsScalarBool(loc, cond))
5914 {
5915 return falseExpression;
5916 }
Olli Etuaho52901742015-04-15 13:42:45 +03005917
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005918 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005919 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005920 std::stringstream reasonStream;
5921 reasonStream << "mismatching ternary operator operand types '"
5922 << trueExpression->getCompleteString() << " and '"
5923 << falseExpression->getCompleteString() << "'";
5924 std::string reason = reasonStream.str();
5925 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005926 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005927 }
Olli Etuahode318b22016-10-25 16:18:25 +01005928 if (IsOpaqueType(trueExpression->getBasicType()))
5929 {
5930 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005931 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005932 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5933 // Note that structs containing opaque types don't need to be checked as structs are
5934 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005935 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005936 return falseExpression;
5937 }
5938
Jiajia Qinbc585152017-06-23 15:42:17 +08005939 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5940 falseExpression->getMemoryQualifier().writeonly)
5941 {
5942 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5943 return falseExpression;
5944 }
5945
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005946 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005947 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005948 // ESSL 3.00.6 section 5.7:
5949 // Ternary operator support is optional for arrays. No certainty that it works across all
5950 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5951 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005952 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005953 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005954 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005955 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005956 }
Olli Etuaho94050052017-05-08 14:17:44 +03005957 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5958 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005959 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005960 return falseExpression;
5961 }
5962
Corentin Wallez0d959252016-07-12 17:26:32 -04005963 // WebGL2 section 5.26, the following results in an error:
5964 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005965 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005966 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005967 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005968 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005969 }
5970
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005971 // Note that the node resulting from here can be a constant union without being qualified as
5972 // constant.
5973 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5974 node->setLine(loc);
5975
5976 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005977}
Olli Etuaho49300862015-02-20 14:54:49 +02005978
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005979//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005980// Parse an array of strings using yyparse.
5981//
5982// Returns 0 for success.
5983//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005984int PaParseStrings(size_t count,
5985 const char *const string[],
5986 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305987 TParseContext *context)
5988{
Yunchao He4f285442017-04-21 12:15:49 +08005989 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005990 return 1;
5991
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005992 if (glslang_initialize(context))
5993 return 1;
5994
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005995 int error = glslang_scan(count, string, length, context);
5996 if (!error)
5997 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005998
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005999 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006000
alokp@chromium.org6b495712012-06-29 00:06:58 +00006001 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006002}
Jamie Madill45bcc782016-11-07 13:58:48 -05006003
6004} // namespace sh