blob: bdfafa4d4bb92bdaa574a77d99ad96e576f24f0d [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 Etuahodefe3932018-02-13 11:56:09 +02002642 symbolTable.addInvariantVarying(identifier);
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;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003231 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003232 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 Madill185fb402015-06-12 15:48:48 -04003285
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003286 bool wasDefined = false;
3287 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3288 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003289 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003290 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003291 }
Jamie Madill185fb402015-06-12 15:48:48 -04003292
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003293 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003294 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003295 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003296
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003297 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003298 setLoopNestingLevel(0);
3299}
3300
Jamie Madillb98c3a82015-07-23 14:26:04 -04003301TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003302{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003303 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003304 // We don't know at this point whether this is a function definition or a prototype.
3305 // The definition production code will check for redefinitions.
3306 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003307 //
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303308
Olli Etuahod80f2942017-11-06 12:44:45 +02003309 for (size_t i = 0u; i < function->getParamCount(); ++i)
3310 {
3311 auto &param = function->getParam(i);
3312 if (param.type->isStructSpecifier())
3313 {
3314 // ESSL 3.00.6 section 12.10.
3315 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003316 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003317 }
3318 }
3319
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003320 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303321 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003322 const UnmangledBuiltIn *builtIn =
3323 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3324 if (builtIn &&
3325 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3326 {
3327 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3328 // functions. Therefore overloading or redefining builtin functions is an error.
3329 error(location, "Name of a built-in function cannot be redeclared as function",
3330 function->name());
3331 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303332 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003333 else
3334 {
3335 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3336 // this applies to redeclarations as well.
3337 const TSymbol *builtIn =
3338 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3339 if (builtIn)
3340 {
3341 error(location, "built-in functions cannot be redefined", function->name());
3342 }
3343 }
3344
3345 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3346 // here.
3347 const TFunction *prevDec =
3348 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3349 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003350 {
3351 if (prevDec->getReturnType() != function->getReturnType())
3352 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003353 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003354 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003355 }
3356 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3357 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003358 if (prevDec->getParam(i).type->getQualifier() !=
3359 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003360 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003361 error(location,
3362 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003363 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003364 }
3365 }
3366 }
3367
Jamie Madill185fb402015-06-12 15:48:48 -04003368 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003369 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3370 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003371 if (prevSym)
3372 {
3373 if (!prevSym->isFunction())
3374 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003375 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003376 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003377 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003378 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003379 // Parsing is at the inner scope level of the function's arguments and body statement at this
3380 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3381 // scope.
3382 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003383
Olli Etuaho78d13742017-01-18 13:06:10 +00003384 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003385 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003386 {
3387 if (function->getParamCount() > 0)
3388 {
3389 error(location, "function cannot take any parameter(s)", "main");
3390 }
3391 if (function->getReturnType().getBasicType() != EbtVoid)
3392 {
3393 error(location, "main function cannot return a value",
3394 function->getReturnType().getBasicString());
3395 }
3396 }
3397
Jamie Madill185fb402015-06-12 15:48:48 -04003398 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003399 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3400 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003401 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3402 //
3403 return function;
3404}
3405
Olli Etuaho9de84a52016-06-14 17:36:01 +03003406TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003407 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003408 const TSourceLoc &location)
3409{
3410 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3411 {
3412 error(location, "no qualifiers allowed for function return",
3413 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003414 }
3415 if (!type.layoutQualifier.isEmpty())
3416 {
3417 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003418 }
jchen10cc2a10e2017-05-03 14:05:12 +08003419 // make sure an opaque type is not involved as well...
3420 std::string reason(getBasicString(type.getBasicType()));
3421 reason += "s can't be function return values";
3422 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003423 if (mShaderVersion < 300)
3424 {
3425 // Array return values are forbidden, but there's also no valid syntax for declaring array
3426 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003427 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003428
3429 if (type.isStructureContainingArrays())
3430 {
3431 // ESSL 1.00.17 section 6.1 Function Definitions
3432 error(location, "structures containing arrays can't be function return values",
3433 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003434 }
3435 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003436
3437 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003438 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003439}
3440
Olli Etuaho697bf652018-02-16 11:50:54 +02003441TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3442 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003443{
Olli Etuaho697bf652018-02-16 11:50:54 +02003444 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003445}
3446
Olli Etuaho95ed1942018-02-01 14:01:19 +02003447TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003448{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003449 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003450 {
3451 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3452 "[]");
3453 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003454 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003455 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003456 error(publicType.getLine(), "constructor can't be a structure definition",
3457 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003458 }
3459
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003460 TType *type = new TType(publicType);
3461 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003462 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003463 error(publicType.getLine(), "cannot construct this type",
3464 getBasicString(publicType.getBasicType()));
3465 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003466 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003467 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003468}
3469
Olli Etuaho55bde912017-10-25 13:41:13 +03003470void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3471 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003472 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003473 TType *arrayType)
3474{
3475 if (arrayType->isUnsizedArray())
3476 {
3477 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003478 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003479 }
3480}
3481
3482TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003483 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003484 const TSourceLoc &nameLoc)
3485{
Olli Etuaho55bde912017-10-25 13:41:13 +03003486 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003487 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003488 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003489 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003490 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003491 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003492 checkIsNotReserved(nameLoc, name);
3493 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003494 return param;
3495}
3496
Olli Etuaho55bde912017-10-25 13:41:13 +03003497TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003498 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003499 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003500{
Olli Etuaho55bde912017-10-25 13:41:13 +03003501 TType *type = new TType(publicType);
3502 return parseParameterDeclarator(type, name, nameLoc);
3503}
3504
Olli Etuahofbb1c792018-01-19 16:26:59 +02003505TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003506 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003507 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003508 const TSourceLoc &arrayLoc,
3509 TPublicType *elementType)
3510{
3511 checkArrayElementIsNotArray(arrayLoc, *elementType);
3512 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003513 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003514 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003515}
3516
Olli Etuaho95ed1942018-02-01 14:01:19 +02003517bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3518 const TIntermSequence &arguments,
3519 TType type,
3520 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003521{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003522 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003523 {
3524 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3525 return false;
3526 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003527 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003528 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003529 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003530 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003531 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3532 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003533 {
3534 error(line, "constructing from a non-dereferenced array", "constructor");
3535 return false;
3536 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003537 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003538 {
3539 if (dimensionalityFromElement == 1u)
3540 {
3541 error(line, "implicitly sized array of arrays constructor argument is not an array",
3542 "constructor");
3543 }
3544 else
3545 {
3546 error(line,
3547 "implicitly sized array of arrays constructor argument dimensionality is too "
3548 "low",
3549 "constructor");
3550 }
3551 return false;
3552 }
3553 }
3554 return true;
3555}
3556
Jamie Madillb98c3a82015-07-23 14:26:04 -04003557// This function is used to test for the correctness of the parameters passed to various constructor
3558// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003559//
Olli Etuaho856c4972016-08-08 11:38:39 +03003560// 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 +00003561//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003562TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003563{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003564 TType type = fnCall->constructorType();
3565 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003566 if (type.isUnsizedArray())
3567 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003568 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003569 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003570 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003571 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003572 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003573 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003574 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003575 if (type.getOutermostArraySize() == 0u)
3576 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003577 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003578 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003579 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003580 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003581 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003582 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003583 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003584 }
3585 }
3586 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003587 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003588
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003589 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003590 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003591 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003592 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003593
Olli Etuaho95ed1942018-02-01 14:01:19 +02003594 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003595 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003596
Olli Etuaho765924f2018-01-04 12:48:36 +02003597 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003598}
3599
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003600//
3601// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003602// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003603//
Olli Etuaho13389b62016-10-16 11:48:18 +01003604TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003605 const TTypeQualifierBuilder &typeQualifierBuilder,
3606 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003607 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003608 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003609 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003610 const TSourceLoc &instanceLine,
3611 TIntermTyped *arrayIndex,
3612 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003613{
Olli Etuaho856c4972016-08-08 11:38:39 +03003614 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003615
Olli Etuaho77ba4082016-12-16 12:01:18 +00003616 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003617
Jiajia Qinbc585152017-06-23 15:42:17 +08003618 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003619 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003620 error(typeQualifier.line,
3621 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3622 "3.10",
3623 getQualifierString(typeQualifier.qualifier));
3624 }
3625 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3626 {
3627 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003628 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003629 }
3630
Martin Radev70866b82016-07-22 15:27:42 +03003631 if (typeQualifier.invariant)
3632 {
3633 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3634 }
3635
Jiajia Qinbc585152017-06-23 15:42:17 +08003636 if (typeQualifier.qualifier != EvqBuffer)
3637 {
3638 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3639 }
Olli Etuaho43364892017-02-13 16:00:12 +00003640
jchen10af713a22017-04-19 09:10:56 +08003641 // add array index
3642 unsigned int arraySize = 0;
3643 if (arrayIndex != nullptr)
3644 {
3645 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3646 }
3647
3648 if (mShaderVersion < 310)
3649 {
3650 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3651 }
3652 else
3653 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003654 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3655 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003656 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003657
Andrei Volykhina5527072017-03-22 16:46:30 +03003658 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3659
Jamie Madill099c0f32013-06-20 11:55:52 -04003660 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003661 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003662 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3663 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003664
Jamie Madill099c0f32013-06-20 11:55:52 -04003665 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3666 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003667 if (typeQualifier.qualifier == EvqUniform)
3668 {
3669 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3670 }
3671 else if (typeQualifier.qualifier == EvqBuffer)
3672 {
3673 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3674 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003675 }
3676
Jamie Madill1566ef72013-06-20 11:55:54 -04003677 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3678 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003679 if (typeQualifier.qualifier == EvqUniform)
3680 {
3681 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3682 }
3683 else if (typeQualifier.qualifier == EvqBuffer)
3684 {
3685 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3686 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003687 }
3688
Olli Etuaho856c4972016-08-08 11:38:39 +03003689 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003690
Martin Radev2cc85b32016-08-05 16:22:53 +03003691 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3692
Jamie Madill98493dd2013-07-08 14:39:03 -04003693 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303694 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3695 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003696 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303697 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003698 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303699 {
jchen10cc2a10e2017-05-03 14:05:12 +08003700 std::string reason("unsupported type - ");
3701 reason += fieldType->getBasicString();
3702 reason += " types are not allowed in interface blocks";
3703 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003704 }
3705
Jamie Madill98493dd2013-07-08 14:39:03 -04003706 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003707 switch (qualifier)
3708 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003709 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003710 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003711 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003712 if (typeQualifier.qualifier == EvqBuffer)
3713 {
3714 error(field->line(), "invalid qualifier on shader storage block member",
3715 getQualifierString(qualifier));
3716 }
3717 break;
3718 case EvqBuffer:
3719 if (typeQualifier.qualifier == EvqUniform)
3720 {
3721 error(field->line(), "invalid qualifier on uniform block member",
3722 getQualifierString(qualifier));
3723 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003724 break;
3725 default:
3726 error(field->line(), "invalid qualifier on interface block member",
3727 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003728 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003729 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003730
Martin Radev70866b82016-07-22 15:27:42 +03003731 if (fieldType->isInvariant())
3732 {
3733 error(field->line(), "invalid qualifier on interface block member", "invariant");
3734 }
3735
Jamie Madilla5efff92013-06-06 11:56:47 -04003736 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003737 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003738 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003739 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003740
Jamie Madill98493dd2013-07-08 14:39:03 -04003741 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003742 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003743 error(field->line(), "invalid layout qualifier: cannot be used here",
3744 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003745 }
3746
Jamie Madill98493dd2013-07-08 14:39:03 -04003747 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003748 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003749 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003750 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003751 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003752 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003753 warning(field->line(),
3754 "extraneous layout qualifier: only has an effect on matrix types",
3755 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003756 }
3757
Jamie Madill98493dd2013-07-08 14:39:03 -04003758 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003759
Olli Etuahoebee5b32017-11-23 12:56:32 +02003760 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3761 typeQualifier.qualifier != EvqBuffer)
3762 {
3763 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3764 checkIsNotUnsizedArray(field->line(),
3765 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003766 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003767 }
3768
Jiajia Qinbc585152017-06-23 15:42:17 +08003769 if (typeQualifier.qualifier == EvqBuffer)
3770 {
3771 // set memory qualifiers
3772 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3773 // qualified with a memory qualifier, it is as if all of its members were declared with
3774 // the same memory qualifier.
3775 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3776 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3777 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3778 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3779 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3780 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3781 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3782 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3783 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3784 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3785 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003786 }
3787
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003788 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003789 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003790 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3791 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003792 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003793 }
3794
Olli Etuahob60d30f2018-01-16 12:31:06 +02003795 TType *interfaceBlockType =
3796 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003797 if (arrayIndex != nullptr)
3798 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003799 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003800 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003801
Olli Etuaho195be942017-12-04 23:40:14 +02003802 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003803 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3804 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003805 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003806 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003807 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003808
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003809 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003810 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003811 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003812 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3813 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003814 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003815 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003816
3817 // set parent pointer of the field variable
3818 fieldType->setInterfaceBlock(interfaceBlock);
3819
Olli Etuahob60d30f2018-01-16 12:31:06 +02003820 fieldType->setQualifier(typeQualifier.qualifier);
3821
Olli Etuaho195be942017-12-04 23:40:14 +02003822 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003823 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuahob60d30f2018-01-16 12:31:06 +02003824 if (!symbolTable.declareVariable(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303825 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003826 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003827 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003828 }
3829 }
3830 }
3831 else
3832 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003833 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003834
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003835 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003836 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303837 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003838 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003839 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003840 }
3841
Olli Etuaho195be942017-12-04 23:40:14 +02003842 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3843 blockSymbol->setLine(typeQualifier.line);
3844 TIntermDeclaration *declaration = new TIntermDeclaration();
3845 declaration->appendDeclarator(blockSymbol);
3846 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003847
3848 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003849 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003850}
3851
Olli Etuahofbb1c792018-01-19 16:26:59 +02003852void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3853 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003854{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003855 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003856
3857 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003858 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303859 if (mStructNestingLevel > 1)
3860 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003861 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003862 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003863}
3864
3865void TParseContext::exitStructDeclaration()
3866{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003867 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003868}
3869
Olli Etuaho8a176262016-08-16 14:23:01 +03003870void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003871{
Jamie Madillacb4b812016-11-07 13:50:29 -05003872 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303873 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003874 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003875 }
3876
Arun Patole7e7e68d2015-05-22 12:02:25 +05303877 if (field.type()->getBasicType() != EbtStruct)
3878 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003879 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003880 }
3881
3882 // We're already inside a structure definition at this point, so add
3883 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303884 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3885 {
Jamie Madill41a49272014-03-18 16:10:13 -04003886 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003887 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3888 {
3889 // This may happen in case there are nested struct definitions. While they are also
3890 // invalid GLSL, they don't cause a syntax error.
3891 reasonStream << "Struct nesting";
3892 }
3893 else
3894 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003895 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003896 }
3897 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003898 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003899 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003900 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003901 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003902}
3903
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003904//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003905// Parse an array index expression
3906//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003907TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3908 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303909 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003910{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003911 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3912 {
3913 if (baseExpression->getAsSymbolNode())
3914 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303915 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003916 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003917 }
3918 else
3919 {
3920 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3921 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003922
Olli Etuaho3ec75682017-07-05 17:02:55 +03003923 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003924 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003925
Jiawei Shaod8105a02017-08-08 09:54:36 +08003926 if (baseExpression->getQualifier() == EvqPerVertexIn)
3927 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003928 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003929 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3930 {
3931 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3932 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3933 }
3934 }
3935
Jamie Madill21c1e452014-12-29 11:33:41 -05003936 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3937
Olli Etuaho36b05142015-11-12 13:10:42 +02003938 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3939 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3940 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3941 // index is a constant expression.
3942 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3943 {
3944 if (baseExpression->isInterfaceBlock())
3945 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003946 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003947 switch (baseExpression->getQualifier())
3948 {
3949 case EvqPerVertexIn:
3950 break;
3951 case EvqUniform:
3952 case EvqBuffer:
3953 error(location,
3954 "array indexes for uniform block arrays and shader storage block arrays "
3955 "must be constant integral expressions",
3956 "[");
3957 break;
3958 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003959 // We can reach here only in error cases.
3960 ASSERT(mDiagnostics->numErrors() > 0);
3961 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003962 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003963 }
3964 else if (baseExpression->getQualifier() == EvqFragmentOut)
3965 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003966 error(location,
3967 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003968 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003969 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3970 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003971 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003972 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003973 }
3974
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003975 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003976 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003977 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3978 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3979 // constant fold expressions that are not constant expressions). The most compatible way to
3980 // handle this case is to report a warning instead of an error and force the index to be in
3981 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003982 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003983 int index = 0;
3984 if (indexConstantUnion->getBasicType() == EbtInt)
3985 {
3986 index = indexConstantUnion->getIConst(0);
3987 }
3988 else if (indexConstantUnion->getBasicType() == EbtUInt)
3989 {
3990 index = static_cast<int>(indexConstantUnion->getUConst(0));
3991 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003992
3993 int safeIndex = -1;
3994
Olli Etuahoebee5b32017-11-23 12:56:32 +02003995 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04003996 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003997 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
3998 safeIndex = 0;
3999 }
4000
4001 if (!baseExpression->getType().isUnsizedArray())
4002 {
4003 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004004 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004005 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004006 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004007 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4008 {
4009 outOfRangeError(outOfRangeIndexIsError, location,
4010 "array index for gl_FragData must be zero when "
4011 "GL_EXT_draw_buffers is disabled",
4012 "[]");
4013 safeIndex = 0;
4014 }
4015 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004016 }
4017 // Only do generic out-of-range check if similar error hasn't already been reported.
4018 if (safeIndex < 0)
4019 {
4020 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004021 {
4022 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4023 baseExpression->getOutermostArraySize(),
4024 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004025 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004026 else if (baseExpression->isMatrix())
4027 {
4028 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4029 baseExpression->getType().getCols(),
4030 "matrix field selection out of range");
4031 }
4032 else
4033 {
4034 ASSERT(baseExpression->isVector());
4035 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4036 baseExpression->getType().getNominalSize(),
4037 "vector field selection out of range");
4038 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004039 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004040
Olli Etuahoebee5b32017-11-23 12:56:32 +02004041 ASSERT(safeIndex >= 0);
4042 // Data of constant unions can't be changed, because it may be shared with other
4043 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4044 // sanitized object.
4045 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4046 {
4047 TConstantUnion *safeConstantUnion = new TConstantUnion();
4048 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004049 indexExpression = new TIntermConstantUnion(
4050 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4051 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004052 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004053
Olli Etuahoebee5b32017-11-23 12:56:32 +02004054 TIntermBinary *node =
4055 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4056 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004057 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004058 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004059 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004060
4061 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4062 node->setLine(location);
4063 // Indirect indexing can never be constant folded.
4064 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004065}
4066
Olli Etuahoebee5b32017-11-23 12:56:32 +02004067int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4068 const TSourceLoc &location,
4069 int index,
4070 int arraySize,
4071 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004072{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004073 // Should not reach here with an unsized / runtime-sized array.
4074 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004075 // A negative index should already have been checked.
4076 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004077 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004078 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004079 std::stringstream reasonStream;
4080 reasonStream << reason << " '" << index << "'";
4081 std::string token = reasonStream.str();
4082 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004083 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004084 }
4085 return index;
4086}
4087
Jamie Madillb98c3a82015-07-23 14:26:04 -04004088TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4089 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004090 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004091 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004092{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004093 if (baseExpression->isArray())
4094 {
4095 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004096 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004097 }
4098
4099 if (baseExpression->isVector())
4100 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004101 TVector<int> fieldOffsets;
4102 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4103 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004104 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004105 fieldOffsets.resize(1);
4106 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004107 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004108 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4109 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004110
Olli Etuaho765924f2018-01-04 12:48:36 +02004111 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004112 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004113 else if (baseExpression->getBasicType() == EbtStruct)
4114 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304115 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004116 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004117 {
4118 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004119 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004120 }
4121 else
4122 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004123 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004125 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004126 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004127 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004128 {
4129 fieldFound = true;
4130 break;
4131 }
4132 }
4133 if (fieldFound)
4134 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004135 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004136 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004137 TIntermBinary *node =
4138 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4139 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004140 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004141 }
4142 else
4143 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004144 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004145 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004146 }
4147 }
4148 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004149 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004150 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304151 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004152 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004153 {
4154 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004155 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004156 }
4157 else
4158 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004159 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004160 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004161 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004162 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004163 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004164 {
4165 fieldFound = true;
4166 break;
4167 }
4168 }
4169 if (fieldFound)
4170 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004171 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004172 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004173 TIntermBinary *node =
4174 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4175 node->setLine(dotLocation);
4176 // Indexing interface blocks can never be constant folded.
4177 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004178 }
4179 else
4180 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004181 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004182 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004183 }
4184 }
4185 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004186 else
4187 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004188 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004189 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004190 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004191 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004192 }
4193 else
4194 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304195 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004196 " field selection requires structure, vector, or interface block on left hand "
4197 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004198 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004199 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004200 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004201 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004202}
4203
Olli Etuahofbb1c792018-01-19 16:26:59 +02004204TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004205 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004206{
Jamie Madill2f294c92017-11-20 14:47:26 -05004207 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004208
4209 if (qualifierType == "shared")
4210 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004211 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004212 {
4213 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4214 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004215 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004216 }
4217 else if (qualifierType == "packed")
4218 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004219 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004220 {
4221 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4222 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004223 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004224 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004225 else if (qualifierType == "std430")
4226 {
4227 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4228 qualifier.blockStorage = EbsStd430;
4229 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004230 else if (qualifierType == "std140")
4231 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004232 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004233 }
4234 else if (qualifierType == "row_major")
4235 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004236 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004237 }
4238 else if (qualifierType == "column_major")
4239 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004240 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004241 }
4242 else if (qualifierType == "location")
4243 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004244 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004245 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004246 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004247 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004248 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004249 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4250 {
4251 qualifier.yuv = true;
4252 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004253 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004254 else if (qualifierType == "rgba32f")
4255 {
4256 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4257 qualifier.imageInternalFormat = EiifRGBA32F;
4258 }
4259 else if (qualifierType == "rgba16f")
4260 {
4261 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4262 qualifier.imageInternalFormat = EiifRGBA16F;
4263 }
4264 else if (qualifierType == "r32f")
4265 {
4266 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4267 qualifier.imageInternalFormat = EiifR32F;
4268 }
4269 else if (qualifierType == "rgba8")
4270 {
4271 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4272 qualifier.imageInternalFormat = EiifRGBA8;
4273 }
4274 else if (qualifierType == "rgba8_snorm")
4275 {
4276 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4277 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4278 }
4279 else if (qualifierType == "rgba32i")
4280 {
4281 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4282 qualifier.imageInternalFormat = EiifRGBA32I;
4283 }
4284 else if (qualifierType == "rgba16i")
4285 {
4286 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4287 qualifier.imageInternalFormat = EiifRGBA16I;
4288 }
4289 else if (qualifierType == "rgba8i")
4290 {
4291 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4292 qualifier.imageInternalFormat = EiifRGBA8I;
4293 }
4294 else if (qualifierType == "r32i")
4295 {
4296 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4297 qualifier.imageInternalFormat = EiifR32I;
4298 }
4299 else if (qualifierType == "rgba32ui")
4300 {
4301 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4302 qualifier.imageInternalFormat = EiifRGBA32UI;
4303 }
4304 else if (qualifierType == "rgba16ui")
4305 {
4306 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4307 qualifier.imageInternalFormat = EiifRGBA16UI;
4308 }
4309 else if (qualifierType == "rgba8ui")
4310 {
4311 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4312 qualifier.imageInternalFormat = EiifRGBA8UI;
4313 }
4314 else if (qualifierType == "r32ui")
4315 {
4316 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4317 qualifier.imageInternalFormat = EiifR32UI;
4318 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004319 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4320 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004321 {
4322 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4323 qualifier.primitiveType = EptPoints;
4324 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004325 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4326 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004327 {
4328 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4329 qualifier.primitiveType = EptLines;
4330 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004331 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4332 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004333 {
4334 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4335 qualifier.primitiveType = EptLinesAdjacency;
4336 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004337 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4338 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004339 {
4340 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4341 qualifier.primitiveType = EptTriangles;
4342 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004343 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4344 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004345 {
4346 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4347 qualifier.primitiveType = EptTrianglesAdjacency;
4348 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004349 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4350 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004351 {
4352 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4353 qualifier.primitiveType = EptLineStrip;
4354 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004355 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4356 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004357 {
4358 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4359 qualifier.primitiveType = EptTriangleStrip;
4360 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004361
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004362 else
4363 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004364 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004365 }
4366
Jamie Madilla5efff92013-06-06 11:56:47 -04004367 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004368}
4369
Olli Etuahofbb1c792018-01-19 16:26:59 +02004370void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004371 const TSourceLoc &qualifierTypeLine,
4372 int intValue,
4373 const TSourceLoc &intValueLine,
4374 const std::string &intValueString,
4375 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004376 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004377{
Olli Etuaho856c4972016-08-08 11:38:39 +03004378 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004379 if (intValue < 1)
4380 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004381 std::stringstream reasonStream;
4382 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4383 std::string reason = reasonStream.str();
4384 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004385 }
4386 (*localSize)[index] = intValue;
4387}
4388
Olli Etuaho09b04a22016-12-15 13:30:26 +00004389void TParseContext::parseNumViews(int intValue,
4390 const TSourceLoc &intValueLine,
4391 const std::string &intValueString,
4392 int *numViews)
4393{
4394 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4395 // specification.
4396 if (intValue < 1)
4397 {
4398 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4399 }
4400 *numViews = intValue;
4401}
4402
Shaob5cc1192017-07-06 10:47:20 +08004403void TParseContext::parseInvocations(int intValue,
4404 const TSourceLoc &intValueLine,
4405 const std::string &intValueString,
4406 int *numInvocations)
4407{
4408 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4409 // it doesn't make sense to accept invocations <= 0.
4410 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4411 {
4412 error(intValueLine,
4413 "out of range: invocations must be in the range of [1, "
4414 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4415 intValueString.c_str());
4416 }
4417 else
4418 {
4419 *numInvocations = intValue;
4420 }
4421}
4422
4423void TParseContext::parseMaxVertices(int intValue,
4424 const TSourceLoc &intValueLine,
4425 const std::string &intValueString,
4426 int *maxVertices)
4427{
4428 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4429 // it doesn't make sense to accept max_vertices < 0.
4430 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4431 {
4432 error(
4433 intValueLine,
4434 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4435 intValueString.c_str());
4436 }
4437 else
4438 {
4439 *maxVertices = intValue;
4440 }
4441}
4442
Olli Etuahofbb1c792018-01-19 16:26:59 +02004443TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004444 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004445 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304446 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004447{
Jamie Madill2f294c92017-11-20 14:47:26 -05004448 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004449
Martin Radev802abe02016-08-04 17:48:32 +03004450 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004451
Martin Radev802abe02016-08-04 17:48:32 +03004452 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004453 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004454 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004455 if (intValue < 0)
4456 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004457 error(intValueLine, "out of range: location must be non-negative",
4458 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004459 }
4460 else
4461 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004462 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004463 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004464 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004465 }
Olli Etuaho43364892017-02-13 16:00:12 +00004466 else if (qualifierType == "binding")
4467 {
4468 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4469 if (intValue < 0)
4470 {
4471 error(intValueLine, "out of range: binding must be non-negative",
4472 intValueString.c_str());
4473 }
4474 else
4475 {
4476 qualifier.binding = intValue;
4477 }
4478 }
jchen104cdac9e2017-05-08 11:01:20 +08004479 else if (qualifierType == "offset")
4480 {
4481 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4482 if (intValue < 0)
4483 {
4484 error(intValueLine, "out of range: offset must be non-negative",
4485 intValueString.c_str());
4486 }
4487 else
4488 {
4489 qualifier.offset = intValue;
4490 }
4491 }
Martin Radev802abe02016-08-04 17:48:32 +03004492 else if (qualifierType == "local_size_x")
4493 {
4494 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4495 &qualifier.localSize);
4496 }
4497 else if (qualifierType == "local_size_y")
4498 {
4499 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4500 &qualifier.localSize);
4501 }
4502 else if (qualifierType == "local_size_z")
4503 {
4504 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4505 &qualifier.localSize);
4506 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004507 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004508 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004509 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4510 {
4511 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4512 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004513 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004514 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4515 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004516 {
4517 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4518 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004519 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4520 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004521 {
4522 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4523 }
4524
Martin Radev802abe02016-08-04 17:48:32 +03004525 else
4526 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004527 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004528 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004529
Jamie Madilla5efff92013-06-06 11:56:47 -04004530 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004531}
4532
Olli Etuaho613b9592016-09-05 12:05:53 +03004533TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4534{
4535 return new TTypeQualifierBuilder(
4536 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4537 mShaderVersion);
4538}
4539
Olli Etuahocce89652017-06-19 16:04:09 +03004540TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4541 const TSourceLoc &loc)
4542{
4543 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4544 return new TStorageQualifierWrapper(qualifier, loc);
4545}
4546
4547TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4548{
4549 if (getShaderType() == GL_VERTEX_SHADER)
4550 {
4551 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4552 }
4553 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4554}
4555
4556TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4557{
4558 if (declaringFunction())
4559 {
4560 return new TStorageQualifierWrapper(EvqIn, loc);
4561 }
Shaob5cc1192017-07-06 10:47:20 +08004562
4563 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004564 {
Shaob5cc1192017-07-06 10:47:20 +08004565 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004566 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004567 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004568 {
4569 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4570 }
4571 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004572 }
Shaob5cc1192017-07-06 10:47:20 +08004573 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004574 {
Shaob5cc1192017-07-06 10:47:20 +08004575 if (mShaderVersion < 300)
4576 {
4577 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4578 }
4579 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004580 }
Shaob5cc1192017-07-06 10:47:20 +08004581 case GL_COMPUTE_SHADER:
4582 {
4583 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4584 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004585 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004586 {
4587 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4588 }
4589 default:
4590 {
4591 UNREACHABLE();
4592 return new TStorageQualifierWrapper(EvqLast, loc);
4593 }
Olli Etuahocce89652017-06-19 16:04:09 +03004594 }
Olli Etuahocce89652017-06-19 16:04:09 +03004595}
4596
4597TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4598{
4599 if (declaringFunction())
4600 {
4601 return new TStorageQualifierWrapper(EvqOut, loc);
4602 }
Shaob5cc1192017-07-06 10:47:20 +08004603 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004604 {
Shaob5cc1192017-07-06 10:47:20 +08004605 case GL_VERTEX_SHADER:
4606 {
4607 if (mShaderVersion < 300)
4608 {
4609 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4610 }
4611 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4612 }
4613 case GL_FRAGMENT_SHADER:
4614 {
4615 if (mShaderVersion < 300)
4616 {
4617 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4618 }
4619 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4620 }
4621 case GL_COMPUTE_SHADER:
4622 {
4623 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4624 return new TStorageQualifierWrapper(EvqLast, loc);
4625 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004626 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004627 {
4628 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4629 }
4630 default:
4631 {
4632 UNREACHABLE();
4633 return new TStorageQualifierWrapper(EvqLast, loc);
4634 }
Olli Etuahocce89652017-06-19 16:04:09 +03004635 }
Olli Etuahocce89652017-06-19 16:04:09 +03004636}
4637
4638TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4639{
4640 if (!declaringFunction())
4641 {
4642 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4643 }
4644 return new TStorageQualifierWrapper(EvqInOut, loc);
4645}
4646
Jamie Madillb98c3a82015-07-23 14:26:04 -04004647TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004648 TLayoutQualifier rightQualifier,
4649 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004650{
Martin Radevc28888b2016-07-22 15:27:42 +03004651 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004652 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004653}
4654
Olli Etuahofbb1c792018-01-19 16:26:59 +02004655TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4656 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004657{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004658 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004659 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004660}
4661
Olli Etuahofbb1c792018-01-19 16:26:59 +02004662TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004663 const TSourceLoc &loc,
4664 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004665{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004666 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004667 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004668}
4669
Olli Etuaho722bfb52017-10-26 17:00:11 +03004670void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4671 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004672 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004673 const TSourceLoc &location)
4674{
4675 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4676 {
4677 if ((*fieldIter)->name() == name)
4678 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004679 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004680 }
4681 }
4682}
4683
4684TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4685{
4686 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4687 ++fieldIter)
4688 {
4689 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4690 location);
4691 }
4692 return fields;
4693}
4694
Olli Etuaho4de340a2016-12-16 09:32:03 +00004695TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4696 const TFieldList *newlyAddedFields,
4697 const TSourceLoc &location)
4698{
4699 for (TField *field : *newlyAddedFields)
4700 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004701 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4702 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004703 processedFields->push_back(field);
4704 }
4705 return processedFields;
4706}
4707
Martin Radev70866b82016-07-22 15:27:42 +03004708TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4709 const TTypeQualifierBuilder &typeQualifierBuilder,
4710 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004711 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004712{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004713 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004714
Martin Radev70866b82016-07-22 15:27:42 +03004715 typeSpecifier->qualifier = typeQualifier.qualifier;
4716 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004717 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004718 typeSpecifier->invariant = typeQualifier.invariant;
4719 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304720 {
Martin Radev70866b82016-07-22 15:27:42 +03004721 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004722 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004723 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004724}
4725
Jamie Madillb98c3a82015-07-23 14:26:04 -04004726TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004727 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004728{
Martin Radev4a9cd802016-09-01 16:51:51 +03004729 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4730 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004731
Olli Etuahofbb1c792018-01-19 16:26:59 +02004732 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004733 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004734
Martin Radev4a9cd802016-09-01 16:51:51 +03004735 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004736
Olli Etuahod5f44c92017-11-29 17:15:40 +02004737 TFieldList *fieldList = new TFieldList();
4738
4739 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304740 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004741 TType *type = new TType(typeSpecifier);
4742 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304743 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004744 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004745 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004746 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004747 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004748
Olli Etuahod5f44c92017-11-29 17:15:40 +02004749 TField *field = new TField(type, declarator->name(), declarator->line());
4750 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4751 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004752 }
4753
Olli Etuahod5f44c92017-11-29 17:15:40 +02004754 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004755}
4756
Martin Radev4a9cd802016-09-01 16:51:51 +03004757TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4758 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004759 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004760 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004761{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004762 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004763 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004764 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004765 structSymbolType = SymbolType::Empty;
4766 }
4767 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004768
Jamie Madill9b820842015-02-12 10:40:10 -05004769 // Store a bool in the struct if we're at global scope, to allow us to
4770 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004771 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004772
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004773 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004774 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004775 checkIsNotReserved(nameLine, structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004776 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304777 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004778 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004779 }
4780 }
4781
4782 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004783 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004784 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004785 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004786 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004787 switch (qualifier)
4788 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004789 case EvqGlobal:
4790 case EvqTemporary:
4791 break;
4792 default:
4793 error(field.line(), "invalid qualifier on struct member",
4794 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004795 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004796 }
Martin Radev70866b82016-07-22 15:27:42 +03004797 if (field.type()->isInvariant())
4798 {
4799 error(field.line(), "invalid qualifier on struct member", "invariant");
4800 }
jchen104cdac9e2017-05-08 11:01:20 +08004801 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4802 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004803 {
4804 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4805 }
4806
Olli Etuahoebee5b32017-11-23 12:56:32 +02004807 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004808 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004809
Olli Etuaho43364892017-02-13 16:00:12 +00004810 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4811
4812 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004813
4814 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004815 }
4816
Martin Radev4a9cd802016-09-01 16:51:51 +03004817 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004818 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004819 exitStructDeclaration();
4820
Martin Radev4a9cd802016-09-01 16:51:51 +03004821 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004822}
4823
Jamie Madillb98c3a82015-07-23 14:26:04 -04004824TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004825 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004826 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004827{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004828 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004829 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004830 init->isVector())
4831 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004832 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4833 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004834 return nullptr;
4835 }
4836
Olli Etuaho923ecef2017-10-11 12:01:38 +03004837 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004838 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004839 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004840 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004841 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004842 }
4843
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004844 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4845 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004846 return node;
4847}
4848
4849TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4850{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004851 if (mSwitchNestingLevel == 0)
4852 {
4853 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004854 return nullptr;
4855 }
4856 if (condition == nullptr)
4857 {
4858 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004859 return nullptr;
4860 }
4861 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004862 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004863 {
4864 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004865 }
4866 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004867 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4868 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4869 // fold in case labels.
4870 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004871 {
4872 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004873 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004874 TIntermCase *node = new TIntermCase(condition);
4875 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004876 return node;
4877}
4878
4879TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4880{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004881 if (mSwitchNestingLevel == 0)
4882 {
4883 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004884 return nullptr;
4885 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004886 TIntermCase *node = new TIntermCase(nullptr);
4887 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004888 return node;
4889}
4890
Jamie Madillb98c3a82015-07-23 14:26:04 -04004891TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4892 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004893 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004894{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004895 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004896
4897 switch (op)
4898 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004899 case EOpLogicalNot:
4900 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4901 child->isVector())
4902 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004903 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004904 return nullptr;
4905 }
4906 break;
4907 case EOpBitwiseNot:
4908 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4909 child->isMatrix() || child->isArray())
4910 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004911 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004912 return nullptr;
4913 }
4914 break;
4915 case EOpPostIncrement:
4916 case EOpPreIncrement:
4917 case EOpPostDecrement:
4918 case EOpPreDecrement:
4919 case EOpNegative:
4920 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004921 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4922 child->getBasicType() == EbtBool || child->isArray() ||
4923 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004924 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004925 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004926 return nullptr;
4927 }
Nico Weber41b072b2018-02-09 10:01:32 -05004928 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004929 // Operators for built-ins are already type checked against their prototype.
4930 default:
4931 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004932 }
4933
Jiajia Qinbc585152017-06-23 15:42:17 +08004934 if (child->getMemoryQualifier().writeonly)
4935 {
4936 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4937 return nullptr;
4938 }
4939
Olli Etuahof119a262016-08-19 15:54:22 +03004940 TIntermUnary *node = new TIntermUnary(op, child);
4941 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004942
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004943 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004944}
4945
Olli Etuaho09b22472015-02-11 11:47:26 +02004946TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4947{
Olli Etuahocce89652017-06-19 16:04:09 +03004948 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004949 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004950 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004951 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004952 return child;
4953 }
4954 return node;
4955}
4956
Jamie Madillb98c3a82015-07-23 14:26:04 -04004957TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4958 TIntermTyped *child,
4959 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004960{
Olli Etuaho856c4972016-08-08 11:38:39 +03004961 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004962 return addUnaryMath(op, child, loc);
4963}
4964
Olli Etuaho765924f2018-01-04 12:48:36 +02004965TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4966{
4967 // If we can, we should return the folded version of the expression for subsequent parsing. This
4968 // enables folding the containing expression during parsing as well, instead of the separate
4969 // FoldExpressions() step where folding nested expressions requires multiple full AST
4970 // traversals.
4971
4972 // Even if folding fails the fold() functions return some node representing the expression,
4973 // typically the original node. So "folded" can be assumed to be non-null.
4974 TIntermTyped *folded = expression->fold(mDiagnostics);
4975 ASSERT(folded != nullptr);
4976 if (folded->getQualifier() == expression->getQualifier())
4977 {
4978 // We need this expression to have the correct qualifier when validating the consuming
4979 // expression. So we can only return the folded node from here in case it has the same
4980 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4981 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4982 // 1. (true ? 1.0 : non_constant)
4983 // 2. (non_constant, 1.0)
4984 return folded;
4985 }
4986 return expression;
4987}
4988
Jamie Madillb98c3a82015-07-23 14:26:04 -04004989bool TParseContext::binaryOpCommonCheck(TOperator op,
4990 TIntermTyped *left,
4991 TIntermTyped *right,
4992 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004993{
jchen10b4cf5652017-05-05 18:51:17 +08004994 // Check opaque types are not allowed to be operands in expressions other than array indexing
4995 // and structure member selection.
4996 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4997 {
4998 switch (op)
4999 {
5000 case EOpIndexDirect:
5001 case EOpIndexIndirect:
5002 break;
jchen10b4cf5652017-05-05 18:51:17 +08005003
5004 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05005005 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08005006 error(loc, "Invalid operation for variables with an opaque type",
5007 GetOperatorString(op));
5008 return false;
5009 }
5010 }
jchen10cc2a10e2017-05-03 14:05:12 +08005011
Jiajia Qinbc585152017-06-23 15:42:17 +08005012 if (right->getMemoryQualifier().writeonly)
5013 {
5014 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5015 return false;
5016 }
5017
5018 if (left->getMemoryQualifier().writeonly)
5019 {
5020 switch (op)
5021 {
5022 case EOpAssign:
5023 case EOpInitialize:
5024 case EOpIndexDirect:
5025 case EOpIndexIndirect:
5026 case EOpIndexDirectStruct:
5027 case EOpIndexDirectInterfaceBlock:
5028 break;
5029 default:
5030 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5031 return false;
5032 }
5033 }
5034
Olli Etuaho244be012016-08-18 15:26:02 +03005035 if (left->getType().getStruct() || right->getType().getStruct())
5036 {
5037 switch (op)
5038 {
5039 case EOpIndexDirectStruct:
5040 ASSERT(left->getType().getStruct());
5041 break;
5042 case EOpEqual:
5043 case EOpNotEqual:
5044 case EOpAssign:
5045 case EOpInitialize:
5046 if (left->getType() != right->getType())
5047 {
5048 return false;
5049 }
5050 break;
5051 default:
5052 error(loc, "Invalid operation for structs", GetOperatorString(op));
5053 return false;
5054 }
5055 }
5056
Olli Etuaho94050052017-05-08 14:17:44 +03005057 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5058 {
5059 switch (op)
5060 {
5061 case EOpIndexDirectInterfaceBlock:
5062 ASSERT(left->getType().getInterfaceBlock());
5063 break;
5064 default:
5065 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5066 return false;
5067 }
5068 }
5069
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005070 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005071 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005072 error(loc, "array / non-array mismatch", GetOperatorString(op));
5073 return false;
5074 }
5075
5076 if (left->isArray())
5077 {
5078 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005079 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005080 {
5081 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5082 return false;
5083 }
5084
Olli Etuahoe79904c2015-03-18 16:56:42 +02005085 switch (op)
5086 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005087 case EOpEqual:
5088 case EOpNotEqual:
5089 case EOpAssign:
5090 case EOpInitialize:
5091 break;
5092 default:
5093 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5094 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005095 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005096 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005097 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005098 {
5099 error(loc, "array size mismatch", GetOperatorString(op));
5100 return false;
5101 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005102 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005103
5104 // Check ops which require integer / ivec parameters
5105 bool isBitShift = false;
5106 switch (op)
5107 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005108 case EOpBitShiftLeft:
5109 case EOpBitShiftRight:
5110 case EOpBitShiftLeftAssign:
5111 case EOpBitShiftRightAssign:
5112 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5113 // check that the basic type is an integer type.
5114 isBitShift = true;
5115 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5116 {
5117 return false;
5118 }
5119 break;
5120 case EOpBitwiseAnd:
5121 case EOpBitwiseXor:
5122 case EOpBitwiseOr:
5123 case EOpBitwiseAndAssign:
5124 case EOpBitwiseXorAssign:
5125 case EOpBitwiseOrAssign:
5126 // It is enough to check the type of only one operand, since later it
5127 // is checked that the operand types match.
5128 if (!IsInteger(left->getBasicType()))
5129 {
5130 return false;
5131 }
5132 break;
5133 default:
5134 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005135 }
5136
5137 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5138 // So the basic type should usually match.
5139 if (!isBitShift && left->getBasicType() != right->getBasicType())
5140 {
5141 return false;
5142 }
5143
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005144 // Check that:
5145 // 1. Type sizes match exactly on ops that require that.
5146 // 2. Restrictions for structs that contain arrays or samplers are respected.
5147 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005148 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005149 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005150 case EOpAssign:
5151 case EOpInitialize:
5152 case EOpEqual:
5153 case EOpNotEqual:
5154 // ESSL 1.00 sections 5.7, 5.8, 5.9
5155 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5156 {
5157 error(loc, "undefined operation for structs containing arrays",
5158 GetOperatorString(op));
5159 return false;
5160 }
5161 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5162 // we interpret the spec so that this extends to structs containing samplers,
5163 // similarly to ESSL 1.00 spec.
5164 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5165 left->getType().isStructureContainingSamplers())
5166 {
5167 error(loc, "undefined operation for structs containing samplers",
5168 GetOperatorString(op));
5169 return false;
5170 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005171
Olli Etuahoe1805592017-01-02 16:41:20 +00005172 if ((left->getNominalSize() != right->getNominalSize()) ||
5173 (left->getSecondarySize() != right->getSecondarySize()))
5174 {
5175 error(loc, "dimension mismatch", GetOperatorString(op));
5176 return false;
5177 }
5178 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005179 case EOpLessThan:
5180 case EOpGreaterThan:
5181 case EOpLessThanEqual:
5182 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005183 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005184 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005185 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005186 return false;
5187 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005188 break;
5189 case EOpAdd:
5190 case EOpSub:
5191 case EOpDiv:
5192 case EOpIMod:
5193 case EOpBitShiftLeft:
5194 case EOpBitShiftRight:
5195 case EOpBitwiseAnd:
5196 case EOpBitwiseXor:
5197 case EOpBitwiseOr:
5198 case EOpAddAssign:
5199 case EOpSubAssign:
5200 case EOpDivAssign:
5201 case EOpIModAssign:
5202 case EOpBitShiftLeftAssign:
5203 case EOpBitShiftRightAssign:
5204 case EOpBitwiseAndAssign:
5205 case EOpBitwiseXorAssign:
5206 case EOpBitwiseOrAssign:
5207 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5208 {
5209 return false;
5210 }
5211
5212 // Are the sizes compatible?
5213 if (left->getNominalSize() != right->getNominalSize() ||
5214 left->getSecondarySize() != right->getSecondarySize())
5215 {
5216 // If the nominal sizes of operands do not match:
5217 // One of them must be a scalar.
5218 if (!left->isScalar() && !right->isScalar())
5219 return false;
5220
5221 // In the case of compound assignment other than multiply-assign,
5222 // the right side needs to be a scalar. Otherwise a vector/matrix
5223 // would be assigned to a scalar. A scalar can't be shifted by a
5224 // vector either.
5225 if (!right->isScalar() &&
5226 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5227 return false;
5228 }
5229 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005230 default:
5231 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005232 }
5233
Olli Etuahod6b14282015-03-17 14:31:35 +02005234 return true;
5235}
5236
Olli Etuaho1dded802016-08-18 18:13:13 +03005237bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5238 const TType &left,
5239 const TType &right)
5240{
5241 switch (op)
5242 {
5243 case EOpMul:
5244 case EOpMulAssign:
5245 return left.getNominalSize() == right.getNominalSize() &&
5246 left.getSecondarySize() == right.getSecondarySize();
5247 case EOpVectorTimesScalar:
5248 return true;
5249 case EOpVectorTimesScalarAssign:
5250 ASSERT(!left.isMatrix() && !right.isMatrix());
5251 return left.isVector() && !right.isVector();
5252 case EOpVectorTimesMatrix:
5253 return left.getNominalSize() == right.getRows();
5254 case EOpVectorTimesMatrixAssign:
5255 ASSERT(!left.isMatrix() && right.isMatrix());
5256 return left.isVector() && left.getNominalSize() == right.getRows() &&
5257 left.getNominalSize() == right.getCols();
5258 case EOpMatrixTimesVector:
5259 return left.getCols() == right.getNominalSize();
5260 case EOpMatrixTimesScalar:
5261 return true;
5262 case EOpMatrixTimesScalarAssign:
5263 ASSERT(left.isMatrix() && !right.isMatrix());
5264 return !right.isVector();
5265 case EOpMatrixTimesMatrix:
5266 return left.getCols() == right.getRows();
5267 case EOpMatrixTimesMatrixAssign:
5268 ASSERT(left.isMatrix() && right.isMatrix());
5269 // We need to check two things:
5270 // 1. The matrix multiplication step is valid.
5271 // 2. The result will have the same number of columns as the lvalue.
5272 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5273
5274 default:
5275 UNREACHABLE();
5276 return false;
5277 }
5278}
5279
Jamie Madillb98c3a82015-07-23 14:26:04 -04005280TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5281 TIntermTyped *left,
5282 TIntermTyped *right,
5283 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005284{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005285 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005286 return nullptr;
5287
Olli Etuahofc1806e2015-03-17 13:03:11 +02005288 switch (op)
5289 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005290 case EOpEqual:
5291 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005292 case EOpLessThan:
5293 case EOpGreaterThan:
5294 case EOpLessThanEqual:
5295 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005296 break;
5297 case EOpLogicalOr:
5298 case EOpLogicalXor:
5299 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005300 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5301 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005302 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005303 {
5304 return nullptr;
5305 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005306 // Basic types matching should have been already checked.
5307 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005308 break;
5309 case EOpAdd:
5310 case EOpSub:
5311 case EOpDiv:
5312 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005313 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5314 !right->getType().getStruct());
5315 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005316 {
5317 return nullptr;
5318 }
5319 break;
5320 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005321 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5322 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005323 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005324 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005325 {
5326 return nullptr;
5327 }
5328 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005329 default:
5330 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005331 }
5332
Olli Etuaho1dded802016-08-18 18:13:13 +03005333 if (op == EOpMul)
5334 {
5335 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5336 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5337 {
5338 return nullptr;
5339 }
5340 }
5341
Olli Etuaho3fdec912016-08-18 15:08:06 +03005342 TIntermBinary *node = new TIntermBinary(op, left, right);
5343 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005344 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005345}
5346
Jamie Madillb98c3a82015-07-23 14:26:04 -04005347TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5348 TIntermTyped *left,
5349 TIntermTyped *right,
5350 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005351{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005352 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005353 if (node == 0)
5354 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005355 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5356 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005357 return left;
5358 }
5359 return node;
5360}
5361
Jamie Madillb98c3a82015-07-23 14:26:04 -04005362TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5363 TIntermTyped *left,
5364 TIntermTyped *right,
5365 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005366{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005367 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005368 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005369 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005370 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5371 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005372 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005373 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005374 }
5375 return node;
5376}
5377
Olli Etuaho13389b62016-10-16 11:48:18 +01005378TIntermBinary *TParseContext::createAssign(TOperator op,
5379 TIntermTyped *left,
5380 TIntermTyped *right,
5381 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005382{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005383 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005384 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005385 if (op == EOpMulAssign)
5386 {
5387 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5388 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5389 {
5390 return nullptr;
5391 }
5392 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005393 TIntermBinary *node = new TIntermBinary(op, left, right);
5394 node->setLine(loc);
5395
Olli Etuaho3fdec912016-08-18 15:08:06 +03005396 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005397 }
5398 return nullptr;
5399}
5400
Jamie Madillb98c3a82015-07-23 14:26:04 -04005401TIntermTyped *TParseContext::addAssign(TOperator op,
5402 TIntermTyped *left,
5403 TIntermTyped *right,
5404 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005405{
Olli Etuahocce89652017-06-19 16:04:09 +03005406 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005407 TIntermTyped *node = createAssign(op, left, right, loc);
5408 if (node == nullptr)
5409 {
5410 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005411 return left;
5412 }
5413 return node;
5414}
5415
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005416TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5417 TIntermTyped *right,
5418 const TSourceLoc &loc)
5419{
Corentin Wallez0d959252016-07-12 17:26:32 -04005420 // WebGL2 section 5.26, the following results in an error:
5421 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005422 if (mShaderSpec == SH_WEBGL2_SPEC &&
5423 (left->isArray() || left->getBasicType() == EbtVoid ||
5424 left->getType().isStructureContainingArrays() || right->isArray() ||
5425 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005426 {
5427 error(loc,
5428 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5429 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005430 }
5431
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005432 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho765924f2018-01-04 12:48:36 +02005433
5434 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005435}
5436
Olli Etuaho49300862015-02-20 14:54:49 +02005437TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5438{
5439 switch (op)
5440 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005441 case EOpContinue:
5442 if (mLoopNestingLevel <= 0)
5443 {
5444 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005445 }
5446 break;
5447 case EOpBreak:
5448 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5449 {
5450 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005451 }
5452 break;
5453 case EOpReturn:
5454 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5455 {
5456 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005457 }
5458 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005459 case EOpKill:
5460 if (mShaderType != GL_FRAGMENT_SHADER)
5461 {
5462 error(loc, "discard supported in fragment shaders only", "discard");
5463 }
5464 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005465 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005466 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005467 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005468 }
Olli Etuahocce89652017-06-19 16:04:09 +03005469 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005470}
5471
Jamie Madillb98c3a82015-07-23 14:26:04 -04005472TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005473 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005474 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005475{
Olli Etuahocce89652017-06-19 16:04:09 +03005476 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005477 {
Olli Etuahocce89652017-06-19 16:04:09 +03005478 ASSERT(op == EOpReturn);
5479 mFunctionReturnsValue = true;
5480 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5481 {
5482 error(loc, "void function cannot return a value", "return");
5483 }
5484 else if (*mCurrentFunctionType != expression->getType())
5485 {
5486 error(loc, "function return is not matching type:", "return");
5487 }
Olli Etuaho49300862015-02-20 14:54:49 +02005488 }
Olli Etuahocce89652017-06-19 16:04:09 +03005489 TIntermBranch *node = new TIntermBranch(op, expression);
5490 node->setLine(loc);
5491 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005492}
5493
Martin Radev84aa2dc2017-09-11 15:51:02 +03005494void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5495{
5496 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005497 const ImmutableString &name = functionCall->getFunction()->name();
5498 bool isTextureGather = name == kTextureGatherName;
5499 bool isTextureGatherOffset = name == kTextureGatherOffsetName;
Martin Radev84aa2dc2017-09-11 15:51:02 +03005500 if (isTextureGather || isTextureGatherOffset)
5501 {
5502 TIntermNode *componentNode = nullptr;
5503 TIntermSequence *arguments = functionCall->getSequence();
5504 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5505 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5506 ASSERT(sampler != nullptr);
5507 switch (sampler->getBasicType())
5508 {
5509 case EbtSampler2D:
5510 case EbtISampler2D:
5511 case EbtUSampler2D:
5512 case EbtSampler2DArray:
5513 case EbtISampler2DArray:
5514 case EbtUSampler2DArray:
5515 if ((isTextureGather && arguments->size() == 3u) ||
5516 (isTextureGatherOffset && arguments->size() == 4u))
5517 {
5518 componentNode = arguments->back();
5519 }
5520 break;
5521 case EbtSamplerCube:
5522 case EbtISamplerCube:
5523 case EbtUSamplerCube:
5524 ASSERT(!isTextureGatherOffset);
5525 if (arguments->size() == 3u)
5526 {
5527 componentNode = arguments->back();
5528 }
5529 break;
5530 case EbtSampler2DShadow:
5531 case EbtSampler2DArrayShadow:
5532 case EbtSamplerCubeShadow:
5533 break;
5534 default:
5535 UNREACHABLE();
5536 break;
5537 }
5538 if (componentNode)
5539 {
5540 const TIntermConstantUnion *componentConstantUnion =
5541 componentNode->getAsConstantUnion();
5542 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5543 {
5544 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuahofbb1c792018-01-19 16:26:59 +02005545 name);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005546 }
5547 else
5548 {
5549 int component = componentConstantUnion->getIConst(0);
5550 if (component < 0 || component > 3)
5551 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005552 error(functionCall->getLine(), "Component must be in the range [0;3]", name);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005553 }
5554 }
5555 }
5556 }
5557}
5558
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005559void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5560{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005561 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005562 const ImmutableString &name = functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005563 TIntermNode *offset = nullptr;
5564 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005565 bool useTextureGatherOffsetConstraints = false;
Olli Etuahofbb1c792018-01-19 16:26:59 +02005566 if (name == kTexelFetchOffsetName || name == kTextureLodOffsetName ||
5567 name == kTextureProjLodOffsetName || name == kTextureGradOffsetName ||
5568 name == kTextureProjGradOffsetName)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005569 {
5570 offset = arguments->back();
5571 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005572 else if (name == kTextureOffsetName || name == kTextureProjOffsetName)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005573 {
5574 // A bias parameter might follow the offset parameter.
5575 ASSERT(arguments->size() >= 3);
5576 offset = (*arguments)[2];
5577 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005578 else if (name == kTextureGatherOffsetName)
Martin Radev84aa2dc2017-09-11 15:51:02 +03005579 {
5580 ASSERT(arguments->size() >= 3u);
5581 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5582 ASSERT(sampler != nullptr);
5583 switch (sampler->getBasicType())
5584 {
5585 case EbtSampler2D:
5586 case EbtISampler2D:
5587 case EbtUSampler2D:
5588 case EbtSampler2DArray:
5589 case EbtISampler2DArray:
5590 case EbtUSampler2DArray:
5591 offset = (*arguments)[2];
5592 break;
5593 case EbtSampler2DShadow:
5594 case EbtSampler2DArrayShadow:
5595 offset = (*arguments)[3];
5596 break;
5597 default:
5598 UNREACHABLE();
5599 break;
5600 }
5601 useTextureGatherOffsetConstraints = true;
5602 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005603 if (offset != nullptr)
5604 {
5605 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5606 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5607 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005608 error(functionCall->getLine(), "Texture offset must be a constant expression", name);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005609 }
5610 else
5611 {
5612 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5613 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005614 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005615 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5616 : mMinProgramTexelOffset;
5617 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5618 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005619 for (size_t i = 0u; i < size; ++i)
5620 {
5621 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005622 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005623 {
5624 std::stringstream tokenStream;
5625 tokenStream << offsetValue;
5626 std::string token = tokenStream.str();
5627 error(offset->getLine(), "Texture offset value out of valid range",
5628 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005629 }
5630 }
5631 }
5632 }
5633}
5634
Jiajia Qina3106c52017-11-03 09:39:39 +08005635void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5636{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005637 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005638 const ImmutableString &functionName = functionCall->getFunction()->name();
Olli Etuahobed35d72017-12-20 16:36:26 +02005639 if (IsAtomicBuiltin(functionName))
Jiajia Qina3106c52017-11-03 09:39:39 +08005640 {
5641 TIntermSequence *arguments = functionCall->getSequence();
5642 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5643
5644 if (IsBufferOrSharedVariable(memNode))
5645 {
5646 return;
5647 }
5648
5649 while (memNode->getAsBinaryNode())
5650 {
5651 memNode = memNode->getAsBinaryNode()->getLeft();
5652 if (IsBufferOrSharedVariable(memNode))
5653 {
5654 return;
5655 }
5656 }
5657
5658 error(memNode->getLine(),
5659 "The value passed to the mem argument of an atomic memory function does not "
5660 "correspond to a buffer or shared variable.",
Olli Etuahofbb1c792018-01-19 16:26:59 +02005661 functionName);
Jiajia Qina3106c52017-11-03 09:39:39 +08005662 }
5663}
5664
Martin Radev2cc85b32016-08-05 16:22:53 +03005665// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5666void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5667{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005668 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005669
Olli Etuahofbb1c792018-01-19 16:26:59 +02005670 if (functionCall->getFunction()->isImageFunction())
Martin Radev2cc85b32016-08-05 16:22:53 +03005671 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005672 const ImmutableString &name = functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005673 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005674 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005675
Olli Etuaho485eefd2017-02-14 17:40:06 +00005676 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005677
Olli Etuahofbb1c792018-01-19 16:26:59 +02005678 if (strcmp(name.data() + 5u, "Store") == 0)
Martin Radev2cc85b32016-08-05 16:22:53 +03005679 {
5680 if (memoryQualifier.readonly)
5681 {
5682 error(imageNode->getLine(),
5683 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005684 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005685 }
5686 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005687 else if (strcmp(name.data() + 5u, "Load") == 0)
Martin Radev2cc85b32016-08-05 16:22:53 +03005688 {
5689 if (memoryQualifier.writeonly)
5690 {
5691 error(imageNode->getLine(),
5692 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005693 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005694 }
5695 }
5696 }
5697}
5698
5699// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5700void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5701 const TFunction *functionDefinition,
5702 const TIntermAggregate *functionCall)
5703{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005704 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005705
5706 const TIntermSequence &arguments = *functionCall->getSequence();
5707
5708 ASSERT(functionDefinition->getParamCount() == arguments.size());
5709
5710 for (size_t i = 0; i < arguments.size(); ++i)
5711 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005712 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5713 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005714 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5715 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5716
5717 if (IsImage(functionArgumentType.getBasicType()))
5718 {
5719 const TMemoryQualifier &functionArgumentMemoryQualifier =
5720 functionArgumentType.getMemoryQualifier();
5721 const TMemoryQualifier &functionParameterMemoryQualifier =
5722 functionParameterType.getMemoryQualifier();
5723 if (functionArgumentMemoryQualifier.readonly &&
5724 !functionParameterMemoryQualifier.readonly)
5725 {
5726 error(functionCall->getLine(),
5727 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005728 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005729 }
5730
5731 if (functionArgumentMemoryQualifier.writeonly &&
5732 !functionParameterMemoryQualifier.writeonly)
5733 {
5734 error(functionCall->getLine(),
5735 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005736 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005737 }
Martin Radev049edfa2016-11-11 14:35:37 +02005738
5739 if (functionArgumentMemoryQualifier.coherent &&
5740 !functionParameterMemoryQualifier.coherent)
5741 {
5742 error(functionCall->getLine(),
5743 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005744 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005745 }
5746
5747 if (functionArgumentMemoryQualifier.volatileQualifier &&
5748 !functionParameterMemoryQualifier.volatileQualifier)
5749 {
5750 error(functionCall->getLine(),
5751 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005752 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005753 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005754 }
5755 }
5756}
5757
Olli Etuaho95ed1942018-02-01 14:01:19 +02005758TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005759{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005760 if (fnCall->thisNode() != nullptr)
5761 {
5762 return addMethod(fnCall, loc);
5763 }
5764 if (fnCall->isConstructor())
5765 {
5766 return addConstructor(fnCall, loc);
5767 }
5768 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005769}
5770
Olli Etuaho95ed1942018-02-01 14:01:19 +02005771TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005772{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005773 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005774 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5775 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5776 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005777 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005778 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005779 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005780 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005781 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005782 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005783 {
5784 error(loc, "method takes no parameters", "length");
5785 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005786 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005787 {
5788 error(loc, "length can only be called on arrays", "length");
5789 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005790 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005791 mGeometryShaderInputPrimitiveType == EptUndefined)
5792 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005793 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005794 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5795 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005796 else
5797 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005798 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005799 node->setLine(loc);
5800 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005801 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005802 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005803}
5804
Olli Etuaho95ed1942018-02-01 14:01:19 +02005805TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005806 const TSourceLoc &loc)
5807{
Olli Etuaho697bf652018-02-16 11:50:54 +02005808 // First check whether the function has been hidden by a variable name or struct typename by
5809 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5810 // with a matching argument list.
5811 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005812 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005813 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005814 }
5815 else
5816 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005817 // There are no inner functions, so it's enough to look for user-defined functions in the
5818 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005819 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005820 if (symbol != nullptr)
5821 {
5822 // A user-defined function - could be an overloaded built-in as well.
5823 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5824 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5825 TIntermAggregate *callNode =
5826 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5827 callNode->setLine(loc);
5828 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5829 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5830 return callNode;
5831 }
5832
5833 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005834 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005835 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005836 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005837 }
5838 else
5839 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005840 // A built-in function.
5841 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005842 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005843
Olli Etuaho37b697e2018-01-29 12:19:27 +02005844 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005845 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005846 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005847 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005848 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005849 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005850 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005851 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005852 if (fnCandidate->getParamCount() == 1)
5853 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005854 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005855 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005856 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005857 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005858 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005859 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005860 TIntermAggregate *callNode =
5861 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005862 callNode->setLine(loc);
5863
Olli Etuahoe80825e2018-02-16 10:24:53 +02005864 // Some built-in functions have out parameters too.
5865 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5866
5867 // See if we can constant fold a built-in. Note that this may be possible
5868 // even if it is not const-qualified.
5869 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005870 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005871
5872 // This is a built-in function with no op associated with it.
5873 TIntermAggregate *callNode =
5874 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5875 callNode->setLine(loc);
5876 checkTextureOffsetConst(callNode);
5877 checkTextureGather(callNode);
5878 checkImageMemoryAccessForBuiltinFunctions(callNode);
5879 checkAtomicMemoryBuiltinFunctions(callNode);
5880 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5881 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005882 }
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