blob: fbfa4541ed2e815df942527ac7f8621ba2e50fc0 [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"
Olli Etuahod5f44c92017-11-29 17:15:40 +020014#include "compiler/translator/Declarator.h"
Olli Etuaho2bfe9f62018-03-02 16:53:29 +020015#include "compiler/translator/ParseContext_autogen.h"
Kai Ninomiya614dd0f2017-11-22 14:04:48 -080016#include "compiler/translator/StaticType.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030017#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080018#include "compiler/translator/ValidateSwitch.h"
19#include "compiler/translator/glslang.h"
Olli Etuahoc26214d2018-03-16 10:43:11 +020020#include "compiler/translator/tree_util/IntermNode_util.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030021#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000022
Jamie Madill45bcc782016-11-07 13:58:48 -050023namespace sh
24{
25
alokp@chromium.org8b851c62012-06-15 16:25:11 +000026///////////////////////////////////////////////////////////////////////
27//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028// Sub- vector and matrix fields
29//
30////////////////////////////////////////////////////////////////////////
31
Martin Radev2cc85b32016-08-05 16:22:53 +030032namespace
33{
34
35const int kWebGLMaxStructNesting = 4;
36
Olli Etuaho0f684632017-07-13 12:42:15 +030037bool ContainsSampler(const TStructure *structType);
38
Martin Radev2cc85b32016-08-05 16:22:53 +030039bool ContainsSampler(const TType &type)
40{
41 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030042 {
Martin Radev2cc85b32016-08-05 16:22:53 +030043 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030044 }
jchen10cc2a10e2017-05-03 14:05:12 +080045 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030046 {
Olli Etuaho0f684632017-07-13 12:42:15 +030047 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030048 }
49
50 return false;
51}
52
Olli Etuaho0f684632017-07-13 12:42:15 +030053bool ContainsSampler(const TStructure *structType)
54{
55 for (const auto &field : structType->fields())
56 {
57 if (ContainsSampler(*field->type()))
58 return true;
59 }
60 return false;
61}
62
Olli Etuaho485eefd2017-02-14 17:40:06 +000063// Get a token from an image argument to use as an error message token.
64const char *GetImageArgumentToken(TIntermTyped *imageNode)
65{
66 ASSERT(IsImage(imageNode->getBasicType()));
67 while (imageNode->getAsBinaryNode() &&
68 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
69 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
70 {
71 imageNode = imageNode->getAsBinaryNode()->getLeft();
72 }
73 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
74 if (imageSymbol)
75 {
Olli Etuahofbb1c792018-01-19 16:26:59 +020076 return imageSymbol->getName().data();
Olli Etuaho485eefd2017-02-14 17:40:06 +000077 }
78 return "image";
79}
80
Olli Etuahocce89652017-06-19 16:04:09 +030081bool CanSetDefaultPrecisionOnType(const TPublicType &type)
82{
83 if (!SupportsPrecision(type.getBasicType()))
84 {
85 return false;
86 }
87 if (type.getBasicType() == EbtUInt)
88 {
89 // ESSL 3.00.4 section 4.5.4
90 return false;
91 }
92 if (type.isAggregate())
93 {
94 // Not allowed to set for aggregate types
95 return false;
96 }
97 return true;
98}
99
Jiawei Shaod8105a02017-08-08 09:54:36 +0800100// Map input primitive types to input array sizes in a geometry shader.
101GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
102{
103 switch (primitiveType)
104 {
105 case EptPoints:
106 return 1u;
107 case EptLines:
108 return 2u;
109 case EptTriangles:
110 return 3u;
111 case EptLinesAdjacency:
112 return 4u;
113 case EptTrianglesAdjacency:
114 return 6u;
115 default:
116 UNREACHABLE();
117 return 0u;
118 }
119}
120
Jiajia Qina3106c52017-11-03 09:39:39 +0800121bool IsBufferOrSharedVariable(TIntermTyped *var)
122{
123 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
124 var->getQualifier() == EvqShared)
125 {
126 return true;
127 }
128 return false;
129}
130
Martin Radev2cc85b32016-08-05 16:22:53 +0300131} // namespace
132
jchen104cdac9e2017-05-08 11:01:20 +0800133// This tracks each binding point's current default offset for inheritance of subsequent
134// variables using the same binding, and keeps offsets unique and non overlapping.
135// See GLSL ES 3.1, section 4.4.6.
136class TParseContext::AtomicCounterBindingState
137{
138 public:
139 AtomicCounterBindingState() : mDefaultOffset(0) {}
140 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
141 // newly inserted span.
142 int insertSpan(int start, size_t length)
143 {
144 gl::RangeI newSpan(start, start + static_cast<int>(length));
145 for (const auto &span : mSpans)
146 {
147 if (newSpan.intersects(span))
148 {
149 return -1;
150 }
151 }
152 mSpans.push_back(newSpan);
153 mDefaultOffset = newSpan.high();
154 return start;
155 }
156 // Inserts a new span starting from the default offset.
157 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
158 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
159
160 private:
161 int mDefaultOffset;
162 std::vector<gl::RangeI> mSpans;
163};
164
Jamie Madillacb4b812016-11-07 13:50:29 -0500165TParseContext::TParseContext(TSymbolTable &symt,
166 TExtensionBehavior &ext,
167 sh::GLenum type,
168 ShShaderSpec spec,
169 ShCompileOptions options,
170 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000171 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500172 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300173 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300174 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500175 mShaderType(type),
176 mShaderSpec(spec),
177 mCompileOptions(options),
178 mShaderVersion(100),
179 mTreeRoot(nullptr),
180 mLoopNestingLevel(0),
181 mStructNestingLevel(0),
182 mSwitchNestingLevel(0),
183 mCurrentFunctionType(nullptr),
184 mFunctionReturnsValue(false),
185 mChecksPrecisionErrors(checksPrecErrors),
186 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800187 mDefaultUniformMatrixPacking(EmpColumnMajor),
188 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
189 mDefaultBufferMatrixPacking(EmpColumnMajor),
190 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000191 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500192 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000193 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500194 mShaderVersion,
195 mShaderType,
196 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000197 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500198 mScanner(nullptr),
199 mUsesFragData(false),
200 mUsesFragColor(false),
201 mUsesSecondaryOutputs(false),
202 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
203 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300204 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
205 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500206 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500207 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000208 mNumViews(-1),
209 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000210 mMaxImageUnits(resources.MaxImageUnits),
211 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000212 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800213 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800214 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800215 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800216 mDeclaringFunction(false),
217 mGeometryShaderInputPrimitiveType(EptUndefined),
218 mGeometryShaderOutputPrimitiveType(EptUndefined),
219 mGeometryShaderInvocations(0),
220 mGeometryShaderMaxVertices(-1),
221 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800222 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Olli Etuahoc74ec1a2018-01-09 15:23:28 +0200223 mGlInVariableWithArraySize(nullptr)
Jamie Madillacb4b812016-11-07 13:50:29 -0500224{
Jamie Madillacb4b812016-11-07 13:50:29 -0500225}
226
jchen104cdac9e2017-05-08 11:01:20 +0800227TParseContext::~TParseContext()
228{
229}
230
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300231bool TParseContext::parseVectorFields(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200232 const ImmutableString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400233 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300234 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300236 ASSERT(fieldOffsets);
Olli Etuahofbb1c792018-01-19 16:26:59 +0200237 size_t fieldCount = compString.length();
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300238 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530239 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200240 error(line, "illegal vector field selection", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000241 return false;
242 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300243 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244
Jamie Madillb98c3a82015-07-23 14:26:04 -0400245 enum
246 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000247 exyzw,
248 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000249 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000250 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300252 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530253 {
254 switch (compString[i])
255 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400256 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300257 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400258 fieldSet[i] = exyzw;
259 break;
260 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300261 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400262 fieldSet[i] = ergba;
263 break;
264 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300265 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 fieldSet[i] = estpq;
267 break;
268 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300269 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400270 fieldSet[i] = exyzw;
271 break;
272 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300273 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400274 fieldSet[i] = ergba;
275 break;
276 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300277 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400278 fieldSet[i] = estpq;
279 break;
280 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300281 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400282 fieldSet[i] = exyzw;
283 break;
284 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300285 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400286 fieldSet[i] = ergba;
287 break;
288 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300289 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400290 fieldSet[i] = estpq;
291 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530292
Jamie Madillb98c3a82015-07-23 14:26:04 -0400293 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300294 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400295 fieldSet[i] = exyzw;
296 break;
297 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300298 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400299 fieldSet[i] = ergba;
300 break;
301 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300302 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400303 fieldSet[i] = estpq;
304 break;
305 default:
Olli Etuahofbb1c792018-01-19 16:26:59 +0200306 error(line, "illegal vector field selection", compString);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400307 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 }
309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300311 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530312 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300313 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530314 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200315 error(line, "vector field selection out of range", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000316 return false;
317 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318
Arun Patole7e7e68d2015-05-22 12:02:25 +0530319 if (i > 0)
320 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400321 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530322 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200323 error(line, "illegal - vector component fields not from the same set", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000324 return false;
325 }
326 }
327 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000329 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330}
331
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332///////////////////////////////////////////////////////////////////////
333//
334// Errors
335//
336////////////////////////////////////////////////////////////////////////
337
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338//
339// Used by flex/bison to output all syntax and parsing errors.
340//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000341void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000343 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344}
345
Olli Etuahofbb1c792018-01-19 16:26:59 +0200346void TParseContext::error(const TSourceLoc &loc, const char *reason, const ImmutableString &token)
347{
348 mDiagnostics->error(loc, reason, token.data());
349}
350
Olli Etuaho4de340a2016-12-16 09:32:03 +0000351void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530352{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000353 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000354}
355
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200356void TParseContext::outOfRangeError(bool isError,
357 const TSourceLoc &loc,
358 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000359 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200360{
361 if (isError)
362 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000363 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200364 }
365 else
366 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000367 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200368 }
369}
370
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371//
372// Same error message for all places assignments don't work.
373//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530374void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000375{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000376 std::stringstream reasonStream;
377 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
378 std::string reason = reasonStream.str();
379 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380}
381
382//
383// Same error message for all places unary operations don't work.
384//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530385void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000387 std::stringstream reasonStream;
388 reasonStream << "wrong operand type - no operation '" << op
389 << "' exists that takes an operand of type " << operand
390 << " (or there is no acceptable conversion)";
391 std::string reason = reasonStream.str();
392 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393}
394
395//
396// Same error message for all binary operations don't work.
397//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400398void TParseContext::binaryOpError(const TSourceLoc &line,
399 const char *op,
400 TString left,
401 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000403 std::stringstream reasonStream;
404 reasonStream << "wrong operand types - no operation '" << op
405 << "' exists that takes a left-hand operand of type '" << left
406 << "' and a right operand of type '" << right
407 << "' (or there is no acceptable conversion)";
408 std::string reason = reasonStream.str();
409 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410}
411
Olli Etuaho856c4972016-08-08 11:38:39 +0300412void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
413 TPrecision precision,
414 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530415{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400416 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300417 return;
Martin Radev70866b82016-07-22 15:27:42 +0300418
419 if (precision != EbpUndefined && !SupportsPrecision(type))
420 {
421 error(line, "illegal type for precision qualifier", getBasicString(type));
422 }
423
Olli Etuaho183d7e22015-11-20 15:59:09 +0200424 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530425 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200426 switch (type)
427 {
428 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400429 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300430 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200431 case EbtInt:
432 case EbtUInt:
433 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400434 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300435 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200436 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800437 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200438 {
jchen10cc2a10e2017-05-03 14:05:12 +0800439 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300440 return;
441 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200442 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000443 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000444}
445
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000446// Both test and if necessary, spit out an error, to see if the node is really
447// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300448bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449{
Olli Etuahob6fa0432016-09-28 16:28:05 +0100450 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100451 if (swizzleNode)
452 {
453 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
454 if (ok && swizzleNode->hasDuplicateOffsets())
455 {
456 error(line, " l-value of swizzle cannot have duplicate components", op);
457 return false;
458 }
459 return ok;
460 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000461
Olli Etuahodaf120b2018-03-20 14:21:10 +0200462 TIntermBinary *binaryNode = node->getAsBinaryNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530463 if (binaryNode)
464 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400465 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530466 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400467 case EOpIndexDirect:
468 case EOpIndexIndirect:
469 case EOpIndexDirectStruct:
470 case EOpIndexDirectInterfaceBlock:
Qin Jiajia76bf01d2018-02-22 14:11:34 +0800471 if (node->getMemoryQualifier().readonly)
472 {
473 error(line, "can't modify a readonly variable", op);
474 return false;
475 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300476 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400477 default:
478 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000479 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000480 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300481 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000482 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483
jchen10cc2a10e2017-05-03 14:05:12 +0800484 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530485 switch (node->getQualifier())
486 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400487 case EvqConst:
488 message = "can't modify a const";
489 break;
490 case EvqConstReadOnly:
491 message = "can't modify a const";
492 break;
493 case EvqAttribute:
494 message = "can't modify an attribute";
495 break;
496 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400497 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800498 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800499 case EvqFlatIn:
500 case EvqSmoothIn:
501 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400502 message = "can't modify an input";
503 break;
504 case EvqUniform:
505 message = "can't modify a uniform";
506 break;
507 case EvqVaryingIn:
508 message = "can't modify a varying";
509 break;
510 case EvqFragCoord:
511 message = "can't modify gl_FragCoord";
512 break;
513 case EvqFrontFacing:
514 message = "can't modify gl_FrontFacing";
515 break;
516 case EvqPointCoord:
517 message = "can't modify gl_PointCoord";
518 break;
Martin Radevb0883602016-08-04 17:48:58 +0300519 case EvqNumWorkGroups:
520 message = "can't modify gl_NumWorkGroups";
521 break;
522 case EvqWorkGroupSize:
523 message = "can't modify gl_WorkGroupSize";
524 break;
525 case EvqWorkGroupID:
526 message = "can't modify gl_WorkGroupID";
527 break;
528 case EvqLocalInvocationID:
529 message = "can't modify gl_LocalInvocationID";
530 break;
531 case EvqGlobalInvocationID:
532 message = "can't modify gl_GlobalInvocationID";
533 break;
534 case EvqLocalInvocationIndex:
535 message = "can't modify gl_LocalInvocationIndex";
536 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300537 case EvqViewIDOVR:
538 message = "can't modify gl_ViewID_OVR";
539 break;
Martin Radev802abe02016-08-04 17:48:32 +0300540 case EvqComputeIn:
541 message = "can't modify work group size variable";
542 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800543 case EvqPerVertexIn:
544 message = "can't modify any member in gl_in";
545 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800546 case EvqPrimitiveIDIn:
547 message = "can't modify gl_PrimitiveIDIn";
548 break;
549 case EvqInvocationID:
550 message = "can't modify gl_InvocationID";
551 break;
552 case EvqPrimitiveID:
553 if (mShaderType == GL_FRAGMENT_SHADER)
554 {
555 message = "can't modify gl_PrimitiveID in a fragment shader";
556 }
557 break;
558 case EvqLayer:
559 if (mShaderType == GL_FRAGMENT_SHADER)
560 {
561 message = "can't modify gl_Layer in a fragment shader";
562 }
563 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400564 default:
565 //
566 // Type that can't be written to?
567 //
568 if (node->getBasicType() == EbtVoid)
569 {
570 message = "can't modify void";
571 }
jchen10cc2a10e2017-05-03 14:05:12 +0800572 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400573 {
jchen10cc2a10e2017-05-03 14:05:12 +0800574 message = "can't modify a variable with type ";
575 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300576 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800577 else if (node->getMemoryQualifier().readonly)
578 {
579 message = "can't modify a readonly variable";
580 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000581 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000582
Olli Etuahodaf120b2018-03-20 14:21:10 +0200583 ASSERT(binaryNode == nullptr && swizzleNode == nullptr);
584 TIntermSymbol *symNode = node->getAsSymbolNode();
585 if (message.empty() && symNode != nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530586 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300587 return true;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000588 }
Olli Etuahodaf120b2018-03-20 14:21:10 +0200589
590 std::stringstream reasonStream;
591 reasonStream << "l-value required";
592 if (!message.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530593 {
Olli Etuahodaf120b2018-03-20 14:21:10 +0200594 if (symNode)
595 {
596 // Symbol inside an expression can't be nameless.
597 ASSERT(symNode->variable().symbolType() != SymbolType::Empty);
598 const ImmutableString &symbol = symNode->getName();
599 reasonStream << " (" << message << " \"" << symbol << "\")";
600 }
601 else
602 {
603 reasonStream << " (" << message << ")";
604 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000605 }
Olli Etuahodaf120b2018-03-20 14:21:10 +0200606 std::string reason = reasonStream.str();
607 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608
Olli Etuaho8a176262016-08-16 14:23:01 +0300609 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000610}
611
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612// Both test, and if necessary spit out an error, to see if the node is really
613// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300614void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000615{
Olli Etuaho383b7912016-08-05 11:22:59 +0300616 if (node->getQualifier() != EvqConst)
617 {
618 error(node->getLine(), "constant expression required", "");
619 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620}
621
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622// Both test, and if necessary spit out an error, to see if the node is really
623// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300624void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625{
Olli Etuaho383b7912016-08-05 11:22:59 +0300626 if (!node->isScalarInt())
627 {
628 error(node->getLine(), "integer expression required", token);
629 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000630}
631
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632// Both test, and if necessary spit out an error, to see if we are currently
633// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800634bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635{
Olli Etuaho856c4972016-08-08 11:38:39 +0300636 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300637 {
638 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800639 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300640 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800641 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642}
643
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300644// ESSL 3.00.5 sections 3.8 and 3.9.
645// If it starts "gl_" or contains two consecutive underscores, it's reserved.
646// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuahofbb1c792018-01-19 16:26:59 +0200647bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const ImmutableString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530649 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahofbb1c792018-01-19 16:26:59 +0200650 if (identifier.beginsWith("gl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530651 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300652 error(line, reservedErrMsg, "gl_");
653 return false;
654 }
655 if (sh::IsWebGLBasedSpec(mShaderSpec))
656 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200657 if (identifier.beginsWith("webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530658 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300659 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300660 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200662 if (identifier.beginsWith("_webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300664 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300665 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000666 }
667 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200668 if (identifier.contains("__"))
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300669 {
670 error(line,
671 "identifiers containing two consecutive underscores (__) are reserved as "
672 "possible future keywords",
Olli Etuahofbb1c792018-01-19 16:26:59 +0200673 identifier);
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300674 return false;
675 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300676 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000677}
678
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300679// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300680bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuaho95ed1942018-02-01 14:01:19 +0200681 const TIntermSequence &arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300682 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683{
Olli Etuaho95ed1942018-02-01 14:01:19 +0200684 if (arguments.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530685 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200686 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300687 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000688 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200689
Olli Etuaho95ed1942018-02-01 14:01:19 +0200690 for (TIntermNode *arg : arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530691 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300692 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200693 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300694 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200695 {
jchen10cc2a10e2017-05-03 14:05:12 +0800696 std::string reason("cannot convert a variable with type ");
697 reason += getBasicString(argTyped->getBasicType());
698 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300699 return false;
700 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800701 else if (argTyped->getMemoryQualifier().writeonly)
702 {
703 error(line, "cannot convert a variable with writeonly", "constructor");
704 return false;
705 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200706 if (argTyped->getBasicType() == EbtVoid)
707 {
708 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300709 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200710 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000711 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000712
Olli Etuaho856c4972016-08-08 11:38:39 +0300713 if (type.isArray())
714 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300715 // The size of an unsized constructor should already have been determined.
716 ASSERT(!type.isUnsizedArray());
Olli Etuaho95ed1942018-02-01 14:01:19 +0200717 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300718 {
719 error(line, "array constructor needs one argument per array element", "constructor");
720 return false;
721 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300722 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
723 // the array.
Olli Etuaho95ed1942018-02-01 14:01:19 +0200724 for (TIntermNode *const &argNode : arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300725 {
726 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300727 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500728 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300729 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500730 return false;
731 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300732 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300733 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000734 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300735 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300736 }
737 }
738 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300739 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300740 {
741 const TFieldList &fields = type.getStruct()->fields();
Olli Etuaho95ed1942018-02-01 14:01:19 +0200742 if (fields.size() != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300743 {
744 error(line,
745 "Number of constructor parameters does not match the number of structure fields",
746 "constructor");
747 return false;
748 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300749
750 for (size_t i = 0; i < fields.size(); i++)
751 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200752 if (i >= arguments.size() ||
753 arguments[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300754 {
755 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000756 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300757 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300758 }
759 }
760 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300761 else
762 {
763 // We're constructing a scalar, vector, or matrix.
764
765 // Note: It's okay to have too many components available, but not okay to have unused
766 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
767 // there is an extra argument, so 'overFull' will become true.
768
769 size_t size = 0;
770 bool full = false;
771 bool overFull = false;
772 bool matrixArg = false;
Olli Etuaho95ed1942018-02-01 14:01:19 +0200773 for (TIntermNode *arg : arguments)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300774 {
775 const TIntermTyped *argTyped = arg->getAsTyped();
776 ASSERT(argTyped != nullptr);
777
Olli Etuaho487b63a2017-05-23 15:55:09 +0300778 if (argTyped->getBasicType() == EbtStruct)
779 {
780 error(line, "a struct cannot be used as a constructor argument for this type",
781 "constructor");
782 return false;
783 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300784 if (argTyped->getType().isArray())
785 {
786 error(line, "constructing from a non-dereferenced array", "constructor");
787 return false;
788 }
789 if (argTyped->getType().isMatrix())
790 {
791 matrixArg = true;
792 }
793
794 size += argTyped->getType().getObjectSize();
795 if (full)
796 {
797 overFull = true;
798 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300799 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300800 {
801 full = true;
802 }
803 }
804
805 if (type.isMatrix() && matrixArg)
806 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200807 if (arguments.size() != 1)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300808 {
809 error(line, "constructing matrix from matrix can only take one argument",
810 "constructor");
811 return false;
812 }
813 }
814 else
815 {
816 if (size != 1 && size < type.getObjectSize())
817 {
818 error(line, "not enough data provided for construction", "constructor");
819 return false;
820 }
821 if (overFull)
822 {
823 error(line, "too many arguments", "constructor");
824 return false;
825 }
826 }
827 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300828
Olli Etuaho8a176262016-08-16 14:23:01 +0300829 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830}
831
Jamie Madillb98c3a82015-07-23 14:26:04 -0400832// This function checks to see if a void variable has been declared and raise an error message for
833// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834//
835// returns true in case of an error
836//
Olli Etuaho856c4972016-08-08 11:38:39 +0300837bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200838 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400839 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300841 if (type == EbtVoid)
842 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200843 error(line, "illegal use of type 'void'", identifier);
Olli Etuaho8a176262016-08-16 14:23:01 +0300844 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300845 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846
Olli Etuaho8a176262016-08-16 14:23:01 +0300847 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848}
849
Jamie Madillb98c3a82015-07-23 14:26:04 -0400850// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300851// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300852bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300854 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530855 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000856 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300857 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530858 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300859 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
Jamie Madillb98c3a82015-07-23 14:26:04 -0400862// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300863// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300864void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865{
Martin Radev4a9cd802016-09-01 16:51:51 +0300866 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530867 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000868 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530869 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870}
871
jchen10cc2a10e2017-05-03 14:05:12 +0800872bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
873 const TTypeSpecifierNonArray &pType,
874 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530876 if (pType.type == EbtStruct)
877 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300878 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530879 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000880 std::stringstream reasonStream;
881 reasonStream << reason << " (structure contains a sampler)";
882 std::string reasonStr = reasonStream.str();
883 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300884 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000885 }
jchen10cc2a10e2017-05-03 14:05:12 +0800886 // only samplers need to be checked from structs, since other opaque types can't be struct
887 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300888 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530889 }
jchen10cc2a10e2017-05-03 14:05:12 +0800890 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530891 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000892 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300893 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000894 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895
Olli Etuaho8a176262016-08-16 14:23:01 +0300896 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897}
898
Olli Etuaho856c4972016-08-08 11:38:39 +0300899void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
900 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400901{
902 if (pType.layoutQualifier.location != -1)
903 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400904 error(line, "location must only be specified for a single input or output variable",
905 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400906 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400907}
908
Olli Etuaho856c4972016-08-08 11:38:39 +0300909void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
910 const TLayoutQualifier &layoutQualifier)
911{
912 if (layoutQualifier.location != -1)
913 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000914 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
915 if (mShaderVersion >= 310)
916 {
917 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800918 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000919 }
920 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300921 }
922}
923
Qin Jiajiaca68d982017-09-18 16:41:56 +0800924void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
925 const TLayoutBlockStorage &blockStorage,
926 const TQualifier &qualifier)
927{
928 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
929 {
930 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
931 }
932}
933
Martin Radev2cc85b32016-08-05 16:22:53 +0300934void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
935 TQualifier qualifier,
936 const TType &type)
937{
Martin Radev2cc85b32016-08-05 16:22:53 +0300938 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800939 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530940 {
jchen10cc2a10e2017-05-03 14:05:12 +0800941 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000942 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943}
944
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300946unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530948 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000949
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200950 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
951 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
952 // fold as array size.
953 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000954 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000955 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300956 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000957 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958
Olli Etuaho856c4972016-08-08 11:38:39 +0300959 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400960
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000961 if (constant->getBasicType() == EbtUInt)
962 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300963 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000964 }
965 else
966 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300967 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000968
Olli Etuaho856c4972016-08-08 11:38:39 +0300969 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000970 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400971 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300972 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000973 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400974
Olli Etuaho856c4972016-08-08 11:38:39 +0300975 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400976 }
977
Olli Etuaho856c4972016-08-08 11:38:39 +0300978 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400979 {
980 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300981 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400982 }
983
984 // The size of arrays is restricted here to prevent issues further down the
985 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
986 // 4096 registers so this should be reasonable even for aggressively optimizable code.
987 const unsigned int sizeLimit = 65536;
988
Olli Etuaho856c4972016-08-08 11:38:39 +0300989 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400990 {
991 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300992 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000993 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300994
995 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996}
997
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300999bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1000 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001{
Olli Etuaho8a176262016-08-16 14:23:01 +03001002 if ((elementQualifier.qualifier == EvqAttribute) ||
1003 (elementQualifier.qualifier == EvqVertexIn) ||
1004 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001005 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001006 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001007 TType(elementQualifier).getQualifierString());
1008 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001009 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010
Olli Etuaho8a176262016-08-16 14:23:01 +03001011 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012}
1013
Olli Etuaho8a176262016-08-16 14:23:01 +03001014// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001015bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1016 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001018 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001019 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001020 error(line, "cannot declare arrays of arrays",
1021 TType(elementType).getCompleteString().c_str());
1022 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001024 return true;
1025}
1026
1027// Check if this qualified element type can be formed into an array. This is only called when array
1028// brackets are associated with an identifier in a declaration, like this:
1029// float a[2];
1030// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1031// are associated with the type, like this:
1032// float[2] a;
1033bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1034 const TPublicType &elementType)
1035{
1036 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1037 {
1038 return false;
1039 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001040 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1041 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1042 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001043 // Geometry shader requires each user-defined input be declared as arrays or inside input
1044 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1045 // interface matching, such variables and blocks are treated as though they were not declared
1046 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001047 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001048 sh::IsVarying(elementType.qualifier) &&
1049 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001050 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001051 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001052 TType(elementType).getCompleteString().c_str());
1053 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001054 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001055 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001056}
1057
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001059void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001060 const ImmutableString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001061 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062{
Olli Etuaho3739d232015-04-08 12:23:44 +03001063 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001064 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001065 {
1066 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001067 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001068
1069 // Generate informative error messages for ESSL1.
1070 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001071 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001072 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301073 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001074 "structures containing arrays may not be declared constant since they cannot be "
1075 "initialized",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001076 identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001077 }
1078 else
1079 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001080 error(line, "variables with qualifier 'const' must be initialized", identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001081 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001082 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001083 // This will make the type sized if it isn't sized yet.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001084 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized", identifier,
1085 type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086}
1087
Olli Etuaho2935c582015-04-08 14:32:06 +03001088// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089// and update the symbol table.
1090//
Olli Etuaho2935c582015-04-08 14:32:06 +03001091// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001093bool TParseContext::declareVariable(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001094 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001095 const TType *type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001096 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097{
Olli Etuaho2935c582015-04-08 14:32:06 +03001098 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099
Olli Etuahofbb1c792018-01-19 16:26:59 +02001100 (*variable) = new TVariable(&symbolTable, identifier, type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02001101
Olli Etuahob60d30f2018-01-16 12:31:06 +02001102 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001103
Olli Etuaho856c4972016-08-08 11:38:39 +03001104 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105
Olli Etuaho2935c582015-04-08 14:32:06 +03001106 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahofbb1c792018-01-19 16:26:59 +02001107 if (type->isArray() && identifier.beginsWith("gl_LastFragData"))
Olli Etuaho2935c582015-04-08 14:32:06 +03001108 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001109 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02001110 symbolTable.findBuiltIn(ImmutableString("gl_MaxDrawBuffers"), mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001111 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001112 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001113 error(line, "redeclaration of gl_LastFragData as an array of arrays", identifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001114 return false;
1115 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001116 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001117 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001118 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001119 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001120 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001121 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001122 }
1123 }
1124 else
1125 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001126 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001127 identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001128 return false;
1129 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131
Olli Etuaho8a176262016-08-16 14:23:01 +03001132 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001133 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134
Olli Etuaho437664b2018-02-28 15:38:14 +02001135 if (!symbolTable.declare(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001136 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001137 error(line, "redefinition", identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001138 return false;
1139 }
1140
Olli Etuahob60d30f2018-01-16 12:31:06 +02001141 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001142 return false;
1143
1144 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001145}
1146
Martin Radev70866b82016-07-22 15:27:42 +03001147void TParseContext::checkIsParameterQualifierValid(
1148 const TSourceLoc &line,
1149 const TTypeQualifierBuilder &typeQualifierBuilder,
1150 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301151{
Olli Etuahocce89652017-06-19 16:04:09 +03001152 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001153 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001154
1155 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301156 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001157 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1158 }
1159
1160 if (!IsImage(type->getBasicType()))
1161 {
Olli Etuaho43364892017-02-13 16:00:12 +00001162 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001163 }
1164 else
1165 {
1166 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001167 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001168
Martin Radev70866b82016-07-22 15:27:42 +03001169 type->setQualifier(typeQualifier.qualifier);
1170
1171 if (typeQualifier.precision != EbpUndefined)
1172 {
1173 type->setPrecision(typeQualifier.precision);
1174 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001175}
1176
Olli Etuaho703671e2017-11-08 17:47:18 +02001177template <size_t size>
1178bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1179 const std::array<TExtension, size> &extensions)
1180{
1181 ASSERT(!extensions.empty());
1182 const TExtensionBehavior &extBehavior = extensionBehavior();
1183
1184 bool canUseWithWarning = false;
1185 bool canUseWithoutWarning = false;
1186
1187 const char *errorMsgString = "";
1188 TExtension errorMsgExtension = TExtension::UNDEFINED;
1189
1190 for (TExtension extension : extensions)
1191 {
1192 auto extIter = extBehavior.find(extension);
1193 if (canUseWithWarning)
1194 {
1195 // We already have an extension that we can use, but with a warning.
1196 // See if we can use the alternative extension without a warning.
1197 if (extIter == extBehavior.end())
1198 {
1199 continue;
1200 }
1201 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1202 {
1203 canUseWithoutWarning = true;
1204 break;
1205 }
1206 continue;
1207 }
1208 if (extIter == extBehavior.end())
1209 {
1210 errorMsgString = "extension is not supported";
1211 errorMsgExtension = extension;
1212 }
1213 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1214 {
1215 errorMsgString = "extension is disabled";
1216 errorMsgExtension = extension;
1217 }
1218 else if (extIter->second == EBhWarn)
1219 {
1220 errorMsgExtension = extension;
1221 canUseWithWarning = true;
1222 }
1223 else
1224 {
1225 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1226 canUseWithoutWarning = true;
1227 break;
1228 }
1229 }
1230
1231 if (canUseWithoutWarning)
1232 {
1233 return true;
1234 }
1235 if (canUseWithWarning)
1236 {
1237 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1238 return true;
1239 }
1240 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1241 return false;
1242}
1243
1244template bool TParseContext::checkCanUseOneOfExtensions(
1245 const TSourceLoc &line,
1246 const std::array<TExtension, 1> &extensions);
1247template bool TParseContext::checkCanUseOneOfExtensions(
1248 const TSourceLoc &line,
1249 const std::array<TExtension, 2> &extensions);
1250template bool TParseContext::checkCanUseOneOfExtensions(
1251 const TSourceLoc &line,
1252 const std::array<TExtension, 3> &extensions);
1253
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001254bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001255{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001256 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001257 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001258}
1259
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001260// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1261// compile-time or link-time errors are the same whether or not the declaration is empty".
1262// This function implements all the checks that are done on qualifiers regardless of if the
1263// declaration is empty.
1264void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1265 const sh::TLayoutQualifier &layoutQualifier,
1266 const TSourceLoc &location)
1267{
1268 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1269 {
1270 error(location, "Shared memory declarations cannot have layout specified", "layout");
1271 }
1272
1273 if (layoutQualifier.matrixPacking != EmpUnspecified)
1274 {
1275 error(location, "layout qualifier only valid for interface blocks",
1276 getMatrixPackingString(layoutQualifier.matrixPacking));
1277 return;
1278 }
1279
1280 if (layoutQualifier.blockStorage != EbsUnspecified)
1281 {
1282 error(location, "layout qualifier only valid for interface blocks",
1283 getBlockStorageString(layoutQualifier.blockStorage));
1284 return;
1285 }
1286
1287 if (qualifier == EvqFragmentOut)
1288 {
1289 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1290 {
1291 error(location, "invalid layout qualifier combination", "yuv");
1292 return;
1293 }
1294 }
1295 else
1296 {
1297 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1298 }
1299
Olli Etuaho95468d12017-05-04 11:14:34 +03001300 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1301 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001302 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1303 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001304 {
1305 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1306 }
1307
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001308 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001309 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001310 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001311 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001312 // We're not checking whether the uniform location is in range here since that depends on
1313 // the type of the variable.
1314 // The type can only be fully determined for non-empty declarations.
1315 }
1316 if (!canHaveLocation)
1317 {
1318 checkLocationIsNotSpecified(location, layoutQualifier);
1319 }
1320}
1321
jchen104cdac9e2017-05-08 11:01:20 +08001322void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1323 const TSourceLoc &location)
1324{
1325 if (publicType.precision != EbpHigh)
1326 {
1327 error(location, "Can only be highp", "atomic counter");
1328 }
1329 // dEQP enforces compile error if location is specified. See uniform_location.test.
1330 if (publicType.layoutQualifier.location != -1)
1331 {
1332 error(location, "location must not be set for atomic_uint", "layout");
1333 }
1334 if (publicType.layoutQualifier.binding == -1)
1335 {
1336 error(location, "no binding specified", "atomic counter");
1337 }
1338}
1339
Olli Etuaho55bde912017-10-25 13:41:13 +03001340void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001341{
Olli Etuaho55bde912017-10-25 13:41:13 +03001342 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001343 {
1344 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1345 // error. It is assumed that this applies to empty declarations as well.
1346 error(location, "empty array declaration needs to specify a size", "");
1347 }
Martin Radevb8b01222016-11-20 23:25:53 +02001348}
1349
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001350// These checks are done for all declarations that are non-empty. They're done for non-empty
1351// declarations starting a declarator list, and declarators that follow an empty declaration.
1352void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1353 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001354{
Olli Etuahofa33d582015-04-09 14:33:12 +03001355 switch (publicType.qualifier)
1356 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001357 case EvqVaryingIn:
1358 case EvqVaryingOut:
1359 case EvqAttribute:
1360 case EvqVertexIn:
1361 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001362 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001363 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001364 {
1365 error(identifierLocation, "cannot be used with a structure",
1366 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001367 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001368 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001369 break;
1370 case EvqBuffer:
1371 if (publicType.getBasicType() != EbtInterfaceBlock)
1372 {
1373 error(identifierLocation,
1374 "cannot declare buffer variables at global scope(outside a block)",
1375 getQualifierString(publicType.qualifier));
1376 return;
1377 }
1378 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001379 default:
1380 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001381 }
jchen10cc2a10e2017-05-03 14:05:12 +08001382 std::string reason(getBasicString(publicType.getBasicType()));
1383 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001384 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001385 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001386 {
1387 return;
1388 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001389
Andrei Volykhina5527072017-03-22 16:46:30 +03001390 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1391 publicType.qualifier != EvqConst) &&
1392 publicType.getBasicType() == EbtYuvCscStandardEXT)
1393 {
1394 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1395 getQualifierString(publicType.qualifier));
1396 return;
1397 }
1398
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001399 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1400 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001401 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1402 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001403 TType type(publicType);
1404 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001405 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001406 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1407 publicType.layoutQualifier);
1408 }
1409 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001410
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001411 // check for layout qualifier issues
1412 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001413
Martin Radev2cc85b32016-08-05 16:22:53 +03001414 if (IsImage(publicType.getBasicType()))
1415 {
1416
1417 switch (layoutQualifier.imageInternalFormat)
1418 {
1419 case EiifRGBA32F:
1420 case EiifRGBA16F:
1421 case EiifR32F:
1422 case EiifRGBA8:
1423 case EiifRGBA8_SNORM:
1424 if (!IsFloatImage(publicType.getBasicType()))
1425 {
1426 error(identifierLocation,
1427 "internal image format requires a floating image type",
1428 getBasicString(publicType.getBasicType()));
1429 return;
1430 }
1431 break;
1432 case EiifRGBA32I:
1433 case EiifRGBA16I:
1434 case EiifRGBA8I:
1435 case EiifR32I:
1436 if (!IsIntegerImage(publicType.getBasicType()))
1437 {
1438 error(identifierLocation,
1439 "internal image format requires an integer image type",
1440 getBasicString(publicType.getBasicType()));
1441 return;
1442 }
1443 break;
1444 case EiifRGBA32UI:
1445 case EiifRGBA16UI:
1446 case EiifRGBA8UI:
1447 case EiifR32UI:
1448 if (!IsUnsignedImage(publicType.getBasicType()))
1449 {
1450 error(identifierLocation,
1451 "internal image format requires an unsigned image type",
1452 getBasicString(publicType.getBasicType()));
1453 return;
1454 }
1455 break;
1456 case EiifUnspecified:
1457 error(identifierLocation, "layout qualifier", "No image internal format specified");
1458 return;
1459 default:
1460 error(identifierLocation, "layout qualifier", "unrecognized token");
1461 return;
1462 }
1463
1464 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1465 switch (layoutQualifier.imageInternalFormat)
1466 {
1467 case EiifR32F:
1468 case EiifR32I:
1469 case EiifR32UI:
1470 break;
1471 default:
1472 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1473 {
1474 error(identifierLocation, "layout qualifier",
1475 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1476 "image variables must be qualified readonly and/or writeonly");
1477 return;
1478 }
1479 break;
1480 }
1481 }
1482 else
1483 {
Olli Etuaho43364892017-02-13 16:00:12 +00001484 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001485 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1486 }
jchen104cdac9e2017-05-08 11:01:20 +08001487
1488 if (IsAtomicCounter(publicType.getBasicType()))
1489 {
1490 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1491 }
1492 else
1493 {
1494 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1495 }
Olli Etuaho43364892017-02-13 16:00:12 +00001496}
Martin Radev2cc85b32016-08-05 16:22:53 +03001497
Olli Etuaho43364892017-02-13 16:00:12 +00001498void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1499{
1500 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001501 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1502 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1503 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1504 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1505 // when it comes to which shaders are accepted by the compiler.
1506 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001507 if (IsImage(type.getBasicType()))
1508 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001509 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1510 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001511 }
1512 else if (IsSampler(type.getBasicType()))
1513 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001514 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1515 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001516 }
jchen104cdac9e2017-05-08 11:01:20 +08001517 else if (IsAtomicCounter(type.getBasicType()))
1518 {
1519 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1520 }
Olli Etuaho43364892017-02-13 16:00:12 +00001521 else
1522 {
1523 ASSERT(!IsOpaqueType(type.getBasicType()));
1524 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001525 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001526}
1527
Olli Etuaho856c4972016-08-08 11:38:39 +03001528void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001529 const ImmutableString &layoutQualifierName,
Olli Etuaho856c4972016-08-08 11:38:39 +03001530 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001531{
1532
1533 if (mShaderVersion < versionRequired)
1534 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001535 error(location, "invalid layout qualifier: not supported", layoutQualifierName);
Martin Radev802abe02016-08-04 17:48:32 +03001536 }
1537}
1538
Olli Etuaho856c4972016-08-08 11:38:39 +03001539bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1540 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001541{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001542 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001543 for (size_t i = 0u; i < localSize.size(); ++i)
1544 {
1545 if (localSize[i] != -1)
1546 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001547 error(location,
1548 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1549 "global layout declaration",
1550 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001551 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001552 }
1553 }
1554
Olli Etuaho8a176262016-08-16 14:23:01 +03001555 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001556}
1557
Olli Etuaho43364892017-02-13 16:00:12 +00001558void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001559 TLayoutImageInternalFormat internalFormat)
1560{
1561 if (internalFormat != EiifUnspecified)
1562 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001563 error(location, "invalid layout qualifier: only valid when used with images",
1564 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001565 }
Olli Etuaho43364892017-02-13 16:00:12 +00001566}
1567
1568void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1569{
1570 if (binding != -1)
1571 {
1572 error(location,
1573 "invalid layout qualifier: only valid when used with opaque types or blocks",
1574 "binding");
1575 }
1576}
1577
jchen104cdac9e2017-05-08 11:01:20 +08001578void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1579{
1580 if (offset != -1)
1581 {
1582 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1583 "offset");
1584 }
1585}
1586
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001587void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1588 int binding,
1589 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001590{
1591 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001592 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001593 {
1594 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1595 }
1596}
1597
1598void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1599 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001600 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001601{
1602 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001603 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001604 {
1605 error(location, "sampler binding greater than maximum texture units", "binding");
1606 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001607}
1608
Jiajia Qinbc585152017-06-23 15:42:17 +08001609void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1610 const TQualifier &qualifier,
1611 int binding,
1612 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001613{
1614 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001615 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001616 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001617 if (binding + size > mMaxUniformBufferBindings)
1618 {
1619 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1620 "binding");
1621 }
1622 }
1623 else if (qualifier == EvqBuffer)
1624 {
1625 if (binding + size > mMaxShaderStorageBufferBindings)
1626 {
1627 error(location,
1628 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1629 "binding");
1630 }
jchen10af713a22017-04-19 09:10:56 +08001631 }
1632}
jchen104cdac9e2017-05-08 11:01:20 +08001633void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1634{
1635 if (binding >= mMaxAtomicCounterBindings)
1636 {
1637 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1638 "binding");
1639 }
1640}
jchen10af713a22017-04-19 09:10:56 +08001641
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001642void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1643 int objectLocationCount,
1644 const TLayoutQualifier &layoutQualifier)
1645{
1646 int loc = layoutQualifier.location;
1647 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1648 {
1649 error(location, "Uniform location out of range", "location");
1650 }
1651}
1652
Andrei Volykhina5527072017-03-22 16:46:30 +03001653void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1654{
1655 if (yuv != false)
1656 {
1657 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1658 }
1659}
1660
Jiajia Qinbc585152017-06-23 15:42:17 +08001661void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1662 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001663{
1664 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1665 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001666 TQualifier qual = fnCandidate->getParam(i)->getType().getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001667 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1668 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1669 qual == EvqInOut || qual == EvqConstReadOnly))
1670 {
1671 if (argument->getMemoryQualifier().writeonly)
1672 {
1673 error(argument->getLine(),
1674 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001675 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001676 return;
1677 }
1678 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001679 if (qual == EvqOut || qual == EvqInOut)
1680 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001681 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001682 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001683 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001684 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001685 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001686 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001687 }
1688 }
1689 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001690}
1691
Martin Radev70866b82016-07-22 15:27:42 +03001692void TParseContext::checkInvariantVariableQualifier(bool invariant,
1693 const TQualifier qualifier,
1694 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001695{
Martin Radev70866b82016-07-22 15:27:42 +03001696 if (!invariant)
1697 return;
1698
1699 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001700 {
Martin Radev70866b82016-07-22 15:27:42 +03001701 // input variables in the fragment shader can be also qualified as invariant
1702 if (!sh::CanBeInvariantESSL1(qualifier))
1703 {
1704 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1705 }
1706 }
1707 else
1708 {
1709 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1710 {
1711 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1712 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001713 }
1714}
1715
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001716bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001717{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001718 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001719}
1720
Jamie Madillb98c3a82015-07-23 14:26:04 -04001721void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1722 const char *extName,
1723 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001724{
1725 pp::SourceLocation srcLoc;
1726 srcLoc.file = loc.first_file;
1727 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001728 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001729}
1730
Jamie Madillb98c3a82015-07-23 14:26:04 -04001731void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1732 const char *name,
1733 const char *value,
1734 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001735{
1736 pp::SourceLocation srcLoc;
1737 srcLoc.file = loc.first_file;
1738 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001739 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001740}
1741
Martin Radev4c4c8e72016-08-04 12:25:34 +03001742sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001743{
Jamie Madill2f294c92017-11-20 14:47:26 -05001744 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001745 for (size_t i = 0u; i < result.size(); ++i)
1746 {
1747 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1748 {
1749 result[i] = 1;
1750 }
1751 else
1752 {
1753 result[i] = mComputeShaderLocalSize[i];
1754 }
1755 }
1756 return result;
1757}
1758
Olli Etuaho56229f12017-07-10 14:16:33 +03001759TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1760 const TSourceLoc &line)
1761{
1762 TIntermConstantUnion *node = new TIntermConstantUnion(
1763 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1764 node->setLine(line);
1765 return node;
1766}
1767
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768/////////////////////////////////////////////////////////////////////////////////
1769//
1770// Non-Errors.
1771//
1772/////////////////////////////////////////////////////////////////////////////////
1773
Jamie Madill5c097022014-08-20 16:38:32 -04001774const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001775 const ImmutableString &name,
Jamie Madill5c097022014-08-20 16:38:32 -04001776 const TSymbol *symbol)
1777{
Jamie Madill5c097022014-08-20 16:38:32 -04001778 if (!symbol)
1779 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001780 error(location, "undeclared identifier", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001781 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001782 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001783
1784 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001785 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001786 error(location, "variable expected", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001787 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001788 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001789
1790 const TVariable *variable = static_cast<const TVariable *>(symbol);
1791
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001792 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001793 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001794 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001795 }
1796
Olli Etuaho0f684632017-07-13 12:42:15 +03001797 // Reject shaders using both gl_FragData and gl_FragColor
1798 TQualifier qualifier = variable->getType().getQualifier();
1799 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001800 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001801 mUsesFragData = true;
1802 }
1803 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1804 {
1805 mUsesFragColor = true;
1806 }
1807 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1808 {
1809 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001810 }
1811
Olli Etuaho0f684632017-07-13 12:42:15 +03001812 // This validation is not quite correct - it's only an error to write to
1813 // both FragData and FragColor. For simplicity, and because users shouldn't
1814 // be rewarded for reading from undefined varaibles, return an error
1815 // if they are both referenced, rather than assigned.
1816 if (mUsesFragData && mUsesFragColor)
1817 {
1818 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1819 if (mUsesSecondaryOutputs)
1820 {
1821 errorMessage =
1822 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1823 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1824 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02001825 error(location, errorMessage, name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001826 }
1827
1828 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1829 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1830 qualifier == EvqWorkGroupSize)
1831 {
1832 error(location,
1833 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1834 "gl_WorkGroupSize");
1835 }
Jamie Madill5c097022014-08-20 16:38:32 -04001836 return variable;
1837}
1838
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001839TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001840 const ImmutableString &name,
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001841 const TSymbol *symbol)
1842{
1843 const TVariable *variable = getNamedVariable(location, name, symbol);
1844
Olli Etuaho0f684632017-07-13 12:42:15 +03001845 if (!variable)
1846 {
1847 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1848 node->setLine(location);
1849 return node;
1850 }
1851
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001852 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001853 TIntermTyped *node = nullptr;
1854
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001855 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001856 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001857 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001858 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001859 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001860 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001861 {
1862 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1863 // needs to be added to the AST as a constant and not as a symbol.
1864 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1865 TConstantUnion *constArray = new TConstantUnion[3];
1866 for (size_t i = 0; i < 3; ++i)
1867 {
1868 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1869 }
1870
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001871 ASSERT(variableType.getBasicType() == EbtUInt);
1872 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001873
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001874 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001875 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001876 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001877 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001878 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1879 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001880 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02001881 ASSERT(mGlInVariableWithArraySize != nullptr);
1882 node = new TIntermSymbol(mGlInVariableWithArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001883 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001884 else
1885 {
Olli Etuaho195be942017-12-04 23:40:14 +02001886 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001887 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001888 ASSERT(node != nullptr);
1889 node->setLine(location);
1890 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001891}
1892
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893// Initializers show up in several places in the grammar. Have one set of
1894// code to handle them here.
1895//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001896// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001897bool TParseContext::executeInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001898 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001899 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001900 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001901 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001902{
Olli Etuaho13389b62016-10-16 11:48:18 +01001903 ASSERT(initNode != nullptr);
1904 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905
Olli Etuahob60d30f2018-01-16 12:31:06 +02001906 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001907 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001908 // In case initializer is not an array or type has more dimensions than initializer, this
1909 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1910 // actually is an array or not. Having a non-array initializer for an unsized array will
1911 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001912 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001913 type->sizeUnsizedArrays(arraySizes);
1914 }
1915
1916 const TQualifier qualifier = type->getQualifier();
1917
1918 bool constError = false;
1919 if (qualifier == EvqConst)
1920 {
1921 if (EvqConst != initializer->getType().getQualifier())
1922 {
1923 std::stringstream reasonStream;
1924 reasonStream << "assigning non-constant to '" << type->getCompleteString() << "'";
1925 std::string reason = reasonStream.str();
1926 error(line, reason.c_str(), "=");
1927
1928 // We're still going to declare the variable to avoid extra error messages.
1929 type->setQualifier(EvqTemporary);
1930 constError = true;
1931 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001932 }
Olli Etuaho195be942017-12-04 23:40:14 +02001933
1934 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001935 if (!declareVariable(line, identifier, type, &variable))
1936 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001937 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939
Olli Etuahob60d30f2018-01-16 12:31:06 +02001940 if (constError)
1941 {
1942 return false;
1943 }
1944
Olli Etuahob0c645e2015-05-12 14:25:36 +03001945 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001946 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001947 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001948 {
1949 // Error message does not completely match behavior with ESSL 1.00, but
1950 // we want to steer developers towards only using constant expressions.
1951 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001952 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001953 }
1954 if (globalInitWarning)
1955 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001956 warning(
1957 line,
1958 "global variable initializers should be constant expressions "
1959 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1960 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001961 }
1962
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001963 // identifier must be of type constant, a global, or a temporary
Arun Patole7e7e68d2015-05-22 12:02:25 +05301964 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1965 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001966 error(line, " cannot initialize this type of qualifier ",
1967 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001968 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001969 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001970
1971 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
1972 intermSymbol->setLine(line);
1973
1974 if (!binaryOpCommonCheck(EOpInitialize, intermSymbol, initializer, line))
1975 {
1976 assignError(line, "=", variable->getType().getCompleteString(),
1977 initializer->getCompleteString());
1978 return false;
1979 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980
Arun Patole7e7e68d2015-05-22 12:02:25 +05301981 if (qualifier == EvqConst)
1982 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001983 // Save the constant folded value to the variable if possible.
1984 const TConstantUnion *constArray = initializer->getConstantValue();
1985 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301986 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001987 variable->shareConstPointer(constArray);
1988 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001989 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001990 ASSERT(*initNode == nullptr);
1991 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001992 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001993 }
1994 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001995
Olli Etuahob60d30f2018-01-16 12:31:06 +02001996 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
1997 (*initNode)->setLine(line);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001998 return true;
1999}
2000
2001TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002002 const ImmutableString &identifier,
Olli Etuaho914b79a2017-06-19 16:03:19 +03002003 TIntermTyped *initializer,
2004 const TSourceLoc &loc)
2005{
2006 checkIsScalarBool(loc, pType);
2007 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002008 TType *type = new TType(pType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002009 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002010 {
2011 // The initializer is valid. The init condition needs to have a node - either the
2012 // initializer node, or a constant node in case the initialized variable is const and won't
2013 // be recorded in the AST.
2014 if (initNode == nullptr)
2015 {
2016 return initializer;
2017 }
2018 else
2019 {
2020 TIntermDeclaration *declaration = new TIntermDeclaration();
2021 declaration->appendDeclarator(initNode);
2022 return declaration;
2023 }
2024 }
2025 return nullptr;
2026}
2027
2028TIntermNode *TParseContext::addLoop(TLoopType type,
2029 TIntermNode *init,
2030 TIntermNode *cond,
2031 TIntermTyped *expr,
2032 TIntermNode *body,
2033 const TSourceLoc &line)
2034{
2035 TIntermNode *node = nullptr;
2036 TIntermTyped *typedCond = nullptr;
2037 if (cond)
2038 {
2039 typedCond = cond->getAsTyped();
2040 }
2041 if (cond == nullptr || typedCond)
2042 {
Olli Etuahocce89652017-06-19 16:04:09 +03002043 if (type == ELoopDoWhile)
2044 {
2045 checkIsScalarBool(line, typedCond);
2046 }
2047 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2048 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2049 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2050 !typedCond->isVector()));
2051
Olli Etuaho3ec75682017-07-05 17:02:55 +03002052 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002053 node->setLine(line);
2054 return node;
2055 }
2056
Olli Etuahocce89652017-06-19 16:04:09 +03002057 ASSERT(type != ELoopDoWhile);
2058
Olli Etuaho914b79a2017-06-19 16:03:19 +03002059 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2060 ASSERT(declaration);
2061 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2062 ASSERT(declarator->getLeft()->getAsSymbolNode());
2063
2064 // The condition is a declaration. In the AST representation we don't support declarations as
2065 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2066 // the loop.
2067 TIntermBlock *block = new TIntermBlock();
2068
2069 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2070 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2071 block->appendStatement(declareCondition);
2072
2073 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2074 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002075 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002076 block->appendStatement(loop);
2077 loop->setLine(line);
2078 block->setLine(line);
2079 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080}
2081
Olli Etuahocce89652017-06-19 16:04:09 +03002082TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2083 TIntermNodePair code,
2084 const TSourceLoc &loc)
2085{
Olli Etuaho56229f12017-07-10 14:16:33 +03002086 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002087
2088 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002089 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002090 {
2091 if (cond->getAsConstantUnion()->getBConst(0) == true)
2092 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002093 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002094 }
2095 else
2096 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002097 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002098 }
2099 }
2100
Olli Etuaho3ec75682017-07-05 17:02:55 +03002101 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002102 node->setLine(loc);
2103
2104 return node;
2105}
2106
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002107void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2108{
2109 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2110 typeSpecifier->getBasicType());
2111
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002112 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002113 {
2114 error(typeSpecifier->getLine(), "not supported", "first-class array");
2115 typeSpecifier->clearArrayness();
2116 }
2117}
2118
Martin Radev70866b82016-07-22 15:27:42 +03002119TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302120 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002121{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002122 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002123
Martin Radev70866b82016-07-22 15:27:42 +03002124 TPublicType returnType = typeSpecifier;
2125 returnType.qualifier = typeQualifier.qualifier;
2126 returnType.invariant = typeQualifier.invariant;
2127 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002128 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002129 returnType.precision = typeSpecifier.precision;
2130
2131 if (typeQualifier.precision != EbpUndefined)
2132 {
2133 returnType.precision = typeQualifier.precision;
2134 }
2135
Martin Radev4a9cd802016-09-01 16:51:51 +03002136 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2137 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002138
Martin Radev4a9cd802016-09-01 16:51:51 +03002139 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2140 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002141
Martin Radev4a9cd802016-09-01 16:51:51 +03002142 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002143
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002144 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002145 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002146 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002147 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002148 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002149 returnType.clearArrayness();
2150 }
2151
Martin Radev70866b82016-07-22 15:27:42 +03002152 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002153 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002154 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002155 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002156 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002157 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002158
Martin Radev70866b82016-07-22 15:27:42 +03002159 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002160 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002161 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002162 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002163 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002164 }
2165 }
2166 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002167 {
Martin Radev70866b82016-07-22 15:27:42 +03002168 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002169 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002170 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002171 }
Martin Radev70866b82016-07-22 15:27:42 +03002172 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2173 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002174 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002175 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2176 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002177 }
Martin Radev70866b82016-07-22 15:27:42 +03002178 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002179 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002180 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002181 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002182 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002183 }
2184
2185 return returnType;
2186}
2187
Olli Etuaho856c4972016-08-08 11:38:39 +03002188void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2189 const TPublicType &type,
2190 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002191{
2192 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002193 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002194 {
2195 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002196 }
2197
2198 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2199 switch (qualifier)
2200 {
2201 case EvqVertexIn:
2202 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002203 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002204 {
2205 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002206 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002207 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002208 return;
2209 case EvqFragmentOut:
2210 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002211 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002212 {
2213 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002214 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002215 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002216 return;
2217 default:
2218 break;
2219 }
2220
2221 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2222 // restrictions.
2223 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002224 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2225 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002226 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2227 {
2228 error(qualifierLocation, "must use 'flat' interpolation here",
2229 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002230 }
2231
Martin Radev4a9cd802016-09-01 16:51:51 +03002232 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002233 {
2234 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2235 // These restrictions are only implied by the ESSL 3.00 spec, but
2236 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002237 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002238 {
2239 error(qualifierLocation, "cannot be an array of structures",
2240 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002241 }
2242 if (type.isStructureContainingArrays())
2243 {
2244 error(qualifierLocation, "cannot be a structure containing an array",
2245 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002246 }
2247 if (type.isStructureContainingType(EbtStruct))
2248 {
2249 error(qualifierLocation, "cannot be a structure containing a structure",
2250 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002251 }
2252 if (type.isStructureContainingType(EbtBool))
2253 {
2254 error(qualifierLocation, "cannot be a structure containing a bool",
2255 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002256 }
2257 }
2258}
2259
Martin Radev2cc85b32016-08-05 16:22:53 +03002260void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2261{
2262 if (qualifier.getType() == QtStorage)
2263 {
2264 const TStorageQualifierWrapper &storageQualifier =
2265 static_cast<const TStorageQualifierWrapper &>(qualifier);
2266 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2267 !symbolTable.atGlobalLevel())
2268 {
2269 error(storageQualifier.getLine(),
2270 "Local variables can only use the const storage qualifier.",
2271 storageQualifier.getQualifierString().c_str());
2272 }
2273 }
2274}
2275
Olli Etuaho43364892017-02-13 16:00:12 +00002276void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002277 const TSourceLoc &location)
2278{
Jiajia Qinbc585152017-06-23 15:42:17 +08002279 const std::string reason(
2280 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2281 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002282 if (memoryQualifier.readonly)
2283 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002284 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002285 }
2286 if (memoryQualifier.writeonly)
2287 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002288 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002289 }
Martin Radev049edfa2016-11-11 14:35:37 +02002290 if (memoryQualifier.coherent)
2291 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002292 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002293 }
2294 if (memoryQualifier.restrictQualifier)
2295 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002296 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002297 }
2298 if (memoryQualifier.volatileQualifier)
2299 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002300 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002301 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002302}
2303
jchen104cdac9e2017-05-08 11:01:20 +08002304// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2305// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002306void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2307 const TSourceLoc &loc,
2308 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002309{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002310 if (!IsAtomicCounter(type->getBasicType()))
2311 {
2312 return;
2313 }
2314
2315 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2316 : kAtomicCounterSize;
2317 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2318 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002319 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002320 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002321 {
2322 offset = bindingState.appendSpan(size);
2323 }
2324 else
2325 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002326 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002327 }
2328 if (offset == -1)
2329 {
2330 error(loc, "Offset overlapping", "atomic counter");
2331 return;
2332 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002333 layoutQualifier.offset = offset;
2334 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002335}
2336
Olli Etuaho454c34c2017-10-25 16:35:56 +03002337void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002338 const ImmutableString &token,
Olli Etuaho454c34c2017-10-25 16:35:56 +03002339 TType *type)
2340{
2341 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2342 {
2343 if (type->isArray() && type->getOutermostArraySize() == 0u)
2344 {
2345 // Set size for the unsized geometry shader inputs if they are declared after a valid
2346 // input primitive declaration.
2347 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2348 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002349 ASSERT(mGlInVariableWithArraySize != nullptr);
2350 type->sizeOutermostUnsizedArray(
2351 mGlInVariableWithArraySize->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002352 }
2353 else
2354 {
2355 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2356 // An input can be declared without an array size if there is a previous layout
2357 // which specifies the size.
2358 error(location,
2359 "Missing a valid input primitive declaration before declaring an unsized "
2360 "array input",
2361 token);
2362 }
2363 }
2364 else if (type->isArray())
2365 {
2366 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2367 }
2368 else
2369 {
2370 error(location, "Geometry shader input variable must be declared as an array", token);
2371 }
2372 }
2373}
2374
Olli Etuaho13389b62016-10-16 11:48:18 +01002375TIntermDeclaration *TParseContext::parseSingleDeclaration(
2376 TPublicType &publicType,
2377 const TSourceLoc &identifierOrTypeLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002378 const ImmutableString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002379{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002380 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002381 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2382 mDirectiveHandler.pragma().stdgl.invariantAll)
2383 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002384 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002385
2386 // The directive handler has already taken care of rejecting invalid uses of this pragma
2387 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2388 // affected variable declarations:
2389 //
2390 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2391 // elsewhere, in TranslatorGLSL.)
2392 //
2393 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2394 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2395 // the way this is currently implemented we have to enable this compiler option before
2396 // parsing the shader and determining the shading language version it uses. If this were
2397 // implemented as a post-pass, the workaround could be more targeted.
2398 //
2399 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2400 // the specification, but there are desktop OpenGL drivers that expect that this is the
2401 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2402 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2403 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002404 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002405 }
2406 }
2407
Olli Etuahofbb1c792018-01-19 16:26:59 +02002408 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier, type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002409
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002410 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2411 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002412
Olli Etuahobab4c082015-04-24 16:38:49 +03002413 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002414 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002415
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002416 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002417 if (emptyDeclaration)
2418 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002419 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002420 // In most cases we don't need to create a symbol node for an empty declaration.
2421 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002422 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002423 {
Olli Etuaho195be942017-12-04 23:40:14 +02002424 TVariable *emptyVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02002425 new TVariable(&symbolTable, ImmutableString(""), type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002426 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002427 }
jchen104cdac9e2017-05-08 11:01:20 +08002428 else if (IsAtomicCounter(publicType.getBasicType()))
2429 {
2430 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2431 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002432 }
2433 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002434 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002435 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002436
Olli Etuahob60d30f2018-01-16 12:31:06 +02002437 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002438
Olli Etuahob60d30f2018-01-16 12:31:06 +02002439 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002440
Olli Etuaho2935c582015-04-08 14:32:06 +03002441 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002442 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002443 {
Olli Etuaho195be942017-12-04 23:40:14 +02002444 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002445 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002446 }
2447
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002448 TIntermDeclaration *declaration = new TIntermDeclaration();
2449 declaration->setLine(identifierOrTypeLocation);
2450 if (symbol)
2451 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002452 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002453 declaration->appendDeclarator(symbol);
2454 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002455 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002456}
2457
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002458TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2459 TPublicType &elementType,
2460 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002461 const ImmutableString &identifier,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002462 const TSourceLoc &indexLocation,
2463 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002464{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002465 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002466
Olli Etuaho55bde912017-10-25 13:41:13 +03002467 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002468 identifierLocation);
2469
Olli Etuaho55bde912017-10-25 13:41:13 +03002470 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002471
Olli Etuaho55bde912017-10-25 13:41:13 +03002472 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002473
Olli Etuahob60d30f2018-01-16 12:31:06 +02002474 TType *arrayType = new TType(elementType);
2475 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002476
Olli Etuahofbb1c792018-01-19 16:26:59 +02002477 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002478
Olli Etuahob60d30f2018-01-16 12:31:06 +02002479 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002480
Olli Etuahob60d30f2018-01-16 12:31:06 +02002481 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002482
Olli Etuaho13389b62016-10-16 11:48:18 +01002483 TIntermDeclaration *declaration = new TIntermDeclaration();
2484 declaration->setLine(identifierLocation);
2485
Olli Etuaho195be942017-12-04 23:40:14 +02002486 TVariable *variable = nullptr;
2487 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002488 {
Olli Etuaho195be942017-12-04 23:40:14 +02002489 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002490 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002491 declaration->appendDeclarator(symbol);
2492 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002493
Olli Etuaho13389b62016-10-16 11:48:18 +01002494 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002495}
2496
Olli Etuaho13389b62016-10-16 11:48:18 +01002497TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2498 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002499 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002500 const TSourceLoc &initLocation,
2501 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002502{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002503 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002504
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002505 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2506 identifierLocation);
2507
2508 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002509
Olli Etuaho13389b62016-10-16 11:48:18 +01002510 TIntermDeclaration *declaration = new TIntermDeclaration();
2511 declaration->setLine(identifierLocation);
2512
2513 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002514 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002515 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002516 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002517 if (initNode)
2518 {
2519 declaration->appendDeclarator(initNode);
2520 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002521 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002522 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002523}
2524
Olli Etuaho13389b62016-10-16 11:48:18 +01002525TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002526 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002527 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002528 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002529 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002530 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002531 const TSourceLoc &initLocation,
2532 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002533{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002534 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002535
Olli Etuaho55bde912017-10-25 13:41:13 +03002536 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002537 identifierLocation);
2538
Olli Etuaho55bde912017-10-25 13:41:13 +03002539 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002540
Olli Etuaho55bde912017-10-25 13:41:13 +03002541 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002542
Olli Etuahob60d30f2018-01-16 12:31:06 +02002543 TType *arrayType = new TType(elementType);
2544 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002545
Olli Etuaho13389b62016-10-16 11:48:18 +01002546 TIntermDeclaration *declaration = new TIntermDeclaration();
2547 declaration->setLine(identifierLocation);
2548
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002549 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002550 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002551 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002552 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002553 if (initNode)
2554 {
2555 declaration->appendDeclarator(initNode);
2556 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002557 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002558
2559 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002560}
2561
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002562TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002563 const TTypeQualifierBuilder &typeQualifierBuilder,
2564 const TSourceLoc &identifierLoc,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002565 const ImmutableString &identifier,
Martin Radev70866b82016-07-22 15:27:42 +03002566 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002567{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002568 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002569
Martin Radev70866b82016-07-22 15:27:42 +03002570 if (!typeQualifier.invariant)
2571 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002572 error(identifierLoc, "Expected invariant", identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002573 return nullptr;
2574 }
2575 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2576 {
2577 return nullptr;
2578 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002579 if (!symbol)
2580 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002581 error(identifierLoc, "undeclared identifier declared as invariant", identifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002582 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002583 }
Martin Radev70866b82016-07-22 15:27:42 +03002584 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002585 {
Martin Radev70866b82016-07-22 15:27:42 +03002586 error(identifierLoc, "invariant declaration specifies qualifier",
2587 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002588 }
Martin Radev70866b82016-07-22 15:27:42 +03002589 if (typeQualifier.precision != EbpUndefined)
2590 {
2591 error(identifierLoc, "invariant declaration specifies precision",
2592 getPrecisionString(typeQualifier.precision));
2593 }
2594 if (!typeQualifier.layoutQualifier.isEmpty())
2595 {
2596 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2597 }
2598
2599 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002600 if (!variable)
2601 {
2602 return nullptr;
2603 }
Martin Radev70866b82016-07-22 15:27:42 +03002604 const TType &type = variable->getType();
2605
2606 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2607 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002608 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002609
Olli Etuahodefe3932018-02-13 11:56:09 +02002610 symbolTable.addInvariantVarying(identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002611
Olli Etuaho195be942017-12-04 23:40:14 +02002612 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002613 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002614
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002615 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002616}
2617
Olli Etuaho13389b62016-10-16 11:48:18 +01002618void TParseContext::parseDeclarator(TPublicType &publicType,
2619 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002620 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002621 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002622{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002623 // If the declaration starting this declarator list was empty (example: int,), some checks were
2624 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002625 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002626 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002627 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2628 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002629 }
2630
Olli Etuaho856c4972016-08-08 11:38:39 +03002631 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002632
Olli Etuahob60d30f2018-01-16 12:31:06 +02002633 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002634
Olli Etuahofbb1c792018-01-19 16:26:59 +02002635 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002636
Olli Etuahob60d30f2018-01-16 12:31:06 +02002637 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002638
Olli Etuahob60d30f2018-01-16 12:31:06 +02002639 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002640
Olli Etuaho195be942017-12-04 23:40:14 +02002641 TVariable *variable = nullptr;
2642 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002643 {
Olli Etuaho195be942017-12-04 23:40:14 +02002644 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002645 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002646 declarationOut->appendDeclarator(symbol);
2647 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002648}
2649
Olli Etuaho55bde912017-10-25 13:41:13 +03002650void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002651 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002652 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002653 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002654 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002655 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002656{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002657 // If the declaration starting this declarator list was empty (example: int,), some checks were
2658 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002659 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002660 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002661 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002662 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002663 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002664
Olli Etuaho55bde912017-10-25 13:41:13 +03002665 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002666
Olli Etuaho55bde912017-10-25 13:41:13 +03002667 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002668 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002669 TType *arrayType = new TType(elementType);
2670 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002671
Olli Etuahofbb1c792018-01-19 16:26:59 +02002672 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002673
Olli Etuahob60d30f2018-01-16 12:31:06 +02002674 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002675
Olli Etuahob60d30f2018-01-16 12:31:06 +02002676 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002677
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002678 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002679 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002680 {
Olli Etuaho195be942017-12-04 23:40:14 +02002681 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002682 symbol->setLine(identifierLocation);
2683 declarationOut->appendDeclarator(symbol);
2684 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002685 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002686}
2687
Olli Etuaho13389b62016-10-16 11:48:18 +01002688void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2689 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002690 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002691 const TSourceLoc &initLocation,
2692 TIntermTyped *initializer,
2693 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002694{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002695 // If the declaration starting this declarator list was empty (example: int,), some checks were
2696 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002697 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002698 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002699 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2700 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002701 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002702
Olli Etuaho856c4972016-08-08 11:38:39 +03002703 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002704
Olli Etuaho13389b62016-10-16 11:48:18 +01002705 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002706 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002707 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002708 {
2709 //
2710 // build the intermediate representation
2711 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002712 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002713 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002714 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002715 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002716 }
2717}
2718
Olli Etuaho55bde912017-10-25 13:41:13 +03002719void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002720 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002721 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002722 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002723 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002724 const TSourceLoc &initLocation,
2725 TIntermTyped *initializer,
2726 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002727{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002728 // If the declaration starting this declarator list was empty (example: int,), some checks were
2729 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002730 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002731 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002732 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002733 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002734 }
2735
Olli Etuaho55bde912017-10-25 13:41:13 +03002736 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002737
Olli Etuaho55bde912017-10-25 13:41:13 +03002738 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002739
Olli Etuahob60d30f2018-01-16 12:31:06 +02002740 TType *arrayType = new TType(elementType);
2741 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002742
2743 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002744 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002745 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002746 {
2747 if (initNode)
2748 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002749 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002750 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002751 }
2752}
2753
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002754TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2755{
2756 // It's simpler to parse an empty statement as a constant expression rather than having a
2757 // different type of node just for empty statements, that will be pruned from the AST anyway.
2758 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2759 node->setLine(location);
2760 return node;
2761}
2762
jchen104cdac9e2017-05-08 11:01:20 +08002763void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2764 const TSourceLoc &location)
2765{
2766 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2767 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2768 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2769 {
2770 error(location, "Requires both binding and offset", "layout");
2771 return;
2772 }
2773 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2774}
2775
Olli Etuahocce89652017-06-19 16:04:09 +03002776void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2777 const TPublicType &type,
2778 const TSourceLoc &loc)
2779{
2780 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2781 !getFragmentPrecisionHigh())
2782 {
2783 error(loc, "precision is not supported in fragment shader", "highp");
2784 }
2785
2786 if (!CanSetDefaultPrecisionOnType(type))
2787 {
2788 error(loc, "illegal type argument for default precision qualifier",
2789 getBasicString(type.getBasicType()));
2790 return;
2791 }
2792 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2793}
2794
Shaob5cc1192017-07-06 10:47:20 +08002795bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2796{
2797 switch (typeQualifier.layoutQualifier.primitiveType)
2798 {
2799 case EptLines:
2800 case EptLinesAdjacency:
2801 case EptTriangles:
2802 case EptTrianglesAdjacency:
2803 return typeQualifier.qualifier == EvqGeometryIn;
2804
2805 case EptLineStrip:
2806 case EptTriangleStrip:
2807 return typeQualifier.qualifier == EvqGeometryOut;
2808
2809 case EptPoints:
2810 return true;
2811
2812 default:
2813 UNREACHABLE();
2814 return false;
2815 }
2816}
2817
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002818void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2819 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002820{
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002821 if (mGlInVariableWithArraySize == nullptr)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002822 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002823 const TSymbol *glPerVertex = symbolTable.findBuiltIn(ImmutableString("gl_PerVertex"), 310);
Olli Etuahodd21ecf2018-01-10 12:42:09 +02002824 const TInterfaceBlock *glPerVertexBlock = static_cast<const TInterfaceBlock *>(glPerVertex);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002825 TType *glInType = new TType(glPerVertexBlock, EvqPerVertexIn, TLayoutQualifier::Create());
2826 glInType->makeArray(inputArraySize);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002827 mGlInVariableWithArraySize =
Olli Etuahofbb1c792018-01-19 16:26:59 +02002828 new TVariable(&symbolTable, ImmutableString("gl_in"), glInType, SymbolType::BuiltIn,
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002829 TExtension::EXT_geometry_shader);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002830 }
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002831 else if (mGlInVariableWithArraySize->getType().getOutermostArraySize() != inputArraySize)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002832 {
2833 error(line,
2834 "Array size or input primitive declaration doesn't match the size of earlier sized "
2835 "array inputs.",
2836 "layout");
2837 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002838}
2839
Shaob5cc1192017-07-06 10:47:20 +08002840bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2841{
2842 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2843
2844 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2845
2846 if (layoutQualifier.maxVertices != -1)
2847 {
2848 error(typeQualifier.line,
2849 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2850 return false;
2851 }
2852
2853 // Set mGeometryInputPrimitiveType if exists
2854 if (layoutQualifier.primitiveType != EptUndefined)
2855 {
2856 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2857 {
2858 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2859 return false;
2860 }
2861
2862 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2863 {
2864 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002865 setGeometryShaderInputArraySize(
2866 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2867 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002868 }
2869 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2870 {
2871 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2872 "layout");
2873 return false;
2874 }
2875 }
2876
2877 // Set mGeometryInvocations if exists
2878 if (layoutQualifier.invocations > 0)
2879 {
2880 if (mGeometryShaderInvocations == 0)
2881 {
2882 mGeometryShaderInvocations = layoutQualifier.invocations;
2883 }
2884 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2885 {
2886 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2887 "layout");
2888 return false;
2889 }
2890 }
2891
2892 return true;
2893}
2894
2895bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2896{
2897 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2898
2899 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2900
2901 if (layoutQualifier.invocations > 0)
2902 {
2903 error(typeQualifier.line,
2904 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2905 return false;
2906 }
2907
2908 // Set mGeometryOutputPrimitiveType if exists
2909 if (layoutQualifier.primitiveType != EptUndefined)
2910 {
2911 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2912 {
2913 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2914 return false;
2915 }
2916
2917 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2918 {
2919 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2920 }
2921 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2922 {
2923 error(typeQualifier.line,
2924 "primitive doesn't match earlier output primitive declaration", "layout");
2925 return false;
2926 }
2927 }
2928
2929 // Set mGeometryMaxVertices if exists
2930 if (layoutQualifier.maxVertices > -1)
2931 {
2932 if (mGeometryShaderMaxVertices == -1)
2933 {
2934 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2935 }
2936 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2937 {
2938 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2939 "layout");
2940 return false;
2941 }
2942 }
2943
2944 return true;
2945}
2946
Martin Radev70866b82016-07-22 15:27:42 +03002947void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002948{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002949 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002950 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002951
Martin Radev70866b82016-07-22 15:27:42 +03002952 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2953 typeQualifier.line);
2954
Jamie Madillc2128ff2016-07-04 10:26:17 -04002955 // It should never be the case, but some strange parser errors can send us here.
2956 if (layoutQualifier.isEmpty())
2957 {
2958 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002959 return;
2960 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002961
Martin Radev802abe02016-08-04 17:48:32 +03002962 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002963 {
Olli Etuaho43364892017-02-13 16:00:12 +00002964 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002965 return;
2966 }
2967
Olli Etuaho43364892017-02-13 16:00:12 +00002968 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2969
2970 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002971
2972 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2973
Andrei Volykhina5527072017-03-22 16:46:30 +03002974 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2975
jchen104cdac9e2017-05-08 11:01:20 +08002976 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2977
Qin Jiajiaca68d982017-09-18 16:41:56 +08002978 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2979 typeQualifier.qualifier);
2980
Martin Radev802abe02016-08-04 17:48:32 +03002981 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002982 {
Martin Radev802abe02016-08-04 17:48:32 +03002983 if (mComputeShaderLocalSizeDeclared &&
2984 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2985 {
2986 error(typeQualifier.line, "Work group size does not match the previous declaration",
2987 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002988 return;
2989 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002990
Martin Radev802abe02016-08-04 17:48:32 +03002991 if (mShaderVersion < 310)
2992 {
2993 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002994 return;
2995 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002996
Martin Radev4c4c8e72016-08-04 12:25:34 +03002997 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002998 {
2999 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003000 return;
3001 }
3002
3003 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003004 symbolTable.findBuiltIn(ImmutableString("gl_MaxComputeWorkGroupSize"), mShaderVersion));
Martin Radev802abe02016-08-04 17:48:32 +03003005
3006 const TConstantUnion *maxComputeWorkGroupSizeData =
3007 maxComputeWorkGroupSize->getConstPointer();
3008
3009 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3010 {
3011 if (layoutQualifier.localSize[i] != -1)
3012 {
3013 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3014 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3015 if (mComputeShaderLocalSize[i] < 1 ||
3016 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3017 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003018 std::stringstream reasonStream;
3019 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3020 << maxComputeWorkGroupSizeValue;
3021 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003022
Olli Etuaho4de340a2016-12-16 09:32:03 +00003023 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003024 return;
3025 }
3026 }
3027 }
3028
3029 mComputeShaderLocalSizeDeclared = true;
3030 }
Shaob5cc1192017-07-06 10:47:20 +08003031 else if (typeQualifier.qualifier == EvqGeometryIn)
3032 {
3033 if (mShaderVersion < 310)
3034 {
3035 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3036 return;
3037 }
3038
3039 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3040 {
3041 return;
3042 }
3043 }
3044 else if (typeQualifier.qualifier == EvqGeometryOut)
3045 {
3046 if (mShaderVersion < 310)
3047 {
3048 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3049 "layout");
3050 return;
3051 }
3052
3053 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3054 {
3055 return;
3056 }
3057 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003058 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3059 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003060 {
3061 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3062 // specification.
3063 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3064 {
3065 error(typeQualifier.line, "Number of views does not match the previous declaration",
3066 "layout");
3067 return;
3068 }
3069
3070 if (layoutQualifier.numViews == -1)
3071 {
3072 error(typeQualifier.line, "No num_views specified", "layout");
3073 return;
3074 }
3075
3076 if (layoutQualifier.numViews > mMaxNumViews)
3077 {
3078 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3079 "layout");
3080 return;
3081 }
3082
3083 mNumViews = layoutQualifier.numViews;
3084 }
Martin Radev802abe02016-08-04 17:48:32 +03003085 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003086 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003087 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003088 {
Martin Radev802abe02016-08-04 17:48:32 +03003089 return;
3090 }
3091
Jiajia Qinbc585152017-06-23 15:42:17 +08003092 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003093 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003094 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003095 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003096 return;
3097 }
3098
3099 if (mShaderVersion < 300)
3100 {
3101 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3102 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003103 return;
3104 }
3105
Olli Etuaho09b04a22016-12-15 13:30:26 +00003106 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003107
3108 if (layoutQualifier.matrixPacking != EmpUnspecified)
3109 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003110 if (typeQualifier.qualifier == EvqUniform)
3111 {
3112 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3113 }
3114 else if (typeQualifier.qualifier == EvqBuffer)
3115 {
3116 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3117 }
Martin Radev802abe02016-08-04 17:48:32 +03003118 }
3119
3120 if (layoutQualifier.blockStorage != EbsUnspecified)
3121 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003122 if (typeQualifier.qualifier == EvqUniform)
3123 {
3124 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3125 }
3126 else if (typeQualifier.qualifier == EvqBuffer)
3127 {
3128 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3129 }
Martin Radev802abe02016-08-04 17:48:32 +03003130 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003131 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003132}
3133
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003134TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3135 const TFunction &function,
3136 const TSourceLoc &location,
3137 bool insertParametersToSymbolTable)
3138{
Olli Etuahobed35d72017-12-20 16:36:26 +02003139 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003140
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003141 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003142 prototype->setLine(location);
3143
3144 for (size_t i = 0; i < function.getParamCount(); i++)
3145 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003146 const TVariable *param = function.getParam(i);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003147
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003148 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3149 // be used for unused args).
Olli Etuahod4bd9632018-03-08 16:32:44 +02003150 if (param->symbolType() != SymbolType::Empty)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003151 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003152 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003153 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003154 if (!symbolTable.declare(const_cast<TVariable *>(param)))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003155 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003156 error(location, "redefinition", param->name());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003157 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003158 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003159 // Unsized type of a named parameter should have already been checked and sanitized.
Olli Etuahod4bd9632018-03-08 16:32:44 +02003160 ASSERT(!param->getType().isUnsizedArray());
Olli Etuaho55bde912017-10-25 13:41:13 +03003161 }
3162 else
3163 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003164 if (param->getType().isUnsizedArray())
Olli Etuaho55bde912017-10-25 13:41:13 +03003165 {
3166 error(location, "function parameter array must be sized at compile time", "[]");
3167 // We don't need to size the arrays since the parameter is unnamed and hence
3168 // inaccessible.
3169 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003170 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003171 }
3172 return prototype;
3173}
3174
Olli Etuaho16c745a2017-01-16 17:02:27 +00003175TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3176 const TFunction &parsedFunction,
3177 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003178{
Olli Etuaho476197f2016-10-11 13:59:08 +01003179 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3180 // first declaration. Either way the instance in the symbol table is used to track whether the
3181 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003182 bool hadPrototypeDeclaration = false;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003183 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003184 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3185
3186 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003187 {
3188 // ESSL 1.00.17 section 4.2.7.
3189 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3190 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003191 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003192
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003193 TIntermFunctionPrototype *prototype =
3194 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003195
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003196 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003197
3198 if (!symbolTable.atGlobalLevel())
3199 {
3200 // ESSL 3.00.4 section 4.2.4.
3201 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003202 }
3203
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003204 return prototype;
3205}
3206
Olli Etuaho336b1472016-10-05 16:37:55 +01003207TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003208 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003209 TIntermBlock *functionBody,
3210 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003211{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003212 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003213 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3214 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003215 error(location,
3216 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003217 }
3218
Olli Etuahof51fdd22016-10-03 10:03:40 +01003219 if (functionBody == nullptr)
3220 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003221 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003222 functionBody->setLine(location);
3223 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003224 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003225 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003226 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003227
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003228 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003229 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003230}
3231
Olli Etuaho476197f2016-10-11 13:59:08 +01003232void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003233 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003234 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003235{
Olli Etuaho476197f2016-10-11 13:59:08 +01003236 ASSERT(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003237
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003238 bool wasDefined = false;
3239 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3240 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003241 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003242 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003243 }
Jamie Madill185fb402015-06-12 15:48:48 -04003244
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003245 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003246 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003247 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003248
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003249 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003250 setLoopNestingLevel(0);
3251}
3252
Jamie Madillb98c3a82015-07-23 14:26:04 -04003253TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003254{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003255 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003256 // We don't know at this point whether this is a function definition or a prototype.
3257 // The definition production code will check for redefinitions.
3258 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003259 //
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303260
Olli Etuahod80f2942017-11-06 12:44:45 +02003261 for (size_t i = 0u; i < function->getParamCount(); ++i)
3262 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003263 const TVariable *param = function->getParam(i);
3264 if (param->getType().isStructSpecifier())
Olli Etuahod80f2942017-11-06 12:44:45 +02003265 {
3266 // ESSL 3.00.6 section 12.10.
3267 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003268 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003269 }
3270 }
3271
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003272 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303273 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003274 const UnmangledBuiltIn *builtIn =
3275 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3276 if (builtIn &&
3277 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3278 {
3279 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3280 // functions. Therefore overloading or redefining builtin functions is an error.
3281 error(location, "Name of a built-in function cannot be redeclared as function",
3282 function->name());
3283 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303284 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003285 else
3286 {
3287 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3288 // this applies to redeclarations as well.
3289 const TSymbol *builtIn =
3290 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3291 if (builtIn)
3292 {
3293 error(location, "built-in functions cannot be redefined", function->name());
3294 }
3295 }
3296
3297 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3298 // here.
3299 const TFunction *prevDec =
3300 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3301 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003302 {
3303 if (prevDec->getReturnType() != function->getReturnType())
3304 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003305 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003306 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003307 }
3308 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3309 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003310 if (prevDec->getParam(i)->getType().getQualifier() !=
3311 function->getParam(i)->getType().getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003312 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003313 error(location,
3314 "function must have the same parameter qualifiers in all of its declarations",
Olli Etuahod4bd9632018-03-08 16:32:44 +02003315 function->getParam(i)->getType().getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003316 }
3317 }
3318 }
3319
Jamie Madill185fb402015-06-12 15:48:48 -04003320 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003321 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3322 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003323 if (prevSym)
3324 {
3325 if (!prevSym->isFunction())
3326 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003327 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003328 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003329 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003330 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003331 // Parsing is at the inner scope level of the function's arguments and body statement at this
3332 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3333 // scope.
3334 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003335
Olli Etuaho78d13742017-01-18 13:06:10 +00003336 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003337 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003338 {
3339 if (function->getParamCount() > 0)
3340 {
3341 error(location, "function cannot take any parameter(s)", "main");
3342 }
3343 if (function->getReturnType().getBasicType() != EbtVoid)
3344 {
3345 error(location, "main function cannot return a value",
3346 function->getReturnType().getBasicString());
3347 }
3348 }
3349
Jamie Madill185fb402015-06-12 15:48:48 -04003350 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003351 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3352 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003353 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3354 //
3355 return function;
3356}
3357
Olli Etuaho9de84a52016-06-14 17:36:01 +03003358TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003359 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003360 const TSourceLoc &location)
3361{
3362 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3363 {
3364 error(location, "no qualifiers allowed for function return",
3365 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003366 }
3367 if (!type.layoutQualifier.isEmpty())
3368 {
3369 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003370 }
jchen10cc2a10e2017-05-03 14:05:12 +08003371 // make sure an opaque type is not involved as well...
3372 std::string reason(getBasicString(type.getBasicType()));
3373 reason += "s can't be function return values";
3374 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003375 if (mShaderVersion < 300)
3376 {
3377 // Array return values are forbidden, but there's also no valid syntax for declaring array
3378 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003379 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003380
3381 if (type.isStructureContainingArrays())
3382 {
3383 // ESSL 1.00.17 section 6.1 Function Definitions
3384 error(location, "structures containing arrays can't be function return values",
3385 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003386 }
3387 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003388
3389 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003390 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003391}
3392
Olli Etuaho697bf652018-02-16 11:50:54 +02003393TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3394 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003395{
Olli Etuaho697bf652018-02-16 11:50:54 +02003396 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003397}
3398
Olli Etuaho95ed1942018-02-01 14:01:19 +02003399TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003400{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003401 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003402 {
3403 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3404 "[]");
3405 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003406 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003407 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003408 error(publicType.getLine(), "constructor can't be a structure definition",
3409 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003410 }
3411
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003412 TType *type = new TType(publicType);
3413 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003414 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003415 error(publicType.getLine(), "cannot construct this type",
3416 getBasicString(publicType.getBasicType()));
3417 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003418 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003419 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003420}
3421
Olli Etuaho55bde912017-10-25 13:41:13 +03003422void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3423 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003424 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003425 TType *arrayType)
3426{
3427 if (arrayType->isUnsizedArray())
3428 {
3429 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003430 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003431 }
3432}
3433
3434TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003435 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003436 const TSourceLoc &nameLoc)
3437{
Olli Etuaho55bde912017-10-25 13:41:13 +03003438 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003439 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003440 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003441 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003442 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003443 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003444 checkIsNotReserved(nameLoc, name);
3445 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003446 return param;
3447}
3448
Olli Etuaho55bde912017-10-25 13:41:13 +03003449TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003450 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003451 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003452{
Olli Etuaho55bde912017-10-25 13:41:13 +03003453 TType *type = new TType(publicType);
3454 return parseParameterDeclarator(type, name, nameLoc);
3455}
3456
Olli Etuahofbb1c792018-01-19 16:26:59 +02003457TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003458 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003459 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003460 const TSourceLoc &arrayLoc,
3461 TPublicType *elementType)
3462{
3463 checkArrayElementIsNotArray(arrayLoc, *elementType);
3464 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003465 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003466 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003467}
3468
Olli Etuaho95ed1942018-02-01 14:01:19 +02003469bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3470 const TIntermSequence &arguments,
3471 TType type,
3472 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003473{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003474 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003475 {
3476 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3477 return false;
3478 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003479 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003480 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003481 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003482 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003483 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3484 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003485 {
3486 error(line, "constructing from a non-dereferenced array", "constructor");
3487 return false;
3488 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003489 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003490 {
3491 if (dimensionalityFromElement == 1u)
3492 {
3493 error(line, "implicitly sized array of arrays constructor argument is not an array",
3494 "constructor");
3495 }
3496 else
3497 {
3498 error(line,
3499 "implicitly sized array of arrays constructor argument dimensionality is too "
3500 "low",
3501 "constructor");
3502 }
3503 return false;
3504 }
3505 }
3506 return true;
3507}
3508
Jamie Madillb98c3a82015-07-23 14:26:04 -04003509// This function is used to test for the correctness of the parameters passed to various constructor
3510// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003511//
Olli Etuaho856c4972016-08-08 11:38:39 +03003512// 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 +00003513//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003514TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003515{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003516 TType type = fnCall->constructorType();
3517 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003518 if (type.isUnsizedArray())
3519 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003520 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003521 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003522 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003523 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003524 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003525 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003526 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003527 if (type.getOutermostArraySize() == 0u)
3528 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003529 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003530 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003531 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003532 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003533 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003534 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003535 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003536 }
3537 }
3538 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003539 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003540
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003541 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003542 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003543 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003544 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003545
Olli Etuaho95ed1942018-02-01 14:01:19 +02003546 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003547 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003548
Olli Etuaho765924f2018-01-04 12:48:36 +02003549 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003550}
3551
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003552//
3553// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003554// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003555//
Olli Etuaho13389b62016-10-16 11:48:18 +01003556TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003557 const TTypeQualifierBuilder &typeQualifierBuilder,
3558 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003559 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003560 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003561 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003562 const TSourceLoc &instanceLine,
3563 TIntermTyped *arrayIndex,
3564 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003565{
Olli Etuaho856c4972016-08-08 11:38:39 +03003566 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003567
Olli Etuaho77ba4082016-12-16 12:01:18 +00003568 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003569
Jiajia Qinbc585152017-06-23 15:42:17 +08003570 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003571 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003572 error(typeQualifier.line,
3573 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3574 "3.10",
3575 getQualifierString(typeQualifier.qualifier));
3576 }
3577 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3578 {
3579 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003580 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003581 }
3582
Martin Radev70866b82016-07-22 15:27:42 +03003583 if (typeQualifier.invariant)
3584 {
3585 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3586 }
3587
Jiajia Qinbc585152017-06-23 15:42:17 +08003588 if (typeQualifier.qualifier != EvqBuffer)
3589 {
3590 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3591 }
Olli Etuaho43364892017-02-13 16:00:12 +00003592
jchen10af713a22017-04-19 09:10:56 +08003593 // add array index
3594 unsigned int arraySize = 0;
3595 if (arrayIndex != nullptr)
3596 {
3597 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3598 }
3599
3600 if (mShaderVersion < 310)
3601 {
3602 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3603 }
3604 else
3605 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003606 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3607 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003608 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003609
Andrei Volykhina5527072017-03-22 16:46:30 +03003610 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3611
Jamie Madill099c0f32013-06-20 11:55:52 -04003612 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003613 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003614 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3615 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003616
Jamie Madill099c0f32013-06-20 11:55:52 -04003617 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3618 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003619 if (typeQualifier.qualifier == EvqUniform)
3620 {
3621 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3622 }
3623 else if (typeQualifier.qualifier == EvqBuffer)
3624 {
3625 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3626 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003627 }
3628
Jamie Madill1566ef72013-06-20 11:55:54 -04003629 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3630 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003631 if (typeQualifier.qualifier == EvqUniform)
3632 {
3633 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3634 }
3635 else if (typeQualifier.qualifier == EvqBuffer)
3636 {
3637 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3638 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003639 }
3640
Olli Etuaho856c4972016-08-08 11:38:39 +03003641 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003642
Martin Radev2cc85b32016-08-05 16:22:53 +03003643 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3644
Jamie Madill98493dd2013-07-08 14:39:03 -04003645 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303646 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3647 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003648 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303649 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003650 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303651 {
jchen10cc2a10e2017-05-03 14:05:12 +08003652 std::string reason("unsupported type - ");
3653 reason += fieldType->getBasicString();
3654 reason += " types are not allowed in interface blocks";
3655 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003656 }
3657
Jamie Madill98493dd2013-07-08 14:39:03 -04003658 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003659 switch (qualifier)
3660 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003661 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003662 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003663 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003664 if (typeQualifier.qualifier == EvqBuffer)
3665 {
3666 error(field->line(), "invalid qualifier on shader storage block member",
3667 getQualifierString(qualifier));
3668 }
3669 break;
3670 case EvqBuffer:
3671 if (typeQualifier.qualifier == EvqUniform)
3672 {
3673 error(field->line(), "invalid qualifier on uniform block member",
3674 getQualifierString(qualifier));
3675 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003676 break;
3677 default:
3678 error(field->line(), "invalid qualifier on interface block member",
3679 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003680 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003681 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003682
Martin Radev70866b82016-07-22 15:27:42 +03003683 if (fieldType->isInvariant())
3684 {
3685 error(field->line(), "invalid qualifier on interface block member", "invariant");
3686 }
3687
Jamie Madilla5efff92013-06-06 11:56:47 -04003688 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003689 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003690 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003691 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003692
Jamie Madill98493dd2013-07-08 14:39:03 -04003693 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003694 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003695 error(field->line(), "invalid layout qualifier: cannot be used here",
3696 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003697 }
3698
Jamie Madill98493dd2013-07-08 14:39:03 -04003699 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003700 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003701 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003702 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003703 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003704 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003705 warning(field->line(),
3706 "extraneous layout qualifier: only has an effect on matrix types",
3707 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003708 }
3709
Jamie Madill98493dd2013-07-08 14:39:03 -04003710 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003711
Olli Etuahoebee5b32017-11-23 12:56:32 +02003712 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3713 typeQualifier.qualifier != EvqBuffer)
3714 {
3715 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3716 checkIsNotUnsizedArray(field->line(),
3717 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003718 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003719 }
3720
Jiajia Qinbc585152017-06-23 15:42:17 +08003721 if (typeQualifier.qualifier == EvqBuffer)
3722 {
3723 // set memory qualifiers
3724 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3725 // qualified with a memory qualifier, it is as if all of its members were declared with
3726 // the same memory qualifier.
3727 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3728 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3729 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3730 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3731 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3732 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3733 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3734 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3735 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3736 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3737 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003738 }
3739
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003740 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003741 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003742 if (!symbolTable.declare(interfaceBlock))
Olli Etuaho378c3a52017-12-04 11:32:13 +02003743 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003744 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003745 }
3746
Olli Etuahob60d30f2018-01-16 12:31:06 +02003747 TType *interfaceBlockType =
3748 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003749 if (arrayIndex != nullptr)
3750 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003751 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003752 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003753
Olli Etuaho195be942017-12-04 23:40:14 +02003754 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003755 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3756 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003757 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003758 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003759 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003760
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003761 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003762 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003763 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003764 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3765 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003766 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003767 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003768
3769 // set parent pointer of the field variable
3770 fieldType->setInterfaceBlock(interfaceBlock);
3771
Olli Etuahob60d30f2018-01-16 12:31:06 +02003772 fieldType->setQualifier(typeQualifier.qualifier);
3773
Olli Etuaho195be942017-12-04 23:40:14 +02003774 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003775 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003776 if (!symbolTable.declare(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303777 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003778 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003779 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003780 }
3781 }
3782 }
3783 else
3784 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003785 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003786
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003787 // add a symbol for this interface block
Olli Etuaho437664b2018-02-28 15:38:14 +02003788 if (!symbolTable.declare(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303789 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003790 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003791 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003792 }
3793
Olli Etuaho195be942017-12-04 23:40:14 +02003794 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3795 blockSymbol->setLine(typeQualifier.line);
3796 TIntermDeclaration *declaration = new TIntermDeclaration();
3797 declaration->appendDeclarator(blockSymbol);
3798 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003799
3800 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003801 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003802}
3803
Olli Etuahofbb1c792018-01-19 16:26:59 +02003804void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3805 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003806{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003807 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003808
3809 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003810 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303811 if (mStructNestingLevel > 1)
3812 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003813 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003814 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003815}
3816
3817void TParseContext::exitStructDeclaration()
3818{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003819 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003820}
3821
Olli Etuaho8a176262016-08-16 14:23:01 +03003822void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003823{
Jamie Madillacb4b812016-11-07 13:50:29 -05003824 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303825 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003826 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003827 }
3828
Arun Patole7e7e68d2015-05-22 12:02:25 +05303829 if (field.type()->getBasicType() != EbtStruct)
3830 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003831 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003832 }
3833
3834 // We're already inside a structure definition at this point, so add
3835 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303836 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3837 {
Jamie Madill41a49272014-03-18 16:10:13 -04003838 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003839 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3840 {
3841 // This may happen in case there are nested struct definitions. While they are also
3842 // invalid GLSL, they don't cause a syntax error.
3843 reasonStream << "Struct nesting";
3844 }
3845 else
3846 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003847 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003848 }
3849 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003850 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003851 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003852 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003853 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003854}
3855
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003856//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003857// Parse an array index expression
3858//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003859TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3860 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303861 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003862{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003863 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3864 {
3865 if (baseExpression->getAsSymbolNode())
3866 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303867 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003868 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003869 }
3870 else
3871 {
3872 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3873 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003874
Olli Etuaho3ec75682017-07-05 17:02:55 +03003875 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003876 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003877
Jiawei Shaod8105a02017-08-08 09:54:36 +08003878 if (baseExpression->getQualifier() == EvqPerVertexIn)
3879 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003880 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003881 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3882 {
3883 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3884 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3885 }
3886 }
3887
Jamie Madill21c1e452014-12-29 11:33:41 -05003888 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3889
Olli Etuaho36b05142015-11-12 13:10:42 +02003890 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3891 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3892 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3893 // index is a constant expression.
3894 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3895 {
3896 if (baseExpression->isInterfaceBlock())
3897 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003898 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003899 switch (baseExpression->getQualifier())
3900 {
3901 case EvqPerVertexIn:
3902 break;
3903 case EvqUniform:
3904 case EvqBuffer:
3905 error(location,
3906 "array indexes for uniform block arrays and shader storage block arrays "
3907 "must be constant integral expressions",
3908 "[");
3909 break;
3910 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003911 // We can reach here only in error cases.
3912 ASSERT(mDiagnostics->numErrors() > 0);
3913 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003914 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003915 }
3916 else if (baseExpression->getQualifier() == EvqFragmentOut)
3917 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003918 error(location,
3919 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003920 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003921 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3922 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003923 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003924 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003925 }
3926
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003927 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003928 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003929 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3930 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3931 // constant fold expressions that are not constant expressions). The most compatible way to
3932 // handle this case is to report a warning instead of an error and force the index to be in
3933 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003934 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003935 int index = 0;
3936 if (indexConstantUnion->getBasicType() == EbtInt)
3937 {
3938 index = indexConstantUnion->getIConst(0);
3939 }
3940 else if (indexConstantUnion->getBasicType() == EbtUInt)
3941 {
3942 index = static_cast<int>(indexConstantUnion->getUConst(0));
3943 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003944
3945 int safeIndex = -1;
3946
Olli Etuahoebee5b32017-11-23 12:56:32 +02003947 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04003948 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003949 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
3950 safeIndex = 0;
3951 }
3952
3953 if (!baseExpression->getType().isUnsizedArray())
3954 {
3955 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03003956 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003957 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003958 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003959 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
3960 {
3961 outOfRangeError(outOfRangeIndexIsError, location,
3962 "array index for gl_FragData must be zero when "
3963 "GL_EXT_draw_buffers is disabled",
3964 "[]");
3965 safeIndex = 0;
3966 }
3967 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02003968 }
3969 // Only do generic out-of-range check if similar error hasn't already been reported.
3970 if (safeIndex < 0)
3971 {
3972 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02003973 {
3974 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
3975 baseExpression->getOutermostArraySize(),
3976 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003977 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02003978 else if (baseExpression->isMatrix())
3979 {
3980 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
3981 baseExpression->getType().getCols(),
3982 "matrix field selection out of range");
3983 }
3984 else
3985 {
3986 ASSERT(baseExpression->isVector());
3987 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
3988 baseExpression->getType().getNominalSize(),
3989 "vector field selection out of range");
3990 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02003991 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003992
Olli Etuahoebee5b32017-11-23 12:56:32 +02003993 ASSERT(safeIndex >= 0);
3994 // Data of constant unions can't be changed, because it may be shared with other
3995 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3996 // sanitized object.
3997 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
3998 {
3999 TConstantUnion *safeConstantUnion = new TConstantUnion();
4000 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004001 indexExpression = new TIntermConstantUnion(
4002 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4003 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004004 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004005
Olli Etuahoebee5b32017-11-23 12:56:32 +02004006 TIntermBinary *node =
4007 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4008 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004009 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004010 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004011 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004012
4013 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4014 node->setLine(location);
4015 // Indirect indexing can never be constant folded.
4016 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004017}
4018
Olli Etuahoebee5b32017-11-23 12:56:32 +02004019int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4020 const TSourceLoc &location,
4021 int index,
4022 int arraySize,
4023 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004024{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004025 // Should not reach here with an unsized / runtime-sized array.
4026 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004027 // A negative index should already have been checked.
4028 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004029 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004030 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004031 std::stringstream reasonStream;
4032 reasonStream << reason << " '" << index << "'";
4033 std::string token = reasonStream.str();
4034 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004035 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004036 }
4037 return index;
4038}
4039
Jamie Madillb98c3a82015-07-23 14:26:04 -04004040TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4041 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004042 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004043 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004044{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004045 if (baseExpression->isArray())
4046 {
4047 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004048 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004049 }
4050
4051 if (baseExpression->isVector())
4052 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004053 TVector<int> fieldOffsets;
4054 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4055 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004056 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004057 fieldOffsets.resize(1);
4058 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004059 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004060 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4061 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004062
Olli Etuaho765924f2018-01-04 12:48:36 +02004063 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004064 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004065 else if (baseExpression->getBasicType() == EbtStruct)
4066 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304067 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004068 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004069 {
4070 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004071 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004072 }
4073 else
4074 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004075 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004076 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004077 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004078 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004079 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004080 {
4081 fieldFound = true;
4082 break;
4083 }
4084 }
4085 if (fieldFound)
4086 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004087 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004088 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004089 TIntermBinary *node =
4090 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4091 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004092 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004093 }
4094 else
4095 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004096 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004097 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004098 }
4099 }
4100 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004101 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004102 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304103 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004104 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004105 {
4106 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004107 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004108 }
4109 else
4110 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004111 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004112 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004113 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004114 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004115 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004116 {
4117 fieldFound = true;
4118 break;
4119 }
4120 }
4121 if (fieldFound)
4122 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004123 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004124 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004125 TIntermBinary *node =
4126 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4127 node->setLine(dotLocation);
4128 // Indexing interface blocks can never be constant folded.
4129 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004130 }
4131 else
4132 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004133 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004134 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004135 }
4136 }
4137 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004138 else
4139 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004140 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004141 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004142 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004143 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004144 }
4145 else
4146 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304147 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004148 " field selection requires structure, vector, or interface block on left hand "
4149 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004150 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004151 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004152 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004153 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004154}
4155
Olli Etuahofbb1c792018-01-19 16:26:59 +02004156TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004157 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004158{
Jamie Madill2f294c92017-11-20 14:47:26 -05004159 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004160
4161 if (qualifierType == "shared")
4162 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004163 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004164 {
4165 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4166 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004167 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004168 }
4169 else if (qualifierType == "packed")
4170 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004171 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004172 {
4173 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4174 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004175 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004176 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004177 else if (qualifierType == "std430")
4178 {
4179 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4180 qualifier.blockStorage = EbsStd430;
4181 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004182 else if (qualifierType == "std140")
4183 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004184 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004185 }
4186 else if (qualifierType == "row_major")
4187 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004188 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004189 }
4190 else if (qualifierType == "column_major")
4191 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004192 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004193 }
4194 else if (qualifierType == "location")
4195 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004196 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004197 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004198 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004199 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004200 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004201 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4202 {
4203 qualifier.yuv = true;
4204 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004205 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004206 else if (qualifierType == "rgba32f")
4207 {
4208 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4209 qualifier.imageInternalFormat = EiifRGBA32F;
4210 }
4211 else if (qualifierType == "rgba16f")
4212 {
4213 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4214 qualifier.imageInternalFormat = EiifRGBA16F;
4215 }
4216 else if (qualifierType == "r32f")
4217 {
4218 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4219 qualifier.imageInternalFormat = EiifR32F;
4220 }
4221 else if (qualifierType == "rgba8")
4222 {
4223 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4224 qualifier.imageInternalFormat = EiifRGBA8;
4225 }
4226 else if (qualifierType == "rgba8_snorm")
4227 {
4228 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4229 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4230 }
4231 else if (qualifierType == "rgba32i")
4232 {
4233 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4234 qualifier.imageInternalFormat = EiifRGBA32I;
4235 }
4236 else if (qualifierType == "rgba16i")
4237 {
4238 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4239 qualifier.imageInternalFormat = EiifRGBA16I;
4240 }
4241 else if (qualifierType == "rgba8i")
4242 {
4243 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4244 qualifier.imageInternalFormat = EiifRGBA8I;
4245 }
4246 else if (qualifierType == "r32i")
4247 {
4248 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4249 qualifier.imageInternalFormat = EiifR32I;
4250 }
4251 else if (qualifierType == "rgba32ui")
4252 {
4253 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4254 qualifier.imageInternalFormat = EiifRGBA32UI;
4255 }
4256 else if (qualifierType == "rgba16ui")
4257 {
4258 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4259 qualifier.imageInternalFormat = EiifRGBA16UI;
4260 }
4261 else if (qualifierType == "rgba8ui")
4262 {
4263 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4264 qualifier.imageInternalFormat = EiifRGBA8UI;
4265 }
4266 else if (qualifierType == "r32ui")
4267 {
4268 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4269 qualifier.imageInternalFormat = EiifR32UI;
4270 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004271 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4272 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004273 {
4274 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4275 qualifier.primitiveType = EptPoints;
4276 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004277 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4278 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004279 {
4280 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4281 qualifier.primitiveType = EptLines;
4282 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004283 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4284 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004285 {
4286 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4287 qualifier.primitiveType = EptLinesAdjacency;
4288 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004289 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4290 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004291 {
4292 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4293 qualifier.primitiveType = EptTriangles;
4294 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004295 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4296 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004297 {
4298 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4299 qualifier.primitiveType = EptTrianglesAdjacency;
4300 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004301 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4302 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004303 {
4304 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4305 qualifier.primitiveType = EptLineStrip;
4306 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004307 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4308 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004309 {
4310 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4311 qualifier.primitiveType = EptTriangleStrip;
4312 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004313
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004314 else
4315 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004316 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004317 }
4318
Jamie Madilla5efff92013-06-06 11:56:47 -04004319 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004320}
4321
Olli Etuahofbb1c792018-01-19 16:26:59 +02004322void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004323 const TSourceLoc &qualifierTypeLine,
4324 int intValue,
4325 const TSourceLoc &intValueLine,
4326 const std::string &intValueString,
4327 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004328 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004329{
Olli Etuaho856c4972016-08-08 11:38:39 +03004330 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004331 if (intValue < 1)
4332 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004333 std::stringstream reasonStream;
4334 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4335 std::string reason = reasonStream.str();
4336 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004337 }
4338 (*localSize)[index] = intValue;
4339}
4340
Olli Etuaho09b04a22016-12-15 13:30:26 +00004341void TParseContext::parseNumViews(int intValue,
4342 const TSourceLoc &intValueLine,
4343 const std::string &intValueString,
4344 int *numViews)
4345{
4346 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4347 // specification.
4348 if (intValue < 1)
4349 {
4350 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4351 }
4352 *numViews = intValue;
4353}
4354
Shaob5cc1192017-07-06 10:47:20 +08004355void TParseContext::parseInvocations(int intValue,
4356 const TSourceLoc &intValueLine,
4357 const std::string &intValueString,
4358 int *numInvocations)
4359{
4360 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4361 // it doesn't make sense to accept invocations <= 0.
4362 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4363 {
4364 error(intValueLine,
4365 "out of range: invocations must be in the range of [1, "
4366 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4367 intValueString.c_str());
4368 }
4369 else
4370 {
4371 *numInvocations = intValue;
4372 }
4373}
4374
4375void TParseContext::parseMaxVertices(int intValue,
4376 const TSourceLoc &intValueLine,
4377 const std::string &intValueString,
4378 int *maxVertices)
4379{
4380 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4381 // it doesn't make sense to accept max_vertices < 0.
4382 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4383 {
4384 error(
4385 intValueLine,
4386 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4387 intValueString.c_str());
4388 }
4389 else
4390 {
4391 *maxVertices = intValue;
4392 }
4393}
4394
Olli Etuahofbb1c792018-01-19 16:26:59 +02004395TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004396 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004397 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304398 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004399{
Jamie Madill2f294c92017-11-20 14:47:26 -05004400 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004401
Martin Radev802abe02016-08-04 17:48:32 +03004402 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004403
Martin Radev802abe02016-08-04 17:48:32 +03004404 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004405 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004406 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004407 if (intValue < 0)
4408 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004409 error(intValueLine, "out of range: location must be non-negative",
4410 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004411 }
4412 else
4413 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004414 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004415 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004416 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004417 }
Olli Etuaho43364892017-02-13 16:00:12 +00004418 else if (qualifierType == "binding")
4419 {
4420 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4421 if (intValue < 0)
4422 {
4423 error(intValueLine, "out of range: binding must be non-negative",
4424 intValueString.c_str());
4425 }
4426 else
4427 {
4428 qualifier.binding = intValue;
4429 }
4430 }
jchen104cdac9e2017-05-08 11:01:20 +08004431 else if (qualifierType == "offset")
4432 {
4433 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4434 if (intValue < 0)
4435 {
4436 error(intValueLine, "out of range: offset must be non-negative",
4437 intValueString.c_str());
4438 }
4439 else
4440 {
4441 qualifier.offset = intValue;
4442 }
4443 }
Martin Radev802abe02016-08-04 17:48:32 +03004444 else if (qualifierType == "local_size_x")
4445 {
4446 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4447 &qualifier.localSize);
4448 }
4449 else if (qualifierType == "local_size_y")
4450 {
4451 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4452 &qualifier.localSize);
4453 }
4454 else if (qualifierType == "local_size_z")
4455 {
4456 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4457 &qualifier.localSize);
4458 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004459 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004460 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004461 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4462 {
4463 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4464 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004465 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004466 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4467 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004468 {
4469 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4470 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004471 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4472 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004473 {
4474 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4475 }
4476
Martin Radev802abe02016-08-04 17:48:32 +03004477 else
4478 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004479 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004480 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004481
Jamie Madilla5efff92013-06-06 11:56:47 -04004482 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004483}
4484
Olli Etuaho613b9592016-09-05 12:05:53 +03004485TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4486{
4487 return new TTypeQualifierBuilder(
4488 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4489 mShaderVersion);
4490}
4491
Olli Etuahocce89652017-06-19 16:04:09 +03004492TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4493 const TSourceLoc &loc)
4494{
4495 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4496 return new TStorageQualifierWrapper(qualifier, loc);
4497}
4498
4499TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4500{
4501 if (getShaderType() == GL_VERTEX_SHADER)
4502 {
4503 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4504 }
4505 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4506}
4507
4508TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4509{
4510 if (declaringFunction())
4511 {
4512 return new TStorageQualifierWrapper(EvqIn, loc);
4513 }
Shaob5cc1192017-07-06 10:47:20 +08004514
4515 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004516 {
Shaob5cc1192017-07-06 10:47:20 +08004517 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004518 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004519 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004520 {
4521 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4522 }
4523 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004524 }
Shaob5cc1192017-07-06 10:47:20 +08004525 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004526 {
Shaob5cc1192017-07-06 10:47:20 +08004527 if (mShaderVersion < 300)
4528 {
4529 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4530 }
4531 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004532 }
Shaob5cc1192017-07-06 10:47:20 +08004533 case GL_COMPUTE_SHADER:
4534 {
4535 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4536 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004537 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004538 {
4539 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4540 }
4541 default:
4542 {
4543 UNREACHABLE();
4544 return new TStorageQualifierWrapper(EvqLast, loc);
4545 }
Olli Etuahocce89652017-06-19 16:04:09 +03004546 }
Olli Etuahocce89652017-06-19 16:04:09 +03004547}
4548
4549TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4550{
4551 if (declaringFunction())
4552 {
4553 return new TStorageQualifierWrapper(EvqOut, loc);
4554 }
Shaob5cc1192017-07-06 10:47:20 +08004555 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004556 {
Shaob5cc1192017-07-06 10:47:20 +08004557 case GL_VERTEX_SHADER:
4558 {
4559 if (mShaderVersion < 300)
4560 {
4561 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4562 }
4563 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4564 }
4565 case GL_FRAGMENT_SHADER:
4566 {
4567 if (mShaderVersion < 300)
4568 {
4569 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4570 }
4571 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4572 }
4573 case GL_COMPUTE_SHADER:
4574 {
4575 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4576 return new TStorageQualifierWrapper(EvqLast, loc);
4577 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004578 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004579 {
4580 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4581 }
4582 default:
4583 {
4584 UNREACHABLE();
4585 return new TStorageQualifierWrapper(EvqLast, loc);
4586 }
Olli Etuahocce89652017-06-19 16:04:09 +03004587 }
Olli Etuahocce89652017-06-19 16:04:09 +03004588}
4589
4590TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4591{
4592 if (!declaringFunction())
4593 {
4594 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4595 }
4596 return new TStorageQualifierWrapper(EvqInOut, loc);
4597}
4598
Jamie Madillb98c3a82015-07-23 14:26:04 -04004599TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004600 TLayoutQualifier rightQualifier,
4601 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004602{
Martin Radevc28888b2016-07-22 15:27:42 +03004603 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004604 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004605}
4606
Olli Etuahofbb1c792018-01-19 16:26:59 +02004607TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4608 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004609{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004610 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004611 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004612}
4613
Olli Etuahofbb1c792018-01-19 16:26:59 +02004614TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004615 const TSourceLoc &loc,
4616 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004617{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004618 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004619 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004620}
4621
Olli Etuaho722bfb52017-10-26 17:00:11 +03004622void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4623 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004624 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004625 const TSourceLoc &location)
4626{
4627 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4628 {
4629 if ((*fieldIter)->name() == name)
4630 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004631 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004632 }
4633 }
4634}
4635
4636TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4637{
4638 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4639 ++fieldIter)
4640 {
4641 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4642 location);
4643 }
4644 return fields;
4645}
4646
Olli Etuaho4de340a2016-12-16 09:32:03 +00004647TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4648 const TFieldList *newlyAddedFields,
4649 const TSourceLoc &location)
4650{
4651 for (TField *field : *newlyAddedFields)
4652 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004653 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4654 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004655 processedFields->push_back(field);
4656 }
4657 return processedFields;
4658}
4659
Martin Radev70866b82016-07-22 15:27:42 +03004660TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4661 const TTypeQualifierBuilder &typeQualifierBuilder,
4662 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004663 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004664{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004665 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004666
Martin Radev70866b82016-07-22 15:27:42 +03004667 typeSpecifier->qualifier = typeQualifier.qualifier;
4668 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004669 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004670 typeSpecifier->invariant = typeQualifier.invariant;
4671 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304672 {
Martin Radev70866b82016-07-22 15:27:42 +03004673 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004674 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004675 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004676}
4677
Jamie Madillb98c3a82015-07-23 14:26:04 -04004678TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004679 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004680{
Martin Radev4a9cd802016-09-01 16:51:51 +03004681 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4682 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004683
Olli Etuahofbb1c792018-01-19 16:26:59 +02004684 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004685 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004686
Martin Radev4a9cd802016-09-01 16:51:51 +03004687 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004688
Olli Etuahod5f44c92017-11-29 17:15:40 +02004689 TFieldList *fieldList = new TFieldList();
4690
4691 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304692 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004693 TType *type = new TType(typeSpecifier);
4694 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304695 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004696 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004697 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004698 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004699 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004700
Olli Etuahod5f44c92017-11-29 17:15:40 +02004701 TField *field = new TField(type, declarator->name(), declarator->line());
4702 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4703 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004704 }
4705
Olli Etuahod5f44c92017-11-29 17:15:40 +02004706 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004707}
4708
Martin Radev4a9cd802016-09-01 16:51:51 +03004709TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4710 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004711 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004712 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004713{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004714 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004715 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004716 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004717 structSymbolType = SymbolType::Empty;
4718 }
4719 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004720
Jamie Madill9b820842015-02-12 10:40:10 -05004721 // Store a bool in the struct if we're at global scope, to allow us to
4722 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004723 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004724
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004725 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004726 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004727 checkIsNotReserved(nameLine, structName);
Olli Etuaho437664b2018-02-28 15:38:14 +02004728 if (!symbolTable.declare(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304729 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004730 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004731 }
4732 }
4733
4734 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004735 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004736 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004737 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004738 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004739 switch (qualifier)
4740 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004741 case EvqGlobal:
4742 case EvqTemporary:
4743 break;
4744 default:
4745 error(field.line(), "invalid qualifier on struct member",
4746 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004747 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004748 }
Martin Radev70866b82016-07-22 15:27:42 +03004749 if (field.type()->isInvariant())
4750 {
4751 error(field.line(), "invalid qualifier on struct member", "invariant");
4752 }
jchen104cdac9e2017-05-08 11:01:20 +08004753 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4754 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004755 {
4756 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4757 }
4758
Olli Etuahoebee5b32017-11-23 12:56:32 +02004759 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004760 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004761
Olli Etuaho43364892017-02-13 16:00:12 +00004762 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4763
4764 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004765
4766 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004767 }
4768
Martin Radev4a9cd802016-09-01 16:51:51 +03004769 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004770 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004771 exitStructDeclaration();
4772
Martin Radev4a9cd802016-09-01 16:51:51 +03004773 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004774}
4775
Jamie Madillb98c3a82015-07-23 14:26:04 -04004776TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004777 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004778 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004779{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004780 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004781 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004782 init->isVector())
4783 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004784 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4785 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004786 return nullptr;
4787 }
4788
Olli Etuaho923ecef2017-10-11 12:01:38 +03004789 ASSERT(statementList);
Olli Etuahod05f9642018-03-05 12:13:26 +02004790 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004791 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004792 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004793 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004794 }
4795
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004796 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4797 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004798 return node;
4799}
4800
4801TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4802{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004803 if (mSwitchNestingLevel == 0)
4804 {
4805 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004806 return nullptr;
4807 }
4808 if (condition == nullptr)
4809 {
4810 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004811 return nullptr;
4812 }
4813 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004814 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004815 {
4816 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004817 }
4818 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004819 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4820 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4821 // fold in case labels.
4822 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004823 {
4824 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004825 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004826 TIntermCase *node = new TIntermCase(condition);
4827 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004828 return node;
4829}
4830
4831TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4832{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004833 if (mSwitchNestingLevel == 0)
4834 {
4835 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004836 return nullptr;
4837 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004838 TIntermCase *node = new TIntermCase(nullptr);
4839 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004840 return node;
4841}
4842
Jamie Madillb98c3a82015-07-23 14:26:04 -04004843TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4844 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004845 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004846{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004847 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004848
4849 switch (op)
4850 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004851 case EOpLogicalNot:
4852 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4853 child->isVector())
4854 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004855 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004856 return nullptr;
4857 }
4858 break;
4859 case EOpBitwiseNot:
4860 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4861 child->isMatrix() || child->isArray())
4862 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004863 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004864 return nullptr;
4865 }
4866 break;
4867 case EOpPostIncrement:
4868 case EOpPreIncrement:
4869 case EOpPostDecrement:
4870 case EOpPreDecrement:
4871 case EOpNegative:
4872 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004873 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4874 child->getBasicType() == EbtBool || child->isArray() ||
4875 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004876 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004877 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004878 return nullptr;
4879 }
Nico Weber41b072b2018-02-09 10:01:32 -05004880 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004881 // Operators for built-ins are already type checked against their prototype.
4882 default:
4883 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004884 }
4885
Jiajia Qinbc585152017-06-23 15:42:17 +08004886 if (child->getMemoryQualifier().writeonly)
4887 {
4888 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4889 return nullptr;
4890 }
4891
Olli Etuahof119a262016-08-19 15:54:22 +03004892 TIntermUnary *node = new TIntermUnary(op, child);
4893 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004894
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004895 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004896}
4897
Olli Etuaho09b22472015-02-11 11:47:26 +02004898TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4899{
Olli Etuahocce89652017-06-19 16:04:09 +03004900 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004901 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004902 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004903 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004904 return child;
4905 }
4906 return node;
4907}
4908
Jamie Madillb98c3a82015-07-23 14:26:04 -04004909TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4910 TIntermTyped *child,
4911 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004912{
Olli Etuaho856c4972016-08-08 11:38:39 +03004913 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004914 return addUnaryMath(op, child, loc);
4915}
4916
Olli Etuaho765924f2018-01-04 12:48:36 +02004917TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4918{
4919 // If we can, we should return the folded version of the expression for subsequent parsing. This
4920 // enables folding the containing expression during parsing as well, instead of the separate
4921 // FoldExpressions() step where folding nested expressions requires multiple full AST
4922 // traversals.
4923
4924 // Even if folding fails the fold() functions return some node representing the expression,
4925 // typically the original node. So "folded" can be assumed to be non-null.
4926 TIntermTyped *folded = expression->fold(mDiagnostics);
4927 ASSERT(folded != nullptr);
4928 if (folded->getQualifier() == expression->getQualifier())
4929 {
4930 // We need this expression to have the correct qualifier when validating the consuming
4931 // expression. So we can only return the folded node from here in case it has the same
4932 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4933 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4934 // 1. (true ? 1.0 : non_constant)
4935 // 2. (non_constant, 1.0)
4936 return folded;
4937 }
4938 return expression;
4939}
4940
Jamie Madillb98c3a82015-07-23 14:26:04 -04004941bool TParseContext::binaryOpCommonCheck(TOperator op,
4942 TIntermTyped *left,
4943 TIntermTyped *right,
4944 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004945{
jchen10b4cf5652017-05-05 18:51:17 +08004946 // Check opaque types are not allowed to be operands in expressions other than array indexing
4947 // and structure member selection.
4948 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4949 {
4950 switch (op)
4951 {
4952 case EOpIndexDirect:
4953 case EOpIndexIndirect:
4954 break;
jchen10b4cf5652017-05-05 18:51:17 +08004955
4956 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05004957 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08004958 error(loc, "Invalid operation for variables with an opaque type",
4959 GetOperatorString(op));
4960 return false;
4961 }
4962 }
jchen10cc2a10e2017-05-03 14:05:12 +08004963
Jiajia Qinbc585152017-06-23 15:42:17 +08004964 if (right->getMemoryQualifier().writeonly)
4965 {
4966 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4967 return false;
4968 }
4969
4970 if (left->getMemoryQualifier().writeonly)
4971 {
4972 switch (op)
4973 {
4974 case EOpAssign:
4975 case EOpInitialize:
4976 case EOpIndexDirect:
4977 case EOpIndexIndirect:
4978 case EOpIndexDirectStruct:
4979 case EOpIndexDirectInterfaceBlock:
4980 break;
4981 default:
4982 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4983 return false;
4984 }
4985 }
4986
Olli Etuaho244be012016-08-18 15:26:02 +03004987 if (left->getType().getStruct() || right->getType().getStruct())
4988 {
4989 switch (op)
4990 {
4991 case EOpIndexDirectStruct:
4992 ASSERT(left->getType().getStruct());
4993 break;
4994 case EOpEqual:
4995 case EOpNotEqual:
4996 case EOpAssign:
4997 case EOpInitialize:
4998 if (left->getType() != right->getType())
4999 {
5000 return false;
5001 }
5002 break;
5003 default:
5004 error(loc, "Invalid operation for structs", GetOperatorString(op));
5005 return false;
5006 }
5007 }
5008
Olli Etuaho94050052017-05-08 14:17:44 +03005009 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5010 {
5011 switch (op)
5012 {
5013 case EOpIndexDirectInterfaceBlock:
5014 ASSERT(left->getType().getInterfaceBlock());
5015 break;
5016 default:
5017 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5018 return false;
5019 }
5020 }
5021
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005022 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005023 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005024 error(loc, "array / non-array mismatch", GetOperatorString(op));
5025 return false;
5026 }
5027
5028 if (left->isArray())
5029 {
5030 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005031 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005032 {
5033 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5034 return false;
5035 }
5036
Olli Etuahoe79904c2015-03-18 16:56:42 +02005037 switch (op)
5038 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005039 case EOpEqual:
5040 case EOpNotEqual:
5041 case EOpAssign:
5042 case EOpInitialize:
5043 break;
5044 default:
5045 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5046 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005047 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005048 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005049 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005050 {
5051 error(loc, "array size mismatch", GetOperatorString(op));
5052 return false;
5053 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005054 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005055
5056 // Check ops which require integer / ivec parameters
5057 bool isBitShift = false;
5058 switch (op)
5059 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005060 case EOpBitShiftLeft:
5061 case EOpBitShiftRight:
5062 case EOpBitShiftLeftAssign:
5063 case EOpBitShiftRightAssign:
5064 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5065 // check that the basic type is an integer type.
5066 isBitShift = true;
5067 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5068 {
5069 return false;
5070 }
5071 break;
5072 case EOpBitwiseAnd:
5073 case EOpBitwiseXor:
5074 case EOpBitwiseOr:
5075 case EOpBitwiseAndAssign:
5076 case EOpBitwiseXorAssign:
5077 case EOpBitwiseOrAssign:
5078 // It is enough to check the type of only one operand, since later it
5079 // is checked that the operand types match.
5080 if (!IsInteger(left->getBasicType()))
5081 {
5082 return false;
5083 }
5084 break;
5085 default:
5086 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005087 }
5088
5089 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5090 // So the basic type should usually match.
5091 if (!isBitShift && left->getBasicType() != right->getBasicType())
5092 {
5093 return false;
5094 }
5095
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005096 // Check that:
5097 // 1. Type sizes match exactly on ops that require that.
5098 // 2. Restrictions for structs that contain arrays or samplers are respected.
5099 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005100 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005101 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005102 case EOpAssign:
5103 case EOpInitialize:
5104 case EOpEqual:
5105 case EOpNotEqual:
5106 // ESSL 1.00 sections 5.7, 5.8, 5.9
5107 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5108 {
5109 error(loc, "undefined operation for structs containing arrays",
5110 GetOperatorString(op));
5111 return false;
5112 }
5113 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5114 // we interpret the spec so that this extends to structs containing samplers,
5115 // similarly to ESSL 1.00 spec.
5116 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5117 left->getType().isStructureContainingSamplers())
5118 {
5119 error(loc, "undefined operation for structs containing samplers",
5120 GetOperatorString(op));
5121 return false;
5122 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005123
Olli Etuahoe1805592017-01-02 16:41:20 +00005124 if ((left->getNominalSize() != right->getNominalSize()) ||
5125 (left->getSecondarySize() != right->getSecondarySize()))
5126 {
5127 error(loc, "dimension mismatch", GetOperatorString(op));
5128 return false;
5129 }
5130 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005131 case EOpLessThan:
5132 case EOpGreaterThan:
5133 case EOpLessThanEqual:
5134 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005135 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005136 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005137 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005138 return false;
5139 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005140 break;
5141 case EOpAdd:
5142 case EOpSub:
5143 case EOpDiv:
5144 case EOpIMod:
5145 case EOpBitShiftLeft:
5146 case EOpBitShiftRight:
5147 case EOpBitwiseAnd:
5148 case EOpBitwiseXor:
5149 case EOpBitwiseOr:
5150 case EOpAddAssign:
5151 case EOpSubAssign:
5152 case EOpDivAssign:
5153 case EOpIModAssign:
5154 case EOpBitShiftLeftAssign:
5155 case EOpBitShiftRightAssign:
5156 case EOpBitwiseAndAssign:
5157 case EOpBitwiseXorAssign:
5158 case EOpBitwiseOrAssign:
5159 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5160 {
5161 return false;
5162 }
5163
5164 // Are the sizes compatible?
5165 if (left->getNominalSize() != right->getNominalSize() ||
5166 left->getSecondarySize() != right->getSecondarySize())
5167 {
5168 // If the nominal sizes of operands do not match:
5169 // One of them must be a scalar.
5170 if (!left->isScalar() && !right->isScalar())
5171 return false;
5172
5173 // In the case of compound assignment other than multiply-assign,
5174 // the right side needs to be a scalar. Otherwise a vector/matrix
5175 // would be assigned to a scalar. A scalar can't be shifted by a
5176 // vector either.
5177 if (!right->isScalar() &&
5178 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5179 return false;
5180 }
5181 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005182 default:
5183 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005184 }
5185
Olli Etuahod6b14282015-03-17 14:31:35 +02005186 return true;
5187}
5188
Olli Etuaho1dded802016-08-18 18:13:13 +03005189bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5190 const TType &left,
5191 const TType &right)
5192{
5193 switch (op)
5194 {
5195 case EOpMul:
5196 case EOpMulAssign:
5197 return left.getNominalSize() == right.getNominalSize() &&
5198 left.getSecondarySize() == right.getSecondarySize();
5199 case EOpVectorTimesScalar:
5200 return true;
5201 case EOpVectorTimesScalarAssign:
5202 ASSERT(!left.isMatrix() && !right.isMatrix());
5203 return left.isVector() && !right.isVector();
5204 case EOpVectorTimesMatrix:
5205 return left.getNominalSize() == right.getRows();
5206 case EOpVectorTimesMatrixAssign:
5207 ASSERT(!left.isMatrix() && right.isMatrix());
5208 return left.isVector() && left.getNominalSize() == right.getRows() &&
5209 left.getNominalSize() == right.getCols();
5210 case EOpMatrixTimesVector:
5211 return left.getCols() == right.getNominalSize();
5212 case EOpMatrixTimesScalar:
5213 return true;
5214 case EOpMatrixTimesScalarAssign:
5215 ASSERT(left.isMatrix() && !right.isMatrix());
5216 return !right.isVector();
5217 case EOpMatrixTimesMatrix:
5218 return left.getCols() == right.getRows();
5219 case EOpMatrixTimesMatrixAssign:
5220 ASSERT(left.isMatrix() && right.isMatrix());
5221 // We need to check two things:
5222 // 1. The matrix multiplication step is valid.
5223 // 2. The result will have the same number of columns as the lvalue.
5224 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5225
5226 default:
5227 UNREACHABLE();
5228 return false;
5229 }
5230}
5231
Jamie Madillb98c3a82015-07-23 14:26:04 -04005232TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5233 TIntermTyped *left,
5234 TIntermTyped *right,
5235 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005236{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005237 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005238 return nullptr;
5239
Olli Etuahofc1806e2015-03-17 13:03:11 +02005240 switch (op)
5241 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005242 case EOpEqual:
5243 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005244 case EOpLessThan:
5245 case EOpGreaterThan:
5246 case EOpLessThanEqual:
5247 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005248 break;
5249 case EOpLogicalOr:
5250 case EOpLogicalXor:
5251 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005252 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5253 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005254 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005255 {
5256 return nullptr;
5257 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005258 // Basic types matching should have been already checked.
5259 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005260 break;
5261 case EOpAdd:
5262 case EOpSub:
5263 case EOpDiv:
5264 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005265 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5266 !right->getType().getStruct());
5267 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005268 {
5269 return nullptr;
5270 }
5271 break;
5272 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005273 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5274 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005275 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005276 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005277 {
5278 return nullptr;
5279 }
5280 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005281 default:
5282 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005283 }
5284
Olli Etuaho1dded802016-08-18 18:13:13 +03005285 if (op == EOpMul)
5286 {
5287 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5288 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5289 {
5290 return nullptr;
5291 }
5292 }
5293
Olli Etuaho3fdec912016-08-18 15:08:06 +03005294 TIntermBinary *node = new TIntermBinary(op, left, right);
5295 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005296 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005297}
5298
Jamie Madillb98c3a82015-07-23 14:26:04 -04005299TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5300 TIntermTyped *left,
5301 TIntermTyped *right,
5302 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005303{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005304 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005305 if (node == 0)
5306 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005307 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5308 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005309 return left;
5310 }
5311 return node;
5312}
5313
Jamie Madillb98c3a82015-07-23 14:26:04 -04005314TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5315 TIntermTyped *left,
5316 TIntermTyped *right,
5317 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005318{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005319 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005320 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005321 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005322 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5323 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005324 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005325 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005326 }
5327 return node;
5328}
5329
Olli Etuaho13389b62016-10-16 11:48:18 +01005330TIntermBinary *TParseContext::createAssign(TOperator op,
5331 TIntermTyped *left,
5332 TIntermTyped *right,
5333 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005334{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005335 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005336 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005337 if (op == EOpMulAssign)
5338 {
5339 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5340 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5341 {
5342 return nullptr;
5343 }
5344 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005345 TIntermBinary *node = new TIntermBinary(op, left, right);
5346 node->setLine(loc);
5347
Olli Etuaho3fdec912016-08-18 15:08:06 +03005348 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005349 }
5350 return nullptr;
5351}
5352
Jamie Madillb98c3a82015-07-23 14:26:04 -04005353TIntermTyped *TParseContext::addAssign(TOperator op,
5354 TIntermTyped *left,
5355 TIntermTyped *right,
5356 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005357{
Olli Etuahocce89652017-06-19 16:04:09 +03005358 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005359 TIntermTyped *node = createAssign(op, left, right, loc);
5360 if (node == nullptr)
5361 {
5362 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005363 return left;
5364 }
5365 return node;
5366}
5367
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005368TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5369 TIntermTyped *right,
5370 const TSourceLoc &loc)
5371{
Corentin Wallez0d959252016-07-12 17:26:32 -04005372 // WebGL2 section 5.26, the following results in an error:
5373 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005374 if (mShaderSpec == SH_WEBGL2_SPEC &&
5375 (left->isArray() || left->getBasicType() == EbtVoid ||
5376 left->getType().isStructureContainingArrays() || right->isArray() ||
5377 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005378 {
5379 error(loc,
5380 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5381 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005382 }
5383
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005384 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho765924f2018-01-04 12:48:36 +02005385
5386 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005387}
5388
Olli Etuaho49300862015-02-20 14:54:49 +02005389TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5390{
5391 switch (op)
5392 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005393 case EOpContinue:
5394 if (mLoopNestingLevel <= 0)
5395 {
5396 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005397 }
5398 break;
5399 case EOpBreak:
5400 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5401 {
5402 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005403 }
5404 break;
5405 case EOpReturn:
5406 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5407 {
5408 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005409 }
5410 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005411 case EOpKill:
5412 if (mShaderType != GL_FRAGMENT_SHADER)
5413 {
5414 error(loc, "discard supported in fragment shaders only", "discard");
5415 }
5416 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005417 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005418 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005419 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005420 }
Olli Etuahocce89652017-06-19 16:04:09 +03005421 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005422}
5423
Jamie Madillb98c3a82015-07-23 14:26:04 -04005424TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005425 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005426 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005427{
Olli Etuahocce89652017-06-19 16:04:09 +03005428 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005429 {
Olli Etuahocce89652017-06-19 16:04:09 +03005430 ASSERT(op == EOpReturn);
5431 mFunctionReturnsValue = true;
5432 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5433 {
5434 error(loc, "void function cannot return a value", "return");
5435 }
5436 else if (*mCurrentFunctionType != expression->getType())
5437 {
5438 error(loc, "function return is not matching type:", "return");
5439 }
Olli Etuaho49300862015-02-20 14:54:49 +02005440 }
Olli Etuahocce89652017-06-19 16:04:09 +03005441 TIntermBranch *node = new TIntermBranch(op, expression);
5442 node->setLine(loc);
5443 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005444}
5445
Martin Radev84aa2dc2017-09-11 15:51:02 +03005446void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5447{
5448 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005449 const TFunction *func = functionCall->getFunction();
5450 if (BuiltInGroup::isTextureGather(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005451 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005452 bool isTextureGatherOffset = BuiltInGroup::isTextureGatherOffset(func);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005453 TIntermNode *componentNode = nullptr;
5454 TIntermSequence *arguments = functionCall->getSequence();
5455 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5456 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5457 ASSERT(sampler != nullptr);
5458 switch (sampler->getBasicType())
5459 {
5460 case EbtSampler2D:
5461 case EbtISampler2D:
5462 case EbtUSampler2D:
5463 case EbtSampler2DArray:
5464 case EbtISampler2DArray:
5465 case EbtUSampler2DArray:
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005466 if ((!isTextureGatherOffset && arguments->size() == 3u) ||
Martin Radev84aa2dc2017-09-11 15:51:02 +03005467 (isTextureGatherOffset && arguments->size() == 4u))
5468 {
5469 componentNode = arguments->back();
5470 }
5471 break;
5472 case EbtSamplerCube:
5473 case EbtISamplerCube:
5474 case EbtUSamplerCube:
5475 ASSERT(!isTextureGatherOffset);
5476 if (arguments->size() == 3u)
5477 {
5478 componentNode = arguments->back();
5479 }
5480 break;
5481 case EbtSampler2DShadow:
5482 case EbtSampler2DArrayShadow:
5483 case EbtSamplerCubeShadow:
5484 break;
5485 default:
5486 UNREACHABLE();
5487 break;
5488 }
5489 if (componentNode)
5490 {
5491 const TIntermConstantUnion *componentConstantUnion =
5492 componentNode->getAsConstantUnion();
5493 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5494 {
5495 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005496 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005497 }
5498 else
5499 {
5500 int component = componentConstantUnion->getIConst(0);
5501 if (component < 0 || component > 3)
5502 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005503 error(functionCall->getLine(), "Component must be in the range [0;3]",
5504 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005505 }
5506 }
5507 }
5508 }
5509}
5510
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005511void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5512{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005513 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005514 const TFunction *func = functionCall->getFunction();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005515 TIntermNode *offset = nullptr;
5516 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005517 bool useTextureGatherOffsetConstraints = false;
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005518 if (BuiltInGroup::isTextureOffsetNoBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005519 {
5520 offset = arguments->back();
5521 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005522 else if (BuiltInGroup::isTextureOffsetBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005523 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005524 // A bias parameter follows the offset parameter.
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005525 ASSERT(arguments->size() >= 3);
5526 offset = (*arguments)[2];
5527 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005528 else if (BuiltInGroup::isTextureGatherOffset(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005529 {
5530 ASSERT(arguments->size() >= 3u);
5531 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5532 ASSERT(sampler != nullptr);
5533 switch (sampler->getBasicType())
5534 {
5535 case EbtSampler2D:
5536 case EbtISampler2D:
5537 case EbtUSampler2D:
5538 case EbtSampler2DArray:
5539 case EbtISampler2DArray:
5540 case EbtUSampler2DArray:
5541 offset = (*arguments)[2];
5542 break;
5543 case EbtSampler2DShadow:
5544 case EbtSampler2DArrayShadow:
5545 offset = (*arguments)[3];
5546 break;
5547 default:
5548 UNREACHABLE();
5549 break;
5550 }
5551 useTextureGatherOffsetConstraints = true;
5552 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005553 if (offset != nullptr)
5554 {
5555 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5556 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5557 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005558 error(functionCall->getLine(), "Texture offset must be a constant expression",
5559 func->name());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005560 }
5561 else
5562 {
5563 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5564 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005565 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005566 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5567 : mMinProgramTexelOffset;
5568 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5569 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005570 for (size_t i = 0u; i < size; ++i)
5571 {
5572 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005573 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005574 {
5575 std::stringstream tokenStream;
5576 tokenStream << offsetValue;
5577 std::string token = tokenStream.str();
5578 error(offset->getLine(), "Texture offset value out of valid range",
5579 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005580 }
5581 }
5582 }
5583 }
5584}
5585
Jiajia Qina3106c52017-11-03 09:39:39 +08005586void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5587{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005588 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005589 const TFunction *func = functionCall->getFunction();
5590 if (BuiltInGroup::isAtomicMemory(func))
Jiajia Qina3106c52017-11-03 09:39:39 +08005591 {
5592 TIntermSequence *arguments = functionCall->getSequence();
5593 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5594
5595 if (IsBufferOrSharedVariable(memNode))
5596 {
5597 return;
5598 }
5599
5600 while (memNode->getAsBinaryNode())
5601 {
5602 memNode = memNode->getAsBinaryNode()->getLeft();
5603 if (IsBufferOrSharedVariable(memNode))
5604 {
5605 return;
5606 }
5607 }
5608
5609 error(memNode->getLine(),
5610 "The value passed to the mem argument of an atomic memory function does not "
5611 "correspond to a buffer or shared variable.",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005612 func->name());
Jiajia Qina3106c52017-11-03 09:39:39 +08005613 }
5614}
5615
Martin Radev2cc85b32016-08-05 16:22:53 +03005616// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5617void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5618{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005619 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005620
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005621 const TFunction *func = functionCall->getFunction();
5622
5623 if (BuiltInGroup::isImage(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005624 {
5625 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005626 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005627
Olli Etuaho485eefd2017-02-14 17:40:06 +00005628 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005629
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005630 if (BuiltInGroup::isImageStore(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005631 {
5632 if (memoryQualifier.readonly)
5633 {
5634 error(imageNode->getLine(),
5635 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005636 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005637 }
5638 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005639 else if (BuiltInGroup::isImageLoad(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005640 {
5641 if (memoryQualifier.writeonly)
5642 {
5643 error(imageNode->getLine(),
5644 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005645 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005646 }
5647 }
5648 }
5649}
5650
5651// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5652void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5653 const TFunction *functionDefinition,
5654 const TIntermAggregate *functionCall)
5655{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005656 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005657
5658 const TIntermSequence &arguments = *functionCall->getSequence();
5659
5660 ASSERT(functionDefinition->getParamCount() == arguments.size());
5661
5662 for (size_t i = 0; i < arguments.size(); ++i)
5663 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005664 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5665 const TType &functionArgumentType = typedArgument->getType();
Olli Etuahod4bd9632018-03-08 16:32:44 +02005666 const TType &functionParameterType = functionDefinition->getParam(i)->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005667 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5668
5669 if (IsImage(functionArgumentType.getBasicType()))
5670 {
5671 const TMemoryQualifier &functionArgumentMemoryQualifier =
5672 functionArgumentType.getMemoryQualifier();
5673 const TMemoryQualifier &functionParameterMemoryQualifier =
5674 functionParameterType.getMemoryQualifier();
5675 if (functionArgumentMemoryQualifier.readonly &&
5676 !functionParameterMemoryQualifier.readonly)
5677 {
5678 error(functionCall->getLine(),
5679 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005680 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005681 }
5682
5683 if (functionArgumentMemoryQualifier.writeonly &&
5684 !functionParameterMemoryQualifier.writeonly)
5685 {
5686 error(functionCall->getLine(),
5687 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005688 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005689 }
Martin Radev049edfa2016-11-11 14:35:37 +02005690
5691 if (functionArgumentMemoryQualifier.coherent &&
5692 !functionParameterMemoryQualifier.coherent)
5693 {
5694 error(functionCall->getLine(),
5695 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005696 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005697 }
5698
5699 if (functionArgumentMemoryQualifier.volatileQualifier &&
5700 !functionParameterMemoryQualifier.volatileQualifier)
5701 {
5702 error(functionCall->getLine(),
5703 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005704 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005705 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005706 }
5707 }
5708}
5709
Olli Etuaho95ed1942018-02-01 14:01:19 +02005710TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005711{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005712 if (fnCall->thisNode() != nullptr)
5713 {
5714 return addMethod(fnCall, loc);
5715 }
5716 if (fnCall->isConstructor())
5717 {
5718 return addConstructor(fnCall, loc);
5719 }
5720 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005721}
5722
Olli Etuaho95ed1942018-02-01 14:01:19 +02005723TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005724{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005725 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005726 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5727 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5728 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005729 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005730 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005731 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005732 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005733 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005734 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005735 {
5736 error(loc, "method takes no parameters", "length");
5737 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005738 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005739 {
5740 error(loc, "length can only be called on arrays", "length");
5741 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005742 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005743 mGeometryShaderInputPrimitiveType == EptUndefined)
5744 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005745 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005746 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5747 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005748 else
5749 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005750 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005751 node->setLine(loc);
5752 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005753 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005754 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005755}
5756
Olli Etuaho95ed1942018-02-01 14:01:19 +02005757TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005758 const TSourceLoc &loc)
5759{
Olli Etuaho697bf652018-02-16 11:50:54 +02005760 // First check whether the function has been hidden by a variable name or struct typename by
5761 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5762 // with a matching argument list.
5763 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005764 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005765 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005766 }
5767 else
5768 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005769 // There are no inner functions, so it's enough to look for user-defined functions in the
5770 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005771 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005772 if (symbol != nullptr)
5773 {
5774 // A user-defined function - could be an overloaded built-in as well.
5775 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5776 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5777 TIntermAggregate *callNode =
5778 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5779 callNode->setLine(loc);
5780 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5781 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5782 return callNode;
5783 }
5784
5785 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005786 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005787 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005788 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005789 }
5790 else
5791 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005792 // A built-in function.
5793 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005794 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005795
Olli Etuaho37b697e2018-01-29 12:19:27 +02005796 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005797 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005798 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005799 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005800 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005801 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005802 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005803 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005804 if (fnCandidate->getParamCount() == 1)
5805 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005806 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005807 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005808 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005809 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005810 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005811 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005812 TIntermAggregate *callNode =
5813 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005814 callNode->setLine(loc);
5815
Olli Etuahoe80825e2018-02-16 10:24:53 +02005816 // Some built-in functions have out parameters too.
5817 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5818
5819 // See if we can constant fold a built-in. Note that this may be possible
5820 // even if it is not const-qualified.
5821 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005822 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005823
5824 // This is a built-in function with no op associated with it.
5825 TIntermAggregate *callNode =
5826 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5827 callNode->setLine(loc);
5828 checkTextureOffsetConst(callNode);
5829 checkTextureGather(callNode);
5830 checkImageMemoryAccessForBuiltinFunctions(callNode);
5831 checkAtomicMemoryBuiltinFunctions(callNode);
5832 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5833 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005834 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005835 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005836
5837 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005838 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005839}
5840
Jamie Madillb98c3a82015-07-23 14:26:04 -04005841TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005842 TIntermTyped *trueExpression,
5843 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005844 const TSourceLoc &loc)
5845{
Olli Etuaho56229f12017-07-10 14:16:33 +03005846 if (!checkIsScalarBool(loc, cond))
5847 {
5848 return falseExpression;
5849 }
Olli Etuaho52901742015-04-15 13:42:45 +03005850
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005851 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005852 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005853 std::stringstream reasonStream;
5854 reasonStream << "mismatching ternary operator operand types '"
5855 << trueExpression->getCompleteString() << " and '"
5856 << falseExpression->getCompleteString() << "'";
5857 std::string reason = reasonStream.str();
5858 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005859 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005860 }
Olli Etuahode318b22016-10-25 16:18:25 +01005861 if (IsOpaqueType(trueExpression->getBasicType()))
5862 {
5863 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005864 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005865 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5866 // Note that structs containing opaque types don't need to be checked as structs are
5867 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005868 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005869 return falseExpression;
5870 }
5871
Jiajia Qinbc585152017-06-23 15:42:17 +08005872 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5873 falseExpression->getMemoryQualifier().writeonly)
5874 {
5875 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5876 return falseExpression;
5877 }
5878
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005879 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005880 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005881 // ESSL 3.00.6 section 5.7:
5882 // Ternary operator support is optional for arrays. No certainty that it works across all
5883 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5884 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005885 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005886 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005887 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005888 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005889 }
Olli Etuaho94050052017-05-08 14:17:44 +03005890 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5891 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005892 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005893 return falseExpression;
5894 }
5895
Corentin Wallez0d959252016-07-12 17:26:32 -04005896 // WebGL2 section 5.26, the following results in an error:
5897 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005898 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005899 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005900 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005901 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005902 }
5903
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005904 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5905 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005906 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005907}
Olli Etuaho49300862015-02-20 14:54:49 +02005908
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005909//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005910// Parse an array of strings using yyparse.
5911//
5912// Returns 0 for success.
5913//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005914int PaParseStrings(size_t count,
5915 const char *const string[],
5916 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305917 TParseContext *context)
5918{
Yunchao He4f285442017-04-21 12:15:49 +08005919 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005920 return 1;
5921
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005922 if (glslang_initialize(context))
5923 return 1;
5924
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005925 int error = glslang_scan(count, string, length, context);
5926 if (!error)
5927 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005928
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005929 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005930
alokp@chromium.org6b495712012-06-29 00:06:58 +00005931 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005932}
Jamie Madill45bcc782016-11-07 13:58:48 -05005933
5934} // namespace sh