blob: d91257682064a749031c2b8affac6b8f37b2132d [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 Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.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 Etuaho37ad4742015-04-27 13:18:50 +030020#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021
Jamie Madill45bcc782016-11-07 13:58:48 -050022namespace sh
23{
24
alokp@chromium.org8b851c62012-06-15 16:25:11 +000025///////////////////////////////////////////////////////////////////////
26//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027// Sub- vector and matrix fields
28//
29////////////////////////////////////////////////////////////////////////
30
Martin Radev2cc85b32016-08-05 16:22:53 +030031namespace
32{
33
34const int kWebGLMaxStructNesting = 4;
35
Olli Etuahofbb1c792018-01-19 16:26:59 +020036constexpr const ImmutableString kTexelFetchOffsetName("texelFetchOffsetName");
37constexpr const ImmutableString kTextureLodOffsetName("textureLodOffset");
38constexpr const ImmutableString kTextureProjLodOffsetName("textureProjLodOffset");
39constexpr const ImmutableString kTextureGradOffsetName("textureGradOffset");
40constexpr const ImmutableString kTextureProjGradOffsetName("textureProjGradOffset");
41constexpr const ImmutableString kTextureOffsetName("textureOffset");
42constexpr const ImmutableString kTextureProjOffsetName("textureProjOffset");
43constexpr const ImmutableString kTextureGatherName("textureGather");
44constexpr const ImmutableString kTextureGatherOffsetName("textureGatherOffset");
Jiajia Qina3106c52017-11-03 09:39:39 +080045
Olli Etuahofbb1c792018-01-19 16:26:59 +020046constexpr const std::array<ImmutableString, 8> kAtomicBuiltin = {
47 {ImmutableString("atomicAdd"), ImmutableString("atomicMin"), ImmutableString("atomicMax"),
48 ImmutableString("atomicAnd"), ImmutableString("atomicOr"), ImmutableString("atomicXor"),
49 ImmutableString("atomicExchange"), ImmutableString("atomicCompSwap")}};
50
51bool IsAtomicBuiltin(const ImmutableString &name)
Jiajia Qina3106c52017-11-03 09:39:39 +080052{
53 for (size_t i = 0; i < kAtomicBuiltin.size(); ++i)
54 {
Olli Etuahofbb1c792018-01-19 16:26:59 +020055 if (name == kAtomicBuiltin[i])
Jiajia Qina3106c52017-11-03 09:39:39 +080056 {
57 return true;
58 }
59 }
60 return false;
61}
62
Olli Etuaho0f684632017-07-13 12:42:15 +030063bool ContainsSampler(const TStructure *structType);
64
Martin Radev2cc85b32016-08-05 16:22:53 +030065bool ContainsSampler(const TType &type)
66{
67 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030068 {
Martin Radev2cc85b32016-08-05 16:22:53 +030069 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030070 }
jchen10cc2a10e2017-05-03 14:05:12 +080071 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030072 {
Olli Etuaho0f684632017-07-13 12:42:15 +030073 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030074 }
75
76 return false;
77}
78
Olli Etuaho0f684632017-07-13 12:42:15 +030079bool ContainsSampler(const TStructure *structType)
80{
81 for (const auto &field : structType->fields())
82 {
83 if (ContainsSampler(*field->type()))
84 return true;
85 }
86 return false;
87}
88
Olli Etuaho485eefd2017-02-14 17:40:06 +000089// Get a token from an image argument to use as an error message token.
90const char *GetImageArgumentToken(TIntermTyped *imageNode)
91{
92 ASSERT(IsImage(imageNode->getBasicType()));
93 while (imageNode->getAsBinaryNode() &&
94 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
95 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
96 {
97 imageNode = imageNode->getAsBinaryNode()->getLeft();
98 }
99 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
100 if (imageSymbol)
101 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200102 return imageSymbol->getName().data();
Olli Etuaho485eefd2017-02-14 17:40:06 +0000103 }
104 return "image";
105}
106
Olli Etuahocce89652017-06-19 16:04:09 +0300107bool CanSetDefaultPrecisionOnType(const TPublicType &type)
108{
109 if (!SupportsPrecision(type.getBasicType()))
110 {
111 return false;
112 }
113 if (type.getBasicType() == EbtUInt)
114 {
115 // ESSL 3.00.4 section 4.5.4
116 return false;
117 }
118 if (type.isAggregate())
119 {
120 // Not allowed to set for aggregate types
121 return false;
122 }
123 return true;
124}
125
Jiawei Shaod8105a02017-08-08 09:54:36 +0800126// Map input primitive types to input array sizes in a geometry shader.
127GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
128{
129 switch (primitiveType)
130 {
131 case EptPoints:
132 return 1u;
133 case EptLines:
134 return 2u;
135 case EptTriangles:
136 return 3u;
137 case EptLinesAdjacency:
138 return 4u;
139 case EptTrianglesAdjacency:
140 return 6u;
141 default:
142 UNREACHABLE();
143 return 0u;
144 }
145}
146
Jiajia Qina3106c52017-11-03 09:39:39 +0800147bool IsBufferOrSharedVariable(TIntermTyped *var)
148{
149 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
150 var->getQualifier() == EvqShared)
151 {
152 return true;
153 }
154 return false;
155}
156
Martin Radev2cc85b32016-08-05 16:22:53 +0300157} // namespace
158
jchen104cdac9e2017-05-08 11:01:20 +0800159// This tracks each binding point's current default offset for inheritance of subsequent
160// variables using the same binding, and keeps offsets unique and non overlapping.
161// See GLSL ES 3.1, section 4.4.6.
162class TParseContext::AtomicCounterBindingState
163{
164 public:
165 AtomicCounterBindingState() : mDefaultOffset(0) {}
166 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
167 // newly inserted span.
168 int insertSpan(int start, size_t length)
169 {
170 gl::RangeI newSpan(start, start + static_cast<int>(length));
171 for (const auto &span : mSpans)
172 {
173 if (newSpan.intersects(span))
174 {
175 return -1;
176 }
177 }
178 mSpans.push_back(newSpan);
179 mDefaultOffset = newSpan.high();
180 return start;
181 }
182 // Inserts a new span starting from the default offset.
183 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
184 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
185
186 private:
187 int mDefaultOffset;
188 std::vector<gl::RangeI> mSpans;
189};
190
Jamie Madillacb4b812016-11-07 13:50:29 -0500191TParseContext::TParseContext(TSymbolTable &symt,
192 TExtensionBehavior &ext,
193 sh::GLenum type,
194 ShShaderSpec spec,
195 ShCompileOptions options,
196 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000197 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500198 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300199 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300200 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500201 mShaderType(type),
202 mShaderSpec(spec),
203 mCompileOptions(options),
204 mShaderVersion(100),
205 mTreeRoot(nullptr),
206 mLoopNestingLevel(0),
207 mStructNestingLevel(0),
208 mSwitchNestingLevel(0),
209 mCurrentFunctionType(nullptr),
210 mFunctionReturnsValue(false),
211 mChecksPrecisionErrors(checksPrecErrors),
212 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800213 mDefaultUniformMatrixPacking(EmpColumnMajor),
214 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
215 mDefaultBufferMatrixPacking(EmpColumnMajor),
216 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000217 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500218 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000219 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500220 mShaderVersion,
221 mShaderType,
222 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000223 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500224 mScanner(nullptr),
225 mUsesFragData(false),
226 mUsesFragColor(false),
227 mUsesSecondaryOutputs(false),
228 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
229 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300230 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
231 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500232 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500233 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000234 mNumViews(-1),
235 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000236 mMaxImageUnits(resources.MaxImageUnits),
237 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000238 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800239 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800240 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800241 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800242 mDeclaringFunction(false),
243 mGeometryShaderInputPrimitiveType(EptUndefined),
244 mGeometryShaderOutputPrimitiveType(EptUndefined),
245 mGeometryShaderInvocations(0),
246 mGeometryShaderMaxVertices(-1),
247 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800248 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Olli Etuahoc74ec1a2018-01-09 15:23:28 +0200249 mGlInVariableWithArraySize(nullptr)
Jamie Madillacb4b812016-11-07 13:50:29 -0500250{
Jamie Madillacb4b812016-11-07 13:50:29 -0500251}
252
jchen104cdac9e2017-05-08 11:01:20 +0800253TParseContext::~TParseContext()
254{
255}
256
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300257bool TParseContext::parseVectorFields(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200258 const ImmutableString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300260 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300262 ASSERT(fieldOffsets);
Olli Etuahofbb1c792018-01-19 16:26:59 +0200263 size_t fieldCount = compString.length();
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300264 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530265 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200266 error(line, "illegal vector field selection", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000267 return false;
268 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300269 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270
Jamie Madillb98c3a82015-07-23 14:26:04 -0400271 enum
272 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000273 exyzw,
274 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000275 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000276 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300278 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530279 {
280 switch (compString[i])
281 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400282 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300283 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400284 fieldSet[i] = exyzw;
285 break;
286 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300287 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400288 fieldSet[i] = ergba;
289 break;
290 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300291 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400292 fieldSet[i] = estpq;
293 break;
294 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300295 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400296 fieldSet[i] = exyzw;
297 break;
298 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300299 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400300 fieldSet[i] = ergba;
301 break;
302 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300303 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400304 fieldSet[i] = estpq;
305 break;
306 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300307 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400308 fieldSet[i] = exyzw;
309 break;
310 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300311 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400312 fieldSet[i] = ergba;
313 break;
314 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300315 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400316 fieldSet[i] = estpq;
317 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530318
Jamie Madillb98c3a82015-07-23 14:26:04 -0400319 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300320 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400321 fieldSet[i] = exyzw;
322 break;
323 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300324 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400325 fieldSet[i] = ergba;
326 break;
327 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300328 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400329 fieldSet[i] = estpq;
330 break;
331 default:
Olli Etuahofbb1c792018-01-19 16:26:59 +0200332 error(line, "illegal vector field selection", compString);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400333 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000334 }
335 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300337 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530338 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300339 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530340 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200341 error(line, "vector field selection out of range", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000342 return false;
343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
Arun Patole7e7e68d2015-05-22 12:02:25 +0530345 if (i > 0)
346 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400347 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530348 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200349 error(line, "illegal - vector component fields not from the same set", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000350 return false;
351 }
352 }
353 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000355 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356}
357
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358///////////////////////////////////////////////////////////////////////
359//
360// Errors
361//
362////////////////////////////////////////////////////////////////////////
363
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364//
365// Used by flex/bison to output all syntax and parsing errors.
366//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000367void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000369 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370}
371
Olli Etuahofbb1c792018-01-19 16:26:59 +0200372void TParseContext::error(const TSourceLoc &loc, const char *reason, const ImmutableString &token)
373{
374 mDiagnostics->error(loc, reason, token.data());
375}
376
Olli Etuaho4de340a2016-12-16 09:32:03 +0000377void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530378{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000379 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000380}
381
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200382void TParseContext::outOfRangeError(bool isError,
383 const TSourceLoc &loc,
384 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000385 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200386{
387 if (isError)
388 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000389 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200390 }
391 else
392 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000393 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200394 }
395}
396
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397//
398// Same error message for all places assignments don't work.
399//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530400void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000402 std::stringstream reasonStream;
403 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
404 std::string reason = reasonStream.str();
405 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406}
407
408//
409// Same error message for all places unary operations don't work.
410//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530411void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000413 std::stringstream reasonStream;
414 reasonStream << "wrong operand type - no operation '" << op
415 << "' exists that takes an operand of type " << operand
416 << " (or there is no acceptable conversion)";
417 std::string reason = reasonStream.str();
418 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419}
420
421//
422// Same error message for all binary operations don't work.
423//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400424void TParseContext::binaryOpError(const TSourceLoc &line,
425 const char *op,
426 TString left,
427 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000429 std::stringstream reasonStream;
430 reasonStream << "wrong operand types - no operation '" << op
431 << "' exists that takes a left-hand operand of type '" << left
432 << "' and a right operand of type '" << right
433 << "' (or there is no acceptable conversion)";
434 std::string reason = reasonStream.str();
435 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436}
437
Olli Etuaho856c4972016-08-08 11:38:39 +0300438void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
439 TPrecision precision,
440 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530441{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400442 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300443 return;
Martin Radev70866b82016-07-22 15:27:42 +0300444
445 if (precision != EbpUndefined && !SupportsPrecision(type))
446 {
447 error(line, "illegal type for precision qualifier", getBasicString(type));
448 }
449
Olli Etuaho183d7e22015-11-20 15:59:09 +0200450 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530451 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200452 switch (type)
453 {
454 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400455 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300456 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200457 case EbtInt:
458 case EbtUInt:
459 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400460 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300461 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200462 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800463 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200464 {
jchen10cc2a10e2017-05-03 14:05:12 +0800465 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300466 return;
467 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200468 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000469 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000470}
471
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000472// Both test and if necessary, spit out an error, to see if the node is really
473// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300474bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500476 TIntermSymbol *symNode = node->getAsSymbolNode();
477 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100478 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
479
480 if (swizzleNode)
481 {
482 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
483 if (ok && swizzleNode->hasDuplicateOffsets())
484 {
485 error(line, " l-value of swizzle cannot have duplicate components", op);
486 return false;
487 }
488 return ok;
489 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490
Arun Patole7e7e68d2015-05-22 12:02:25 +0530491 if (binaryNode)
492 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400493 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530494 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400495 case EOpIndexDirect:
496 case EOpIndexIndirect:
497 case EOpIndexDirectStruct:
498 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300499 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400500 default:
501 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000502 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000503 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300504 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000505 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506
jchen10cc2a10e2017-05-03 14:05:12 +0800507 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530508 switch (node->getQualifier())
509 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400510 case EvqConst:
511 message = "can't modify a const";
512 break;
513 case EvqConstReadOnly:
514 message = "can't modify a const";
515 break;
516 case EvqAttribute:
517 message = "can't modify an attribute";
518 break;
519 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400520 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800521 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800522 case EvqFlatIn:
523 case EvqSmoothIn:
524 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400525 message = "can't modify an input";
526 break;
527 case EvqUniform:
528 message = "can't modify a uniform";
529 break;
530 case EvqVaryingIn:
531 message = "can't modify a varying";
532 break;
533 case EvqFragCoord:
534 message = "can't modify gl_FragCoord";
535 break;
536 case EvqFrontFacing:
537 message = "can't modify gl_FrontFacing";
538 break;
539 case EvqPointCoord:
540 message = "can't modify gl_PointCoord";
541 break;
Martin Radevb0883602016-08-04 17:48:58 +0300542 case EvqNumWorkGroups:
543 message = "can't modify gl_NumWorkGroups";
544 break;
545 case EvqWorkGroupSize:
546 message = "can't modify gl_WorkGroupSize";
547 break;
548 case EvqWorkGroupID:
549 message = "can't modify gl_WorkGroupID";
550 break;
551 case EvqLocalInvocationID:
552 message = "can't modify gl_LocalInvocationID";
553 break;
554 case EvqGlobalInvocationID:
555 message = "can't modify gl_GlobalInvocationID";
556 break;
557 case EvqLocalInvocationIndex:
558 message = "can't modify gl_LocalInvocationIndex";
559 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300560 case EvqViewIDOVR:
561 message = "can't modify gl_ViewID_OVR";
562 break;
Martin Radev802abe02016-08-04 17:48:32 +0300563 case EvqComputeIn:
564 message = "can't modify work group size variable";
565 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800566 case EvqPerVertexIn:
567 message = "can't modify any member in gl_in";
568 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800569 case EvqPrimitiveIDIn:
570 message = "can't modify gl_PrimitiveIDIn";
571 break;
572 case EvqInvocationID:
573 message = "can't modify gl_InvocationID";
574 break;
575 case EvqPrimitiveID:
576 if (mShaderType == GL_FRAGMENT_SHADER)
577 {
578 message = "can't modify gl_PrimitiveID in a fragment shader";
579 }
580 break;
581 case EvqLayer:
582 if (mShaderType == GL_FRAGMENT_SHADER)
583 {
584 message = "can't modify gl_Layer in a fragment shader";
585 }
586 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400587 default:
588 //
589 // Type that can't be written to?
590 //
591 if (node->getBasicType() == EbtVoid)
592 {
593 message = "can't modify void";
594 }
jchen10cc2a10e2017-05-03 14:05:12 +0800595 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400596 {
jchen10cc2a10e2017-05-03 14:05:12 +0800597 message = "can't modify a variable with type ";
598 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300599 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800600 else if (node->getMemoryQualifier().readonly)
601 {
602 message = "can't modify a readonly variable";
603 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605
jchen10cc2a10e2017-05-03 14:05:12 +0800606 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530607 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000608 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609
Olli Etuaho8a176262016-08-16 14:23:01 +0300610 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000611 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000613 //
614 // Everything else is okay, no error.
615 //
jchen10cc2a10e2017-05-03 14:05:12 +0800616 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300617 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000618
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000619 //
620 // If we get here, we have an error and a message.
621 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530622 if (symNode)
623 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200624 // Symbol inside an expression can't be nameless.
625 ASSERT(symNode->variable().symbolType() != SymbolType::Empty);
626
Olli Etuahofbb1c792018-01-19 16:26:59 +0200627 const ImmutableString &symbol = symNode->getName();
Olli Etuaho4de340a2016-12-16 09:32:03 +0000628 std::stringstream reasonStream;
629 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
630 std::string reason = reasonStream.str();
631 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000632 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530633 else
634 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000635 std::stringstream reasonStream;
636 reasonStream << "l-value required (" << message << ")";
637 std::string reason = reasonStream.str();
638 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000639 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000640
Olli Etuaho8a176262016-08-16 14:23:01 +0300641 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642}
643
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644// Both test, and if necessary spit out an error, to see if the node is really
645// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300646void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647{
Olli Etuaho383b7912016-08-05 11:22:59 +0300648 if (node->getQualifier() != EvqConst)
649 {
650 error(node->getLine(), "constant expression required", "");
651 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652}
653
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654// Both test, and if necessary spit out an error, to see if the node is really
655// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300656void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657{
Olli Etuaho383b7912016-08-05 11:22:59 +0300658 if (!node->isScalarInt())
659 {
660 error(node->getLine(), "integer expression required", token);
661 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662}
663
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664// Both test, and if necessary spit out an error, to see if we are currently
665// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800666bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667{
Olli Etuaho856c4972016-08-08 11:38:39 +0300668 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300669 {
670 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800671 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300672 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800673 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674}
675
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300676// ESSL 3.00.5 sections 3.8 and 3.9.
677// If it starts "gl_" or contains two consecutive underscores, it's reserved.
678// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuahofbb1c792018-01-19 16:26:59 +0200679bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const ImmutableString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000680{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530681 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahofbb1c792018-01-19 16:26:59 +0200682 if (identifier.beginsWith("gl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530683 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300684 error(line, reservedErrMsg, "gl_");
685 return false;
686 }
687 if (sh::IsWebGLBasedSpec(mShaderSpec))
688 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200689 if (identifier.beginsWith("webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530690 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300691 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300692 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000693 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200694 if (identifier.beginsWith("_webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530695 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300696 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300697 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000698 }
699 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200700 if (identifier.contains("__"))
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300701 {
702 error(line,
703 "identifiers containing two consecutive underscores (__) are reserved as "
704 "possible future keywords",
Olli Etuahofbb1c792018-01-19 16:26:59 +0200705 identifier);
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300706 return false;
707 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300708 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000709}
710
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300711// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300712bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuaho95ed1942018-02-01 14:01:19 +0200713 const TIntermSequence &arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300714 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000715{
Olli Etuaho95ed1942018-02-01 14:01:19 +0200716 if (arguments.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530717 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200718 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300719 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000720 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200721
Olli Etuaho95ed1942018-02-01 14:01:19 +0200722 for (TIntermNode *arg : arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530723 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300724 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200725 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300726 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200727 {
jchen10cc2a10e2017-05-03 14:05:12 +0800728 std::string reason("cannot convert a variable with type ");
729 reason += getBasicString(argTyped->getBasicType());
730 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300731 return false;
732 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800733 else if (argTyped->getMemoryQualifier().writeonly)
734 {
735 error(line, "cannot convert a variable with writeonly", "constructor");
736 return false;
737 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200738 if (argTyped->getBasicType() == EbtVoid)
739 {
740 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300741 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200742 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744
Olli Etuaho856c4972016-08-08 11:38:39 +0300745 if (type.isArray())
746 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300747 // The size of an unsized constructor should already have been determined.
748 ASSERT(!type.isUnsizedArray());
Olli Etuaho95ed1942018-02-01 14:01:19 +0200749 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300750 {
751 error(line, "array constructor needs one argument per array element", "constructor");
752 return false;
753 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300754 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
755 // the array.
Olli Etuaho95ed1942018-02-01 14:01:19 +0200756 for (TIntermNode *const &argNode : arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300757 {
758 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300759 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500760 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300761 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500762 return false;
763 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300764 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300765 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000766 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300767 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300768 }
769 }
770 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300771 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300772 {
773 const TFieldList &fields = type.getStruct()->fields();
Olli Etuaho95ed1942018-02-01 14:01:19 +0200774 if (fields.size() != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300775 {
776 error(line,
777 "Number of constructor parameters does not match the number of structure fields",
778 "constructor");
779 return false;
780 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300781
782 for (size_t i = 0; i < fields.size(); i++)
783 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200784 if (i >= arguments.size() ||
785 arguments[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300786 {
787 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000788 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300789 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300790 }
791 }
792 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300793 else
794 {
795 // We're constructing a scalar, vector, or matrix.
796
797 // Note: It's okay to have too many components available, but not okay to have unused
798 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
799 // there is an extra argument, so 'overFull' will become true.
800
801 size_t size = 0;
802 bool full = false;
803 bool overFull = false;
804 bool matrixArg = false;
Olli Etuaho95ed1942018-02-01 14:01:19 +0200805 for (TIntermNode *arg : arguments)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300806 {
807 const TIntermTyped *argTyped = arg->getAsTyped();
808 ASSERT(argTyped != nullptr);
809
Olli Etuaho487b63a2017-05-23 15:55:09 +0300810 if (argTyped->getBasicType() == EbtStruct)
811 {
812 error(line, "a struct cannot be used as a constructor argument for this type",
813 "constructor");
814 return false;
815 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300816 if (argTyped->getType().isArray())
817 {
818 error(line, "constructing from a non-dereferenced array", "constructor");
819 return false;
820 }
821 if (argTyped->getType().isMatrix())
822 {
823 matrixArg = true;
824 }
825
826 size += argTyped->getType().getObjectSize();
827 if (full)
828 {
829 overFull = true;
830 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300831 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300832 {
833 full = true;
834 }
835 }
836
837 if (type.isMatrix() && matrixArg)
838 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200839 if (arguments.size() != 1)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300840 {
841 error(line, "constructing matrix from matrix can only take one argument",
842 "constructor");
843 return false;
844 }
845 }
846 else
847 {
848 if (size != 1 && size < type.getObjectSize())
849 {
850 error(line, "not enough data provided for construction", "constructor");
851 return false;
852 }
853 if (overFull)
854 {
855 error(line, "too many arguments", "constructor");
856 return false;
857 }
858 }
859 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300860
Olli Etuaho8a176262016-08-16 14:23:01 +0300861 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
Jamie Madillb98c3a82015-07-23 14:26:04 -0400864// This function checks to see if a void variable has been declared and raise an error message for
865// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866//
867// returns true in case of an error
868//
Olli Etuaho856c4972016-08-08 11:38:39 +0300869bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200870 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400871 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300873 if (type == EbtVoid)
874 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200875 error(line, "illegal use of type 'void'", identifier);
Olli Etuaho8a176262016-08-16 14:23:01 +0300876 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300877 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878
Olli Etuaho8a176262016-08-16 14:23:01 +0300879 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880}
881
Jamie Madillb98c3a82015-07-23 14:26:04 -0400882// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300883// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300884bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300886 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530887 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000888 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300889 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530890 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300891 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892}
893
Jamie Madillb98c3a82015-07-23 14:26:04 -0400894// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300895// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300896void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897{
Martin Radev4a9cd802016-09-01 16:51:51 +0300898 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530899 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000900 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530901 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902}
903
jchen10cc2a10e2017-05-03 14:05:12 +0800904bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
905 const TTypeSpecifierNonArray &pType,
906 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530908 if (pType.type == EbtStruct)
909 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300910 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530911 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000912 std::stringstream reasonStream;
913 reasonStream << reason << " (structure contains a sampler)";
914 std::string reasonStr = reasonStream.str();
915 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300916 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000917 }
jchen10cc2a10e2017-05-03 14:05:12 +0800918 // only samplers need to be checked from structs, since other opaque types can't be struct
919 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300920 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530921 }
jchen10cc2a10e2017-05-03 14:05:12 +0800922 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530923 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000924 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300925 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000926 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927
Olli Etuaho8a176262016-08-16 14:23:01 +0300928 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929}
930
Olli Etuaho856c4972016-08-08 11:38:39 +0300931void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
932 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400933{
934 if (pType.layoutQualifier.location != -1)
935 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400936 error(line, "location must only be specified for a single input or output variable",
937 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400938 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400939}
940
Olli Etuaho856c4972016-08-08 11:38:39 +0300941void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
942 const TLayoutQualifier &layoutQualifier)
943{
944 if (layoutQualifier.location != -1)
945 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000946 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
947 if (mShaderVersion >= 310)
948 {
949 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800950 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000951 }
952 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300953 }
954}
955
Qin Jiajiaca68d982017-09-18 16:41:56 +0800956void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
957 const TLayoutBlockStorage &blockStorage,
958 const TQualifier &qualifier)
959{
960 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
961 {
962 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
963 }
964}
965
Martin Radev2cc85b32016-08-05 16:22:53 +0300966void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
967 TQualifier qualifier,
968 const TType &type)
969{
Martin Radev2cc85b32016-08-05 16:22:53 +0300970 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800971 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530972 {
jchen10cc2a10e2017-05-03 14:05:12 +0800973 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975}
976
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300978unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530980 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000981
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200982 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
983 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
984 // fold as array size.
985 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000986 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000987 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300988 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000989 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000990
Olli Etuaho856c4972016-08-08 11:38:39 +0300991 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400992
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000993 if (constant->getBasicType() == EbtUInt)
994 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300995 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000996 }
997 else
998 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300999 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001000
Olli Etuaho856c4972016-08-08 11:38:39 +03001001 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001002 {
Nicolas Capens906744a2014-06-06 15:18:07 -04001003 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001004 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001005 }
Nicolas Capens906744a2014-06-06 15:18:07 -04001006
Olli Etuaho856c4972016-08-08 11:38:39 +03001007 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -04001008 }
1009
Olli Etuaho856c4972016-08-08 11:38:39 +03001010 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -04001011 {
1012 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001013 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -04001014 }
1015
1016 // The size of arrays is restricted here to prevent issues further down the
1017 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1018 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1019 const unsigned int sizeLimit = 65536;
1020
Olli Etuaho856c4972016-08-08 11:38:39 +03001021 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001022 {
1023 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001024 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001025 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001026
1027 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001031bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1032 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033{
Olli Etuaho8a176262016-08-16 14:23:01 +03001034 if ((elementQualifier.qualifier == EvqAttribute) ||
1035 (elementQualifier.qualifier == EvqVertexIn) ||
1036 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001037 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001038 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001039 TType(elementQualifier).getQualifierString());
1040 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001041 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042
Olli Etuaho8a176262016-08-16 14:23:01 +03001043 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044}
1045
Olli Etuaho8a176262016-08-16 14:23:01 +03001046// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001047bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1048 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001050 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001051 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001052 error(line, "cannot declare arrays of arrays",
1053 TType(elementType).getCompleteString().c_str());
1054 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001055 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001056 return true;
1057}
1058
1059// Check if this qualified element type can be formed into an array. This is only called when array
1060// brackets are associated with an identifier in a declaration, like this:
1061// float a[2];
1062// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1063// are associated with the type, like this:
1064// float[2] a;
1065bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1066 const TPublicType &elementType)
1067{
1068 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1069 {
1070 return false;
1071 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001072 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1073 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1074 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001075 // Geometry shader requires each user-defined input be declared as arrays or inside input
1076 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1077 // interface matching, such variables and blocks are treated as though they were not declared
1078 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001079 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001080 sh::IsVarying(elementType.qualifier) &&
1081 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001082 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001083 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001084 TType(elementType).getCompleteString().c_str());
1085 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001086 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001087 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088}
1089
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001091void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001092 const ImmutableString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001093 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094{
Olli Etuaho3739d232015-04-08 12:23:44 +03001095 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001096 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001097 {
1098 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001099 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001100
1101 // Generate informative error messages for ESSL1.
1102 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001103 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001104 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301105 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001106 "structures containing arrays may not be declared constant since they cannot be "
1107 "initialized",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001108 identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001109 }
1110 else
1111 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001112 error(line, "variables with qualifier 'const' must be initialized", identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001113 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001114 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001115 // This will make the type sized if it isn't sized yet.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001116 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized", identifier,
1117 type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118}
1119
Olli Etuaho2935c582015-04-08 14:32:06 +03001120// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121// and update the symbol table.
1122//
Olli Etuaho2935c582015-04-08 14:32:06 +03001123// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001125bool TParseContext::declareVariable(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001126 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001127 const TType *type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001128 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129{
Olli Etuaho2935c582015-04-08 14:32:06 +03001130 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131
Olli Etuahofbb1c792018-01-19 16:26:59 +02001132 (*variable) = new TVariable(&symbolTable, identifier, type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02001133
Olli Etuahob60d30f2018-01-16 12:31:06 +02001134 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001135
Olli Etuaho856c4972016-08-08 11:38:39 +03001136 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001137
Olli Etuaho2935c582015-04-08 14:32:06 +03001138 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahofbb1c792018-01-19 16:26:59 +02001139 if (type->isArray() && identifier.beginsWith("gl_LastFragData"))
Olli Etuaho2935c582015-04-08 14:32:06 +03001140 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001141 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02001142 symbolTable.findBuiltIn(ImmutableString("gl_MaxDrawBuffers"), mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001143 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001144 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001145 error(line, "redeclaration of gl_LastFragData as an array of arrays", identifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001146 return false;
1147 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001148 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001149 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001150 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001151 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001152 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001153 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001154 }
1155 }
1156 else
1157 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001158 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001159 identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001160 return false;
1161 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001162 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163
Olli Etuaho8a176262016-08-16 14:23:01 +03001164 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001165 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001166
Olli Etuaho195be942017-12-04 23:40:14 +02001167 if (!symbolTable.declareVariable(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001168 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001169 error(line, "redefinition", identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001170 return false;
1171 }
1172
Olli Etuahob60d30f2018-01-16 12:31:06 +02001173 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001174 return false;
1175
1176 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001177}
1178
Martin Radev70866b82016-07-22 15:27:42 +03001179void TParseContext::checkIsParameterQualifierValid(
1180 const TSourceLoc &line,
1181 const TTypeQualifierBuilder &typeQualifierBuilder,
1182 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301183{
Olli Etuahocce89652017-06-19 16:04:09 +03001184 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001185 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001186
1187 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301188 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001189 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1190 }
1191
1192 if (!IsImage(type->getBasicType()))
1193 {
Olli Etuaho43364892017-02-13 16:00:12 +00001194 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001195 }
1196 else
1197 {
1198 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001199 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001200
Martin Radev70866b82016-07-22 15:27:42 +03001201 type->setQualifier(typeQualifier.qualifier);
1202
1203 if (typeQualifier.precision != EbpUndefined)
1204 {
1205 type->setPrecision(typeQualifier.precision);
1206 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001207}
1208
Olli Etuaho703671e2017-11-08 17:47:18 +02001209template <size_t size>
1210bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1211 const std::array<TExtension, size> &extensions)
1212{
1213 ASSERT(!extensions.empty());
1214 const TExtensionBehavior &extBehavior = extensionBehavior();
1215
1216 bool canUseWithWarning = false;
1217 bool canUseWithoutWarning = false;
1218
1219 const char *errorMsgString = "";
1220 TExtension errorMsgExtension = TExtension::UNDEFINED;
1221
1222 for (TExtension extension : extensions)
1223 {
1224 auto extIter = extBehavior.find(extension);
1225 if (canUseWithWarning)
1226 {
1227 // We already have an extension that we can use, but with a warning.
1228 // See if we can use the alternative extension without a warning.
1229 if (extIter == extBehavior.end())
1230 {
1231 continue;
1232 }
1233 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1234 {
1235 canUseWithoutWarning = true;
1236 break;
1237 }
1238 continue;
1239 }
1240 if (extIter == extBehavior.end())
1241 {
1242 errorMsgString = "extension is not supported";
1243 errorMsgExtension = extension;
1244 }
1245 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1246 {
1247 errorMsgString = "extension is disabled";
1248 errorMsgExtension = extension;
1249 }
1250 else if (extIter->second == EBhWarn)
1251 {
1252 errorMsgExtension = extension;
1253 canUseWithWarning = true;
1254 }
1255 else
1256 {
1257 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1258 canUseWithoutWarning = true;
1259 break;
1260 }
1261 }
1262
1263 if (canUseWithoutWarning)
1264 {
1265 return true;
1266 }
1267 if (canUseWithWarning)
1268 {
1269 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1270 return true;
1271 }
1272 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1273 return false;
1274}
1275
1276template bool TParseContext::checkCanUseOneOfExtensions(
1277 const TSourceLoc &line,
1278 const std::array<TExtension, 1> &extensions);
1279template bool TParseContext::checkCanUseOneOfExtensions(
1280 const TSourceLoc &line,
1281 const std::array<TExtension, 2> &extensions);
1282template bool TParseContext::checkCanUseOneOfExtensions(
1283 const TSourceLoc &line,
1284 const std::array<TExtension, 3> &extensions);
1285
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001286bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001287{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001288 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001289 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001290}
1291
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001292// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1293// compile-time or link-time errors are the same whether or not the declaration is empty".
1294// This function implements all the checks that are done on qualifiers regardless of if the
1295// declaration is empty.
1296void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1297 const sh::TLayoutQualifier &layoutQualifier,
1298 const TSourceLoc &location)
1299{
1300 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1301 {
1302 error(location, "Shared memory declarations cannot have layout specified", "layout");
1303 }
1304
1305 if (layoutQualifier.matrixPacking != EmpUnspecified)
1306 {
1307 error(location, "layout qualifier only valid for interface blocks",
1308 getMatrixPackingString(layoutQualifier.matrixPacking));
1309 return;
1310 }
1311
1312 if (layoutQualifier.blockStorage != EbsUnspecified)
1313 {
1314 error(location, "layout qualifier only valid for interface blocks",
1315 getBlockStorageString(layoutQualifier.blockStorage));
1316 return;
1317 }
1318
1319 if (qualifier == EvqFragmentOut)
1320 {
1321 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1322 {
1323 error(location, "invalid layout qualifier combination", "yuv");
1324 return;
1325 }
1326 }
1327 else
1328 {
1329 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1330 }
1331
Olli Etuaho95468d12017-05-04 11:14:34 +03001332 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1333 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001334 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1335 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001336 {
1337 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1338 }
1339
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001340 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001341 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001342 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001343 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001344 // We're not checking whether the uniform location is in range here since that depends on
1345 // the type of the variable.
1346 // The type can only be fully determined for non-empty declarations.
1347 }
1348 if (!canHaveLocation)
1349 {
1350 checkLocationIsNotSpecified(location, layoutQualifier);
1351 }
1352}
1353
jchen104cdac9e2017-05-08 11:01:20 +08001354void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1355 const TSourceLoc &location)
1356{
1357 if (publicType.precision != EbpHigh)
1358 {
1359 error(location, "Can only be highp", "atomic counter");
1360 }
1361 // dEQP enforces compile error if location is specified. See uniform_location.test.
1362 if (publicType.layoutQualifier.location != -1)
1363 {
1364 error(location, "location must not be set for atomic_uint", "layout");
1365 }
1366 if (publicType.layoutQualifier.binding == -1)
1367 {
1368 error(location, "no binding specified", "atomic counter");
1369 }
1370}
1371
Olli Etuaho55bde912017-10-25 13:41:13 +03001372void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001373{
Olli Etuaho55bde912017-10-25 13:41:13 +03001374 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001375 {
1376 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1377 // error. It is assumed that this applies to empty declarations as well.
1378 error(location, "empty array declaration needs to specify a size", "");
1379 }
Martin Radevb8b01222016-11-20 23:25:53 +02001380}
1381
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001382// These checks are done for all declarations that are non-empty. They're done for non-empty
1383// declarations starting a declarator list, and declarators that follow an empty declaration.
1384void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1385 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001386{
Olli Etuahofa33d582015-04-09 14:33:12 +03001387 switch (publicType.qualifier)
1388 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001389 case EvqVaryingIn:
1390 case EvqVaryingOut:
1391 case EvqAttribute:
1392 case EvqVertexIn:
1393 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001394 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001395 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001396 {
1397 error(identifierLocation, "cannot be used with a structure",
1398 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001399 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001400 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001401 break;
1402 case EvqBuffer:
1403 if (publicType.getBasicType() != EbtInterfaceBlock)
1404 {
1405 error(identifierLocation,
1406 "cannot declare buffer variables at global scope(outside a block)",
1407 getQualifierString(publicType.qualifier));
1408 return;
1409 }
1410 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001411 default:
1412 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001413 }
jchen10cc2a10e2017-05-03 14:05:12 +08001414 std::string reason(getBasicString(publicType.getBasicType()));
1415 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001416 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001417 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001418 {
1419 return;
1420 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001421
Andrei Volykhina5527072017-03-22 16:46:30 +03001422 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1423 publicType.qualifier != EvqConst) &&
1424 publicType.getBasicType() == EbtYuvCscStandardEXT)
1425 {
1426 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1427 getQualifierString(publicType.qualifier));
1428 return;
1429 }
1430
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001431 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1432 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001433 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1434 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001435 TType type(publicType);
1436 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001437 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001438 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1439 publicType.layoutQualifier);
1440 }
1441 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001442
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001443 // check for layout qualifier issues
1444 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001445
Martin Radev2cc85b32016-08-05 16:22:53 +03001446 if (IsImage(publicType.getBasicType()))
1447 {
1448
1449 switch (layoutQualifier.imageInternalFormat)
1450 {
1451 case EiifRGBA32F:
1452 case EiifRGBA16F:
1453 case EiifR32F:
1454 case EiifRGBA8:
1455 case EiifRGBA8_SNORM:
1456 if (!IsFloatImage(publicType.getBasicType()))
1457 {
1458 error(identifierLocation,
1459 "internal image format requires a floating image type",
1460 getBasicString(publicType.getBasicType()));
1461 return;
1462 }
1463 break;
1464 case EiifRGBA32I:
1465 case EiifRGBA16I:
1466 case EiifRGBA8I:
1467 case EiifR32I:
1468 if (!IsIntegerImage(publicType.getBasicType()))
1469 {
1470 error(identifierLocation,
1471 "internal image format requires an integer image type",
1472 getBasicString(publicType.getBasicType()));
1473 return;
1474 }
1475 break;
1476 case EiifRGBA32UI:
1477 case EiifRGBA16UI:
1478 case EiifRGBA8UI:
1479 case EiifR32UI:
1480 if (!IsUnsignedImage(publicType.getBasicType()))
1481 {
1482 error(identifierLocation,
1483 "internal image format requires an unsigned image type",
1484 getBasicString(publicType.getBasicType()));
1485 return;
1486 }
1487 break;
1488 case EiifUnspecified:
1489 error(identifierLocation, "layout qualifier", "No image internal format specified");
1490 return;
1491 default:
1492 error(identifierLocation, "layout qualifier", "unrecognized token");
1493 return;
1494 }
1495
1496 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1497 switch (layoutQualifier.imageInternalFormat)
1498 {
1499 case EiifR32F:
1500 case EiifR32I:
1501 case EiifR32UI:
1502 break;
1503 default:
1504 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1505 {
1506 error(identifierLocation, "layout qualifier",
1507 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1508 "image variables must be qualified readonly and/or writeonly");
1509 return;
1510 }
1511 break;
1512 }
1513 }
1514 else
1515 {
Olli Etuaho43364892017-02-13 16:00:12 +00001516 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001517 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1518 }
jchen104cdac9e2017-05-08 11:01:20 +08001519
1520 if (IsAtomicCounter(publicType.getBasicType()))
1521 {
1522 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1523 }
1524 else
1525 {
1526 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1527 }
Olli Etuaho43364892017-02-13 16:00:12 +00001528}
Martin Radev2cc85b32016-08-05 16:22:53 +03001529
Olli Etuaho43364892017-02-13 16:00:12 +00001530void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1531{
1532 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001533 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1534 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1535 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1536 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1537 // when it comes to which shaders are accepted by the compiler.
1538 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001539 if (IsImage(type.getBasicType()))
1540 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001541 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1542 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001543 }
1544 else if (IsSampler(type.getBasicType()))
1545 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001546 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1547 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001548 }
jchen104cdac9e2017-05-08 11:01:20 +08001549 else if (IsAtomicCounter(type.getBasicType()))
1550 {
1551 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1552 }
Olli Etuaho43364892017-02-13 16:00:12 +00001553 else
1554 {
1555 ASSERT(!IsOpaqueType(type.getBasicType()));
1556 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001557 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001558}
1559
Olli Etuaho856c4972016-08-08 11:38:39 +03001560void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001561 const ImmutableString &layoutQualifierName,
Olli Etuaho856c4972016-08-08 11:38:39 +03001562 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001563{
1564
1565 if (mShaderVersion < versionRequired)
1566 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001567 error(location, "invalid layout qualifier: not supported", layoutQualifierName);
Martin Radev802abe02016-08-04 17:48:32 +03001568 }
1569}
1570
Olli Etuaho856c4972016-08-08 11:38:39 +03001571bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1572 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001573{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001574 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001575 for (size_t i = 0u; i < localSize.size(); ++i)
1576 {
1577 if (localSize[i] != -1)
1578 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001579 error(location,
1580 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1581 "global layout declaration",
1582 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001583 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001584 }
1585 }
1586
Olli Etuaho8a176262016-08-16 14:23:01 +03001587 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001588}
1589
Olli Etuaho43364892017-02-13 16:00:12 +00001590void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001591 TLayoutImageInternalFormat internalFormat)
1592{
1593 if (internalFormat != EiifUnspecified)
1594 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001595 error(location, "invalid layout qualifier: only valid when used with images",
1596 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001597 }
Olli Etuaho43364892017-02-13 16:00:12 +00001598}
1599
1600void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1601{
1602 if (binding != -1)
1603 {
1604 error(location,
1605 "invalid layout qualifier: only valid when used with opaque types or blocks",
1606 "binding");
1607 }
1608}
1609
jchen104cdac9e2017-05-08 11:01:20 +08001610void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1611{
1612 if (offset != -1)
1613 {
1614 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1615 "offset");
1616 }
1617}
1618
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001619void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1620 int binding,
1621 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001622{
1623 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001624 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001625 {
1626 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1627 }
1628}
1629
1630void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1631 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001632 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001633{
1634 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001635 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001636 {
1637 error(location, "sampler binding greater than maximum texture units", "binding");
1638 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001639}
1640
Jiajia Qinbc585152017-06-23 15:42:17 +08001641void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1642 const TQualifier &qualifier,
1643 int binding,
1644 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001645{
1646 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001647 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001648 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001649 if (binding + size > mMaxUniformBufferBindings)
1650 {
1651 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1652 "binding");
1653 }
1654 }
1655 else if (qualifier == EvqBuffer)
1656 {
1657 if (binding + size > mMaxShaderStorageBufferBindings)
1658 {
1659 error(location,
1660 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1661 "binding");
1662 }
jchen10af713a22017-04-19 09:10:56 +08001663 }
1664}
jchen104cdac9e2017-05-08 11:01:20 +08001665void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1666{
1667 if (binding >= mMaxAtomicCounterBindings)
1668 {
1669 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1670 "binding");
1671 }
1672}
jchen10af713a22017-04-19 09:10:56 +08001673
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001674void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1675 int objectLocationCount,
1676 const TLayoutQualifier &layoutQualifier)
1677{
1678 int loc = layoutQualifier.location;
1679 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1680 {
1681 error(location, "Uniform location out of range", "location");
1682 }
1683}
1684
Andrei Volykhina5527072017-03-22 16:46:30 +03001685void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1686{
1687 if (yuv != false)
1688 {
1689 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1690 }
1691}
1692
Jiajia Qinbc585152017-06-23 15:42:17 +08001693void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1694 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001695{
1696 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1697 {
1698 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001699 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1700 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1701 qual == EvqInOut || qual == EvqConstReadOnly))
1702 {
1703 if (argument->getMemoryQualifier().writeonly)
1704 {
1705 error(argument->getLine(),
1706 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001707 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001708 return;
1709 }
1710 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001711 if (qual == EvqOut || qual == EvqInOut)
1712 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001713 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001714 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001715 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001716 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001717 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001718 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001719 }
1720 }
1721 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001722}
1723
Martin Radev70866b82016-07-22 15:27:42 +03001724void TParseContext::checkInvariantVariableQualifier(bool invariant,
1725 const TQualifier qualifier,
1726 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001727{
Martin Radev70866b82016-07-22 15:27:42 +03001728 if (!invariant)
1729 return;
1730
1731 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001732 {
Martin Radev70866b82016-07-22 15:27:42 +03001733 // input variables in the fragment shader can be also qualified as invariant
1734 if (!sh::CanBeInvariantESSL1(qualifier))
1735 {
1736 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1737 }
1738 }
1739 else
1740 {
1741 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1742 {
1743 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1744 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001745 }
1746}
1747
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001748bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001749{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001750 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001751}
1752
Jamie Madillb98c3a82015-07-23 14:26:04 -04001753void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1754 const char *extName,
1755 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001756{
1757 pp::SourceLocation srcLoc;
1758 srcLoc.file = loc.first_file;
1759 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001760 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001761}
1762
Jamie Madillb98c3a82015-07-23 14:26:04 -04001763void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1764 const char *name,
1765 const char *value,
1766 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001767{
1768 pp::SourceLocation srcLoc;
1769 srcLoc.file = loc.first_file;
1770 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001771 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001772}
1773
Martin Radev4c4c8e72016-08-04 12:25:34 +03001774sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001775{
Jamie Madill2f294c92017-11-20 14:47:26 -05001776 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001777 for (size_t i = 0u; i < result.size(); ++i)
1778 {
1779 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1780 {
1781 result[i] = 1;
1782 }
1783 else
1784 {
1785 result[i] = mComputeShaderLocalSize[i];
1786 }
1787 }
1788 return result;
1789}
1790
Olli Etuaho56229f12017-07-10 14:16:33 +03001791TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1792 const TSourceLoc &line)
1793{
1794 TIntermConstantUnion *node = new TIntermConstantUnion(
1795 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1796 node->setLine(line);
1797 return node;
1798}
1799
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800/////////////////////////////////////////////////////////////////////////////////
1801//
1802// Non-Errors.
1803//
1804/////////////////////////////////////////////////////////////////////////////////
1805
Jamie Madill5c097022014-08-20 16:38:32 -04001806const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001807 const ImmutableString &name,
Jamie Madill5c097022014-08-20 16:38:32 -04001808 const TSymbol *symbol)
1809{
Jamie Madill5c097022014-08-20 16:38:32 -04001810 if (!symbol)
1811 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001812 error(location, "undeclared identifier", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001813 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001814 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001815
1816 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001817 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001818 error(location, "variable expected", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001819 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001820 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001821
1822 const TVariable *variable = static_cast<const TVariable *>(symbol);
1823
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001824 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001825 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001826 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001827 }
1828
Olli Etuaho0f684632017-07-13 12:42:15 +03001829 // Reject shaders using both gl_FragData and gl_FragColor
1830 TQualifier qualifier = variable->getType().getQualifier();
1831 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001832 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001833 mUsesFragData = true;
1834 }
1835 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1836 {
1837 mUsesFragColor = true;
1838 }
1839 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1840 {
1841 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001842 }
1843
Olli Etuaho0f684632017-07-13 12:42:15 +03001844 // This validation is not quite correct - it's only an error to write to
1845 // both FragData and FragColor. For simplicity, and because users shouldn't
1846 // be rewarded for reading from undefined varaibles, return an error
1847 // if they are both referenced, rather than assigned.
1848 if (mUsesFragData && mUsesFragColor)
1849 {
1850 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1851 if (mUsesSecondaryOutputs)
1852 {
1853 errorMessage =
1854 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1855 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1856 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02001857 error(location, errorMessage, name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001858 }
1859
1860 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1861 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1862 qualifier == EvqWorkGroupSize)
1863 {
1864 error(location,
1865 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1866 "gl_WorkGroupSize");
1867 }
Jamie Madill5c097022014-08-20 16:38:32 -04001868 return variable;
1869}
1870
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001871TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001872 const ImmutableString &name,
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001873 const TSymbol *symbol)
1874{
1875 const TVariable *variable = getNamedVariable(location, name, symbol);
1876
Olli Etuaho0f684632017-07-13 12:42:15 +03001877 if (!variable)
1878 {
1879 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1880 node->setLine(location);
1881 return node;
1882 }
1883
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001884 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001885 TIntermTyped *node = nullptr;
1886
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001887 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001888 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001889 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001890 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001891 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001892 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001893 {
1894 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1895 // needs to be added to the AST as a constant and not as a symbol.
1896 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1897 TConstantUnion *constArray = new TConstantUnion[3];
1898 for (size_t i = 0; i < 3; ++i)
1899 {
1900 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1901 }
1902
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001903 ASSERT(variableType.getBasicType() == EbtUInt);
1904 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001905
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001906 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001907 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001908 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001909 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001910 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1911 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001912 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02001913 ASSERT(mGlInVariableWithArraySize != nullptr);
1914 node = new TIntermSymbol(mGlInVariableWithArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001915 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001916 else
1917 {
Olli Etuaho195be942017-12-04 23:40:14 +02001918 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001919 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001920 ASSERT(node != nullptr);
1921 node->setLine(location);
1922 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001923}
1924
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925// Initializers show up in several places in the grammar. Have one set of
1926// code to handle them here.
1927//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001928// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001929bool TParseContext::executeInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001930 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001931 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001932 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001933 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001934{
Olli Etuaho13389b62016-10-16 11:48:18 +01001935 ASSERT(initNode != nullptr);
1936 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937
Olli Etuahob60d30f2018-01-16 12:31:06 +02001938 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001939 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001940 // In case initializer is not an array or type has more dimensions than initializer, this
1941 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1942 // actually is an array or not. Having a non-array initializer for an unsized array will
1943 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001944 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001945 type->sizeUnsizedArrays(arraySizes);
1946 }
1947
1948 const TQualifier qualifier = type->getQualifier();
1949
1950 bool constError = false;
1951 if (qualifier == EvqConst)
1952 {
1953 if (EvqConst != initializer->getType().getQualifier())
1954 {
1955 std::stringstream reasonStream;
1956 reasonStream << "assigning non-constant to '" << type->getCompleteString() << "'";
1957 std::string reason = reasonStream.str();
1958 error(line, reason.c_str(), "=");
1959
1960 // We're still going to declare the variable to avoid extra error messages.
1961 type->setQualifier(EvqTemporary);
1962 constError = true;
1963 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001964 }
Olli Etuaho195be942017-12-04 23:40:14 +02001965
1966 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001967 if (!declareVariable(line, identifier, type, &variable))
1968 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001969 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001970 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971
Olli Etuahob60d30f2018-01-16 12:31:06 +02001972 if (constError)
1973 {
1974 return false;
1975 }
1976
Olli Etuahob0c645e2015-05-12 14:25:36 +03001977 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001978 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001979 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001980 {
1981 // Error message does not completely match behavior with ESSL 1.00, but
1982 // we want to steer developers towards only using constant expressions.
1983 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001984 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001985 }
1986 if (globalInitWarning)
1987 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001988 warning(
1989 line,
1990 "global variable initializers should be constant expressions "
1991 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1992 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001993 }
1994
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001995 // identifier must be of type constant, a global, or a temporary
Arun Patole7e7e68d2015-05-22 12:02:25 +05301996 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1997 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001998 error(line, " cannot initialize this type of qualifier ",
1999 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002000 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002001 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02002002
2003 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
2004 intermSymbol->setLine(line);
2005
2006 if (!binaryOpCommonCheck(EOpInitialize, intermSymbol, initializer, line))
2007 {
2008 assignError(line, "=", variable->getType().getCompleteString(),
2009 initializer->getCompleteString());
2010 return false;
2011 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002012
Arun Patole7e7e68d2015-05-22 12:02:25 +05302013 if (qualifier == EvqConst)
2014 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002015 // Save the constant folded value to the variable if possible.
2016 const TConstantUnion *constArray = initializer->getConstantValue();
2017 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302018 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002019 variable->shareConstPointer(constArray);
2020 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002021 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03002022 ASSERT(*initNode == nullptr);
2023 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002024 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002025 }
2026 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002027
Olli Etuahob60d30f2018-01-16 12:31:06 +02002028 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
2029 (*initNode)->setLine(line);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002030 return true;
2031}
2032
2033TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002034 const ImmutableString &identifier,
Olli Etuaho914b79a2017-06-19 16:03:19 +03002035 TIntermTyped *initializer,
2036 const TSourceLoc &loc)
2037{
2038 checkIsScalarBool(loc, pType);
2039 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002040 TType *type = new TType(pType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002041 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002042 {
2043 // The initializer is valid. The init condition needs to have a node - either the
2044 // initializer node, or a constant node in case the initialized variable is const and won't
2045 // be recorded in the AST.
2046 if (initNode == nullptr)
2047 {
2048 return initializer;
2049 }
2050 else
2051 {
2052 TIntermDeclaration *declaration = new TIntermDeclaration();
2053 declaration->appendDeclarator(initNode);
2054 return declaration;
2055 }
2056 }
2057 return nullptr;
2058}
2059
2060TIntermNode *TParseContext::addLoop(TLoopType type,
2061 TIntermNode *init,
2062 TIntermNode *cond,
2063 TIntermTyped *expr,
2064 TIntermNode *body,
2065 const TSourceLoc &line)
2066{
2067 TIntermNode *node = nullptr;
2068 TIntermTyped *typedCond = nullptr;
2069 if (cond)
2070 {
2071 typedCond = cond->getAsTyped();
2072 }
2073 if (cond == nullptr || typedCond)
2074 {
Olli Etuahocce89652017-06-19 16:04:09 +03002075 if (type == ELoopDoWhile)
2076 {
2077 checkIsScalarBool(line, typedCond);
2078 }
2079 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2080 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2081 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2082 !typedCond->isVector()));
2083
Olli Etuaho3ec75682017-07-05 17:02:55 +03002084 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002085 node->setLine(line);
2086 return node;
2087 }
2088
Olli Etuahocce89652017-06-19 16:04:09 +03002089 ASSERT(type != ELoopDoWhile);
2090
Olli Etuaho914b79a2017-06-19 16:03:19 +03002091 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2092 ASSERT(declaration);
2093 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2094 ASSERT(declarator->getLeft()->getAsSymbolNode());
2095
2096 // The condition is a declaration. In the AST representation we don't support declarations as
2097 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2098 // the loop.
2099 TIntermBlock *block = new TIntermBlock();
2100
2101 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2102 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2103 block->appendStatement(declareCondition);
2104
2105 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2106 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002107 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002108 block->appendStatement(loop);
2109 loop->setLine(line);
2110 block->setLine(line);
2111 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112}
2113
Olli Etuahocce89652017-06-19 16:04:09 +03002114TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2115 TIntermNodePair code,
2116 const TSourceLoc &loc)
2117{
Olli Etuaho56229f12017-07-10 14:16:33 +03002118 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002119
2120 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002121 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002122 {
2123 if (cond->getAsConstantUnion()->getBConst(0) == true)
2124 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002125 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002126 }
2127 else
2128 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002129 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002130 }
2131 }
2132
Olli Etuaho3ec75682017-07-05 17:02:55 +03002133 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002134 node->setLine(loc);
2135
2136 return node;
2137}
2138
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002139void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2140{
2141 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2142 typeSpecifier->getBasicType());
2143
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002144 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002145 {
2146 error(typeSpecifier->getLine(), "not supported", "first-class array");
2147 typeSpecifier->clearArrayness();
2148 }
2149}
2150
Martin Radev70866b82016-07-22 15:27:42 +03002151TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302152 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002153{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002154 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002155
Martin Radev70866b82016-07-22 15:27:42 +03002156 TPublicType returnType = typeSpecifier;
2157 returnType.qualifier = typeQualifier.qualifier;
2158 returnType.invariant = typeQualifier.invariant;
2159 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002160 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002161 returnType.precision = typeSpecifier.precision;
2162
2163 if (typeQualifier.precision != EbpUndefined)
2164 {
2165 returnType.precision = typeQualifier.precision;
2166 }
2167
Martin Radev4a9cd802016-09-01 16:51:51 +03002168 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2169 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002170
Martin Radev4a9cd802016-09-01 16:51:51 +03002171 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2172 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002173
Martin Radev4a9cd802016-09-01 16:51:51 +03002174 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002175
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002176 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002177 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002178 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002179 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002180 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002181 returnType.clearArrayness();
2182 }
2183
Martin Radev70866b82016-07-22 15:27:42 +03002184 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002185 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002186 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002187 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002188 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002189 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002190
Martin Radev70866b82016-07-22 15:27:42 +03002191 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002192 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002193 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002194 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002195 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002196 }
2197 }
2198 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002199 {
Martin Radev70866b82016-07-22 15:27:42 +03002200 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002201 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002202 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002203 }
Martin Radev70866b82016-07-22 15:27:42 +03002204 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2205 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002206 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002207 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2208 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002209 }
Martin Radev70866b82016-07-22 15:27:42 +03002210 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002211 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002212 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002213 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002214 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002215 }
2216
2217 return returnType;
2218}
2219
Olli Etuaho856c4972016-08-08 11:38:39 +03002220void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2221 const TPublicType &type,
2222 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002223{
2224 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002225 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002226 {
2227 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002228 }
2229
2230 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2231 switch (qualifier)
2232 {
2233 case EvqVertexIn:
2234 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002235 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002236 {
2237 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002238 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002239 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002240 return;
2241 case EvqFragmentOut:
2242 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002243 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002244 {
2245 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002246 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002247 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002248 return;
2249 default:
2250 break;
2251 }
2252
2253 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2254 // restrictions.
2255 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002256 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2257 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002258 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2259 {
2260 error(qualifierLocation, "must use 'flat' interpolation here",
2261 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002262 }
2263
Martin Radev4a9cd802016-09-01 16:51:51 +03002264 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002265 {
2266 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2267 // These restrictions are only implied by the ESSL 3.00 spec, but
2268 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002269 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002270 {
2271 error(qualifierLocation, "cannot be an array of structures",
2272 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002273 }
2274 if (type.isStructureContainingArrays())
2275 {
2276 error(qualifierLocation, "cannot be a structure containing an array",
2277 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002278 }
2279 if (type.isStructureContainingType(EbtStruct))
2280 {
2281 error(qualifierLocation, "cannot be a structure containing a structure",
2282 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002283 }
2284 if (type.isStructureContainingType(EbtBool))
2285 {
2286 error(qualifierLocation, "cannot be a structure containing a bool",
2287 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002288 }
2289 }
2290}
2291
Martin Radev2cc85b32016-08-05 16:22:53 +03002292void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2293{
2294 if (qualifier.getType() == QtStorage)
2295 {
2296 const TStorageQualifierWrapper &storageQualifier =
2297 static_cast<const TStorageQualifierWrapper &>(qualifier);
2298 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2299 !symbolTable.atGlobalLevel())
2300 {
2301 error(storageQualifier.getLine(),
2302 "Local variables can only use the const storage qualifier.",
2303 storageQualifier.getQualifierString().c_str());
2304 }
2305 }
2306}
2307
Olli Etuaho43364892017-02-13 16:00:12 +00002308void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002309 const TSourceLoc &location)
2310{
Jiajia Qinbc585152017-06-23 15:42:17 +08002311 const std::string reason(
2312 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2313 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002314 if (memoryQualifier.readonly)
2315 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002316 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002317 }
2318 if (memoryQualifier.writeonly)
2319 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002320 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002321 }
Martin Radev049edfa2016-11-11 14:35:37 +02002322 if (memoryQualifier.coherent)
2323 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002324 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002325 }
2326 if (memoryQualifier.restrictQualifier)
2327 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002328 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002329 }
2330 if (memoryQualifier.volatileQualifier)
2331 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002332 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002333 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002334}
2335
jchen104cdac9e2017-05-08 11:01:20 +08002336// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2337// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002338void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2339 const TSourceLoc &loc,
2340 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002341{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002342 if (!IsAtomicCounter(type->getBasicType()))
2343 {
2344 return;
2345 }
2346
2347 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2348 : kAtomicCounterSize;
2349 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2350 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002351 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002352 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002353 {
2354 offset = bindingState.appendSpan(size);
2355 }
2356 else
2357 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002358 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002359 }
2360 if (offset == -1)
2361 {
2362 error(loc, "Offset overlapping", "atomic counter");
2363 return;
2364 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002365 layoutQualifier.offset = offset;
2366 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002367}
2368
Olli Etuaho454c34c2017-10-25 16:35:56 +03002369void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002370 const ImmutableString &token,
Olli Etuaho454c34c2017-10-25 16:35:56 +03002371 TType *type)
2372{
2373 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2374 {
2375 if (type->isArray() && type->getOutermostArraySize() == 0u)
2376 {
2377 // Set size for the unsized geometry shader inputs if they are declared after a valid
2378 // input primitive declaration.
2379 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2380 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002381 ASSERT(mGlInVariableWithArraySize != nullptr);
2382 type->sizeOutermostUnsizedArray(
2383 mGlInVariableWithArraySize->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002384 }
2385 else
2386 {
2387 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2388 // An input can be declared without an array size if there is a previous layout
2389 // which specifies the size.
2390 error(location,
2391 "Missing a valid input primitive declaration before declaring an unsized "
2392 "array input",
2393 token);
2394 }
2395 }
2396 else if (type->isArray())
2397 {
2398 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2399 }
2400 else
2401 {
2402 error(location, "Geometry shader input variable must be declared as an array", token);
2403 }
2404 }
2405}
2406
Olli Etuaho13389b62016-10-16 11:48:18 +01002407TIntermDeclaration *TParseContext::parseSingleDeclaration(
2408 TPublicType &publicType,
2409 const TSourceLoc &identifierOrTypeLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002410 const ImmutableString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002411{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002412 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002413 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2414 mDirectiveHandler.pragma().stdgl.invariantAll)
2415 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002416 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002417
2418 // The directive handler has already taken care of rejecting invalid uses of this pragma
2419 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2420 // affected variable declarations:
2421 //
2422 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2423 // elsewhere, in TranslatorGLSL.)
2424 //
2425 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2426 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2427 // the way this is currently implemented we have to enable this compiler option before
2428 // parsing the shader and determining the shading language version it uses. If this were
2429 // implemented as a post-pass, the workaround could be more targeted.
2430 //
2431 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2432 // the specification, but there are desktop OpenGL drivers that expect that this is the
2433 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2434 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2435 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002436 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002437 }
2438 }
2439
Olli Etuahofbb1c792018-01-19 16:26:59 +02002440 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier, type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002441
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002442 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2443 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002444
Olli Etuahobab4c082015-04-24 16:38:49 +03002445 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002446 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002447
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002448 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002449 if (emptyDeclaration)
2450 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002451 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002452 // In most cases we don't need to create a symbol node for an empty declaration.
2453 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002454 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002455 {
Olli Etuaho195be942017-12-04 23:40:14 +02002456 TVariable *emptyVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02002457 new TVariable(&symbolTable, ImmutableString(""), type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002458 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002459 }
jchen104cdac9e2017-05-08 11:01:20 +08002460 else if (IsAtomicCounter(publicType.getBasicType()))
2461 {
2462 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2463 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002464 }
2465 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002466 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002467 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002468
Olli Etuahob60d30f2018-01-16 12:31:06 +02002469 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002470
Olli Etuahob60d30f2018-01-16 12:31:06 +02002471 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002472
Olli Etuaho2935c582015-04-08 14:32:06 +03002473 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002474 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002475 {
Olli Etuaho195be942017-12-04 23:40:14 +02002476 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002477 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002478 }
2479
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002480 TIntermDeclaration *declaration = new TIntermDeclaration();
2481 declaration->setLine(identifierOrTypeLocation);
2482 if (symbol)
2483 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002484 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002485 declaration->appendDeclarator(symbol);
2486 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002487 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002488}
2489
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002490TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2491 TPublicType &elementType,
2492 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002493 const ImmutableString &identifier,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002494 const TSourceLoc &indexLocation,
2495 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002496{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002497 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002498
Olli Etuaho55bde912017-10-25 13:41:13 +03002499 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002500 identifierLocation);
2501
Olli Etuaho55bde912017-10-25 13:41:13 +03002502 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002503
Olli Etuaho55bde912017-10-25 13:41:13 +03002504 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002505
Olli Etuahob60d30f2018-01-16 12:31:06 +02002506 TType *arrayType = new TType(elementType);
2507 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002508
Olli Etuahofbb1c792018-01-19 16:26:59 +02002509 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002510
Olli Etuahob60d30f2018-01-16 12:31:06 +02002511 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002512
Olli Etuahob60d30f2018-01-16 12:31:06 +02002513 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002514
Olli Etuaho13389b62016-10-16 11:48:18 +01002515 TIntermDeclaration *declaration = new TIntermDeclaration();
2516 declaration->setLine(identifierLocation);
2517
Olli Etuaho195be942017-12-04 23:40:14 +02002518 TVariable *variable = nullptr;
2519 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002520 {
Olli Etuaho195be942017-12-04 23:40:14 +02002521 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002522 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002523 declaration->appendDeclarator(symbol);
2524 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002525
Olli Etuaho13389b62016-10-16 11:48:18 +01002526 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002527}
2528
Olli Etuaho13389b62016-10-16 11:48:18 +01002529TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2530 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002531 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002532 const TSourceLoc &initLocation,
2533 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002534{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002535 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002536
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002537 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2538 identifierLocation);
2539
2540 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002541
Olli Etuaho13389b62016-10-16 11:48:18 +01002542 TIntermDeclaration *declaration = new TIntermDeclaration();
2543 declaration->setLine(identifierLocation);
2544
2545 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002546 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002547 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002548 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002549 if (initNode)
2550 {
2551 declaration->appendDeclarator(initNode);
2552 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002553 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002554 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002555}
2556
Olli Etuaho13389b62016-10-16 11:48:18 +01002557TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002558 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002559 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002560 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002561 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002562 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002563 const TSourceLoc &initLocation,
2564 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002566 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002567
Olli Etuaho55bde912017-10-25 13:41:13 +03002568 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002569 identifierLocation);
2570
Olli Etuaho55bde912017-10-25 13:41:13 +03002571 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572
Olli Etuaho55bde912017-10-25 13:41:13 +03002573 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002574
Olli Etuahob60d30f2018-01-16 12:31:06 +02002575 TType *arrayType = new TType(elementType);
2576 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002577
Olli Etuaho13389b62016-10-16 11:48:18 +01002578 TIntermDeclaration *declaration = new TIntermDeclaration();
2579 declaration->setLine(identifierLocation);
2580
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002581 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002582 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002583 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002584 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002585 if (initNode)
2586 {
2587 declaration->appendDeclarator(initNode);
2588 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002589 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002590
2591 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002592}
2593
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002594TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002595 const TTypeQualifierBuilder &typeQualifierBuilder,
2596 const TSourceLoc &identifierLoc,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002597 const ImmutableString &identifier,
Martin Radev70866b82016-07-22 15:27:42 +03002598 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002599{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002600 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002601
Martin Radev70866b82016-07-22 15:27:42 +03002602 if (!typeQualifier.invariant)
2603 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002604 error(identifierLoc, "Expected invariant", identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002605 return nullptr;
2606 }
2607 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2608 {
2609 return nullptr;
2610 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002611 if (!symbol)
2612 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002613 error(identifierLoc, "undeclared identifier declared as invariant", identifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002614 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002615 }
Martin Radev70866b82016-07-22 15:27:42 +03002616 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002617 {
Martin Radev70866b82016-07-22 15:27:42 +03002618 error(identifierLoc, "invariant declaration specifies qualifier",
2619 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002620 }
Martin Radev70866b82016-07-22 15:27:42 +03002621 if (typeQualifier.precision != EbpUndefined)
2622 {
2623 error(identifierLoc, "invariant declaration specifies precision",
2624 getPrecisionString(typeQualifier.precision));
2625 }
2626 if (!typeQualifier.layoutQualifier.isEmpty())
2627 {
2628 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2629 }
2630
2631 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002632 if (!variable)
2633 {
2634 return nullptr;
2635 }
Martin Radev70866b82016-07-22 15:27:42 +03002636 const TType &type = variable->getType();
2637
2638 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2639 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002640 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002641
Olli Etuahofbb1c792018-01-19 16:26:59 +02002642 symbolTable.addInvariantVarying(std::string(identifier.data()));
Martin Radev70866b82016-07-22 15:27:42 +03002643
Olli Etuaho195be942017-12-04 23:40:14 +02002644 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002645 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002646
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002647 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002648}
2649
Olli Etuaho13389b62016-10-16 11:48:18 +01002650void TParseContext::parseDeclarator(TPublicType &publicType,
2651 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002652 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002653 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002654{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002655 // If the declaration starting this declarator list was empty (example: int,), some checks were
2656 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002657 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002658 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002659 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2660 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002661 }
2662
Olli Etuaho856c4972016-08-08 11:38:39 +03002663 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002664
Olli Etuahob60d30f2018-01-16 12:31:06 +02002665 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002666
Olli Etuahofbb1c792018-01-19 16:26:59 +02002667 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002668
Olli Etuahob60d30f2018-01-16 12:31:06 +02002669 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002670
Olli Etuahob60d30f2018-01-16 12:31:06 +02002671 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002672
Olli Etuaho195be942017-12-04 23:40:14 +02002673 TVariable *variable = nullptr;
2674 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002675 {
Olli Etuaho195be942017-12-04 23:40:14 +02002676 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002677 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002678 declarationOut->appendDeclarator(symbol);
2679 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002680}
2681
Olli Etuaho55bde912017-10-25 13:41:13 +03002682void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002683 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002684 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002685 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002686 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002687 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002688{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002689 // If the declaration starting this declarator list was empty (example: int,), some checks were
2690 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002691 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002692 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002693 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002694 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002695 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002696
Olli Etuaho55bde912017-10-25 13:41:13 +03002697 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002698
Olli Etuaho55bde912017-10-25 13:41:13 +03002699 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002700 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002701 TType *arrayType = new TType(elementType);
2702 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002703
Olli Etuahofbb1c792018-01-19 16:26:59 +02002704 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002705
Olli Etuahob60d30f2018-01-16 12:31:06 +02002706 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002707
Olli Etuahob60d30f2018-01-16 12:31:06 +02002708 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002709
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002710 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002711 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002712 {
Olli Etuaho195be942017-12-04 23:40:14 +02002713 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002714 symbol->setLine(identifierLocation);
2715 declarationOut->appendDeclarator(symbol);
2716 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002717 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002718}
2719
Olli Etuaho13389b62016-10-16 11:48:18 +01002720void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2721 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002722 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002723 const TSourceLoc &initLocation,
2724 TIntermTyped *initializer,
2725 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002726{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002727 // If the declaration starting this declarator list was empty (example: int,), some checks were
2728 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002729 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002730 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002731 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2732 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002733 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002734
Olli Etuaho856c4972016-08-08 11:38:39 +03002735 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002736
Olli Etuaho13389b62016-10-16 11:48:18 +01002737 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002738 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002739 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002740 {
2741 //
2742 // build the intermediate representation
2743 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002744 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002745 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002746 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002747 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002748 }
2749}
2750
Olli Etuaho55bde912017-10-25 13:41:13 +03002751void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002752 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002753 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002754 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002755 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002756 const TSourceLoc &initLocation,
2757 TIntermTyped *initializer,
2758 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002759{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002760 // If the declaration starting this declarator list was empty (example: int,), some checks were
2761 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002762 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002763 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002764 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002765 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002766 }
2767
Olli Etuaho55bde912017-10-25 13:41:13 +03002768 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002769
Olli Etuaho55bde912017-10-25 13:41:13 +03002770 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002771
Olli Etuahob60d30f2018-01-16 12:31:06 +02002772 TType *arrayType = new TType(elementType);
2773 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002774
2775 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002776 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002777 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002778 {
2779 if (initNode)
2780 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002781 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002782 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002783 }
2784}
2785
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002786TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2787{
2788 // It's simpler to parse an empty statement as a constant expression rather than having a
2789 // different type of node just for empty statements, that will be pruned from the AST anyway.
2790 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2791 node->setLine(location);
2792 return node;
2793}
2794
jchen104cdac9e2017-05-08 11:01:20 +08002795void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2796 const TSourceLoc &location)
2797{
2798 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2799 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2800 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2801 {
2802 error(location, "Requires both binding and offset", "layout");
2803 return;
2804 }
2805 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2806}
2807
Olli Etuahocce89652017-06-19 16:04:09 +03002808void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2809 const TPublicType &type,
2810 const TSourceLoc &loc)
2811{
2812 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2813 !getFragmentPrecisionHigh())
2814 {
2815 error(loc, "precision is not supported in fragment shader", "highp");
2816 }
2817
2818 if (!CanSetDefaultPrecisionOnType(type))
2819 {
2820 error(loc, "illegal type argument for default precision qualifier",
2821 getBasicString(type.getBasicType()));
2822 return;
2823 }
2824 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2825}
2826
Shaob5cc1192017-07-06 10:47:20 +08002827bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2828{
2829 switch (typeQualifier.layoutQualifier.primitiveType)
2830 {
2831 case EptLines:
2832 case EptLinesAdjacency:
2833 case EptTriangles:
2834 case EptTrianglesAdjacency:
2835 return typeQualifier.qualifier == EvqGeometryIn;
2836
2837 case EptLineStrip:
2838 case EptTriangleStrip:
2839 return typeQualifier.qualifier == EvqGeometryOut;
2840
2841 case EptPoints:
2842 return true;
2843
2844 default:
2845 UNREACHABLE();
2846 return false;
2847 }
2848}
2849
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002850void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2851 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002852{
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002853 if (mGlInVariableWithArraySize == nullptr)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002854 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002855 const TSymbol *glPerVertex = symbolTable.findBuiltIn(ImmutableString("gl_PerVertex"), 310);
Olli Etuahodd21ecf2018-01-10 12:42:09 +02002856 const TInterfaceBlock *glPerVertexBlock = static_cast<const TInterfaceBlock *>(glPerVertex);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002857 TType *glInType = new TType(glPerVertexBlock, EvqPerVertexIn, TLayoutQualifier::Create());
2858 glInType->makeArray(inputArraySize);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002859 mGlInVariableWithArraySize =
Olli Etuahofbb1c792018-01-19 16:26:59 +02002860 new TVariable(&symbolTable, ImmutableString("gl_in"), glInType, SymbolType::BuiltIn,
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002861 TExtension::EXT_geometry_shader);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002862 }
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002863 else if (mGlInVariableWithArraySize->getType().getOutermostArraySize() != inputArraySize)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002864 {
2865 error(line,
2866 "Array size or input primitive declaration doesn't match the size of earlier sized "
2867 "array inputs.",
2868 "layout");
2869 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002870}
2871
Shaob5cc1192017-07-06 10:47:20 +08002872bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2873{
2874 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2875
2876 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2877
2878 if (layoutQualifier.maxVertices != -1)
2879 {
2880 error(typeQualifier.line,
2881 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2882 return false;
2883 }
2884
2885 // Set mGeometryInputPrimitiveType if exists
2886 if (layoutQualifier.primitiveType != EptUndefined)
2887 {
2888 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2889 {
2890 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2891 return false;
2892 }
2893
2894 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2895 {
2896 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002897 setGeometryShaderInputArraySize(
2898 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2899 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002900 }
2901 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2902 {
2903 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2904 "layout");
2905 return false;
2906 }
2907 }
2908
2909 // Set mGeometryInvocations if exists
2910 if (layoutQualifier.invocations > 0)
2911 {
2912 if (mGeometryShaderInvocations == 0)
2913 {
2914 mGeometryShaderInvocations = layoutQualifier.invocations;
2915 }
2916 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2917 {
2918 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2919 "layout");
2920 return false;
2921 }
2922 }
2923
2924 return true;
2925}
2926
2927bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2928{
2929 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2930
2931 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2932
2933 if (layoutQualifier.invocations > 0)
2934 {
2935 error(typeQualifier.line,
2936 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2937 return false;
2938 }
2939
2940 // Set mGeometryOutputPrimitiveType if exists
2941 if (layoutQualifier.primitiveType != EptUndefined)
2942 {
2943 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2944 {
2945 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2946 return false;
2947 }
2948
2949 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2950 {
2951 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2952 }
2953 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2954 {
2955 error(typeQualifier.line,
2956 "primitive doesn't match earlier output primitive declaration", "layout");
2957 return false;
2958 }
2959 }
2960
2961 // Set mGeometryMaxVertices if exists
2962 if (layoutQualifier.maxVertices > -1)
2963 {
2964 if (mGeometryShaderMaxVertices == -1)
2965 {
2966 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2967 }
2968 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2969 {
2970 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2971 "layout");
2972 return false;
2973 }
2974 }
2975
2976 return true;
2977}
2978
Martin Radev70866b82016-07-22 15:27:42 +03002979void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002980{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002981 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002982 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002983
Martin Radev70866b82016-07-22 15:27:42 +03002984 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2985 typeQualifier.line);
2986
Jamie Madillc2128ff2016-07-04 10:26:17 -04002987 // It should never be the case, but some strange parser errors can send us here.
2988 if (layoutQualifier.isEmpty())
2989 {
2990 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002991 return;
2992 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002993
Martin Radev802abe02016-08-04 17:48:32 +03002994 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002995 {
Olli Etuaho43364892017-02-13 16:00:12 +00002996 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002997 return;
2998 }
2999
Olli Etuaho43364892017-02-13 16:00:12 +00003000 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
3001
3002 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003003
3004 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3005
Andrei Volykhina5527072017-03-22 16:46:30 +03003006 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3007
jchen104cdac9e2017-05-08 11:01:20 +08003008 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3009
Qin Jiajiaca68d982017-09-18 16:41:56 +08003010 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3011 typeQualifier.qualifier);
3012
Martin Radev802abe02016-08-04 17:48:32 +03003013 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003014 {
Martin Radev802abe02016-08-04 17:48:32 +03003015 if (mComputeShaderLocalSizeDeclared &&
3016 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3017 {
3018 error(typeQualifier.line, "Work group size does not match the previous declaration",
3019 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003020 return;
3021 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003022
Martin Radev802abe02016-08-04 17:48:32 +03003023 if (mShaderVersion < 310)
3024 {
3025 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003026 return;
3027 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003028
Martin Radev4c4c8e72016-08-04 12:25:34 +03003029 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003030 {
3031 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003032 return;
3033 }
3034
3035 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003036 symbolTable.findBuiltIn(ImmutableString("gl_MaxComputeWorkGroupSize"), mShaderVersion));
Martin Radev802abe02016-08-04 17:48:32 +03003037
3038 const TConstantUnion *maxComputeWorkGroupSizeData =
3039 maxComputeWorkGroupSize->getConstPointer();
3040
3041 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3042 {
3043 if (layoutQualifier.localSize[i] != -1)
3044 {
3045 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3046 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3047 if (mComputeShaderLocalSize[i] < 1 ||
3048 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3049 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003050 std::stringstream reasonStream;
3051 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3052 << maxComputeWorkGroupSizeValue;
3053 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003054
Olli Etuaho4de340a2016-12-16 09:32:03 +00003055 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003056 return;
3057 }
3058 }
3059 }
3060
3061 mComputeShaderLocalSizeDeclared = true;
3062 }
Shaob5cc1192017-07-06 10:47:20 +08003063 else if (typeQualifier.qualifier == EvqGeometryIn)
3064 {
3065 if (mShaderVersion < 310)
3066 {
3067 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3068 return;
3069 }
3070
3071 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3072 {
3073 return;
3074 }
3075 }
3076 else if (typeQualifier.qualifier == EvqGeometryOut)
3077 {
3078 if (mShaderVersion < 310)
3079 {
3080 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3081 "layout");
3082 return;
3083 }
3084
3085 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3086 {
3087 return;
3088 }
3089 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003090 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3091 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003092 {
3093 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3094 // specification.
3095 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3096 {
3097 error(typeQualifier.line, "Number of views does not match the previous declaration",
3098 "layout");
3099 return;
3100 }
3101
3102 if (layoutQualifier.numViews == -1)
3103 {
3104 error(typeQualifier.line, "No num_views specified", "layout");
3105 return;
3106 }
3107
3108 if (layoutQualifier.numViews > mMaxNumViews)
3109 {
3110 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3111 "layout");
3112 return;
3113 }
3114
3115 mNumViews = layoutQualifier.numViews;
3116 }
Martin Radev802abe02016-08-04 17:48:32 +03003117 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003118 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003119 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003120 {
Martin Radev802abe02016-08-04 17:48:32 +03003121 return;
3122 }
3123
Jiajia Qinbc585152017-06-23 15:42:17 +08003124 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003125 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003126 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003127 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003128 return;
3129 }
3130
3131 if (mShaderVersion < 300)
3132 {
3133 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3134 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003135 return;
3136 }
3137
Olli Etuaho09b04a22016-12-15 13:30:26 +00003138 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003139
3140 if (layoutQualifier.matrixPacking != EmpUnspecified)
3141 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003142 if (typeQualifier.qualifier == EvqUniform)
3143 {
3144 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3145 }
3146 else if (typeQualifier.qualifier == EvqBuffer)
3147 {
3148 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3149 }
Martin Radev802abe02016-08-04 17:48:32 +03003150 }
3151
3152 if (layoutQualifier.blockStorage != EbsUnspecified)
3153 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003154 if (typeQualifier.qualifier == EvqUniform)
3155 {
3156 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3157 }
3158 else if (typeQualifier.qualifier == EvqBuffer)
3159 {
3160 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3161 }
Martin Radev802abe02016-08-04 17:48:32 +03003162 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003163 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003164}
3165
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003166TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3167 const TFunction &function,
3168 const TSourceLoc &location,
3169 bool insertParametersToSymbolTable)
3170{
Olli Etuahobed35d72017-12-20 16:36:26 +02003171 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003172
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003173 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003174 prototype->setLine(location);
3175
3176 for (size_t i = 0; i < function.getParamCount(); i++)
3177 {
3178 const TConstParameter &param = function.getParam(i);
3179
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003180 TIntermSymbol *symbol = nullptr;
3181
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003182 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3183 // be used for unused args).
3184 if (param.name != nullptr)
3185 {
Olli Etuaho195be942017-12-04 23:40:14 +02003186 TVariable *variable =
Olli Etuahob60d30f2018-01-16 12:31:06 +02003187 new TVariable(&symbolTable, param.name, param.type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003188 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003189 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003190 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003191 {
Olli Etuaho195be942017-12-04 23:40:14 +02003192 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003193 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003194 error(location, "redefinition", param.name);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003195 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003196 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003197 // Unsized type of a named parameter should have already been checked and sanitized.
3198 ASSERT(!param.type->isUnsizedArray());
3199 }
3200 else
3201 {
3202 if (param.type->isUnsizedArray())
3203 {
3204 error(location, "function parameter array must be sized at compile time", "[]");
3205 // We don't need to size the arrays since the parameter is unnamed and hence
3206 // inaccessible.
3207 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003208 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003209 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003210 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003211 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3212 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003213 TVariable *emptyVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003214 new TVariable(&symbolTable, ImmutableString(""), param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003215 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003216 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003217 symbol->setLine(location);
3218 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003219 }
3220 return prototype;
3221}
3222
Olli Etuaho16c745a2017-01-16 17:02:27 +00003223TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3224 const TFunction &parsedFunction,
3225 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003226{
Olli Etuaho476197f2016-10-11 13:59:08 +01003227 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3228 // first declaration. Either way the instance in the symbol table is used to track whether the
3229 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003230 bool hadPrototypeDeclaration = false;
3231 const TFunction *function = symbolTable.markUserDefinedFunctionHasPrototypeDeclaration(
3232 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3233
3234 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003235 {
3236 // ESSL 1.00.17 section 4.2.7.
3237 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3238 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003239 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003240
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003241 TIntermFunctionPrototype *prototype =
3242 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003243
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003244 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003245
3246 if (!symbolTable.atGlobalLevel())
3247 {
3248 // ESSL 3.00.4 section 4.2.4.
3249 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003250 }
3251
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003252 return prototype;
3253}
3254
Olli Etuaho336b1472016-10-05 16:37:55 +01003255TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003256 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003257 TIntermBlock *functionBody,
3258 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003259{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003260 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003261 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3262 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003263 error(location,
3264 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003265 }
3266
Olli Etuahof51fdd22016-10-03 10:03:40 +01003267 if (functionBody == nullptr)
3268 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003269 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003270 functionBody->setLine(location);
3271 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003272 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003273 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003274 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003275
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003276 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003277 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003278}
3279
Olli Etuaho476197f2016-10-11 13:59:08 +01003280void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003281 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003282 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003283{
Olli Etuaho476197f2016-10-11 13:59:08 +01003284 ASSERT(function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003285 const TSymbol *builtIn =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003286 symbolTable.findBuiltIn(ImmutableString(function->getMangledName()), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003287
3288 if (builtIn)
3289 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003290 error(location, "built-in functions cannot be redefined", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003291 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003292 else
Jamie Madill185fb402015-06-12 15:48:48 -04003293 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003294 bool wasDefined = false;
3295 function =
3296 symbolTable.setUserDefinedFunctionParameterNamesFromDefinition(function, &wasDefined);
3297 if (wasDefined)
Olli Etuaho476197f2016-10-11 13:59:08 +01003298 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003299 error(location, "function already has a body", function->name());
Olli Etuaho476197f2016-10-11 13:59:08 +01003300 }
Jamie Madill185fb402015-06-12 15:48:48 -04003301 }
Jamie Madill185fb402015-06-12 15:48:48 -04003302
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003303 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003304 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003305 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003306
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003307 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003308 setLoopNestingLevel(0);
3309}
3310
Jamie Madillb98c3a82015-07-23 14:26:04 -04003311TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003312{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003313 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003314 // We don't know at this point whether this is a function definition or a prototype.
3315 // The definition production code will check for redefinitions.
3316 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003317 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003318 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3319 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003320 //
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003321 const TFunction *prevDec = static_cast<const TFunction *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003322 symbolTable.find(ImmutableString(function->getMangledName()), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303323
Olli Etuahod80f2942017-11-06 12:44:45 +02003324 for (size_t i = 0u; i < function->getParamCount(); ++i)
3325 {
3326 auto &param = function->getParam(i);
3327 if (param.type->isStructSpecifier())
3328 {
3329 // ESSL 3.00.6 section 12.10.
3330 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003331 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003332 }
3333 }
3334
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003335 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003336 function->name().data(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303337 {
Martin Radevda6254b2016-12-14 17:00:36 +02003338 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303339 // Therefore overloading or redefining builtin functions is an error.
3340 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003341 function->name());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303342 }
3343 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003344 {
3345 if (prevDec->getReturnType() != function->getReturnType())
3346 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003347 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003348 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003349 }
3350 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3351 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003352 if (prevDec->getParam(i).type->getQualifier() !=
3353 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003354 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003355 error(location,
3356 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003357 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003358 }
3359 }
3360 }
3361
Jamie Madill185fb402015-06-12 15:48:48 -04003362 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003363 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3364 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003365 if (prevSym)
3366 {
3367 if (!prevSym->isFunction())
3368 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003369 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003370 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003371 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003372 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003373 // Parsing is at the inner scope level of the function's arguments and body statement at this
3374 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3375 // scope.
3376 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003377
Olli Etuaho78d13742017-01-18 13:06:10 +00003378 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003379 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003380 {
3381 if (function->getParamCount() > 0)
3382 {
3383 error(location, "function cannot take any parameter(s)", "main");
3384 }
3385 if (function->getReturnType().getBasicType() != EbtVoid)
3386 {
3387 error(location, "main function cannot return a value",
3388 function->getReturnType().getBasicString());
3389 }
3390 }
3391
Jamie Madill185fb402015-06-12 15:48:48 -04003392 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003393 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3394 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003395 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3396 //
3397 return function;
3398}
3399
Olli Etuaho9de84a52016-06-14 17:36:01 +03003400TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003401 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003402 const TSourceLoc &location)
3403{
3404 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3405 {
3406 error(location, "no qualifiers allowed for function return",
3407 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003408 }
3409 if (!type.layoutQualifier.isEmpty())
3410 {
3411 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003412 }
jchen10cc2a10e2017-05-03 14:05:12 +08003413 // make sure an opaque type is not involved as well...
3414 std::string reason(getBasicString(type.getBasicType()));
3415 reason += "s can't be function return values";
3416 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003417 if (mShaderVersion < 300)
3418 {
3419 // Array return values are forbidden, but there's also no valid syntax for declaring array
3420 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003421 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003422
3423 if (type.isStructureContainingArrays())
3424 {
3425 // ESSL 1.00.17 section 6.1 Function Definitions
3426 error(location, "structures containing arrays can't be function return values",
3427 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003428 }
3429 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003430
3431 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003432 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003433}
3434
Olli Etuahofbb1c792018-01-19 16:26:59 +02003435TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name)
Olli Etuahocce89652017-06-19 16:04:09 +03003436{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003437 return TFunctionLookup::CreateFunctionCall(name);
Olli Etuahocce89652017-06-19 16:04:09 +03003438}
3439
Olli Etuaho95ed1942018-02-01 14:01:19 +02003440TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003441{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003442 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003443 {
3444 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3445 "[]");
3446 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003447 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003448 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003449 error(publicType.getLine(), "constructor can't be a structure definition",
3450 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003451 }
3452
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003453 TType *type = new TType(publicType);
3454 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003455 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003456 error(publicType.getLine(), "cannot construct this type",
3457 getBasicString(publicType.getBasicType()));
3458 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003459 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003460 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003461}
3462
Olli Etuaho55bde912017-10-25 13:41:13 +03003463void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3464 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003465 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003466 TType *arrayType)
3467{
3468 if (arrayType->isUnsizedArray())
3469 {
3470 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003471 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003472 }
3473}
3474
3475TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003476 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003477 const TSourceLoc &nameLoc)
3478{
Olli Etuaho55bde912017-10-25 13:41:13 +03003479 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003480 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003481 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003482 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003483 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003484 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003485 checkIsNotReserved(nameLoc, name);
3486 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003487 return param;
3488}
3489
Olli Etuaho55bde912017-10-25 13:41:13 +03003490TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003491 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003492 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003493{
Olli Etuaho55bde912017-10-25 13:41:13 +03003494 TType *type = new TType(publicType);
3495 return parseParameterDeclarator(type, name, nameLoc);
3496}
3497
Olli Etuahofbb1c792018-01-19 16:26:59 +02003498TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003499 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003500 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003501 const TSourceLoc &arrayLoc,
3502 TPublicType *elementType)
3503{
3504 checkArrayElementIsNotArray(arrayLoc, *elementType);
3505 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003506 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003507 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003508}
3509
Olli Etuaho95ed1942018-02-01 14:01:19 +02003510bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3511 const TIntermSequence &arguments,
3512 TType type,
3513 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003514{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003515 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003516 {
3517 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3518 return false;
3519 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003520 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003521 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003522 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003523 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003524 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3525 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003526 {
3527 error(line, "constructing from a non-dereferenced array", "constructor");
3528 return false;
3529 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003530 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003531 {
3532 if (dimensionalityFromElement == 1u)
3533 {
3534 error(line, "implicitly sized array of arrays constructor argument is not an array",
3535 "constructor");
3536 }
3537 else
3538 {
3539 error(line,
3540 "implicitly sized array of arrays constructor argument dimensionality is too "
3541 "low",
3542 "constructor");
3543 }
3544 return false;
3545 }
3546 }
3547 return true;
3548}
3549
Jamie Madillb98c3a82015-07-23 14:26:04 -04003550// This function is used to test for the correctness of the parameters passed to various constructor
3551// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003552//
Olli Etuaho856c4972016-08-08 11:38:39 +03003553// 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 +00003554//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003555TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003556{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003557 TType type = fnCall->constructorType();
3558 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003559 if (type.isUnsizedArray())
3560 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003561 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003562 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003563 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003564 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003565 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003566 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003567 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003568 if (type.getOutermostArraySize() == 0u)
3569 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003570 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003571 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003572 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003573 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003574 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003575 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003576 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003577 }
3578 }
3579 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003580 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003581
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003582 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003583 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003584 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003585 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003586
Olli Etuaho95ed1942018-02-01 14:01:19 +02003587 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003588 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003589
Olli Etuaho765924f2018-01-04 12:48:36 +02003590 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003591}
3592
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003593//
3594// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003595// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003596//
Olli Etuaho13389b62016-10-16 11:48:18 +01003597TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003598 const TTypeQualifierBuilder &typeQualifierBuilder,
3599 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003600 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003601 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003602 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003603 const TSourceLoc &instanceLine,
3604 TIntermTyped *arrayIndex,
3605 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003606{
Olli Etuaho856c4972016-08-08 11:38:39 +03003607 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003608
Olli Etuaho77ba4082016-12-16 12:01:18 +00003609 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003610
Jiajia Qinbc585152017-06-23 15:42:17 +08003611 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003612 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003613 error(typeQualifier.line,
3614 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3615 "3.10",
3616 getQualifierString(typeQualifier.qualifier));
3617 }
3618 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3619 {
3620 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003621 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003622 }
3623
Martin Radev70866b82016-07-22 15:27:42 +03003624 if (typeQualifier.invariant)
3625 {
3626 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3627 }
3628
Jiajia Qinbc585152017-06-23 15:42:17 +08003629 if (typeQualifier.qualifier != EvqBuffer)
3630 {
3631 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3632 }
Olli Etuaho43364892017-02-13 16:00:12 +00003633
jchen10af713a22017-04-19 09:10:56 +08003634 // add array index
3635 unsigned int arraySize = 0;
3636 if (arrayIndex != nullptr)
3637 {
3638 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3639 }
3640
3641 if (mShaderVersion < 310)
3642 {
3643 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3644 }
3645 else
3646 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003647 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3648 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003649 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003650
Andrei Volykhina5527072017-03-22 16:46:30 +03003651 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3652
Jamie Madill099c0f32013-06-20 11:55:52 -04003653 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003654 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003655 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3656 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003657
Jamie Madill099c0f32013-06-20 11:55:52 -04003658 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3659 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003660 if (typeQualifier.qualifier == EvqUniform)
3661 {
3662 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3663 }
3664 else if (typeQualifier.qualifier == EvqBuffer)
3665 {
3666 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3667 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003668 }
3669
Jamie Madill1566ef72013-06-20 11:55:54 -04003670 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3671 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003672 if (typeQualifier.qualifier == EvqUniform)
3673 {
3674 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3675 }
3676 else if (typeQualifier.qualifier == EvqBuffer)
3677 {
3678 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3679 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003680 }
3681
Olli Etuaho856c4972016-08-08 11:38:39 +03003682 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003683
Martin Radev2cc85b32016-08-05 16:22:53 +03003684 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3685
Jamie Madill98493dd2013-07-08 14:39:03 -04003686 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303687 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3688 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003689 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303690 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003691 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303692 {
jchen10cc2a10e2017-05-03 14:05:12 +08003693 std::string reason("unsupported type - ");
3694 reason += fieldType->getBasicString();
3695 reason += " types are not allowed in interface blocks";
3696 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003697 }
3698
Jamie Madill98493dd2013-07-08 14:39:03 -04003699 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003700 switch (qualifier)
3701 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003702 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003703 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003704 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003705 if (typeQualifier.qualifier == EvqBuffer)
3706 {
3707 error(field->line(), "invalid qualifier on shader storage block member",
3708 getQualifierString(qualifier));
3709 }
3710 break;
3711 case EvqBuffer:
3712 if (typeQualifier.qualifier == EvqUniform)
3713 {
3714 error(field->line(), "invalid qualifier on uniform block member",
3715 getQualifierString(qualifier));
3716 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003717 break;
3718 default:
3719 error(field->line(), "invalid qualifier on interface block member",
3720 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003721 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003722 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003723
Martin Radev70866b82016-07-22 15:27:42 +03003724 if (fieldType->isInvariant())
3725 {
3726 error(field->line(), "invalid qualifier on interface block member", "invariant");
3727 }
3728
Jamie Madilla5efff92013-06-06 11:56:47 -04003729 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003730 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003731 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003732 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003733
Jamie Madill98493dd2013-07-08 14:39:03 -04003734 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003735 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003736 error(field->line(), "invalid layout qualifier: cannot be used here",
3737 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003738 }
3739
Jamie Madill98493dd2013-07-08 14:39:03 -04003740 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003741 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003742 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003743 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003744 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003745 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003746 warning(field->line(),
3747 "extraneous layout qualifier: only has an effect on matrix types",
3748 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003749 }
3750
Jamie Madill98493dd2013-07-08 14:39:03 -04003751 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003752
Olli Etuahoebee5b32017-11-23 12:56:32 +02003753 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3754 typeQualifier.qualifier != EvqBuffer)
3755 {
3756 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3757 checkIsNotUnsizedArray(field->line(),
3758 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003759 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003760 }
3761
Jiajia Qinbc585152017-06-23 15:42:17 +08003762 if (typeQualifier.qualifier == EvqBuffer)
3763 {
3764 // set memory qualifiers
3765 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3766 // qualified with a memory qualifier, it is as if all of its members were declared with
3767 // the same memory qualifier.
3768 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3769 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3770 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3771 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3772 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3773 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3774 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3775 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3776 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3777 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3778 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003779 }
3780
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003781 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003782 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003783 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3784 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003785 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003786 }
3787
Olli Etuahob60d30f2018-01-16 12:31:06 +02003788 TType *interfaceBlockType =
3789 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003790 if (arrayIndex != nullptr)
3791 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003792 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003793 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003794
Olli Etuaho195be942017-12-04 23:40:14 +02003795 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003796 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3797 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003798 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003799 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003800 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003801
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003802 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003803 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003804 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003805 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3806 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003807 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003808 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003809
3810 // set parent pointer of the field variable
3811 fieldType->setInterfaceBlock(interfaceBlock);
3812
Olli Etuahob60d30f2018-01-16 12:31:06 +02003813 fieldType->setQualifier(typeQualifier.qualifier);
3814
Olli Etuaho195be942017-12-04 23:40:14 +02003815 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003816 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuahob60d30f2018-01-16 12:31:06 +02003817 if (!symbolTable.declareVariable(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303818 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003819 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003820 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003821 }
3822 }
3823 }
3824 else
3825 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003826 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003827
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003828 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003829 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303830 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003831 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003832 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003833 }
3834
Olli Etuaho195be942017-12-04 23:40:14 +02003835 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3836 blockSymbol->setLine(typeQualifier.line);
3837 TIntermDeclaration *declaration = new TIntermDeclaration();
3838 declaration->appendDeclarator(blockSymbol);
3839 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003840
3841 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003842 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003843}
3844
Olli Etuahofbb1c792018-01-19 16:26:59 +02003845void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3846 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003847{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003848 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003849
3850 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003851 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303852 if (mStructNestingLevel > 1)
3853 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003854 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003855 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003856}
3857
3858void TParseContext::exitStructDeclaration()
3859{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003860 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003861}
3862
Olli Etuaho8a176262016-08-16 14:23:01 +03003863void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003864{
Jamie Madillacb4b812016-11-07 13:50:29 -05003865 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303866 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003867 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003868 }
3869
Arun Patole7e7e68d2015-05-22 12:02:25 +05303870 if (field.type()->getBasicType() != EbtStruct)
3871 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003872 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003873 }
3874
3875 // We're already inside a structure definition at this point, so add
3876 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303877 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3878 {
Jamie Madill41a49272014-03-18 16:10:13 -04003879 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003880 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3881 {
3882 // This may happen in case there are nested struct definitions. While they are also
3883 // invalid GLSL, they don't cause a syntax error.
3884 reasonStream << "Struct nesting";
3885 }
3886 else
3887 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003888 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003889 }
3890 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003891 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003892 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003893 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003894 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003895}
3896
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003897//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003898// Parse an array index expression
3899//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003900TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3901 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303902 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003903{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003904 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3905 {
3906 if (baseExpression->getAsSymbolNode())
3907 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303908 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003909 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003910 }
3911 else
3912 {
3913 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3914 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003915
Olli Etuaho3ec75682017-07-05 17:02:55 +03003916 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003917 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003918
Jiawei Shaod8105a02017-08-08 09:54:36 +08003919 if (baseExpression->getQualifier() == EvqPerVertexIn)
3920 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003921 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003922 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3923 {
3924 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3925 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3926 }
3927 }
3928
Jamie Madill21c1e452014-12-29 11:33:41 -05003929 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3930
Olli Etuaho36b05142015-11-12 13:10:42 +02003931 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3932 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3933 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3934 // index is a constant expression.
3935 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3936 {
3937 if (baseExpression->isInterfaceBlock())
3938 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003939 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003940 switch (baseExpression->getQualifier())
3941 {
3942 case EvqPerVertexIn:
3943 break;
3944 case EvqUniform:
3945 case EvqBuffer:
3946 error(location,
3947 "array indexes for uniform block arrays and shader storage block arrays "
3948 "must be constant integral expressions",
3949 "[");
3950 break;
3951 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003952 // We can reach here only in error cases.
3953 ASSERT(mDiagnostics->numErrors() > 0);
3954 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003955 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003956 }
3957 else if (baseExpression->getQualifier() == EvqFragmentOut)
3958 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003959 error(location,
3960 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003961 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003962 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3963 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003964 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003965 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003966 }
3967
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003968 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003969 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003970 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3971 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3972 // constant fold expressions that are not constant expressions). The most compatible way to
3973 // handle this case is to report a warning instead of an error and force the index to be in
3974 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003975 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003976 int index = 0;
3977 if (indexConstantUnion->getBasicType() == EbtInt)
3978 {
3979 index = indexConstantUnion->getIConst(0);
3980 }
3981 else if (indexConstantUnion->getBasicType() == EbtUInt)
3982 {
3983 index = static_cast<int>(indexConstantUnion->getUConst(0));
3984 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003985
3986 int safeIndex = -1;
3987
Olli Etuahoebee5b32017-11-23 12:56:32 +02003988 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04003989 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003990 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
3991 safeIndex = 0;
3992 }
3993
3994 if (!baseExpression->getType().isUnsizedArray())
3995 {
3996 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03003997 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003998 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003999 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004000 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4001 {
4002 outOfRangeError(outOfRangeIndexIsError, location,
4003 "array index for gl_FragData must be zero when "
4004 "GL_EXT_draw_buffers is disabled",
4005 "[]");
4006 safeIndex = 0;
4007 }
4008 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004009 }
4010 // Only do generic out-of-range check if similar error hasn't already been reported.
4011 if (safeIndex < 0)
4012 {
4013 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004014 {
4015 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4016 baseExpression->getOutermostArraySize(),
4017 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004018 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004019 else if (baseExpression->isMatrix())
4020 {
4021 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4022 baseExpression->getType().getCols(),
4023 "matrix field selection out of range");
4024 }
4025 else
4026 {
4027 ASSERT(baseExpression->isVector());
4028 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4029 baseExpression->getType().getNominalSize(),
4030 "vector field selection out of range");
4031 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004032 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004033
Olli Etuahoebee5b32017-11-23 12:56:32 +02004034 ASSERT(safeIndex >= 0);
4035 // Data of constant unions can't be changed, because it may be shared with other
4036 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4037 // sanitized object.
4038 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4039 {
4040 TConstantUnion *safeConstantUnion = new TConstantUnion();
4041 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004042 indexExpression = new TIntermConstantUnion(
4043 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4044 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004045 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004046
Olli Etuahoebee5b32017-11-23 12:56:32 +02004047 TIntermBinary *node =
4048 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4049 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004050 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004051 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004052 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004053
4054 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4055 node->setLine(location);
4056 // Indirect indexing can never be constant folded.
4057 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004058}
4059
Olli Etuahoebee5b32017-11-23 12:56:32 +02004060int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4061 const TSourceLoc &location,
4062 int index,
4063 int arraySize,
4064 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004065{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004066 // Should not reach here with an unsized / runtime-sized array.
4067 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004068 // A negative index should already have been checked.
4069 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004070 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004071 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004072 std::stringstream reasonStream;
4073 reasonStream << reason << " '" << index << "'";
4074 std::string token = reasonStream.str();
4075 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004076 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004077 }
4078 return index;
4079}
4080
Jamie Madillb98c3a82015-07-23 14:26:04 -04004081TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4082 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004083 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004084 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004085{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004086 if (baseExpression->isArray())
4087 {
4088 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004089 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004090 }
4091
4092 if (baseExpression->isVector())
4093 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004094 TVector<int> fieldOffsets;
4095 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4096 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004097 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004098 fieldOffsets.resize(1);
4099 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004100 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004101 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4102 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004103
Olli Etuaho765924f2018-01-04 12:48:36 +02004104 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004105 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004106 else if (baseExpression->getBasicType() == EbtStruct)
4107 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304108 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004109 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004110 {
4111 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004112 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004113 }
4114 else
4115 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004116 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004117 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004118 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004119 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004120 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121 {
4122 fieldFound = true;
4123 break;
4124 }
4125 }
4126 if (fieldFound)
4127 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004128 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004129 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004130 TIntermBinary *node =
4131 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4132 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004133 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004134 }
4135 else
4136 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004137 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004138 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004139 }
4140 }
4141 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004142 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004143 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304144 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004145 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004146 {
4147 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004148 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004149 }
4150 else
4151 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004152 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004153 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004154 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004155 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004156 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004157 {
4158 fieldFound = true;
4159 break;
4160 }
4161 }
4162 if (fieldFound)
4163 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004164 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004165 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004166 TIntermBinary *node =
4167 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4168 node->setLine(dotLocation);
4169 // Indexing interface blocks can never be constant folded.
4170 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004171 }
4172 else
4173 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004174 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004175 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004176 }
4177 }
4178 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004179 else
4180 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004181 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004182 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004183 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004184 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004185 }
4186 else
4187 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304188 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004189 " field selection requires structure, vector, or interface block on left hand "
4190 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004191 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004192 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004193 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004194 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004195}
4196
Olli Etuahofbb1c792018-01-19 16:26:59 +02004197TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004198 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004199{
Jamie Madill2f294c92017-11-20 14:47:26 -05004200 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004201
4202 if (qualifierType == "shared")
4203 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004204 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004205 {
4206 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4207 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004208 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004209 }
4210 else if (qualifierType == "packed")
4211 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004212 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004213 {
4214 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4215 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004216 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004217 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004218 else if (qualifierType == "std430")
4219 {
4220 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4221 qualifier.blockStorage = EbsStd430;
4222 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004223 else if (qualifierType == "std140")
4224 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004225 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004226 }
4227 else if (qualifierType == "row_major")
4228 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004229 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004230 }
4231 else if (qualifierType == "column_major")
4232 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004233 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004234 }
4235 else if (qualifierType == "location")
4236 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004237 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004238 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004239 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004240 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004241 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004242 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4243 {
4244 qualifier.yuv = true;
4245 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004246 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004247 else if (qualifierType == "rgba32f")
4248 {
4249 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4250 qualifier.imageInternalFormat = EiifRGBA32F;
4251 }
4252 else if (qualifierType == "rgba16f")
4253 {
4254 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4255 qualifier.imageInternalFormat = EiifRGBA16F;
4256 }
4257 else if (qualifierType == "r32f")
4258 {
4259 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4260 qualifier.imageInternalFormat = EiifR32F;
4261 }
4262 else if (qualifierType == "rgba8")
4263 {
4264 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4265 qualifier.imageInternalFormat = EiifRGBA8;
4266 }
4267 else if (qualifierType == "rgba8_snorm")
4268 {
4269 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4270 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4271 }
4272 else if (qualifierType == "rgba32i")
4273 {
4274 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4275 qualifier.imageInternalFormat = EiifRGBA32I;
4276 }
4277 else if (qualifierType == "rgba16i")
4278 {
4279 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4280 qualifier.imageInternalFormat = EiifRGBA16I;
4281 }
4282 else if (qualifierType == "rgba8i")
4283 {
4284 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4285 qualifier.imageInternalFormat = EiifRGBA8I;
4286 }
4287 else if (qualifierType == "r32i")
4288 {
4289 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4290 qualifier.imageInternalFormat = EiifR32I;
4291 }
4292 else if (qualifierType == "rgba32ui")
4293 {
4294 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4295 qualifier.imageInternalFormat = EiifRGBA32UI;
4296 }
4297 else if (qualifierType == "rgba16ui")
4298 {
4299 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4300 qualifier.imageInternalFormat = EiifRGBA16UI;
4301 }
4302 else if (qualifierType == "rgba8ui")
4303 {
4304 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4305 qualifier.imageInternalFormat = EiifRGBA8UI;
4306 }
4307 else if (qualifierType == "r32ui")
4308 {
4309 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4310 qualifier.imageInternalFormat = EiifR32UI;
4311 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004312 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4313 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004314 {
4315 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4316 qualifier.primitiveType = EptPoints;
4317 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004318 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4319 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004320 {
4321 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4322 qualifier.primitiveType = EptLines;
4323 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004324 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4325 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004326 {
4327 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4328 qualifier.primitiveType = EptLinesAdjacency;
4329 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004330 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4331 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004332 {
4333 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4334 qualifier.primitiveType = EptTriangles;
4335 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004336 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4337 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004338 {
4339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4340 qualifier.primitiveType = EptTrianglesAdjacency;
4341 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004342 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4343 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004344 {
4345 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4346 qualifier.primitiveType = EptLineStrip;
4347 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004348 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4349 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004350 {
4351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4352 qualifier.primitiveType = EptTriangleStrip;
4353 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004354
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004355 else
4356 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004357 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004358 }
4359
Jamie Madilla5efff92013-06-06 11:56:47 -04004360 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004361}
4362
Olli Etuahofbb1c792018-01-19 16:26:59 +02004363void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004364 const TSourceLoc &qualifierTypeLine,
4365 int intValue,
4366 const TSourceLoc &intValueLine,
4367 const std::string &intValueString,
4368 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004369 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004370{
Olli Etuaho856c4972016-08-08 11:38:39 +03004371 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004372 if (intValue < 1)
4373 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004374 std::stringstream reasonStream;
4375 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4376 std::string reason = reasonStream.str();
4377 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004378 }
4379 (*localSize)[index] = intValue;
4380}
4381
Olli Etuaho09b04a22016-12-15 13:30:26 +00004382void TParseContext::parseNumViews(int intValue,
4383 const TSourceLoc &intValueLine,
4384 const std::string &intValueString,
4385 int *numViews)
4386{
4387 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4388 // specification.
4389 if (intValue < 1)
4390 {
4391 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4392 }
4393 *numViews = intValue;
4394}
4395
Shaob5cc1192017-07-06 10:47:20 +08004396void TParseContext::parseInvocations(int intValue,
4397 const TSourceLoc &intValueLine,
4398 const std::string &intValueString,
4399 int *numInvocations)
4400{
4401 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4402 // it doesn't make sense to accept invocations <= 0.
4403 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4404 {
4405 error(intValueLine,
4406 "out of range: invocations must be in the range of [1, "
4407 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4408 intValueString.c_str());
4409 }
4410 else
4411 {
4412 *numInvocations = intValue;
4413 }
4414}
4415
4416void TParseContext::parseMaxVertices(int intValue,
4417 const TSourceLoc &intValueLine,
4418 const std::string &intValueString,
4419 int *maxVertices)
4420{
4421 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4422 // it doesn't make sense to accept max_vertices < 0.
4423 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4424 {
4425 error(
4426 intValueLine,
4427 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4428 intValueString.c_str());
4429 }
4430 else
4431 {
4432 *maxVertices = intValue;
4433 }
4434}
4435
Olli Etuahofbb1c792018-01-19 16:26:59 +02004436TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004437 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004438 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304439 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004440{
Jamie Madill2f294c92017-11-20 14:47:26 -05004441 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004442
Martin Radev802abe02016-08-04 17:48:32 +03004443 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004444
Martin Radev802abe02016-08-04 17:48:32 +03004445 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004446 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004447 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004448 if (intValue < 0)
4449 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004450 error(intValueLine, "out of range: location must be non-negative",
4451 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004452 }
4453 else
4454 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004455 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004456 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004457 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004458 }
Olli Etuaho43364892017-02-13 16:00:12 +00004459 else if (qualifierType == "binding")
4460 {
4461 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4462 if (intValue < 0)
4463 {
4464 error(intValueLine, "out of range: binding must be non-negative",
4465 intValueString.c_str());
4466 }
4467 else
4468 {
4469 qualifier.binding = intValue;
4470 }
4471 }
jchen104cdac9e2017-05-08 11:01:20 +08004472 else if (qualifierType == "offset")
4473 {
4474 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4475 if (intValue < 0)
4476 {
4477 error(intValueLine, "out of range: offset must be non-negative",
4478 intValueString.c_str());
4479 }
4480 else
4481 {
4482 qualifier.offset = intValue;
4483 }
4484 }
Martin Radev802abe02016-08-04 17:48:32 +03004485 else if (qualifierType == "local_size_x")
4486 {
4487 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4488 &qualifier.localSize);
4489 }
4490 else if (qualifierType == "local_size_y")
4491 {
4492 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4493 &qualifier.localSize);
4494 }
4495 else if (qualifierType == "local_size_z")
4496 {
4497 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4498 &qualifier.localSize);
4499 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004500 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004501 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004502 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4503 {
4504 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4505 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004506 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004507 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4508 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004509 {
4510 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4511 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004512 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4513 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004514 {
4515 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4516 }
4517
Martin Radev802abe02016-08-04 17:48:32 +03004518 else
4519 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004520 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004521 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004522
Jamie Madilla5efff92013-06-06 11:56:47 -04004523 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004524}
4525
Olli Etuaho613b9592016-09-05 12:05:53 +03004526TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4527{
4528 return new TTypeQualifierBuilder(
4529 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4530 mShaderVersion);
4531}
4532
Olli Etuahocce89652017-06-19 16:04:09 +03004533TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4534 const TSourceLoc &loc)
4535{
4536 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4537 return new TStorageQualifierWrapper(qualifier, loc);
4538}
4539
4540TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4541{
4542 if (getShaderType() == GL_VERTEX_SHADER)
4543 {
4544 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4545 }
4546 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4547}
4548
4549TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4550{
4551 if (declaringFunction())
4552 {
4553 return new TStorageQualifierWrapper(EvqIn, loc);
4554 }
Shaob5cc1192017-07-06 10:47:20 +08004555
4556 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004557 {
Shaob5cc1192017-07-06 10:47:20 +08004558 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004559 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004560 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004561 {
4562 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4563 }
4564 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004565 }
Shaob5cc1192017-07-06 10:47:20 +08004566 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004567 {
Shaob5cc1192017-07-06 10:47:20 +08004568 if (mShaderVersion < 300)
4569 {
4570 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4571 }
4572 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004573 }
Shaob5cc1192017-07-06 10:47:20 +08004574 case GL_COMPUTE_SHADER:
4575 {
4576 return new TStorageQualifierWrapper(EvqComputeIn, 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(EvqGeometryIn, 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::parseOutQualifier(const TSourceLoc &loc)
4591{
4592 if (declaringFunction())
4593 {
4594 return new TStorageQualifierWrapper(EvqOut, loc);
4595 }
Shaob5cc1192017-07-06 10:47:20 +08004596 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004597 {
Shaob5cc1192017-07-06 10:47:20 +08004598 case GL_VERTEX_SHADER:
4599 {
4600 if (mShaderVersion < 300)
4601 {
4602 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4603 }
4604 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4605 }
4606 case GL_FRAGMENT_SHADER:
4607 {
4608 if (mShaderVersion < 300)
4609 {
4610 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4611 }
4612 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4613 }
4614 case GL_COMPUTE_SHADER:
4615 {
4616 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4617 return new TStorageQualifierWrapper(EvqLast, loc);
4618 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004619 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004620 {
4621 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4622 }
4623 default:
4624 {
4625 UNREACHABLE();
4626 return new TStorageQualifierWrapper(EvqLast, loc);
4627 }
Olli Etuahocce89652017-06-19 16:04:09 +03004628 }
Olli Etuahocce89652017-06-19 16:04:09 +03004629}
4630
4631TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4632{
4633 if (!declaringFunction())
4634 {
4635 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4636 }
4637 return new TStorageQualifierWrapper(EvqInOut, loc);
4638}
4639
Jamie Madillb98c3a82015-07-23 14:26:04 -04004640TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004641 TLayoutQualifier rightQualifier,
4642 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004643{
Martin Radevc28888b2016-07-22 15:27:42 +03004644 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004645 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004646}
4647
Olli Etuahofbb1c792018-01-19 16:26:59 +02004648TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4649 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004650{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004651 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004652 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004653}
4654
Olli Etuahofbb1c792018-01-19 16:26:59 +02004655TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004656 const TSourceLoc &loc,
4657 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004658{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004659 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004660 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004661}
4662
Olli Etuaho722bfb52017-10-26 17:00:11 +03004663void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4664 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004665 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004666 const TSourceLoc &location)
4667{
4668 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4669 {
4670 if ((*fieldIter)->name() == name)
4671 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004672 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004673 }
4674 }
4675}
4676
4677TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4678{
4679 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4680 ++fieldIter)
4681 {
4682 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4683 location);
4684 }
4685 return fields;
4686}
4687
Olli Etuaho4de340a2016-12-16 09:32:03 +00004688TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4689 const TFieldList *newlyAddedFields,
4690 const TSourceLoc &location)
4691{
4692 for (TField *field : *newlyAddedFields)
4693 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004694 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4695 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004696 processedFields->push_back(field);
4697 }
4698 return processedFields;
4699}
4700
Martin Radev70866b82016-07-22 15:27:42 +03004701TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4702 const TTypeQualifierBuilder &typeQualifierBuilder,
4703 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004704 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004705{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004706 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004707
Martin Radev70866b82016-07-22 15:27:42 +03004708 typeSpecifier->qualifier = typeQualifier.qualifier;
4709 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004710 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004711 typeSpecifier->invariant = typeQualifier.invariant;
4712 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304713 {
Martin Radev70866b82016-07-22 15:27:42 +03004714 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004715 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004716 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004717}
4718
Jamie Madillb98c3a82015-07-23 14:26:04 -04004719TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004720 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004721{
Martin Radev4a9cd802016-09-01 16:51:51 +03004722 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4723 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004724
Olli Etuahofbb1c792018-01-19 16:26:59 +02004725 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004726 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004727
Martin Radev4a9cd802016-09-01 16:51:51 +03004728 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004729
Olli Etuahod5f44c92017-11-29 17:15:40 +02004730 TFieldList *fieldList = new TFieldList();
4731
4732 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304733 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004734 TType *type = new TType(typeSpecifier);
4735 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304736 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004737 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004738 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004739 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004740 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004741
Olli Etuahod5f44c92017-11-29 17:15:40 +02004742 TField *field = new TField(type, declarator->name(), declarator->line());
4743 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4744 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004745 }
4746
Olli Etuahod5f44c92017-11-29 17:15:40 +02004747 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004748}
4749
Martin Radev4a9cd802016-09-01 16:51:51 +03004750TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4751 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004752 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004753 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004754{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004755 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004756 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004757 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004758 structSymbolType = SymbolType::Empty;
4759 }
4760 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004761
Jamie Madill9b820842015-02-12 10:40:10 -05004762 // Store a bool in the struct if we're at global scope, to allow us to
4763 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004764 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004765
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004766 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004767 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004768 checkIsNotReserved(nameLine, structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004769 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304770 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004771 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004772 }
4773 }
4774
4775 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004776 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004777 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004778 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004779 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004780 switch (qualifier)
4781 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004782 case EvqGlobal:
4783 case EvqTemporary:
4784 break;
4785 default:
4786 error(field.line(), "invalid qualifier on struct member",
4787 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004788 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004789 }
Martin Radev70866b82016-07-22 15:27:42 +03004790 if (field.type()->isInvariant())
4791 {
4792 error(field.line(), "invalid qualifier on struct member", "invariant");
4793 }
jchen104cdac9e2017-05-08 11:01:20 +08004794 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4795 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004796 {
4797 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4798 }
4799
Olli Etuahoebee5b32017-11-23 12:56:32 +02004800 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004801 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004802
Olli Etuaho43364892017-02-13 16:00:12 +00004803 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4804
4805 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004806
4807 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004808 }
4809
Martin Radev4a9cd802016-09-01 16:51:51 +03004810 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004811 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004812 exitStructDeclaration();
4813
Martin Radev4a9cd802016-09-01 16:51:51 +03004814 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004815}
4816
Jamie Madillb98c3a82015-07-23 14:26:04 -04004817TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004818 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004819 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004820{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004821 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004822 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004823 init->isVector())
4824 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004825 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4826 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004827 return nullptr;
4828 }
4829
Olli Etuaho923ecef2017-10-11 12:01:38 +03004830 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004831 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004832 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004833 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004834 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004835 }
4836
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004837 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4838 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004839 return node;
4840}
4841
4842TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4843{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004844 if (mSwitchNestingLevel == 0)
4845 {
4846 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004847 return nullptr;
4848 }
4849 if (condition == nullptr)
4850 {
4851 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004852 return nullptr;
4853 }
4854 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004855 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004856 {
4857 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004858 }
4859 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004860 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4861 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4862 // fold in case labels.
4863 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004864 {
4865 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004866 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004867 TIntermCase *node = new TIntermCase(condition);
4868 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004869 return node;
4870}
4871
4872TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4873{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004874 if (mSwitchNestingLevel == 0)
4875 {
4876 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004877 return nullptr;
4878 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004879 TIntermCase *node = new TIntermCase(nullptr);
4880 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004881 return node;
4882}
4883
Jamie Madillb98c3a82015-07-23 14:26:04 -04004884TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4885 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004886 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004887{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004888 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004889
4890 switch (op)
4891 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004892 case EOpLogicalNot:
4893 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4894 child->isVector())
4895 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004896 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004897 return nullptr;
4898 }
4899 break;
4900 case EOpBitwiseNot:
4901 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4902 child->isMatrix() || child->isArray())
4903 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004904 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004905 return nullptr;
4906 }
4907 break;
4908 case EOpPostIncrement:
4909 case EOpPreIncrement:
4910 case EOpPostDecrement:
4911 case EOpPreDecrement:
4912 case EOpNegative:
4913 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004914 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4915 child->getBasicType() == EbtBool || child->isArray() ||
4916 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004917 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004918 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004919 return nullptr;
4920 }
4921 // Operators for built-ins are already type checked against their prototype.
4922 default:
4923 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004924 }
4925
Jiajia Qinbc585152017-06-23 15:42:17 +08004926 if (child->getMemoryQualifier().writeonly)
4927 {
4928 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4929 return nullptr;
4930 }
4931
Olli Etuahof119a262016-08-19 15:54:22 +03004932 TIntermUnary *node = new TIntermUnary(op, child);
4933 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004934
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004935 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004936}
4937
Olli Etuaho09b22472015-02-11 11:47:26 +02004938TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4939{
Olli Etuahocce89652017-06-19 16:04:09 +03004940 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004941 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004942 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004943 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004944 return child;
4945 }
4946 return node;
4947}
4948
Jamie Madillb98c3a82015-07-23 14:26:04 -04004949TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4950 TIntermTyped *child,
4951 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004952{
Olli Etuaho856c4972016-08-08 11:38:39 +03004953 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004954 return addUnaryMath(op, child, loc);
4955}
4956
Olli Etuaho765924f2018-01-04 12:48:36 +02004957TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4958{
4959 // If we can, we should return the folded version of the expression for subsequent parsing. This
4960 // enables folding the containing expression during parsing as well, instead of the separate
4961 // FoldExpressions() step where folding nested expressions requires multiple full AST
4962 // traversals.
4963
4964 // Even if folding fails the fold() functions return some node representing the expression,
4965 // typically the original node. So "folded" can be assumed to be non-null.
4966 TIntermTyped *folded = expression->fold(mDiagnostics);
4967 ASSERT(folded != nullptr);
4968 if (folded->getQualifier() == expression->getQualifier())
4969 {
4970 // We need this expression to have the correct qualifier when validating the consuming
4971 // expression. So we can only return the folded node from here in case it has the same
4972 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4973 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4974 // 1. (true ? 1.0 : non_constant)
4975 // 2. (non_constant, 1.0)
4976 return folded;
4977 }
4978 return expression;
4979}
4980
Jamie Madillb98c3a82015-07-23 14:26:04 -04004981bool TParseContext::binaryOpCommonCheck(TOperator op,
4982 TIntermTyped *left,
4983 TIntermTyped *right,
4984 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004985{
jchen10b4cf5652017-05-05 18:51:17 +08004986 // Check opaque types are not allowed to be operands in expressions other than array indexing
4987 // and structure member selection.
4988 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4989 {
4990 switch (op)
4991 {
4992 case EOpIndexDirect:
4993 case EOpIndexIndirect:
4994 break;
4995 case EOpIndexDirectStruct:
4996 UNREACHABLE();
4997
4998 default:
4999 error(loc, "Invalid operation for variables with an opaque type",
5000 GetOperatorString(op));
5001 return false;
5002 }
5003 }
jchen10cc2a10e2017-05-03 14:05:12 +08005004
Jiajia Qinbc585152017-06-23 15:42:17 +08005005 if (right->getMemoryQualifier().writeonly)
5006 {
5007 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5008 return false;
5009 }
5010
5011 if (left->getMemoryQualifier().writeonly)
5012 {
5013 switch (op)
5014 {
5015 case EOpAssign:
5016 case EOpInitialize:
5017 case EOpIndexDirect:
5018 case EOpIndexIndirect:
5019 case EOpIndexDirectStruct:
5020 case EOpIndexDirectInterfaceBlock:
5021 break;
5022 default:
5023 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5024 return false;
5025 }
5026 }
5027
Olli Etuaho244be012016-08-18 15:26:02 +03005028 if (left->getType().getStruct() || right->getType().getStruct())
5029 {
5030 switch (op)
5031 {
5032 case EOpIndexDirectStruct:
5033 ASSERT(left->getType().getStruct());
5034 break;
5035 case EOpEqual:
5036 case EOpNotEqual:
5037 case EOpAssign:
5038 case EOpInitialize:
5039 if (left->getType() != right->getType())
5040 {
5041 return false;
5042 }
5043 break;
5044 default:
5045 error(loc, "Invalid operation for structs", GetOperatorString(op));
5046 return false;
5047 }
5048 }
5049
Olli Etuaho94050052017-05-08 14:17:44 +03005050 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5051 {
5052 switch (op)
5053 {
5054 case EOpIndexDirectInterfaceBlock:
5055 ASSERT(left->getType().getInterfaceBlock());
5056 break;
5057 default:
5058 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5059 return false;
5060 }
5061 }
5062
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005063 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005064 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005065 error(loc, "array / non-array mismatch", GetOperatorString(op));
5066 return false;
5067 }
5068
5069 if (left->isArray())
5070 {
5071 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005072 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005073 {
5074 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5075 return false;
5076 }
5077
Olli Etuahoe79904c2015-03-18 16:56:42 +02005078 switch (op)
5079 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005080 case EOpEqual:
5081 case EOpNotEqual:
5082 case EOpAssign:
5083 case EOpInitialize:
5084 break;
5085 default:
5086 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5087 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005088 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005089 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005090 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005091 {
5092 error(loc, "array size mismatch", GetOperatorString(op));
5093 return false;
5094 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005095 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005096
5097 // Check ops which require integer / ivec parameters
5098 bool isBitShift = false;
5099 switch (op)
5100 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005101 case EOpBitShiftLeft:
5102 case EOpBitShiftRight:
5103 case EOpBitShiftLeftAssign:
5104 case EOpBitShiftRightAssign:
5105 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5106 // check that the basic type is an integer type.
5107 isBitShift = true;
5108 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5109 {
5110 return false;
5111 }
5112 break;
5113 case EOpBitwiseAnd:
5114 case EOpBitwiseXor:
5115 case EOpBitwiseOr:
5116 case EOpBitwiseAndAssign:
5117 case EOpBitwiseXorAssign:
5118 case EOpBitwiseOrAssign:
5119 // It is enough to check the type of only one operand, since later it
5120 // is checked that the operand types match.
5121 if (!IsInteger(left->getBasicType()))
5122 {
5123 return false;
5124 }
5125 break;
5126 default:
5127 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005128 }
5129
5130 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5131 // So the basic type should usually match.
5132 if (!isBitShift && left->getBasicType() != right->getBasicType())
5133 {
5134 return false;
5135 }
5136
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005137 // Check that:
5138 // 1. Type sizes match exactly on ops that require that.
5139 // 2. Restrictions for structs that contain arrays or samplers are respected.
5140 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005141 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005142 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005143 case EOpAssign:
5144 case EOpInitialize:
5145 case EOpEqual:
5146 case EOpNotEqual:
5147 // ESSL 1.00 sections 5.7, 5.8, 5.9
5148 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5149 {
5150 error(loc, "undefined operation for structs containing arrays",
5151 GetOperatorString(op));
5152 return false;
5153 }
5154 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5155 // we interpret the spec so that this extends to structs containing samplers,
5156 // similarly to ESSL 1.00 spec.
5157 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5158 left->getType().isStructureContainingSamplers())
5159 {
5160 error(loc, "undefined operation for structs containing samplers",
5161 GetOperatorString(op));
5162 return false;
5163 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005164
Olli Etuahoe1805592017-01-02 16:41:20 +00005165 if ((left->getNominalSize() != right->getNominalSize()) ||
5166 (left->getSecondarySize() != right->getSecondarySize()))
5167 {
5168 error(loc, "dimension mismatch", GetOperatorString(op));
5169 return false;
5170 }
5171 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005172 case EOpLessThan:
5173 case EOpGreaterThan:
5174 case EOpLessThanEqual:
5175 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005176 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005177 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005178 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005179 return false;
5180 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005181 break;
5182 case EOpAdd:
5183 case EOpSub:
5184 case EOpDiv:
5185 case EOpIMod:
5186 case EOpBitShiftLeft:
5187 case EOpBitShiftRight:
5188 case EOpBitwiseAnd:
5189 case EOpBitwiseXor:
5190 case EOpBitwiseOr:
5191 case EOpAddAssign:
5192 case EOpSubAssign:
5193 case EOpDivAssign:
5194 case EOpIModAssign:
5195 case EOpBitShiftLeftAssign:
5196 case EOpBitShiftRightAssign:
5197 case EOpBitwiseAndAssign:
5198 case EOpBitwiseXorAssign:
5199 case EOpBitwiseOrAssign:
5200 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5201 {
5202 return false;
5203 }
5204
5205 // Are the sizes compatible?
5206 if (left->getNominalSize() != right->getNominalSize() ||
5207 left->getSecondarySize() != right->getSecondarySize())
5208 {
5209 // If the nominal sizes of operands do not match:
5210 // One of them must be a scalar.
5211 if (!left->isScalar() && !right->isScalar())
5212 return false;
5213
5214 // In the case of compound assignment other than multiply-assign,
5215 // the right side needs to be a scalar. Otherwise a vector/matrix
5216 // would be assigned to a scalar. A scalar can't be shifted by a
5217 // vector either.
5218 if (!right->isScalar() &&
5219 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5220 return false;
5221 }
5222 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005223 default:
5224 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005225 }
5226
Olli Etuahod6b14282015-03-17 14:31:35 +02005227 return true;
5228}
5229
Olli Etuaho1dded802016-08-18 18:13:13 +03005230bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5231 const TType &left,
5232 const TType &right)
5233{
5234 switch (op)
5235 {
5236 case EOpMul:
5237 case EOpMulAssign:
5238 return left.getNominalSize() == right.getNominalSize() &&
5239 left.getSecondarySize() == right.getSecondarySize();
5240 case EOpVectorTimesScalar:
5241 return true;
5242 case EOpVectorTimesScalarAssign:
5243 ASSERT(!left.isMatrix() && !right.isMatrix());
5244 return left.isVector() && !right.isVector();
5245 case EOpVectorTimesMatrix:
5246 return left.getNominalSize() == right.getRows();
5247 case EOpVectorTimesMatrixAssign:
5248 ASSERT(!left.isMatrix() && right.isMatrix());
5249 return left.isVector() && left.getNominalSize() == right.getRows() &&
5250 left.getNominalSize() == right.getCols();
5251 case EOpMatrixTimesVector:
5252 return left.getCols() == right.getNominalSize();
5253 case EOpMatrixTimesScalar:
5254 return true;
5255 case EOpMatrixTimesScalarAssign:
5256 ASSERT(left.isMatrix() && !right.isMatrix());
5257 return !right.isVector();
5258 case EOpMatrixTimesMatrix:
5259 return left.getCols() == right.getRows();
5260 case EOpMatrixTimesMatrixAssign:
5261 ASSERT(left.isMatrix() && right.isMatrix());
5262 // We need to check two things:
5263 // 1. The matrix multiplication step is valid.
5264 // 2. The result will have the same number of columns as the lvalue.
5265 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5266
5267 default:
5268 UNREACHABLE();
5269 return false;
5270 }
5271}
5272
Jamie Madillb98c3a82015-07-23 14:26:04 -04005273TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5274 TIntermTyped *left,
5275 TIntermTyped *right,
5276 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005277{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005278 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005279 return nullptr;
5280
Olli Etuahofc1806e2015-03-17 13:03:11 +02005281 switch (op)
5282 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005283 case EOpEqual:
5284 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005285 case EOpLessThan:
5286 case EOpGreaterThan:
5287 case EOpLessThanEqual:
5288 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005289 break;
5290 case EOpLogicalOr:
5291 case EOpLogicalXor:
5292 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005293 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5294 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005295 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005296 {
5297 return nullptr;
5298 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005299 // Basic types matching should have been already checked.
5300 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005301 break;
5302 case EOpAdd:
5303 case EOpSub:
5304 case EOpDiv:
5305 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005306 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5307 !right->getType().getStruct());
5308 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005309 {
5310 return nullptr;
5311 }
5312 break;
5313 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005314 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5315 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005316 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005317 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005318 {
5319 return nullptr;
5320 }
5321 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005322 default:
5323 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005324 }
5325
Olli Etuaho1dded802016-08-18 18:13:13 +03005326 if (op == EOpMul)
5327 {
5328 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5329 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5330 {
5331 return nullptr;
5332 }
5333 }
5334
Olli Etuaho3fdec912016-08-18 15:08:06 +03005335 TIntermBinary *node = new TIntermBinary(op, left, right);
5336 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005337 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005338}
5339
Jamie Madillb98c3a82015-07-23 14:26:04 -04005340TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5341 TIntermTyped *left,
5342 TIntermTyped *right,
5343 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005344{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005345 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005346 if (node == 0)
5347 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005348 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5349 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005350 return left;
5351 }
5352 return node;
5353}
5354
Jamie Madillb98c3a82015-07-23 14:26:04 -04005355TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5356 TIntermTyped *left,
5357 TIntermTyped *right,
5358 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005359{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005360 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005361 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005362 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005363 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5364 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005365 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005366 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005367 }
5368 return node;
5369}
5370
Olli Etuaho13389b62016-10-16 11:48:18 +01005371TIntermBinary *TParseContext::createAssign(TOperator op,
5372 TIntermTyped *left,
5373 TIntermTyped *right,
5374 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005375{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005376 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005377 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005378 if (op == EOpMulAssign)
5379 {
5380 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5381 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5382 {
5383 return nullptr;
5384 }
5385 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005386 TIntermBinary *node = new TIntermBinary(op, left, right);
5387 node->setLine(loc);
5388
Olli Etuaho3fdec912016-08-18 15:08:06 +03005389 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005390 }
5391 return nullptr;
5392}
5393
Jamie Madillb98c3a82015-07-23 14:26:04 -04005394TIntermTyped *TParseContext::addAssign(TOperator op,
5395 TIntermTyped *left,
5396 TIntermTyped *right,
5397 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005398{
Olli Etuahocce89652017-06-19 16:04:09 +03005399 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005400 TIntermTyped *node = createAssign(op, left, right, loc);
5401 if (node == nullptr)
5402 {
5403 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005404 return left;
5405 }
5406 return node;
5407}
5408
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005409TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5410 TIntermTyped *right,
5411 const TSourceLoc &loc)
5412{
Corentin Wallez0d959252016-07-12 17:26:32 -04005413 // WebGL2 section 5.26, the following results in an error:
5414 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005415 if (mShaderSpec == SH_WEBGL2_SPEC &&
5416 (left->isArray() || left->getBasicType() == EbtVoid ||
5417 left->getType().isStructureContainingArrays() || right->isArray() ||
5418 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005419 {
5420 error(loc,
5421 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5422 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005423 }
5424
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005425 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho765924f2018-01-04 12:48:36 +02005426
5427 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005428}
5429
Olli Etuaho49300862015-02-20 14:54:49 +02005430TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5431{
5432 switch (op)
5433 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005434 case EOpContinue:
5435 if (mLoopNestingLevel <= 0)
5436 {
5437 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005438 }
5439 break;
5440 case EOpBreak:
5441 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5442 {
5443 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005444 }
5445 break;
5446 case EOpReturn:
5447 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5448 {
5449 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005450 }
5451 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005452 case EOpKill:
5453 if (mShaderType != GL_FRAGMENT_SHADER)
5454 {
5455 error(loc, "discard supported in fragment shaders only", "discard");
5456 }
5457 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005458 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005459 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005460 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005461 }
Olli Etuahocce89652017-06-19 16:04:09 +03005462 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005463}
5464
Jamie Madillb98c3a82015-07-23 14:26:04 -04005465TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005466 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005467 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005468{
Olli Etuahocce89652017-06-19 16:04:09 +03005469 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005470 {
Olli Etuahocce89652017-06-19 16:04:09 +03005471 ASSERT(op == EOpReturn);
5472 mFunctionReturnsValue = true;
5473 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5474 {
5475 error(loc, "void function cannot return a value", "return");
5476 }
5477 else if (*mCurrentFunctionType != expression->getType())
5478 {
5479 error(loc, "function return is not matching type:", "return");
5480 }
Olli Etuaho49300862015-02-20 14:54:49 +02005481 }
Olli Etuahocce89652017-06-19 16:04:09 +03005482 TIntermBranch *node = new TIntermBranch(op, expression);
5483 node->setLine(loc);
5484 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005485}
5486
Martin Radev84aa2dc2017-09-11 15:51:02 +03005487void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5488{
5489 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005490 const ImmutableString &name = functionCall->getFunction()->name();
5491 bool isTextureGather = name == kTextureGatherName;
5492 bool isTextureGatherOffset = name == kTextureGatherOffsetName;
Martin Radev84aa2dc2017-09-11 15:51:02 +03005493 if (isTextureGather || isTextureGatherOffset)
5494 {
5495 TIntermNode *componentNode = nullptr;
5496 TIntermSequence *arguments = functionCall->getSequence();
5497 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5498 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5499 ASSERT(sampler != nullptr);
5500 switch (sampler->getBasicType())
5501 {
5502 case EbtSampler2D:
5503 case EbtISampler2D:
5504 case EbtUSampler2D:
5505 case EbtSampler2DArray:
5506 case EbtISampler2DArray:
5507 case EbtUSampler2DArray:
5508 if ((isTextureGather && arguments->size() == 3u) ||
5509 (isTextureGatherOffset && arguments->size() == 4u))
5510 {
5511 componentNode = arguments->back();
5512 }
5513 break;
5514 case EbtSamplerCube:
5515 case EbtISamplerCube:
5516 case EbtUSamplerCube:
5517 ASSERT(!isTextureGatherOffset);
5518 if (arguments->size() == 3u)
5519 {
5520 componentNode = arguments->back();
5521 }
5522 break;
5523 case EbtSampler2DShadow:
5524 case EbtSampler2DArrayShadow:
5525 case EbtSamplerCubeShadow:
5526 break;
5527 default:
5528 UNREACHABLE();
5529 break;
5530 }
5531 if (componentNode)
5532 {
5533 const TIntermConstantUnion *componentConstantUnion =
5534 componentNode->getAsConstantUnion();
5535 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5536 {
5537 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuahofbb1c792018-01-19 16:26:59 +02005538 name);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005539 }
5540 else
5541 {
5542 int component = componentConstantUnion->getIConst(0);
5543 if (component < 0 || component > 3)
5544 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005545 error(functionCall->getLine(), "Component must be in the range [0;3]", name);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005546 }
5547 }
5548 }
5549 }
5550}
5551
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005552void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5553{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005554 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005555 const ImmutableString &name = functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005556 TIntermNode *offset = nullptr;
5557 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005558 bool useTextureGatherOffsetConstraints = false;
Olli Etuahofbb1c792018-01-19 16:26:59 +02005559 if (name == kTexelFetchOffsetName || name == kTextureLodOffsetName ||
5560 name == kTextureProjLodOffsetName || name == kTextureGradOffsetName ||
5561 name == kTextureProjGradOffsetName)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005562 {
5563 offset = arguments->back();
5564 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005565 else if (name == kTextureOffsetName || name == kTextureProjOffsetName)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005566 {
5567 // A bias parameter might follow the offset parameter.
5568 ASSERT(arguments->size() >= 3);
5569 offset = (*arguments)[2];
5570 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005571 else if (name == kTextureGatherOffsetName)
Martin Radev84aa2dc2017-09-11 15:51:02 +03005572 {
5573 ASSERT(arguments->size() >= 3u);
5574 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5575 ASSERT(sampler != nullptr);
5576 switch (sampler->getBasicType())
5577 {
5578 case EbtSampler2D:
5579 case EbtISampler2D:
5580 case EbtUSampler2D:
5581 case EbtSampler2DArray:
5582 case EbtISampler2DArray:
5583 case EbtUSampler2DArray:
5584 offset = (*arguments)[2];
5585 break;
5586 case EbtSampler2DShadow:
5587 case EbtSampler2DArrayShadow:
5588 offset = (*arguments)[3];
5589 break;
5590 default:
5591 UNREACHABLE();
5592 break;
5593 }
5594 useTextureGatherOffsetConstraints = true;
5595 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005596 if (offset != nullptr)
5597 {
5598 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5599 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5600 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005601 error(functionCall->getLine(), "Texture offset must be a constant expression", name);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005602 }
5603 else
5604 {
5605 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5606 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005607 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005608 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5609 : mMinProgramTexelOffset;
5610 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5611 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005612 for (size_t i = 0u; i < size; ++i)
5613 {
5614 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005615 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005616 {
5617 std::stringstream tokenStream;
5618 tokenStream << offsetValue;
5619 std::string token = tokenStream.str();
5620 error(offset->getLine(), "Texture offset value out of valid range",
5621 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005622 }
5623 }
5624 }
5625 }
5626}
5627
Jiajia Qina3106c52017-11-03 09:39:39 +08005628void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5629{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005630 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005631 const ImmutableString &functionName = functionCall->getFunction()->name();
Olli Etuahobed35d72017-12-20 16:36:26 +02005632 if (IsAtomicBuiltin(functionName))
Jiajia Qina3106c52017-11-03 09:39:39 +08005633 {
5634 TIntermSequence *arguments = functionCall->getSequence();
5635 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5636
5637 if (IsBufferOrSharedVariable(memNode))
5638 {
5639 return;
5640 }
5641
5642 while (memNode->getAsBinaryNode())
5643 {
5644 memNode = memNode->getAsBinaryNode()->getLeft();
5645 if (IsBufferOrSharedVariable(memNode))
5646 {
5647 return;
5648 }
5649 }
5650
5651 error(memNode->getLine(),
5652 "The value passed to the mem argument of an atomic memory function does not "
5653 "correspond to a buffer or shared variable.",
Olli Etuahofbb1c792018-01-19 16:26:59 +02005654 functionName);
Jiajia Qina3106c52017-11-03 09:39:39 +08005655 }
5656}
5657
Martin Radev2cc85b32016-08-05 16:22:53 +03005658// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5659void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5660{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005661 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005662
Olli Etuahofbb1c792018-01-19 16:26:59 +02005663 if (functionCall->getFunction()->isImageFunction())
Martin Radev2cc85b32016-08-05 16:22:53 +03005664 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005665 const ImmutableString &name = functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005666 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005667 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005668
Olli Etuaho485eefd2017-02-14 17:40:06 +00005669 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005670
Olli Etuahofbb1c792018-01-19 16:26:59 +02005671 if (strcmp(name.data() + 5u, "Store") == 0)
Martin Radev2cc85b32016-08-05 16:22:53 +03005672 {
5673 if (memoryQualifier.readonly)
5674 {
5675 error(imageNode->getLine(),
5676 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005677 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005678 }
5679 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005680 else if (strcmp(name.data() + 5u, "Load") == 0)
Martin Radev2cc85b32016-08-05 16:22:53 +03005681 {
5682 if (memoryQualifier.writeonly)
5683 {
5684 error(imageNode->getLine(),
5685 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005686 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005687 }
5688 }
5689 }
5690}
5691
5692// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5693void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5694 const TFunction *functionDefinition,
5695 const TIntermAggregate *functionCall)
5696{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005697 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005698
5699 const TIntermSequence &arguments = *functionCall->getSequence();
5700
5701 ASSERT(functionDefinition->getParamCount() == arguments.size());
5702
5703 for (size_t i = 0; i < arguments.size(); ++i)
5704 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005705 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5706 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005707 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5708 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5709
5710 if (IsImage(functionArgumentType.getBasicType()))
5711 {
5712 const TMemoryQualifier &functionArgumentMemoryQualifier =
5713 functionArgumentType.getMemoryQualifier();
5714 const TMemoryQualifier &functionParameterMemoryQualifier =
5715 functionParameterType.getMemoryQualifier();
5716 if (functionArgumentMemoryQualifier.readonly &&
5717 !functionParameterMemoryQualifier.readonly)
5718 {
5719 error(functionCall->getLine(),
5720 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005721 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005722 }
5723
5724 if (functionArgumentMemoryQualifier.writeonly &&
5725 !functionParameterMemoryQualifier.writeonly)
5726 {
5727 error(functionCall->getLine(),
5728 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005729 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005730 }
Martin Radev049edfa2016-11-11 14:35:37 +02005731
5732 if (functionArgumentMemoryQualifier.coherent &&
5733 !functionParameterMemoryQualifier.coherent)
5734 {
5735 error(functionCall->getLine(),
5736 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005737 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005738 }
5739
5740 if (functionArgumentMemoryQualifier.volatileQualifier &&
5741 !functionParameterMemoryQualifier.volatileQualifier)
5742 {
5743 error(functionCall->getLine(),
5744 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005745 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005746 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005747 }
5748 }
5749}
5750
Olli Etuaho95ed1942018-02-01 14:01:19 +02005751TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005752{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005753 if (fnCall->thisNode() != nullptr)
5754 {
5755 return addMethod(fnCall, loc);
5756 }
5757 if (fnCall->isConstructor())
5758 {
5759 return addConstructor(fnCall, loc);
5760 }
5761 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005762}
5763
Olli Etuaho95ed1942018-02-01 14:01:19 +02005764TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005765{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005766 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005767 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5768 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5769 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005770 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005771 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005772 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005773 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005774 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005775 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005776 {
5777 error(loc, "method takes no parameters", "length");
5778 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005779 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005780 {
5781 error(loc, "length can only be called on arrays", "length");
5782 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005783 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005784 mGeometryShaderInputPrimitiveType == EptUndefined)
5785 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005786 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005787 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5788 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005789 else
5790 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005791 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005792 node->setLine(loc);
5793 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005794 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005795 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005796}
5797
Olli Etuaho95ed1942018-02-01 14:01:19 +02005798TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005799 const TSourceLoc &loc)
5800{
5801 // First find by unmangled name to check whether the function name has been
5802 // hidden by a variable name or struct typename.
5803 // If a function is found, check for one with a matching argument list.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005804 const TSymbol *symbol = symbolTable.find(fnCall->name(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005805 if (symbol != nullptr && !symbol->isFunction())
5806 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005807 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005808 }
5809 else
5810 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005811 symbol = symbolTable.find(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005812 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005813 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005814 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005815 }
5816 else
5817 {
5818 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005819 //
5820 // A declared function.
5821 //
Olli Etuaho37b697e2018-01-29 12:19:27 +02005822 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005823 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005824 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005825 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005826 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuaho95ed1942018-02-01 14:01:19 +02005827 if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005828 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005829 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005830 if (fnCandidate->getParamCount() == 1)
5831 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005832 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005833 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005834 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005835 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005836 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005837 }
5838 else
5839 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005840 TIntermAggregate *callNode =
Olli Etuaho95ed1942018-02-01 14:01:19 +02005841 TIntermAggregate::Create(*fnCandidate, op, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005842 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005843
5844 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005845 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305846
Olli Etuaho765924f2018-01-04 12:48:36 +02005847 // See if we can constant fold a built-in. Note that this may be possible
5848 // even if it is not const-qualified.
5849 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005850 }
5851 }
5852 else
5853 {
Olli Etuaho37b697e2018-01-29 12:19:27 +02005854 // This is a real function call.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005855 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005856
Olli Etuaho37b697e2018-01-29 12:19:27 +02005857 // If the symbol type is not BuiltIn, the function is user defined - could be an
5858 // overloaded built-in as well. if the symbol type is BuiltIn, it's a built-in
5859 // function with no op associated with it.
5860 if (fnCandidate->symbolType() == SymbolType::BuiltIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005861 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005862 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate,
5863 &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005864 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005865 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005866 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005867 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005868 }
5869 else
5870 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005871 callNode =
5872 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005873 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005874 }
5875
Jiajia Qinbc585152017-06-23 15:42:17 +08005876 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005877
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005878 callNode->setLine(loc);
5879
5880 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005881 }
5882 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005883 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005884
5885 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005886 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005887}
5888
Jamie Madillb98c3a82015-07-23 14:26:04 -04005889TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005890 TIntermTyped *trueExpression,
5891 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005892 const TSourceLoc &loc)
5893{
Olli Etuaho56229f12017-07-10 14:16:33 +03005894 if (!checkIsScalarBool(loc, cond))
5895 {
5896 return falseExpression;
5897 }
Olli Etuaho52901742015-04-15 13:42:45 +03005898
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005899 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005900 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005901 std::stringstream reasonStream;
5902 reasonStream << "mismatching ternary operator operand types '"
5903 << trueExpression->getCompleteString() << " and '"
5904 << falseExpression->getCompleteString() << "'";
5905 std::string reason = reasonStream.str();
5906 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005907 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005908 }
Olli Etuahode318b22016-10-25 16:18:25 +01005909 if (IsOpaqueType(trueExpression->getBasicType()))
5910 {
5911 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005912 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005913 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5914 // Note that structs containing opaque types don't need to be checked as structs are
5915 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005916 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005917 return falseExpression;
5918 }
5919
Jiajia Qinbc585152017-06-23 15:42:17 +08005920 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5921 falseExpression->getMemoryQualifier().writeonly)
5922 {
5923 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5924 return falseExpression;
5925 }
5926
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005927 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005928 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005929 // ESSL 3.00.6 section 5.7:
5930 // Ternary operator support is optional for arrays. No certainty that it works across all
5931 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5932 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005933 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005934 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005935 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005936 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005937 }
Olli Etuaho94050052017-05-08 14:17:44 +03005938 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5939 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005940 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005941 return falseExpression;
5942 }
5943
Corentin Wallez0d959252016-07-12 17:26:32 -04005944 // WebGL2 section 5.26, the following results in an error:
5945 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005946 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005947 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005948 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005949 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005950 }
5951
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005952 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5953 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005954 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005955}
Olli Etuaho49300862015-02-20 14:54:49 +02005956
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005957//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005958// Parse an array of strings using yyparse.
5959//
5960// Returns 0 for success.
5961//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005962int PaParseStrings(size_t count,
5963 const char *const string[],
5964 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305965 TParseContext *context)
5966{
Yunchao He4f285442017-04-21 12:15:49 +08005967 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005968 return 1;
5969
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005970 if (glslang_initialize(context))
5971 return 1;
5972
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005973 int error = glslang_scan(count, string, length, context);
5974 if (!error)
5975 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005976
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005977 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005978
alokp@chromium.org6b495712012-06-29 00:06:58 +00005979 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005980}
Jamie Madill45bcc782016-11-07 13:58:48 -05005981
5982} // namespace sh