blob: 06cd1c80bf9abbec34bf8f865b8220becc6ea6bc [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;
3231 const TFunction *function = symbolTable.markUserDefinedFunctionHasPrototypeDeclaration(
3232 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3233
3234 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003235 {
3236 // ESSL 1.00.17 section 4.2.7.
3237 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3238 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003239 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003240
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003241 TIntermFunctionPrototype *prototype =
3242 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003243
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003244 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003245
3246 if (!symbolTable.atGlobalLevel())
3247 {
3248 // ESSL 3.00.4 section 4.2.4.
3249 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003250 }
3251
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003252 return prototype;
3253}
3254
Olli Etuaho336b1472016-10-05 16:37:55 +01003255TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003256 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003257 TIntermBlock *functionBody,
3258 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003259{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003260 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003261 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3262 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003263 error(location,
3264 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003265 }
3266
Olli Etuahof51fdd22016-10-03 10:03:40 +01003267 if (functionBody == nullptr)
3268 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003269 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003270 functionBody->setLine(location);
3271 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003272 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003273 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003274 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003275
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003276 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003277 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003278}
3279
Olli Etuaho476197f2016-10-11 13:59:08 +01003280void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003281 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003282 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003283{
Olli Etuaho476197f2016-10-11 13:59:08 +01003284 ASSERT(function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003285 const TSymbol *builtIn =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003286 symbolTable.findBuiltIn(ImmutableString(function->getMangledName()), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003287
3288 if (builtIn)
3289 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003290 error(location, "built-in functions cannot be redefined", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003291 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003292 else
Jamie Madill185fb402015-06-12 15:48:48 -04003293 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003294 bool wasDefined = false;
3295 function =
3296 symbolTable.setUserDefinedFunctionParameterNamesFromDefinition(function, &wasDefined);
3297 if (wasDefined)
Olli Etuaho476197f2016-10-11 13:59:08 +01003298 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003299 error(location, "function already has a body", function->name());
Olli Etuaho476197f2016-10-11 13:59:08 +01003300 }
Jamie Madill185fb402015-06-12 15:48:48 -04003301 }
Jamie Madill185fb402015-06-12 15:48:48 -04003302
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003303 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003304 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003305 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003306
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003307 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003308 setLoopNestingLevel(0);
3309}
3310
Jamie Madillb98c3a82015-07-23 14:26:04 -04003311TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003312{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003313 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003314 // We don't know at this point whether this is a function definition or a prototype.
3315 // The definition production code will check for redefinitions.
3316 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003317 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003318 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3319 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003320 //
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003321 const TFunction *prevDec = static_cast<const TFunction *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003322 symbolTable.find(ImmutableString(function->getMangledName()), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303323
Olli Etuahod80f2942017-11-06 12:44:45 +02003324 for (size_t i = 0u; i < function->getParamCount(); ++i)
3325 {
3326 auto &param = function->getParam(i);
3327 if (param.type->isStructSpecifier())
3328 {
3329 // ESSL 3.00.6 section 12.10.
3330 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003331 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003332 }
3333 }
3334
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003335 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003336 function->name().data(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303337 {
Martin Radevda6254b2016-12-14 17:00:36 +02003338 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303339 // Therefore overloading or redefining builtin functions is an error.
3340 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003341 function->name());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303342 }
3343 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003344 {
3345 if (prevDec->getReturnType() != function->getReturnType())
3346 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003347 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003348 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003349 }
3350 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3351 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003352 if (prevDec->getParam(i).type->getQualifier() !=
3353 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003354 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003355 error(location,
3356 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003357 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003358 }
3359 }
3360 }
3361
Jamie Madill185fb402015-06-12 15:48:48 -04003362 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003363 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3364 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003365 if (prevSym)
3366 {
3367 if (!prevSym->isFunction())
3368 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003369 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003370 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003371 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003372 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003373 // Parsing is at the inner scope level of the function's arguments and body statement at this
3374 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3375 // scope.
3376 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003377
Olli Etuaho78d13742017-01-18 13:06:10 +00003378 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003379 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003380 {
3381 if (function->getParamCount() > 0)
3382 {
3383 error(location, "function cannot take any parameter(s)", "main");
3384 }
3385 if (function->getReturnType().getBasicType() != EbtVoid)
3386 {
3387 error(location, "main function cannot return a value",
3388 function->getReturnType().getBasicString());
3389 }
3390 }
3391
Jamie Madill185fb402015-06-12 15:48:48 -04003392 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003393 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3394 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003395 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3396 //
3397 return function;
3398}
3399
Olli Etuaho9de84a52016-06-14 17:36:01 +03003400TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003401 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003402 const TSourceLoc &location)
3403{
3404 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3405 {
3406 error(location, "no qualifiers allowed for function return",
3407 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003408 }
3409 if (!type.layoutQualifier.isEmpty())
3410 {
3411 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003412 }
jchen10cc2a10e2017-05-03 14:05:12 +08003413 // make sure an opaque type is not involved as well...
3414 std::string reason(getBasicString(type.getBasicType()));
3415 reason += "s can't be function return values";
3416 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003417 if (mShaderVersion < 300)
3418 {
3419 // Array return values are forbidden, but there's also no valid syntax for declaring array
3420 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003421 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003422
3423 if (type.isStructureContainingArrays())
3424 {
3425 // ESSL 1.00.17 section 6.1 Function Definitions
3426 error(location, "structures containing arrays can't be function return values",
3427 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003428 }
3429 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003430
3431 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003432 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003433}
3434
Olli Etuaho697bf652018-02-16 11:50:54 +02003435TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3436 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003437{
Olli Etuaho697bf652018-02-16 11:50:54 +02003438 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003439}
3440
Olli Etuaho95ed1942018-02-01 14:01:19 +02003441TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003442{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003443 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003444 {
3445 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3446 "[]");
3447 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003448 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003449 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003450 error(publicType.getLine(), "constructor can't be a structure definition",
3451 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003452 }
3453
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003454 TType *type = new TType(publicType);
3455 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003456 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003457 error(publicType.getLine(), "cannot construct this type",
3458 getBasicString(publicType.getBasicType()));
3459 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003460 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003461 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003462}
3463
Olli Etuaho55bde912017-10-25 13:41:13 +03003464void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3465 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003466 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003467 TType *arrayType)
3468{
3469 if (arrayType->isUnsizedArray())
3470 {
3471 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003472 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003473 }
3474}
3475
3476TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003477 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003478 const TSourceLoc &nameLoc)
3479{
Olli Etuaho55bde912017-10-25 13:41:13 +03003480 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003481 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003482 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003483 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003484 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003485 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003486 checkIsNotReserved(nameLoc, name);
3487 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003488 return param;
3489}
3490
Olli Etuaho55bde912017-10-25 13:41:13 +03003491TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003492 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003493 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003494{
Olli Etuaho55bde912017-10-25 13:41:13 +03003495 TType *type = new TType(publicType);
3496 return parseParameterDeclarator(type, name, nameLoc);
3497}
3498
Olli Etuahofbb1c792018-01-19 16:26:59 +02003499TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003500 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003501 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003502 const TSourceLoc &arrayLoc,
3503 TPublicType *elementType)
3504{
3505 checkArrayElementIsNotArray(arrayLoc, *elementType);
3506 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003507 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003508 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003509}
3510
Olli Etuaho95ed1942018-02-01 14:01:19 +02003511bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3512 const TIntermSequence &arguments,
3513 TType type,
3514 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003515{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003516 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003517 {
3518 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3519 return false;
3520 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003521 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003522 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003523 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003524 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003525 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3526 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003527 {
3528 error(line, "constructing from a non-dereferenced array", "constructor");
3529 return false;
3530 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003531 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003532 {
3533 if (dimensionalityFromElement == 1u)
3534 {
3535 error(line, "implicitly sized array of arrays constructor argument is not an array",
3536 "constructor");
3537 }
3538 else
3539 {
3540 error(line,
3541 "implicitly sized array of arrays constructor argument dimensionality is too "
3542 "low",
3543 "constructor");
3544 }
3545 return false;
3546 }
3547 }
3548 return true;
3549}
3550
Jamie Madillb98c3a82015-07-23 14:26:04 -04003551// This function is used to test for the correctness of the parameters passed to various constructor
3552// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003553//
Olli Etuaho856c4972016-08-08 11:38:39 +03003554// 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 +00003555//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003556TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003557{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003558 TType type = fnCall->constructorType();
3559 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003560 if (type.isUnsizedArray())
3561 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003562 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003563 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003564 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003565 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003566 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003567 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003568 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003569 if (type.getOutermostArraySize() == 0u)
3570 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003571 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003572 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003573 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003574 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003575 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003576 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003577 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003578 }
3579 }
3580 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003581 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003582
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003583 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003584 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003585 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003586 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003587
Olli Etuaho95ed1942018-02-01 14:01:19 +02003588 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003589 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003590
Olli Etuaho765924f2018-01-04 12:48:36 +02003591 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003592}
3593
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003594//
3595// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003596// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003597//
Olli Etuaho13389b62016-10-16 11:48:18 +01003598TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003599 const TTypeQualifierBuilder &typeQualifierBuilder,
3600 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003601 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003602 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003603 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003604 const TSourceLoc &instanceLine,
3605 TIntermTyped *arrayIndex,
3606 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003607{
Olli Etuaho856c4972016-08-08 11:38:39 +03003608 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003609
Olli Etuaho77ba4082016-12-16 12:01:18 +00003610 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003611
Jiajia Qinbc585152017-06-23 15:42:17 +08003612 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003613 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003614 error(typeQualifier.line,
3615 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3616 "3.10",
3617 getQualifierString(typeQualifier.qualifier));
3618 }
3619 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3620 {
3621 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003622 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003623 }
3624
Martin Radev70866b82016-07-22 15:27:42 +03003625 if (typeQualifier.invariant)
3626 {
3627 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3628 }
3629
Jiajia Qinbc585152017-06-23 15:42:17 +08003630 if (typeQualifier.qualifier != EvqBuffer)
3631 {
3632 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3633 }
Olli Etuaho43364892017-02-13 16:00:12 +00003634
jchen10af713a22017-04-19 09:10:56 +08003635 // add array index
3636 unsigned int arraySize = 0;
3637 if (arrayIndex != nullptr)
3638 {
3639 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3640 }
3641
3642 if (mShaderVersion < 310)
3643 {
3644 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3645 }
3646 else
3647 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003648 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3649 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003650 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003651
Andrei Volykhina5527072017-03-22 16:46:30 +03003652 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3653
Jamie Madill099c0f32013-06-20 11:55:52 -04003654 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003655 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003656 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3657 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003658
Jamie Madill099c0f32013-06-20 11:55:52 -04003659 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3660 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003661 if (typeQualifier.qualifier == EvqUniform)
3662 {
3663 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3664 }
3665 else if (typeQualifier.qualifier == EvqBuffer)
3666 {
3667 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3668 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003669 }
3670
Jamie Madill1566ef72013-06-20 11:55:54 -04003671 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3672 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003673 if (typeQualifier.qualifier == EvqUniform)
3674 {
3675 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3676 }
3677 else if (typeQualifier.qualifier == EvqBuffer)
3678 {
3679 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3680 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003681 }
3682
Olli Etuaho856c4972016-08-08 11:38:39 +03003683 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003684
Martin Radev2cc85b32016-08-05 16:22:53 +03003685 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3686
Jamie Madill98493dd2013-07-08 14:39:03 -04003687 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303688 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3689 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003690 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303691 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003692 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303693 {
jchen10cc2a10e2017-05-03 14:05:12 +08003694 std::string reason("unsupported type - ");
3695 reason += fieldType->getBasicString();
3696 reason += " types are not allowed in interface blocks";
3697 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003698 }
3699
Jamie Madill98493dd2013-07-08 14:39:03 -04003700 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003701 switch (qualifier)
3702 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003703 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003704 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003705 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003706 if (typeQualifier.qualifier == EvqBuffer)
3707 {
3708 error(field->line(), "invalid qualifier on shader storage block member",
3709 getQualifierString(qualifier));
3710 }
3711 break;
3712 case EvqBuffer:
3713 if (typeQualifier.qualifier == EvqUniform)
3714 {
3715 error(field->line(), "invalid qualifier on uniform block member",
3716 getQualifierString(qualifier));
3717 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003718 break;
3719 default:
3720 error(field->line(), "invalid qualifier on interface block member",
3721 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003722 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003723 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003724
Martin Radev70866b82016-07-22 15:27:42 +03003725 if (fieldType->isInvariant())
3726 {
3727 error(field->line(), "invalid qualifier on interface block member", "invariant");
3728 }
3729
Jamie Madilla5efff92013-06-06 11:56:47 -04003730 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003731 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003732 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003733 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003734
Jamie Madill98493dd2013-07-08 14:39:03 -04003735 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003736 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003737 error(field->line(), "invalid layout qualifier: cannot be used here",
3738 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003739 }
3740
Jamie Madill98493dd2013-07-08 14:39:03 -04003741 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003742 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003743 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003744 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003745 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003746 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003747 warning(field->line(),
3748 "extraneous layout qualifier: only has an effect on matrix types",
3749 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003750 }
3751
Jamie Madill98493dd2013-07-08 14:39:03 -04003752 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003753
Olli Etuahoebee5b32017-11-23 12:56:32 +02003754 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3755 typeQualifier.qualifier != EvqBuffer)
3756 {
3757 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3758 checkIsNotUnsizedArray(field->line(),
3759 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003760 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003761 }
3762
Jiajia Qinbc585152017-06-23 15:42:17 +08003763 if (typeQualifier.qualifier == EvqBuffer)
3764 {
3765 // set memory qualifiers
3766 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3767 // qualified with a memory qualifier, it is as if all of its members were declared with
3768 // the same memory qualifier.
3769 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3770 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3771 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3772 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3773 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3774 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3775 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3776 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3777 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3778 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3779 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003780 }
3781
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003782 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003783 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003784 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3785 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003786 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003787 }
3788
Olli Etuahob60d30f2018-01-16 12:31:06 +02003789 TType *interfaceBlockType =
3790 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003791 if (arrayIndex != nullptr)
3792 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003793 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003794 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003795
Olli Etuaho195be942017-12-04 23:40:14 +02003796 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003797 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3798 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003799 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003800 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003801 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003802
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003803 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003804 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003805 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003806 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3807 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003808 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003809 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003810
3811 // set parent pointer of the field variable
3812 fieldType->setInterfaceBlock(interfaceBlock);
3813
Olli Etuahob60d30f2018-01-16 12:31:06 +02003814 fieldType->setQualifier(typeQualifier.qualifier);
3815
Olli Etuaho195be942017-12-04 23:40:14 +02003816 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003817 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuahob60d30f2018-01-16 12:31:06 +02003818 if (!symbolTable.declareVariable(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303819 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003820 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003821 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003822 }
3823 }
3824 }
3825 else
3826 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003827 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003828
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003829 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003830 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303831 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003832 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003833 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003834 }
3835
Olli Etuaho195be942017-12-04 23:40:14 +02003836 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3837 blockSymbol->setLine(typeQualifier.line);
3838 TIntermDeclaration *declaration = new TIntermDeclaration();
3839 declaration->appendDeclarator(blockSymbol);
3840 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003841
3842 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003843 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003844}
3845
Olli Etuahofbb1c792018-01-19 16:26:59 +02003846void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3847 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003848{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003849 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003850
3851 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003852 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303853 if (mStructNestingLevel > 1)
3854 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003855 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003856 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003857}
3858
3859void TParseContext::exitStructDeclaration()
3860{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003861 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003862}
3863
Olli Etuaho8a176262016-08-16 14:23:01 +03003864void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003865{
Jamie Madillacb4b812016-11-07 13:50:29 -05003866 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303867 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003868 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003869 }
3870
Arun Patole7e7e68d2015-05-22 12:02:25 +05303871 if (field.type()->getBasicType() != EbtStruct)
3872 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003873 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003874 }
3875
3876 // We're already inside a structure definition at this point, so add
3877 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303878 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3879 {
Jamie Madill41a49272014-03-18 16:10:13 -04003880 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003881 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3882 {
3883 // This may happen in case there are nested struct definitions. While they are also
3884 // invalid GLSL, they don't cause a syntax error.
3885 reasonStream << "Struct nesting";
3886 }
3887 else
3888 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003889 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003890 }
3891 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003892 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003893 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003894 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003895 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003896}
3897
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003898//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003899// Parse an array index expression
3900//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003901TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3902 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303903 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003904{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003905 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3906 {
3907 if (baseExpression->getAsSymbolNode())
3908 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303909 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003910 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003911 }
3912 else
3913 {
3914 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3915 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003916
Olli Etuaho3ec75682017-07-05 17:02:55 +03003917 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003918 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003919
Jiawei Shaod8105a02017-08-08 09:54:36 +08003920 if (baseExpression->getQualifier() == EvqPerVertexIn)
3921 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003922 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003923 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3924 {
3925 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3926 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3927 }
3928 }
3929
Jamie Madill21c1e452014-12-29 11:33:41 -05003930 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3931
Olli Etuaho36b05142015-11-12 13:10:42 +02003932 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3933 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3934 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3935 // index is a constant expression.
3936 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3937 {
3938 if (baseExpression->isInterfaceBlock())
3939 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003940 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003941 switch (baseExpression->getQualifier())
3942 {
3943 case EvqPerVertexIn:
3944 break;
3945 case EvqUniform:
3946 case EvqBuffer:
3947 error(location,
3948 "array indexes for uniform block arrays and shader storage block arrays "
3949 "must be constant integral expressions",
3950 "[");
3951 break;
3952 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003953 // We can reach here only in error cases.
3954 ASSERT(mDiagnostics->numErrors() > 0);
3955 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003956 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003957 }
3958 else if (baseExpression->getQualifier() == EvqFragmentOut)
3959 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003960 error(location,
3961 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003962 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003963 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3964 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003965 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003966 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003967 }
3968
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003969 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003970 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003971 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3972 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3973 // constant fold expressions that are not constant expressions). The most compatible way to
3974 // handle this case is to report a warning instead of an error and force the index to be in
3975 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003976 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003977 int index = 0;
3978 if (indexConstantUnion->getBasicType() == EbtInt)
3979 {
3980 index = indexConstantUnion->getIConst(0);
3981 }
3982 else if (indexConstantUnion->getBasicType() == EbtUInt)
3983 {
3984 index = static_cast<int>(indexConstantUnion->getUConst(0));
3985 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003986
3987 int safeIndex = -1;
3988
Olli Etuahoebee5b32017-11-23 12:56:32 +02003989 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04003990 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003991 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
3992 safeIndex = 0;
3993 }
3994
3995 if (!baseExpression->getType().isUnsizedArray())
3996 {
3997 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03003998 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003999 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004000 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004001 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4002 {
4003 outOfRangeError(outOfRangeIndexIsError, location,
4004 "array index for gl_FragData must be zero when "
4005 "GL_EXT_draw_buffers is disabled",
4006 "[]");
4007 safeIndex = 0;
4008 }
4009 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004010 }
4011 // Only do generic out-of-range check if similar error hasn't already been reported.
4012 if (safeIndex < 0)
4013 {
4014 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004015 {
4016 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4017 baseExpression->getOutermostArraySize(),
4018 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004019 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004020 else if (baseExpression->isMatrix())
4021 {
4022 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4023 baseExpression->getType().getCols(),
4024 "matrix field selection out of range");
4025 }
4026 else
4027 {
4028 ASSERT(baseExpression->isVector());
4029 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4030 baseExpression->getType().getNominalSize(),
4031 "vector field selection out of range");
4032 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004033 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004034
Olli Etuahoebee5b32017-11-23 12:56:32 +02004035 ASSERT(safeIndex >= 0);
4036 // Data of constant unions can't be changed, because it may be shared with other
4037 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4038 // sanitized object.
4039 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4040 {
4041 TConstantUnion *safeConstantUnion = new TConstantUnion();
4042 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004043 indexExpression = new TIntermConstantUnion(
4044 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4045 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004046 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004047
Olli Etuahoebee5b32017-11-23 12:56:32 +02004048 TIntermBinary *node =
4049 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4050 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004051 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004052 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004053 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004054
4055 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4056 node->setLine(location);
4057 // Indirect indexing can never be constant folded.
4058 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004059}
4060
Olli Etuahoebee5b32017-11-23 12:56:32 +02004061int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4062 const TSourceLoc &location,
4063 int index,
4064 int arraySize,
4065 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004066{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004067 // Should not reach here with an unsized / runtime-sized array.
4068 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004069 // A negative index should already have been checked.
4070 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004071 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004072 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004073 std::stringstream reasonStream;
4074 reasonStream << reason << " '" << index << "'";
4075 std::string token = reasonStream.str();
4076 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004077 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004078 }
4079 return index;
4080}
4081
Jamie Madillb98c3a82015-07-23 14:26:04 -04004082TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4083 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004084 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004085 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004086{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004087 if (baseExpression->isArray())
4088 {
4089 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004090 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004091 }
4092
4093 if (baseExpression->isVector())
4094 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004095 TVector<int> fieldOffsets;
4096 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4097 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004098 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004099 fieldOffsets.resize(1);
4100 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004101 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004102 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4103 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004104
Olli Etuaho765924f2018-01-04 12:48:36 +02004105 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004106 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004107 else if (baseExpression->getBasicType() == EbtStruct)
4108 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304109 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004110 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004111 {
4112 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004113 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004114 }
4115 else
4116 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004117 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004118 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004119 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004120 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004121 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004122 {
4123 fieldFound = true;
4124 break;
4125 }
4126 }
4127 if (fieldFound)
4128 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004129 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004130 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004131 TIntermBinary *node =
4132 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4133 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004134 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004135 }
4136 else
4137 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004138 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004139 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004140 }
4141 }
4142 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004143 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004144 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304145 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004146 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004147 {
4148 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004149 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004150 }
4151 else
4152 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004153 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004154 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004155 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004156 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004157 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004158 {
4159 fieldFound = true;
4160 break;
4161 }
4162 }
4163 if (fieldFound)
4164 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004165 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004166 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004167 TIntermBinary *node =
4168 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4169 node->setLine(dotLocation);
4170 // Indexing interface blocks can never be constant folded.
4171 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004172 }
4173 else
4174 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004175 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004176 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004177 }
4178 }
4179 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004180 else
4181 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004182 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004183 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004184 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004185 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004186 }
4187 else
4188 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304189 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004190 " field selection requires structure, vector, or interface block on left hand "
4191 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004192 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004193 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004194 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004195 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004196}
4197
Olli Etuahofbb1c792018-01-19 16:26:59 +02004198TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004199 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004200{
Jamie Madill2f294c92017-11-20 14:47:26 -05004201 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004202
4203 if (qualifierType == "shared")
4204 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004205 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004206 {
4207 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4208 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004209 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004210 }
4211 else if (qualifierType == "packed")
4212 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004213 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004214 {
4215 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4216 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004217 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004218 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004219 else if (qualifierType == "std430")
4220 {
4221 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4222 qualifier.blockStorage = EbsStd430;
4223 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004224 else if (qualifierType == "std140")
4225 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004226 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004227 }
4228 else if (qualifierType == "row_major")
4229 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004230 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004231 }
4232 else if (qualifierType == "column_major")
4233 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004234 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004235 }
4236 else if (qualifierType == "location")
4237 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004238 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004239 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004240 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004241 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004242 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004243 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4244 {
4245 qualifier.yuv = true;
4246 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004247 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004248 else if (qualifierType == "rgba32f")
4249 {
4250 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4251 qualifier.imageInternalFormat = EiifRGBA32F;
4252 }
4253 else if (qualifierType == "rgba16f")
4254 {
4255 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4256 qualifier.imageInternalFormat = EiifRGBA16F;
4257 }
4258 else if (qualifierType == "r32f")
4259 {
4260 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4261 qualifier.imageInternalFormat = EiifR32F;
4262 }
4263 else if (qualifierType == "rgba8")
4264 {
4265 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4266 qualifier.imageInternalFormat = EiifRGBA8;
4267 }
4268 else if (qualifierType == "rgba8_snorm")
4269 {
4270 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4271 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4272 }
4273 else if (qualifierType == "rgba32i")
4274 {
4275 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4276 qualifier.imageInternalFormat = EiifRGBA32I;
4277 }
4278 else if (qualifierType == "rgba16i")
4279 {
4280 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4281 qualifier.imageInternalFormat = EiifRGBA16I;
4282 }
4283 else if (qualifierType == "rgba8i")
4284 {
4285 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4286 qualifier.imageInternalFormat = EiifRGBA8I;
4287 }
4288 else if (qualifierType == "r32i")
4289 {
4290 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4291 qualifier.imageInternalFormat = EiifR32I;
4292 }
4293 else if (qualifierType == "rgba32ui")
4294 {
4295 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4296 qualifier.imageInternalFormat = EiifRGBA32UI;
4297 }
4298 else if (qualifierType == "rgba16ui")
4299 {
4300 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4301 qualifier.imageInternalFormat = EiifRGBA16UI;
4302 }
4303 else if (qualifierType == "rgba8ui")
4304 {
4305 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4306 qualifier.imageInternalFormat = EiifRGBA8UI;
4307 }
4308 else if (qualifierType == "r32ui")
4309 {
4310 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4311 qualifier.imageInternalFormat = EiifR32UI;
4312 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004313 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4314 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004315 {
4316 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4317 qualifier.primitiveType = EptPoints;
4318 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004319 else if (qualifierType == "lines" && 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 = EptLines;
4324 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004325 else if (qualifierType == "lines_adjacency" && 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 = EptLinesAdjacency;
4330 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004331 else if (qualifierType == "triangles" && 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 = EptTriangles;
4336 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004337 else if (qualifierType == "triangles_adjacency" && 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 = EptTrianglesAdjacency;
4342 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004343 else if (qualifierType == "line_strip" && 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 = EptLineStrip;
4348 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004349 else if (qualifierType == "triangle_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 = EptTriangleStrip;
4354 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004355
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004356 else
4357 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004358 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004359 }
4360
Jamie Madilla5efff92013-06-06 11:56:47 -04004361 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004362}
4363
Olli Etuahofbb1c792018-01-19 16:26:59 +02004364void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004365 const TSourceLoc &qualifierTypeLine,
4366 int intValue,
4367 const TSourceLoc &intValueLine,
4368 const std::string &intValueString,
4369 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004370 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004371{
Olli Etuaho856c4972016-08-08 11:38:39 +03004372 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004373 if (intValue < 1)
4374 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004375 std::stringstream reasonStream;
4376 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4377 std::string reason = reasonStream.str();
4378 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004379 }
4380 (*localSize)[index] = intValue;
4381}
4382
Olli Etuaho09b04a22016-12-15 13:30:26 +00004383void TParseContext::parseNumViews(int intValue,
4384 const TSourceLoc &intValueLine,
4385 const std::string &intValueString,
4386 int *numViews)
4387{
4388 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4389 // specification.
4390 if (intValue < 1)
4391 {
4392 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4393 }
4394 *numViews = intValue;
4395}
4396
Shaob5cc1192017-07-06 10:47:20 +08004397void TParseContext::parseInvocations(int intValue,
4398 const TSourceLoc &intValueLine,
4399 const std::string &intValueString,
4400 int *numInvocations)
4401{
4402 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4403 // it doesn't make sense to accept invocations <= 0.
4404 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4405 {
4406 error(intValueLine,
4407 "out of range: invocations must be in the range of [1, "
4408 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4409 intValueString.c_str());
4410 }
4411 else
4412 {
4413 *numInvocations = intValue;
4414 }
4415}
4416
4417void TParseContext::parseMaxVertices(int intValue,
4418 const TSourceLoc &intValueLine,
4419 const std::string &intValueString,
4420 int *maxVertices)
4421{
4422 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4423 // it doesn't make sense to accept max_vertices < 0.
4424 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4425 {
4426 error(
4427 intValueLine,
4428 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4429 intValueString.c_str());
4430 }
4431 else
4432 {
4433 *maxVertices = intValue;
4434 }
4435}
4436
Olli Etuahofbb1c792018-01-19 16:26:59 +02004437TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004438 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004439 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304440 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004441{
Jamie Madill2f294c92017-11-20 14:47:26 -05004442 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004443
Martin Radev802abe02016-08-04 17:48:32 +03004444 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004445
Martin Radev802abe02016-08-04 17:48:32 +03004446 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004447 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004448 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004449 if (intValue < 0)
4450 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004451 error(intValueLine, "out of range: location must be non-negative",
4452 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004453 }
4454 else
4455 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004456 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004457 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004458 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004459 }
Olli Etuaho43364892017-02-13 16:00:12 +00004460 else if (qualifierType == "binding")
4461 {
4462 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4463 if (intValue < 0)
4464 {
4465 error(intValueLine, "out of range: binding must be non-negative",
4466 intValueString.c_str());
4467 }
4468 else
4469 {
4470 qualifier.binding = intValue;
4471 }
4472 }
jchen104cdac9e2017-05-08 11:01:20 +08004473 else if (qualifierType == "offset")
4474 {
4475 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4476 if (intValue < 0)
4477 {
4478 error(intValueLine, "out of range: offset must be non-negative",
4479 intValueString.c_str());
4480 }
4481 else
4482 {
4483 qualifier.offset = intValue;
4484 }
4485 }
Martin Radev802abe02016-08-04 17:48:32 +03004486 else if (qualifierType == "local_size_x")
4487 {
4488 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4489 &qualifier.localSize);
4490 }
4491 else if (qualifierType == "local_size_y")
4492 {
4493 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4494 &qualifier.localSize);
4495 }
4496 else if (qualifierType == "local_size_z")
4497 {
4498 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4499 &qualifier.localSize);
4500 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004501 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004502 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004503 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4504 {
4505 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4506 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004507 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004508 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4509 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004510 {
4511 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4512 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004513 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4514 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004515 {
4516 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4517 }
4518
Martin Radev802abe02016-08-04 17:48:32 +03004519 else
4520 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004521 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004522 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004523
Jamie Madilla5efff92013-06-06 11:56:47 -04004524 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004525}
4526
Olli Etuaho613b9592016-09-05 12:05:53 +03004527TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4528{
4529 return new TTypeQualifierBuilder(
4530 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4531 mShaderVersion);
4532}
4533
Olli Etuahocce89652017-06-19 16:04:09 +03004534TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4535 const TSourceLoc &loc)
4536{
4537 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4538 return new TStorageQualifierWrapper(qualifier, loc);
4539}
4540
4541TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4542{
4543 if (getShaderType() == GL_VERTEX_SHADER)
4544 {
4545 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4546 }
4547 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4548}
4549
4550TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4551{
4552 if (declaringFunction())
4553 {
4554 return new TStorageQualifierWrapper(EvqIn, loc);
4555 }
Shaob5cc1192017-07-06 10:47:20 +08004556
4557 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004558 {
Shaob5cc1192017-07-06 10:47:20 +08004559 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004560 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004561 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004562 {
4563 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4564 }
4565 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004566 }
Shaob5cc1192017-07-06 10:47:20 +08004567 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004568 {
Shaob5cc1192017-07-06 10:47:20 +08004569 if (mShaderVersion < 300)
4570 {
4571 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4572 }
4573 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004574 }
Shaob5cc1192017-07-06 10:47:20 +08004575 case GL_COMPUTE_SHADER:
4576 {
4577 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4578 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004579 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004580 {
4581 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4582 }
4583 default:
4584 {
4585 UNREACHABLE();
4586 return new TStorageQualifierWrapper(EvqLast, loc);
4587 }
Olli Etuahocce89652017-06-19 16:04:09 +03004588 }
Olli Etuahocce89652017-06-19 16:04:09 +03004589}
4590
4591TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4592{
4593 if (declaringFunction())
4594 {
4595 return new TStorageQualifierWrapper(EvqOut, loc);
4596 }
Shaob5cc1192017-07-06 10:47:20 +08004597 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004598 {
Shaob5cc1192017-07-06 10:47:20 +08004599 case GL_VERTEX_SHADER:
4600 {
4601 if (mShaderVersion < 300)
4602 {
4603 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4604 }
4605 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4606 }
4607 case GL_FRAGMENT_SHADER:
4608 {
4609 if (mShaderVersion < 300)
4610 {
4611 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4612 }
4613 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4614 }
4615 case GL_COMPUTE_SHADER:
4616 {
4617 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4618 return new TStorageQualifierWrapper(EvqLast, loc);
4619 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004620 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004621 {
4622 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4623 }
4624 default:
4625 {
4626 UNREACHABLE();
4627 return new TStorageQualifierWrapper(EvqLast, loc);
4628 }
Olli Etuahocce89652017-06-19 16:04:09 +03004629 }
Olli Etuahocce89652017-06-19 16:04:09 +03004630}
4631
4632TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4633{
4634 if (!declaringFunction())
4635 {
4636 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4637 }
4638 return new TStorageQualifierWrapper(EvqInOut, loc);
4639}
4640
Jamie Madillb98c3a82015-07-23 14:26:04 -04004641TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004642 TLayoutQualifier rightQualifier,
4643 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004644{
Martin Radevc28888b2016-07-22 15:27:42 +03004645 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004646 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004647}
4648
Olli Etuahofbb1c792018-01-19 16:26:59 +02004649TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4650 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004651{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004652 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004653 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004654}
4655
Olli Etuahofbb1c792018-01-19 16:26:59 +02004656TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004657 const TSourceLoc &loc,
4658 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004659{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004660 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004661 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004662}
4663
Olli Etuaho722bfb52017-10-26 17:00:11 +03004664void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4665 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004666 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004667 const TSourceLoc &location)
4668{
4669 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4670 {
4671 if ((*fieldIter)->name() == name)
4672 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004673 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004674 }
4675 }
4676}
4677
4678TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4679{
4680 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4681 ++fieldIter)
4682 {
4683 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4684 location);
4685 }
4686 return fields;
4687}
4688
Olli Etuaho4de340a2016-12-16 09:32:03 +00004689TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4690 const TFieldList *newlyAddedFields,
4691 const TSourceLoc &location)
4692{
4693 for (TField *field : *newlyAddedFields)
4694 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004695 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4696 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004697 processedFields->push_back(field);
4698 }
4699 return processedFields;
4700}
4701
Martin Radev70866b82016-07-22 15:27:42 +03004702TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4703 const TTypeQualifierBuilder &typeQualifierBuilder,
4704 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004705 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004706{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004707 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004708
Martin Radev70866b82016-07-22 15:27:42 +03004709 typeSpecifier->qualifier = typeQualifier.qualifier;
4710 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004711 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004712 typeSpecifier->invariant = typeQualifier.invariant;
4713 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304714 {
Martin Radev70866b82016-07-22 15:27:42 +03004715 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004716 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004717 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004718}
4719
Jamie Madillb98c3a82015-07-23 14:26:04 -04004720TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004721 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004722{
Martin Radev4a9cd802016-09-01 16:51:51 +03004723 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4724 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004725
Olli Etuahofbb1c792018-01-19 16:26:59 +02004726 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004727 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004728
Martin Radev4a9cd802016-09-01 16:51:51 +03004729 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004730
Olli Etuahod5f44c92017-11-29 17:15:40 +02004731 TFieldList *fieldList = new TFieldList();
4732
4733 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304734 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004735 TType *type = new TType(typeSpecifier);
4736 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304737 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004738 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004739 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004740 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004741 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004742
Olli Etuahod5f44c92017-11-29 17:15:40 +02004743 TField *field = new TField(type, declarator->name(), declarator->line());
4744 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4745 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004746 }
4747
Olli Etuahod5f44c92017-11-29 17:15:40 +02004748 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004749}
4750
Martin Radev4a9cd802016-09-01 16:51:51 +03004751TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4752 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004753 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004754 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004755{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004756 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004757 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004758 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004759 structSymbolType = SymbolType::Empty;
4760 }
4761 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004762
Jamie Madill9b820842015-02-12 10:40:10 -05004763 // Store a bool in the struct if we're at global scope, to allow us to
4764 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004765 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004766
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004767 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004768 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004769 checkIsNotReserved(nameLine, structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004770 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304771 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004772 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004773 }
4774 }
4775
4776 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004777 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004778 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004779 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004780 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004781 switch (qualifier)
4782 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004783 case EvqGlobal:
4784 case EvqTemporary:
4785 break;
4786 default:
4787 error(field.line(), "invalid qualifier on struct member",
4788 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004789 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004790 }
Martin Radev70866b82016-07-22 15:27:42 +03004791 if (field.type()->isInvariant())
4792 {
4793 error(field.line(), "invalid qualifier on struct member", "invariant");
4794 }
jchen104cdac9e2017-05-08 11:01:20 +08004795 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4796 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004797 {
4798 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4799 }
4800
Olli Etuahoebee5b32017-11-23 12:56:32 +02004801 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004802 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004803
Olli Etuaho43364892017-02-13 16:00:12 +00004804 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4805
4806 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004807
4808 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004809 }
4810
Martin Radev4a9cd802016-09-01 16:51:51 +03004811 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004812 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004813 exitStructDeclaration();
4814
Martin Radev4a9cd802016-09-01 16:51:51 +03004815 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004816}
4817
Jamie Madillb98c3a82015-07-23 14:26:04 -04004818TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004819 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004820 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004821{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004822 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004823 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004824 init->isVector())
4825 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004826 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4827 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004828 return nullptr;
4829 }
4830
Olli Etuaho923ecef2017-10-11 12:01:38 +03004831 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004832 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004833 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004834 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004835 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004836 }
4837
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004838 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4839 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004840 return node;
4841}
4842
4843TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4844{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004845 if (mSwitchNestingLevel == 0)
4846 {
4847 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004848 return nullptr;
4849 }
4850 if (condition == nullptr)
4851 {
4852 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004853 return nullptr;
4854 }
4855 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004856 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004857 {
4858 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004859 }
4860 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004861 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4862 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4863 // fold in case labels.
4864 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004865 {
4866 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004867 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004868 TIntermCase *node = new TIntermCase(condition);
4869 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004870 return node;
4871}
4872
4873TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4874{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004875 if (mSwitchNestingLevel == 0)
4876 {
4877 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004878 return nullptr;
4879 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004880 TIntermCase *node = new TIntermCase(nullptr);
4881 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004882 return node;
4883}
4884
Jamie Madillb98c3a82015-07-23 14:26:04 -04004885TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4886 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004887 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004888{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004889 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004890
4891 switch (op)
4892 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004893 case EOpLogicalNot:
4894 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4895 child->isVector())
4896 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004897 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004898 return nullptr;
4899 }
4900 break;
4901 case EOpBitwiseNot:
4902 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4903 child->isMatrix() || child->isArray())
4904 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004905 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004906 return nullptr;
4907 }
4908 break;
4909 case EOpPostIncrement:
4910 case EOpPreIncrement:
4911 case EOpPostDecrement:
4912 case EOpPreDecrement:
4913 case EOpNegative:
4914 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004915 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4916 child->getBasicType() == EbtBool || child->isArray() ||
4917 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004918 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004919 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004920 return nullptr;
4921 }
Nico Weber41b072b2018-02-09 10:01:32 -05004922 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004923 // Operators for built-ins are already type checked against their prototype.
4924 default:
4925 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004926 }
4927
Jiajia Qinbc585152017-06-23 15:42:17 +08004928 if (child->getMemoryQualifier().writeonly)
4929 {
4930 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4931 return nullptr;
4932 }
4933
Olli Etuahof119a262016-08-19 15:54:22 +03004934 TIntermUnary *node = new TIntermUnary(op, child);
4935 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004936
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004937 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004938}
4939
Olli Etuaho09b22472015-02-11 11:47:26 +02004940TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4941{
Olli Etuahocce89652017-06-19 16:04:09 +03004942 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004943 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004944 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004945 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004946 return child;
4947 }
4948 return node;
4949}
4950
Jamie Madillb98c3a82015-07-23 14:26:04 -04004951TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4952 TIntermTyped *child,
4953 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004954{
Olli Etuaho856c4972016-08-08 11:38:39 +03004955 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004956 return addUnaryMath(op, child, loc);
4957}
4958
Olli Etuaho765924f2018-01-04 12:48:36 +02004959TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4960{
4961 // If we can, we should return the folded version of the expression for subsequent parsing. This
4962 // enables folding the containing expression during parsing as well, instead of the separate
4963 // FoldExpressions() step where folding nested expressions requires multiple full AST
4964 // traversals.
4965
4966 // Even if folding fails the fold() functions return some node representing the expression,
4967 // typically the original node. So "folded" can be assumed to be non-null.
4968 TIntermTyped *folded = expression->fold(mDiagnostics);
4969 ASSERT(folded != nullptr);
4970 if (folded->getQualifier() == expression->getQualifier())
4971 {
4972 // We need this expression to have the correct qualifier when validating the consuming
4973 // expression. So we can only return the folded node from here in case it has the same
4974 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4975 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4976 // 1. (true ? 1.0 : non_constant)
4977 // 2. (non_constant, 1.0)
4978 return folded;
4979 }
4980 return expression;
4981}
4982
Jamie Madillb98c3a82015-07-23 14:26:04 -04004983bool TParseContext::binaryOpCommonCheck(TOperator op,
4984 TIntermTyped *left,
4985 TIntermTyped *right,
4986 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004987{
jchen10b4cf5652017-05-05 18:51:17 +08004988 // Check opaque types are not allowed to be operands in expressions other than array indexing
4989 // and structure member selection.
4990 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4991 {
4992 switch (op)
4993 {
4994 case EOpIndexDirect:
4995 case EOpIndexIndirect:
4996 break;
jchen10b4cf5652017-05-05 18:51:17 +08004997
4998 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05004999 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08005000 error(loc, "Invalid operation for variables with an opaque type",
5001 GetOperatorString(op));
5002 return false;
5003 }
5004 }
jchen10cc2a10e2017-05-03 14:05:12 +08005005
Jiajia Qinbc585152017-06-23 15:42:17 +08005006 if (right->getMemoryQualifier().writeonly)
5007 {
5008 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5009 return false;
5010 }
5011
5012 if (left->getMemoryQualifier().writeonly)
5013 {
5014 switch (op)
5015 {
5016 case EOpAssign:
5017 case EOpInitialize:
5018 case EOpIndexDirect:
5019 case EOpIndexIndirect:
5020 case EOpIndexDirectStruct:
5021 case EOpIndexDirectInterfaceBlock:
5022 break;
5023 default:
5024 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5025 return false;
5026 }
5027 }
5028
Olli Etuaho244be012016-08-18 15:26:02 +03005029 if (left->getType().getStruct() || right->getType().getStruct())
5030 {
5031 switch (op)
5032 {
5033 case EOpIndexDirectStruct:
5034 ASSERT(left->getType().getStruct());
5035 break;
5036 case EOpEqual:
5037 case EOpNotEqual:
5038 case EOpAssign:
5039 case EOpInitialize:
5040 if (left->getType() != right->getType())
5041 {
5042 return false;
5043 }
5044 break;
5045 default:
5046 error(loc, "Invalid operation for structs", GetOperatorString(op));
5047 return false;
5048 }
5049 }
5050
Olli Etuaho94050052017-05-08 14:17:44 +03005051 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5052 {
5053 switch (op)
5054 {
5055 case EOpIndexDirectInterfaceBlock:
5056 ASSERT(left->getType().getInterfaceBlock());
5057 break;
5058 default:
5059 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5060 return false;
5061 }
5062 }
5063
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005064 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005065 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005066 error(loc, "array / non-array mismatch", GetOperatorString(op));
5067 return false;
5068 }
5069
5070 if (left->isArray())
5071 {
5072 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005073 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005074 {
5075 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5076 return false;
5077 }
5078
Olli Etuahoe79904c2015-03-18 16:56:42 +02005079 switch (op)
5080 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005081 case EOpEqual:
5082 case EOpNotEqual:
5083 case EOpAssign:
5084 case EOpInitialize:
5085 break;
5086 default:
5087 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5088 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005089 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005090 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005091 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005092 {
5093 error(loc, "array size mismatch", GetOperatorString(op));
5094 return false;
5095 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005096 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005097
5098 // Check ops which require integer / ivec parameters
5099 bool isBitShift = false;
5100 switch (op)
5101 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005102 case EOpBitShiftLeft:
5103 case EOpBitShiftRight:
5104 case EOpBitShiftLeftAssign:
5105 case EOpBitShiftRightAssign:
5106 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5107 // check that the basic type is an integer type.
5108 isBitShift = true;
5109 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5110 {
5111 return false;
5112 }
5113 break;
5114 case EOpBitwiseAnd:
5115 case EOpBitwiseXor:
5116 case EOpBitwiseOr:
5117 case EOpBitwiseAndAssign:
5118 case EOpBitwiseXorAssign:
5119 case EOpBitwiseOrAssign:
5120 // It is enough to check the type of only one operand, since later it
5121 // is checked that the operand types match.
5122 if (!IsInteger(left->getBasicType()))
5123 {
5124 return false;
5125 }
5126 break;
5127 default:
5128 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005129 }
5130
5131 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5132 // So the basic type should usually match.
5133 if (!isBitShift && left->getBasicType() != right->getBasicType())
5134 {
5135 return false;
5136 }
5137
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005138 // Check that:
5139 // 1. Type sizes match exactly on ops that require that.
5140 // 2. Restrictions for structs that contain arrays or samplers are respected.
5141 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005142 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005143 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005144 case EOpAssign:
5145 case EOpInitialize:
5146 case EOpEqual:
5147 case EOpNotEqual:
5148 // ESSL 1.00 sections 5.7, 5.8, 5.9
5149 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5150 {
5151 error(loc, "undefined operation for structs containing arrays",
5152 GetOperatorString(op));
5153 return false;
5154 }
5155 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5156 // we interpret the spec so that this extends to structs containing samplers,
5157 // similarly to ESSL 1.00 spec.
5158 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5159 left->getType().isStructureContainingSamplers())
5160 {
5161 error(loc, "undefined operation for structs containing samplers",
5162 GetOperatorString(op));
5163 return false;
5164 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005165
Olli Etuahoe1805592017-01-02 16:41:20 +00005166 if ((left->getNominalSize() != right->getNominalSize()) ||
5167 (left->getSecondarySize() != right->getSecondarySize()))
5168 {
5169 error(loc, "dimension mismatch", GetOperatorString(op));
5170 return false;
5171 }
5172 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005173 case EOpLessThan:
5174 case EOpGreaterThan:
5175 case EOpLessThanEqual:
5176 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005177 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005178 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005179 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005180 return false;
5181 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005182 break;
5183 case EOpAdd:
5184 case EOpSub:
5185 case EOpDiv:
5186 case EOpIMod:
5187 case EOpBitShiftLeft:
5188 case EOpBitShiftRight:
5189 case EOpBitwiseAnd:
5190 case EOpBitwiseXor:
5191 case EOpBitwiseOr:
5192 case EOpAddAssign:
5193 case EOpSubAssign:
5194 case EOpDivAssign:
5195 case EOpIModAssign:
5196 case EOpBitShiftLeftAssign:
5197 case EOpBitShiftRightAssign:
5198 case EOpBitwiseAndAssign:
5199 case EOpBitwiseXorAssign:
5200 case EOpBitwiseOrAssign:
5201 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5202 {
5203 return false;
5204 }
5205
5206 // Are the sizes compatible?
5207 if (left->getNominalSize() != right->getNominalSize() ||
5208 left->getSecondarySize() != right->getSecondarySize())
5209 {
5210 // If the nominal sizes of operands do not match:
5211 // One of them must be a scalar.
5212 if (!left->isScalar() && !right->isScalar())
5213 return false;
5214
5215 // In the case of compound assignment other than multiply-assign,
5216 // the right side needs to be a scalar. Otherwise a vector/matrix
5217 // would be assigned to a scalar. A scalar can't be shifted by a
5218 // vector either.
5219 if (!right->isScalar() &&
5220 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5221 return false;
5222 }
5223 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005224 default:
5225 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005226 }
5227
Olli Etuahod6b14282015-03-17 14:31:35 +02005228 return true;
5229}
5230
Olli Etuaho1dded802016-08-18 18:13:13 +03005231bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5232 const TType &left,
5233 const TType &right)
5234{
5235 switch (op)
5236 {
5237 case EOpMul:
5238 case EOpMulAssign:
5239 return left.getNominalSize() == right.getNominalSize() &&
5240 left.getSecondarySize() == right.getSecondarySize();
5241 case EOpVectorTimesScalar:
5242 return true;
5243 case EOpVectorTimesScalarAssign:
5244 ASSERT(!left.isMatrix() && !right.isMatrix());
5245 return left.isVector() && !right.isVector();
5246 case EOpVectorTimesMatrix:
5247 return left.getNominalSize() == right.getRows();
5248 case EOpVectorTimesMatrixAssign:
5249 ASSERT(!left.isMatrix() && right.isMatrix());
5250 return left.isVector() && left.getNominalSize() == right.getRows() &&
5251 left.getNominalSize() == right.getCols();
5252 case EOpMatrixTimesVector:
5253 return left.getCols() == right.getNominalSize();
5254 case EOpMatrixTimesScalar:
5255 return true;
5256 case EOpMatrixTimesScalarAssign:
5257 ASSERT(left.isMatrix() && !right.isMatrix());
5258 return !right.isVector();
5259 case EOpMatrixTimesMatrix:
5260 return left.getCols() == right.getRows();
5261 case EOpMatrixTimesMatrixAssign:
5262 ASSERT(left.isMatrix() && right.isMatrix());
5263 // We need to check two things:
5264 // 1. The matrix multiplication step is valid.
5265 // 2. The result will have the same number of columns as the lvalue.
5266 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5267
5268 default:
5269 UNREACHABLE();
5270 return false;
5271 }
5272}
5273
Jamie Madillb98c3a82015-07-23 14:26:04 -04005274TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5275 TIntermTyped *left,
5276 TIntermTyped *right,
5277 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005278{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005279 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005280 return nullptr;
5281
Olli Etuahofc1806e2015-03-17 13:03:11 +02005282 switch (op)
5283 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005284 case EOpEqual:
5285 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005286 case EOpLessThan:
5287 case EOpGreaterThan:
5288 case EOpLessThanEqual:
5289 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005290 break;
5291 case EOpLogicalOr:
5292 case EOpLogicalXor:
5293 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005294 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5295 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005296 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005297 {
5298 return nullptr;
5299 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005300 // Basic types matching should have been already checked.
5301 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005302 break;
5303 case EOpAdd:
5304 case EOpSub:
5305 case EOpDiv:
5306 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005307 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5308 !right->getType().getStruct());
5309 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005310 {
5311 return nullptr;
5312 }
5313 break;
5314 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005315 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5316 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005317 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005318 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005319 {
5320 return nullptr;
5321 }
5322 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005323 default:
5324 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005325 }
5326
Olli Etuaho1dded802016-08-18 18:13:13 +03005327 if (op == EOpMul)
5328 {
5329 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5330 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5331 {
5332 return nullptr;
5333 }
5334 }
5335
Olli Etuaho3fdec912016-08-18 15:08:06 +03005336 TIntermBinary *node = new TIntermBinary(op, left, right);
5337 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005338 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005339}
5340
Jamie Madillb98c3a82015-07-23 14:26:04 -04005341TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5342 TIntermTyped *left,
5343 TIntermTyped *right,
5344 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005345{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005346 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005347 if (node == 0)
5348 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005349 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5350 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005351 return left;
5352 }
5353 return node;
5354}
5355
Jamie Madillb98c3a82015-07-23 14:26:04 -04005356TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5357 TIntermTyped *left,
5358 TIntermTyped *right,
5359 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005360{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005361 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005362 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005363 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005364 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5365 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005366 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005367 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005368 }
5369 return node;
5370}
5371
Olli Etuaho13389b62016-10-16 11:48:18 +01005372TIntermBinary *TParseContext::createAssign(TOperator op,
5373 TIntermTyped *left,
5374 TIntermTyped *right,
5375 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005376{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005377 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005378 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005379 if (op == EOpMulAssign)
5380 {
5381 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5382 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5383 {
5384 return nullptr;
5385 }
5386 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005387 TIntermBinary *node = new TIntermBinary(op, left, right);
5388 node->setLine(loc);
5389
Olli Etuaho3fdec912016-08-18 15:08:06 +03005390 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005391 }
5392 return nullptr;
5393}
5394
Jamie Madillb98c3a82015-07-23 14:26:04 -04005395TIntermTyped *TParseContext::addAssign(TOperator op,
5396 TIntermTyped *left,
5397 TIntermTyped *right,
5398 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005399{
Olli Etuahocce89652017-06-19 16:04:09 +03005400 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005401 TIntermTyped *node = createAssign(op, left, right, loc);
5402 if (node == nullptr)
5403 {
5404 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005405 return left;
5406 }
5407 return node;
5408}
5409
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005410TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5411 TIntermTyped *right,
5412 const TSourceLoc &loc)
5413{
Corentin Wallez0d959252016-07-12 17:26:32 -04005414 // WebGL2 section 5.26, the following results in an error:
5415 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005416 if (mShaderSpec == SH_WEBGL2_SPEC &&
5417 (left->isArray() || left->getBasicType() == EbtVoid ||
5418 left->getType().isStructureContainingArrays() || right->isArray() ||
5419 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005420 {
5421 error(loc,
5422 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5423 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005424 }
5425
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005426 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho765924f2018-01-04 12:48:36 +02005427
5428 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005429}
5430
Olli Etuaho49300862015-02-20 14:54:49 +02005431TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5432{
5433 switch (op)
5434 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005435 case EOpContinue:
5436 if (mLoopNestingLevel <= 0)
5437 {
5438 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005439 }
5440 break;
5441 case EOpBreak:
5442 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5443 {
5444 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005445 }
5446 break;
5447 case EOpReturn:
5448 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5449 {
5450 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005451 }
5452 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005453 case EOpKill:
5454 if (mShaderType != GL_FRAGMENT_SHADER)
5455 {
5456 error(loc, "discard supported in fragment shaders only", "discard");
5457 }
5458 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005459 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005460 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005461 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005462 }
Olli Etuahocce89652017-06-19 16:04:09 +03005463 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005464}
5465
Jamie Madillb98c3a82015-07-23 14:26:04 -04005466TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005467 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005468 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005469{
Olli Etuahocce89652017-06-19 16:04:09 +03005470 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005471 {
Olli Etuahocce89652017-06-19 16:04:09 +03005472 ASSERT(op == EOpReturn);
5473 mFunctionReturnsValue = true;
5474 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5475 {
5476 error(loc, "void function cannot return a value", "return");
5477 }
5478 else if (*mCurrentFunctionType != expression->getType())
5479 {
5480 error(loc, "function return is not matching type:", "return");
5481 }
Olli Etuaho49300862015-02-20 14:54:49 +02005482 }
Olli Etuahocce89652017-06-19 16:04:09 +03005483 TIntermBranch *node = new TIntermBranch(op, expression);
5484 node->setLine(loc);
5485 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005486}
5487
Martin Radev84aa2dc2017-09-11 15:51:02 +03005488void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5489{
5490 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005491 const ImmutableString &name = functionCall->getFunction()->name();
5492 bool isTextureGather = name == kTextureGatherName;
5493 bool isTextureGatherOffset = name == kTextureGatherOffsetName;
Martin Radev84aa2dc2017-09-11 15:51:02 +03005494 if (isTextureGather || isTextureGatherOffset)
5495 {
5496 TIntermNode *componentNode = nullptr;
5497 TIntermSequence *arguments = functionCall->getSequence();
5498 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5499 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5500 ASSERT(sampler != nullptr);
5501 switch (sampler->getBasicType())
5502 {
5503 case EbtSampler2D:
5504 case EbtISampler2D:
5505 case EbtUSampler2D:
5506 case EbtSampler2DArray:
5507 case EbtISampler2DArray:
5508 case EbtUSampler2DArray:
5509 if ((isTextureGather && arguments->size() == 3u) ||
5510 (isTextureGatherOffset && arguments->size() == 4u))
5511 {
5512 componentNode = arguments->back();
5513 }
5514 break;
5515 case EbtSamplerCube:
5516 case EbtISamplerCube:
5517 case EbtUSamplerCube:
5518 ASSERT(!isTextureGatherOffset);
5519 if (arguments->size() == 3u)
5520 {
5521 componentNode = arguments->back();
5522 }
5523 break;
5524 case EbtSampler2DShadow:
5525 case EbtSampler2DArrayShadow:
5526 case EbtSamplerCubeShadow:
5527 break;
5528 default:
5529 UNREACHABLE();
5530 break;
5531 }
5532 if (componentNode)
5533 {
5534 const TIntermConstantUnion *componentConstantUnion =
5535 componentNode->getAsConstantUnion();
5536 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5537 {
5538 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuahofbb1c792018-01-19 16:26:59 +02005539 name);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005540 }
5541 else
5542 {
5543 int component = componentConstantUnion->getIConst(0);
5544 if (component < 0 || component > 3)
5545 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005546 error(functionCall->getLine(), "Component must be in the range [0;3]", name);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005547 }
5548 }
5549 }
5550 }
5551}
5552
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005553void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5554{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005555 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005556 const ImmutableString &name = functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005557 TIntermNode *offset = nullptr;
5558 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005559 bool useTextureGatherOffsetConstraints = false;
Olli Etuahofbb1c792018-01-19 16:26:59 +02005560 if (name == kTexelFetchOffsetName || name == kTextureLodOffsetName ||
5561 name == kTextureProjLodOffsetName || name == kTextureGradOffsetName ||
5562 name == kTextureProjGradOffsetName)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005563 {
5564 offset = arguments->back();
5565 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005566 else if (name == kTextureOffsetName || name == kTextureProjOffsetName)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005567 {
5568 // A bias parameter might follow the offset parameter.
5569 ASSERT(arguments->size() >= 3);
5570 offset = (*arguments)[2];
5571 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005572 else if (name == kTextureGatherOffsetName)
Martin Radev84aa2dc2017-09-11 15:51:02 +03005573 {
5574 ASSERT(arguments->size() >= 3u);
5575 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5576 ASSERT(sampler != nullptr);
5577 switch (sampler->getBasicType())
5578 {
5579 case EbtSampler2D:
5580 case EbtISampler2D:
5581 case EbtUSampler2D:
5582 case EbtSampler2DArray:
5583 case EbtISampler2DArray:
5584 case EbtUSampler2DArray:
5585 offset = (*arguments)[2];
5586 break;
5587 case EbtSampler2DShadow:
5588 case EbtSampler2DArrayShadow:
5589 offset = (*arguments)[3];
5590 break;
5591 default:
5592 UNREACHABLE();
5593 break;
5594 }
5595 useTextureGatherOffsetConstraints = true;
5596 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005597 if (offset != nullptr)
5598 {
5599 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5600 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5601 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005602 error(functionCall->getLine(), "Texture offset must be a constant expression", name);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005603 }
5604 else
5605 {
5606 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5607 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005608 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005609 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5610 : mMinProgramTexelOffset;
5611 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5612 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005613 for (size_t i = 0u; i < size; ++i)
5614 {
5615 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005616 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005617 {
5618 std::stringstream tokenStream;
5619 tokenStream << offsetValue;
5620 std::string token = tokenStream.str();
5621 error(offset->getLine(), "Texture offset value out of valid range",
5622 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005623 }
5624 }
5625 }
5626 }
5627}
5628
Jiajia Qina3106c52017-11-03 09:39:39 +08005629void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5630{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005631 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005632 const ImmutableString &functionName = functionCall->getFunction()->name();
Olli Etuahobed35d72017-12-20 16:36:26 +02005633 if (IsAtomicBuiltin(functionName))
Jiajia Qina3106c52017-11-03 09:39:39 +08005634 {
5635 TIntermSequence *arguments = functionCall->getSequence();
5636 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5637
5638 if (IsBufferOrSharedVariable(memNode))
5639 {
5640 return;
5641 }
5642
5643 while (memNode->getAsBinaryNode())
5644 {
5645 memNode = memNode->getAsBinaryNode()->getLeft();
5646 if (IsBufferOrSharedVariable(memNode))
5647 {
5648 return;
5649 }
5650 }
5651
5652 error(memNode->getLine(),
5653 "The value passed to the mem argument of an atomic memory function does not "
5654 "correspond to a buffer or shared variable.",
Olli Etuahofbb1c792018-01-19 16:26:59 +02005655 functionName);
Jiajia Qina3106c52017-11-03 09:39:39 +08005656 }
5657}
5658
Martin Radev2cc85b32016-08-05 16:22:53 +03005659// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5660void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5661{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005662 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005663
Olli Etuahofbb1c792018-01-19 16:26:59 +02005664 if (functionCall->getFunction()->isImageFunction())
Martin Radev2cc85b32016-08-05 16:22:53 +03005665 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005666 const ImmutableString &name = functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005667 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005668 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005669
Olli Etuaho485eefd2017-02-14 17:40:06 +00005670 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005671
Olli Etuahofbb1c792018-01-19 16:26:59 +02005672 if (strcmp(name.data() + 5u, "Store") == 0)
Martin Radev2cc85b32016-08-05 16:22:53 +03005673 {
5674 if (memoryQualifier.readonly)
5675 {
5676 error(imageNode->getLine(),
5677 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005678 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005679 }
5680 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005681 else if (strcmp(name.data() + 5u, "Load") == 0)
Martin Radev2cc85b32016-08-05 16:22:53 +03005682 {
5683 if (memoryQualifier.writeonly)
5684 {
5685 error(imageNode->getLine(),
5686 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005687 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005688 }
5689 }
5690 }
5691}
5692
5693// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5694void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5695 const TFunction *functionDefinition,
5696 const TIntermAggregate *functionCall)
5697{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005698 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005699
5700 const TIntermSequence &arguments = *functionCall->getSequence();
5701
5702 ASSERT(functionDefinition->getParamCount() == arguments.size());
5703
5704 for (size_t i = 0; i < arguments.size(); ++i)
5705 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005706 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5707 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005708 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5709 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5710
5711 if (IsImage(functionArgumentType.getBasicType()))
5712 {
5713 const TMemoryQualifier &functionArgumentMemoryQualifier =
5714 functionArgumentType.getMemoryQualifier();
5715 const TMemoryQualifier &functionParameterMemoryQualifier =
5716 functionParameterType.getMemoryQualifier();
5717 if (functionArgumentMemoryQualifier.readonly &&
5718 !functionParameterMemoryQualifier.readonly)
5719 {
5720 error(functionCall->getLine(),
5721 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005722 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005723 }
5724
5725 if (functionArgumentMemoryQualifier.writeonly &&
5726 !functionParameterMemoryQualifier.writeonly)
5727 {
5728 error(functionCall->getLine(),
5729 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005730 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005731 }
Martin Radev049edfa2016-11-11 14:35:37 +02005732
5733 if (functionArgumentMemoryQualifier.coherent &&
5734 !functionParameterMemoryQualifier.coherent)
5735 {
5736 error(functionCall->getLine(),
5737 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005738 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005739 }
5740
5741 if (functionArgumentMemoryQualifier.volatileQualifier &&
5742 !functionParameterMemoryQualifier.volatileQualifier)
5743 {
5744 error(functionCall->getLine(),
5745 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005746 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005747 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005748 }
5749 }
5750}
5751
Olli Etuaho95ed1942018-02-01 14:01:19 +02005752TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005753{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005754 if (fnCall->thisNode() != nullptr)
5755 {
5756 return addMethod(fnCall, loc);
5757 }
5758 if (fnCall->isConstructor())
5759 {
5760 return addConstructor(fnCall, loc);
5761 }
5762 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005763}
5764
Olli Etuaho95ed1942018-02-01 14:01:19 +02005765TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005766{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005767 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005768 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5769 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5770 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005771 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005772 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005773 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005774 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005775 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005776 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005777 {
5778 error(loc, "method takes no parameters", "length");
5779 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005780 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005781 {
5782 error(loc, "length can only be called on arrays", "length");
5783 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005784 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005785 mGeometryShaderInputPrimitiveType == EptUndefined)
5786 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005787 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005788 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5789 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005790 else
5791 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005792 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005793 node->setLine(loc);
5794 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005795 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005796 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005797}
5798
Olli Etuaho95ed1942018-02-01 14:01:19 +02005799TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005800 const TSourceLoc &loc)
5801{
Olli Etuaho697bf652018-02-16 11:50:54 +02005802 // First check whether the function has been hidden by a variable name or struct typename by
5803 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5804 // with a matching argument list.
5805 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005806 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005807 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005808 }
5809 else
5810 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005811 // There are no inner functions, so it's enough to look for user-defined functions in the
5812 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005813 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005814 if (symbol != nullptr)
5815 {
5816 // A user-defined function - could be an overloaded built-in as well.
5817 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5818 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5819 TIntermAggregate *callNode =
5820 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5821 callNode->setLine(loc);
5822 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5823 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5824 return callNode;
5825 }
5826
5827 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005828 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005829 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005830 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005831 }
5832 else
5833 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005834 // A built-in function.
5835 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005836 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005837
Olli Etuaho37b697e2018-01-29 12:19:27 +02005838 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005839 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005840 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005841 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005842 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005843 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005844 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005845 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005846 if (fnCandidate->getParamCount() == 1)
5847 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005848 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005849 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005850 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005851 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005852 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005853 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005854 TIntermAggregate *callNode =
5855 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005856 callNode->setLine(loc);
5857
Olli Etuahoe80825e2018-02-16 10:24:53 +02005858 // Some built-in functions have out parameters too.
5859 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5860
5861 // See if we can constant fold a built-in. Note that this may be possible
5862 // even if it is not const-qualified.
5863 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005864 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005865
5866 // This is a built-in function with no op associated with it.
5867 TIntermAggregate *callNode =
5868 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5869 callNode->setLine(loc);
5870 checkTextureOffsetConst(callNode);
5871 checkTextureGather(callNode);
5872 checkImageMemoryAccessForBuiltinFunctions(callNode);
5873 checkAtomicMemoryBuiltinFunctions(callNode);
5874 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5875 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005876 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005877 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005878
5879 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005880 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005881}
5882
Jamie Madillb98c3a82015-07-23 14:26:04 -04005883TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005884 TIntermTyped *trueExpression,
5885 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005886 const TSourceLoc &loc)
5887{
Olli Etuaho56229f12017-07-10 14:16:33 +03005888 if (!checkIsScalarBool(loc, cond))
5889 {
5890 return falseExpression;
5891 }
Olli Etuaho52901742015-04-15 13:42:45 +03005892
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005893 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005894 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005895 std::stringstream reasonStream;
5896 reasonStream << "mismatching ternary operator operand types '"
5897 << trueExpression->getCompleteString() << " and '"
5898 << falseExpression->getCompleteString() << "'";
5899 std::string reason = reasonStream.str();
5900 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005901 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005902 }
Olli Etuahode318b22016-10-25 16:18:25 +01005903 if (IsOpaqueType(trueExpression->getBasicType()))
5904 {
5905 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005906 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005907 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5908 // Note that structs containing opaque types don't need to be checked as structs are
5909 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005910 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005911 return falseExpression;
5912 }
5913
Jiajia Qinbc585152017-06-23 15:42:17 +08005914 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5915 falseExpression->getMemoryQualifier().writeonly)
5916 {
5917 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5918 return falseExpression;
5919 }
5920
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005921 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005922 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005923 // ESSL 3.00.6 section 5.7:
5924 // Ternary operator support is optional for arrays. No certainty that it works across all
5925 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5926 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005927 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005928 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005929 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005930 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005931 }
Olli Etuaho94050052017-05-08 14:17:44 +03005932 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5933 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005934 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005935 return falseExpression;
5936 }
5937
Corentin Wallez0d959252016-07-12 17:26:32 -04005938 // WebGL2 section 5.26, the following results in an error:
5939 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005940 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005941 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005942 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005943 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005944 }
5945
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005946 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5947 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005948 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005949}
Olli Etuaho49300862015-02-20 14:54:49 +02005950
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005951//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005952// Parse an array of strings using yyparse.
5953//
5954// Returns 0 for success.
5955//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005956int PaParseStrings(size_t count,
5957 const char *const string[],
5958 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305959 TParseContext *context)
5960{
Yunchao He4f285442017-04-21 12:15:49 +08005961 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005962 return 1;
5963
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005964 if (glslang_initialize(context))
5965 return 1;
5966
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005967 int error = glslang_scan(count, string, length, context);
5968 if (!error)
5969 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005970
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005971 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005972
alokp@chromium.org6b495712012-06-29 00:06:58 +00005973 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005974}
Jamie Madill45bcc782016-11-07 13:58:48 -05005975
5976} // namespace sh