blob: fdc7652e1f97b7979aa4194cfd5e26ef87c16672 [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 Etuaho28cf9202018-03-02 15:54:56 +020036constexpr const ImmutableString kTexelFetchOffsetName("texelFetchOffset");
Olli Etuahofbb1c792018-01-19 16:26:59 +020037constexpr 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:
Qin Jiajia76bf01d2018-02-22 14:11:34 +0800499 if (node->getMemoryQualifier().readonly)
500 {
501 error(line, "can't modify a readonly variable", op);
502 return false;
503 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300504 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400505 default:
506 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000507 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000508 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300509 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000510 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000511
jchen10cc2a10e2017-05-03 14:05:12 +0800512 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530513 switch (node->getQualifier())
514 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400515 case EvqConst:
516 message = "can't modify a const";
517 break;
518 case EvqConstReadOnly:
519 message = "can't modify a const";
520 break;
521 case EvqAttribute:
522 message = "can't modify an attribute";
523 break;
524 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400525 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800526 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800527 case EvqFlatIn:
528 case EvqSmoothIn:
529 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400530 message = "can't modify an input";
531 break;
532 case EvqUniform:
533 message = "can't modify a uniform";
534 break;
535 case EvqVaryingIn:
536 message = "can't modify a varying";
537 break;
538 case EvqFragCoord:
539 message = "can't modify gl_FragCoord";
540 break;
541 case EvqFrontFacing:
542 message = "can't modify gl_FrontFacing";
543 break;
544 case EvqPointCoord:
545 message = "can't modify gl_PointCoord";
546 break;
Martin Radevb0883602016-08-04 17:48:58 +0300547 case EvqNumWorkGroups:
548 message = "can't modify gl_NumWorkGroups";
549 break;
550 case EvqWorkGroupSize:
551 message = "can't modify gl_WorkGroupSize";
552 break;
553 case EvqWorkGroupID:
554 message = "can't modify gl_WorkGroupID";
555 break;
556 case EvqLocalInvocationID:
557 message = "can't modify gl_LocalInvocationID";
558 break;
559 case EvqGlobalInvocationID:
560 message = "can't modify gl_GlobalInvocationID";
561 break;
562 case EvqLocalInvocationIndex:
563 message = "can't modify gl_LocalInvocationIndex";
564 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300565 case EvqViewIDOVR:
566 message = "can't modify gl_ViewID_OVR";
567 break;
Martin Radev802abe02016-08-04 17:48:32 +0300568 case EvqComputeIn:
569 message = "can't modify work group size variable";
570 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800571 case EvqPerVertexIn:
572 message = "can't modify any member in gl_in";
573 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800574 case EvqPrimitiveIDIn:
575 message = "can't modify gl_PrimitiveIDIn";
576 break;
577 case EvqInvocationID:
578 message = "can't modify gl_InvocationID";
579 break;
580 case EvqPrimitiveID:
581 if (mShaderType == GL_FRAGMENT_SHADER)
582 {
583 message = "can't modify gl_PrimitiveID in a fragment shader";
584 }
585 break;
586 case EvqLayer:
587 if (mShaderType == GL_FRAGMENT_SHADER)
588 {
589 message = "can't modify gl_Layer in a fragment shader";
590 }
591 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400592 default:
593 //
594 // Type that can't be written to?
595 //
596 if (node->getBasicType() == EbtVoid)
597 {
598 message = "can't modify void";
599 }
jchen10cc2a10e2017-05-03 14:05:12 +0800600 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400601 {
jchen10cc2a10e2017-05-03 14:05:12 +0800602 message = "can't modify a variable with type ";
603 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300604 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800605 else if (node->getMemoryQualifier().readonly)
606 {
607 message = "can't modify a readonly variable";
608 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000609 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000610
jchen10cc2a10e2017-05-03 14:05:12 +0800611 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530612 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000613 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614
Olli Etuaho8a176262016-08-16 14:23:01 +0300615 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000618 //
619 // Everything else is okay, no error.
620 //
jchen10cc2a10e2017-05-03 14:05:12 +0800621 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300622 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000624 //
625 // If we get here, we have an error and a message.
626 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530627 if (symNode)
628 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200629 // Symbol inside an expression can't be nameless.
630 ASSERT(symNode->variable().symbolType() != SymbolType::Empty);
631
Olli Etuahofbb1c792018-01-19 16:26:59 +0200632 const ImmutableString &symbol = symNode->getName();
Olli Etuaho4de340a2016-12-16 09:32:03 +0000633 std::stringstream reasonStream;
634 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
635 std::string reason = reasonStream.str();
636 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000637 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530638 else
639 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000640 std::stringstream reasonStream;
641 reasonStream << "l-value required (" << message << ")";
642 std::string reason = reasonStream.str();
643 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000644 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645
Olli Etuaho8a176262016-08-16 14:23:01 +0300646 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647}
648
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649// Both test, and if necessary spit out an error, to see if the node is really
650// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300651void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652{
Olli Etuaho383b7912016-08-05 11:22:59 +0300653 if (node->getQualifier() != EvqConst)
654 {
655 error(node->getLine(), "constant expression required", "");
656 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657}
658
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659// Both test, and if necessary spit out an error, to see if the node is really
660// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300661void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662{
Olli Etuaho383b7912016-08-05 11:22:59 +0300663 if (!node->isScalarInt())
664 {
665 error(node->getLine(), "integer expression required", token);
666 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667}
668
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669// Both test, and if necessary spit out an error, to see if we are currently
670// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800671bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000672{
Olli Etuaho856c4972016-08-08 11:38:39 +0300673 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300674 {
675 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800676 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300677 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800678 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679}
680
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300681// ESSL 3.00.5 sections 3.8 and 3.9.
682// If it starts "gl_" or contains two consecutive underscores, it's reserved.
683// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuahofbb1c792018-01-19 16:26:59 +0200684bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const ImmutableString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530686 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahofbb1c792018-01-19 16:26:59 +0200687 if (identifier.beginsWith("gl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530688 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300689 error(line, reservedErrMsg, "gl_");
690 return false;
691 }
692 if (sh::IsWebGLBasedSpec(mShaderSpec))
693 {
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 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200699 if (identifier.beginsWith("_webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530700 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300701 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300702 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000703 }
704 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200705 if (identifier.contains("__"))
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300706 {
707 error(line,
708 "identifiers containing two consecutive underscores (__) are reserved as "
709 "possible future keywords",
Olli Etuahofbb1c792018-01-19 16:26:59 +0200710 identifier);
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300711 return false;
712 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300713 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000714}
715
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300716// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300717bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuaho95ed1942018-02-01 14:01:19 +0200718 const TIntermSequence &arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300719 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000720{
Olli Etuaho95ed1942018-02-01 14:01:19 +0200721 if (arguments.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530722 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200723 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300724 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000725 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200726
Olli Etuaho95ed1942018-02-01 14:01:19 +0200727 for (TIntermNode *arg : arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530728 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300729 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200730 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300731 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200732 {
jchen10cc2a10e2017-05-03 14:05:12 +0800733 std::string reason("cannot convert a variable with type ");
734 reason += getBasicString(argTyped->getBasicType());
735 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300736 return false;
737 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800738 else if (argTyped->getMemoryQualifier().writeonly)
739 {
740 error(line, "cannot convert a variable with writeonly", "constructor");
741 return false;
742 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200743 if (argTyped->getBasicType() == EbtVoid)
744 {
745 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300746 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200747 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000748 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000749
Olli Etuaho856c4972016-08-08 11:38:39 +0300750 if (type.isArray())
751 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300752 // The size of an unsized constructor should already have been determined.
753 ASSERT(!type.isUnsizedArray());
Olli Etuaho95ed1942018-02-01 14:01:19 +0200754 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300755 {
756 error(line, "array constructor needs one argument per array element", "constructor");
757 return false;
758 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300759 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
760 // the array.
Olli Etuaho95ed1942018-02-01 14:01:19 +0200761 for (TIntermNode *const &argNode : arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300762 {
763 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300764 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500765 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300766 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500767 return false;
768 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300769 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300770 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000771 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300772 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300773 }
774 }
775 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300776 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300777 {
778 const TFieldList &fields = type.getStruct()->fields();
Olli Etuaho95ed1942018-02-01 14:01:19 +0200779 if (fields.size() != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300780 {
781 error(line,
782 "Number of constructor parameters does not match the number of structure fields",
783 "constructor");
784 return false;
785 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300786
787 for (size_t i = 0; i < fields.size(); i++)
788 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200789 if (i >= arguments.size() ||
790 arguments[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300791 {
792 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000793 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300794 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300795 }
796 }
797 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300798 else
799 {
800 // We're constructing a scalar, vector, or matrix.
801
802 // Note: It's okay to have too many components available, but not okay to have unused
803 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
804 // there is an extra argument, so 'overFull' will become true.
805
806 size_t size = 0;
807 bool full = false;
808 bool overFull = false;
809 bool matrixArg = false;
Olli Etuaho95ed1942018-02-01 14:01:19 +0200810 for (TIntermNode *arg : arguments)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300811 {
812 const TIntermTyped *argTyped = arg->getAsTyped();
813 ASSERT(argTyped != nullptr);
814
Olli Etuaho487b63a2017-05-23 15:55:09 +0300815 if (argTyped->getBasicType() == EbtStruct)
816 {
817 error(line, "a struct cannot be used as a constructor argument for this type",
818 "constructor");
819 return false;
820 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300821 if (argTyped->getType().isArray())
822 {
823 error(line, "constructing from a non-dereferenced array", "constructor");
824 return false;
825 }
826 if (argTyped->getType().isMatrix())
827 {
828 matrixArg = true;
829 }
830
831 size += argTyped->getType().getObjectSize();
832 if (full)
833 {
834 overFull = true;
835 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300836 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300837 {
838 full = true;
839 }
840 }
841
842 if (type.isMatrix() && matrixArg)
843 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200844 if (arguments.size() != 1)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300845 {
846 error(line, "constructing matrix from matrix can only take one argument",
847 "constructor");
848 return false;
849 }
850 }
851 else
852 {
853 if (size != 1 && size < type.getObjectSize())
854 {
855 error(line, "not enough data provided for construction", "constructor");
856 return false;
857 }
858 if (overFull)
859 {
860 error(line, "too many arguments", "constructor");
861 return false;
862 }
863 }
864 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300865
Olli Etuaho8a176262016-08-16 14:23:01 +0300866 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867}
868
Jamie Madillb98c3a82015-07-23 14:26:04 -0400869// This function checks to see if a void variable has been declared and raise an error message for
870// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871//
872// returns true in case of an error
873//
Olli Etuaho856c4972016-08-08 11:38:39 +0300874bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200875 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400876 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300878 if (type == EbtVoid)
879 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200880 error(line, "illegal use of type 'void'", identifier);
Olli Etuaho8a176262016-08-16 14:23:01 +0300881 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300882 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883
Olli Etuaho8a176262016-08-16 14:23:01 +0300884 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885}
886
Jamie Madillb98c3a82015-07-23 14:26:04 -0400887// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300888// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300889bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300891 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530892 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000893 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300894 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530895 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300896 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897}
898
Jamie Madillb98c3a82015-07-23 14:26:04 -0400899// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300900// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300901void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902{
Martin Radev4a9cd802016-09-01 16:51:51 +0300903 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530904 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000905 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530906 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907}
908
jchen10cc2a10e2017-05-03 14:05:12 +0800909bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
910 const TTypeSpecifierNonArray &pType,
911 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530913 if (pType.type == EbtStruct)
914 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300915 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530916 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000917 std::stringstream reasonStream;
918 reasonStream << reason << " (structure contains a sampler)";
919 std::string reasonStr = reasonStream.str();
920 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300921 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000922 }
jchen10cc2a10e2017-05-03 14:05:12 +0800923 // only samplers need to be checked from structs, since other opaque types can't be struct
924 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300925 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530926 }
jchen10cc2a10e2017-05-03 14:05:12 +0800927 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530928 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000929 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300930 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932
Olli Etuaho8a176262016-08-16 14:23:01 +0300933 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934}
935
Olli Etuaho856c4972016-08-08 11:38:39 +0300936void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
937 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400938{
939 if (pType.layoutQualifier.location != -1)
940 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400941 error(line, "location must only be specified for a single input or output variable",
942 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400943 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400944}
945
Olli Etuaho856c4972016-08-08 11:38:39 +0300946void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
947 const TLayoutQualifier &layoutQualifier)
948{
949 if (layoutQualifier.location != -1)
950 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000951 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
952 if (mShaderVersion >= 310)
953 {
954 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800955 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000956 }
957 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300958 }
959}
960
Qin Jiajiaca68d982017-09-18 16:41:56 +0800961void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
962 const TLayoutBlockStorage &blockStorage,
963 const TQualifier &qualifier)
964{
965 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
966 {
967 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
968 }
969}
970
Martin Radev2cc85b32016-08-05 16:22:53 +0300971void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
972 TQualifier qualifier,
973 const TType &type)
974{
Martin Radev2cc85b32016-08-05 16:22:53 +0300975 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800976 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530977 {
jchen10cc2a10e2017-05-03 14:05:12 +0800978 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000979 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980}
981
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300983unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530985 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000986
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200987 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
988 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
989 // fold as array size.
990 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000991 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000992 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300993 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000994 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995
Olli Etuaho856c4972016-08-08 11:38:39 +0300996 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400997
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000998 if (constant->getBasicType() == EbtUInt)
999 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001000 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001001 }
1002 else
1003 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001004 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001005
Olli Etuaho856c4972016-08-08 11:38:39 +03001006 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001007 {
Nicolas Capens906744a2014-06-06 15:18:07 -04001008 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001009 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001010 }
Nicolas Capens906744a2014-06-06 15:18:07 -04001011
Olli Etuaho856c4972016-08-08 11:38:39 +03001012 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -04001013 }
1014
Olli Etuaho856c4972016-08-08 11:38:39 +03001015 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -04001016 {
1017 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001018 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -04001019 }
1020
1021 // The size of arrays is restricted here to prevent issues further down the
1022 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1023 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1024 const unsigned int sizeLimit = 65536;
1025
Olli Etuaho856c4972016-08-08 11:38:39 +03001026 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001027 {
1028 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001029 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001030 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001031
1032 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033}
1034
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001036bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1037 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038{
Olli Etuaho8a176262016-08-16 14:23:01 +03001039 if ((elementQualifier.qualifier == EvqAttribute) ||
1040 (elementQualifier.qualifier == EvqVertexIn) ||
1041 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001042 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001043 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001044 TType(elementQualifier).getQualifierString());
1045 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001046 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001047
Olli Etuaho8a176262016-08-16 14:23:01 +03001048 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049}
1050
Olli Etuaho8a176262016-08-16 14:23:01 +03001051// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001052bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1053 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001054{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001055 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001056 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001057 error(line, "cannot declare arrays of arrays",
1058 TType(elementType).getCompleteString().c_str());
1059 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001060 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001061 return true;
1062}
1063
1064// Check if this qualified element type can be formed into an array. This is only called when array
1065// brackets are associated with an identifier in a declaration, like this:
1066// float a[2];
1067// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1068// are associated with the type, like this:
1069// float[2] a;
1070bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1071 const TPublicType &elementType)
1072{
1073 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1074 {
1075 return false;
1076 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001077 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1078 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1079 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001080 // Geometry shader requires each user-defined input be declared as arrays or inside input
1081 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1082 // interface matching, such variables and blocks are treated as though they were not declared
1083 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001084 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001085 sh::IsVarying(elementType.qualifier) &&
1086 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001087 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001088 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001089 TType(elementType).getCompleteString().c_str());
1090 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001091 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001092 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093}
1094
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001096void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001097 const ImmutableString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001098 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099{
Olli Etuaho3739d232015-04-08 12:23:44 +03001100 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001101 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001102 {
1103 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001104 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001105
1106 // Generate informative error messages for ESSL1.
1107 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001108 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001109 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301110 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001111 "structures containing arrays may not be declared constant since they cannot be "
1112 "initialized",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001113 identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001114 }
1115 else
1116 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001117 error(line, "variables with qualifier 'const' must be initialized", identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001118 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001119 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001120 // This will make the type sized if it isn't sized yet.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001121 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized", identifier,
1122 type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001123}
1124
Olli Etuaho2935c582015-04-08 14:32:06 +03001125// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001126// and update the symbol table.
1127//
Olli Etuaho2935c582015-04-08 14:32:06 +03001128// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001130bool TParseContext::declareVariable(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001131 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001132 const TType *type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001133 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134{
Olli Etuaho2935c582015-04-08 14:32:06 +03001135 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001136
Olli Etuahofbb1c792018-01-19 16:26:59 +02001137 (*variable) = new TVariable(&symbolTable, identifier, type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02001138
Olli Etuahob60d30f2018-01-16 12:31:06 +02001139 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001140
Olli Etuaho856c4972016-08-08 11:38:39 +03001141 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001142
Olli Etuaho2935c582015-04-08 14:32:06 +03001143 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahofbb1c792018-01-19 16:26:59 +02001144 if (type->isArray() && identifier.beginsWith("gl_LastFragData"))
Olli Etuaho2935c582015-04-08 14:32:06 +03001145 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001146 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02001147 symbolTable.findBuiltIn(ImmutableString("gl_MaxDrawBuffers"), mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001148 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001149 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001150 error(line, "redeclaration of gl_LastFragData as an array of arrays", identifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001151 return false;
1152 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001153 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001154 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001155 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001156 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001157 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001158 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001159 }
1160 }
1161 else
1162 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001163 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001164 identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001165 return false;
1166 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001167 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001168
Olli Etuaho8a176262016-08-16 14:23:01 +03001169 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001170 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001171
Olli Etuaho437664b2018-02-28 15:38:14 +02001172 if (!symbolTable.declare(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001173 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001174 error(line, "redefinition", identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001175 return false;
1176 }
1177
Olli Etuahob60d30f2018-01-16 12:31:06 +02001178 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001179 return false;
1180
1181 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001182}
1183
Martin Radev70866b82016-07-22 15:27:42 +03001184void TParseContext::checkIsParameterQualifierValid(
1185 const TSourceLoc &line,
1186 const TTypeQualifierBuilder &typeQualifierBuilder,
1187 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301188{
Olli Etuahocce89652017-06-19 16:04:09 +03001189 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001190 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001191
1192 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301193 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001194 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1195 }
1196
1197 if (!IsImage(type->getBasicType()))
1198 {
Olli Etuaho43364892017-02-13 16:00:12 +00001199 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001200 }
1201 else
1202 {
1203 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001204 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001205
Martin Radev70866b82016-07-22 15:27:42 +03001206 type->setQualifier(typeQualifier.qualifier);
1207
1208 if (typeQualifier.precision != EbpUndefined)
1209 {
1210 type->setPrecision(typeQualifier.precision);
1211 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001212}
1213
Olli Etuaho703671e2017-11-08 17:47:18 +02001214template <size_t size>
1215bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1216 const std::array<TExtension, size> &extensions)
1217{
1218 ASSERT(!extensions.empty());
1219 const TExtensionBehavior &extBehavior = extensionBehavior();
1220
1221 bool canUseWithWarning = false;
1222 bool canUseWithoutWarning = false;
1223
1224 const char *errorMsgString = "";
1225 TExtension errorMsgExtension = TExtension::UNDEFINED;
1226
1227 for (TExtension extension : extensions)
1228 {
1229 auto extIter = extBehavior.find(extension);
1230 if (canUseWithWarning)
1231 {
1232 // We already have an extension that we can use, but with a warning.
1233 // See if we can use the alternative extension without a warning.
1234 if (extIter == extBehavior.end())
1235 {
1236 continue;
1237 }
1238 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1239 {
1240 canUseWithoutWarning = true;
1241 break;
1242 }
1243 continue;
1244 }
1245 if (extIter == extBehavior.end())
1246 {
1247 errorMsgString = "extension is not supported";
1248 errorMsgExtension = extension;
1249 }
1250 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1251 {
1252 errorMsgString = "extension is disabled";
1253 errorMsgExtension = extension;
1254 }
1255 else if (extIter->second == EBhWarn)
1256 {
1257 errorMsgExtension = extension;
1258 canUseWithWarning = true;
1259 }
1260 else
1261 {
1262 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1263 canUseWithoutWarning = true;
1264 break;
1265 }
1266 }
1267
1268 if (canUseWithoutWarning)
1269 {
1270 return true;
1271 }
1272 if (canUseWithWarning)
1273 {
1274 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1275 return true;
1276 }
1277 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1278 return false;
1279}
1280
1281template bool TParseContext::checkCanUseOneOfExtensions(
1282 const TSourceLoc &line,
1283 const std::array<TExtension, 1> &extensions);
1284template bool TParseContext::checkCanUseOneOfExtensions(
1285 const TSourceLoc &line,
1286 const std::array<TExtension, 2> &extensions);
1287template bool TParseContext::checkCanUseOneOfExtensions(
1288 const TSourceLoc &line,
1289 const std::array<TExtension, 3> &extensions);
1290
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001291bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001292{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001293 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001294 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001295}
1296
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001297// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1298// compile-time or link-time errors are the same whether or not the declaration is empty".
1299// This function implements all the checks that are done on qualifiers regardless of if the
1300// declaration is empty.
1301void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1302 const sh::TLayoutQualifier &layoutQualifier,
1303 const TSourceLoc &location)
1304{
1305 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1306 {
1307 error(location, "Shared memory declarations cannot have layout specified", "layout");
1308 }
1309
1310 if (layoutQualifier.matrixPacking != EmpUnspecified)
1311 {
1312 error(location, "layout qualifier only valid for interface blocks",
1313 getMatrixPackingString(layoutQualifier.matrixPacking));
1314 return;
1315 }
1316
1317 if (layoutQualifier.blockStorage != EbsUnspecified)
1318 {
1319 error(location, "layout qualifier only valid for interface blocks",
1320 getBlockStorageString(layoutQualifier.blockStorage));
1321 return;
1322 }
1323
1324 if (qualifier == EvqFragmentOut)
1325 {
1326 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1327 {
1328 error(location, "invalid layout qualifier combination", "yuv");
1329 return;
1330 }
1331 }
1332 else
1333 {
1334 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1335 }
1336
Olli Etuaho95468d12017-05-04 11:14:34 +03001337 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1338 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001339 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1340 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001341 {
1342 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1343 }
1344
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001345 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001346 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001347 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001348 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001349 // We're not checking whether the uniform location is in range here since that depends on
1350 // the type of the variable.
1351 // The type can only be fully determined for non-empty declarations.
1352 }
1353 if (!canHaveLocation)
1354 {
1355 checkLocationIsNotSpecified(location, layoutQualifier);
1356 }
1357}
1358
jchen104cdac9e2017-05-08 11:01:20 +08001359void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1360 const TSourceLoc &location)
1361{
1362 if (publicType.precision != EbpHigh)
1363 {
1364 error(location, "Can only be highp", "atomic counter");
1365 }
1366 // dEQP enforces compile error if location is specified. See uniform_location.test.
1367 if (publicType.layoutQualifier.location != -1)
1368 {
1369 error(location, "location must not be set for atomic_uint", "layout");
1370 }
1371 if (publicType.layoutQualifier.binding == -1)
1372 {
1373 error(location, "no binding specified", "atomic counter");
1374 }
1375}
1376
Olli Etuaho55bde912017-10-25 13:41:13 +03001377void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001378{
Olli Etuaho55bde912017-10-25 13:41:13 +03001379 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001380 {
1381 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1382 // error. It is assumed that this applies to empty declarations as well.
1383 error(location, "empty array declaration needs to specify a size", "");
1384 }
Martin Radevb8b01222016-11-20 23:25:53 +02001385}
1386
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001387// These checks are done for all declarations that are non-empty. They're done for non-empty
1388// declarations starting a declarator list, and declarators that follow an empty declaration.
1389void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1390 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001391{
Olli Etuahofa33d582015-04-09 14:33:12 +03001392 switch (publicType.qualifier)
1393 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001394 case EvqVaryingIn:
1395 case EvqVaryingOut:
1396 case EvqAttribute:
1397 case EvqVertexIn:
1398 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001399 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001400 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001401 {
1402 error(identifierLocation, "cannot be used with a structure",
1403 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001404 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001405 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001406 break;
1407 case EvqBuffer:
1408 if (publicType.getBasicType() != EbtInterfaceBlock)
1409 {
1410 error(identifierLocation,
1411 "cannot declare buffer variables at global scope(outside a block)",
1412 getQualifierString(publicType.qualifier));
1413 return;
1414 }
1415 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001416 default:
1417 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001418 }
jchen10cc2a10e2017-05-03 14:05:12 +08001419 std::string reason(getBasicString(publicType.getBasicType()));
1420 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001421 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001422 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001423 {
1424 return;
1425 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001426
Andrei Volykhina5527072017-03-22 16:46:30 +03001427 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1428 publicType.qualifier != EvqConst) &&
1429 publicType.getBasicType() == EbtYuvCscStandardEXT)
1430 {
1431 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1432 getQualifierString(publicType.qualifier));
1433 return;
1434 }
1435
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001436 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1437 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001438 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1439 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001440 TType type(publicType);
1441 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001442 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001443 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1444 publicType.layoutQualifier);
1445 }
1446 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001447
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001448 // check for layout qualifier issues
1449 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001450
Martin Radev2cc85b32016-08-05 16:22:53 +03001451 if (IsImage(publicType.getBasicType()))
1452 {
1453
1454 switch (layoutQualifier.imageInternalFormat)
1455 {
1456 case EiifRGBA32F:
1457 case EiifRGBA16F:
1458 case EiifR32F:
1459 case EiifRGBA8:
1460 case EiifRGBA8_SNORM:
1461 if (!IsFloatImage(publicType.getBasicType()))
1462 {
1463 error(identifierLocation,
1464 "internal image format requires a floating image type",
1465 getBasicString(publicType.getBasicType()));
1466 return;
1467 }
1468 break;
1469 case EiifRGBA32I:
1470 case EiifRGBA16I:
1471 case EiifRGBA8I:
1472 case EiifR32I:
1473 if (!IsIntegerImage(publicType.getBasicType()))
1474 {
1475 error(identifierLocation,
1476 "internal image format requires an integer image type",
1477 getBasicString(publicType.getBasicType()));
1478 return;
1479 }
1480 break;
1481 case EiifRGBA32UI:
1482 case EiifRGBA16UI:
1483 case EiifRGBA8UI:
1484 case EiifR32UI:
1485 if (!IsUnsignedImage(publicType.getBasicType()))
1486 {
1487 error(identifierLocation,
1488 "internal image format requires an unsigned image type",
1489 getBasicString(publicType.getBasicType()));
1490 return;
1491 }
1492 break;
1493 case EiifUnspecified:
1494 error(identifierLocation, "layout qualifier", "No image internal format specified");
1495 return;
1496 default:
1497 error(identifierLocation, "layout qualifier", "unrecognized token");
1498 return;
1499 }
1500
1501 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1502 switch (layoutQualifier.imageInternalFormat)
1503 {
1504 case EiifR32F:
1505 case EiifR32I:
1506 case EiifR32UI:
1507 break;
1508 default:
1509 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1510 {
1511 error(identifierLocation, "layout qualifier",
1512 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1513 "image variables must be qualified readonly and/or writeonly");
1514 return;
1515 }
1516 break;
1517 }
1518 }
1519 else
1520 {
Olli Etuaho43364892017-02-13 16:00:12 +00001521 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001522 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1523 }
jchen104cdac9e2017-05-08 11:01:20 +08001524
1525 if (IsAtomicCounter(publicType.getBasicType()))
1526 {
1527 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1528 }
1529 else
1530 {
1531 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1532 }
Olli Etuaho43364892017-02-13 16:00:12 +00001533}
Martin Radev2cc85b32016-08-05 16:22:53 +03001534
Olli Etuaho43364892017-02-13 16:00:12 +00001535void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1536{
1537 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001538 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1539 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1540 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1541 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1542 // when it comes to which shaders are accepted by the compiler.
1543 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001544 if (IsImage(type.getBasicType()))
1545 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001546 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1547 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001548 }
1549 else if (IsSampler(type.getBasicType()))
1550 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001551 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1552 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001553 }
jchen104cdac9e2017-05-08 11:01:20 +08001554 else if (IsAtomicCounter(type.getBasicType()))
1555 {
1556 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1557 }
Olli Etuaho43364892017-02-13 16:00:12 +00001558 else
1559 {
1560 ASSERT(!IsOpaqueType(type.getBasicType()));
1561 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001562 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001563}
1564
Olli Etuaho856c4972016-08-08 11:38:39 +03001565void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001566 const ImmutableString &layoutQualifierName,
Olli Etuaho856c4972016-08-08 11:38:39 +03001567 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001568{
1569
1570 if (mShaderVersion < versionRequired)
1571 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001572 error(location, "invalid layout qualifier: not supported", layoutQualifierName);
Martin Radev802abe02016-08-04 17:48:32 +03001573 }
1574}
1575
Olli Etuaho856c4972016-08-08 11:38:39 +03001576bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1577 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001578{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001579 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001580 for (size_t i = 0u; i < localSize.size(); ++i)
1581 {
1582 if (localSize[i] != -1)
1583 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001584 error(location,
1585 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1586 "global layout declaration",
1587 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001588 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001589 }
1590 }
1591
Olli Etuaho8a176262016-08-16 14:23:01 +03001592 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001593}
1594
Olli Etuaho43364892017-02-13 16:00:12 +00001595void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001596 TLayoutImageInternalFormat internalFormat)
1597{
1598 if (internalFormat != EiifUnspecified)
1599 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001600 error(location, "invalid layout qualifier: only valid when used with images",
1601 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001602 }
Olli Etuaho43364892017-02-13 16:00:12 +00001603}
1604
1605void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1606{
1607 if (binding != -1)
1608 {
1609 error(location,
1610 "invalid layout qualifier: only valid when used with opaque types or blocks",
1611 "binding");
1612 }
1613}
1614
jchen104cdac9e2017-05-08 11:01:20 +08001615void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1616{
1617 if (offset != -1)
1618 {
1619 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1620 "offset");
1621 }
1622}
1623
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001624void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1625 int binding,
1626 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001627{
1628 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001629 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001630 {
1631 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1632 }
1633}
1634
1635void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1636 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001637 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001638{
1639 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001640 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001641 {
1642 error(location, "sampler binding greater than maximum texture units", "binding");
1643 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001644}
1645
Jiajia Qinbc585152017-06-23 15:42:17 +08001646void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1647 const TQualifier &qualifier,
1648 int binding,
1649 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001650{
1651 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001652 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001653 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001654 if (binding + size > mMaxUniformBufferBindings)
1655 {
1656 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1657 "binding");
1658 }
1659 }
1660 else if (qualifier == EvqBuffer)
1661 {
1662 if (binding + size > mMaxShaderStorageBufferBindings)
1663 {
1664 error(location,
1665 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1666 "binding");
1667 }
jchen10af713a22017-04-19 09:10:56 +08001668 }
1669}
jchen104cdac9e2017-05-08 11:01:20 +08001670void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1671{
1672 if (binding >= mMaxAtomicCounterBindings)
1673 {
1674 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1675 "binding");
1676 }
1677}
jchen10af713a22017-04-19 09:10:56 +08001678
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001679void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1680 int objectLocationCount,
1681 const TLayoutQualifier &layoutQualifier)
1682{
1683 int loc = layoutQualifier.location;
1684 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1685 {
1686 error(location, "Uniform location out of range", "location");
1687 }
1688}
1689
Andrei Volykhina5527072017-03-22 16:46:30 +03001690void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1691{
1692 if (yuv != false)
1693 {
1694 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1695 }
1696}
1697
Jiajia Qinbc585152017-06-23 15:42:17 +08001698void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1699 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001700{
1701 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1702 {
1703 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001704 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1705 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1706 qual == EvqInOut || qual == EvqConstReadOnly))
1707 {
1708 if (argument->getMemoryQualifier().writeonly)
1709 {
1710 error(argument->getLine(),
1711 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001712 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001713 return;
1714 }
1715 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001716 if (qual == EvqOut || qual == EvqInOut)
1717 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001718 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001719 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001720 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001721 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001722 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001723 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001724 }
1725 }
1726 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001727}
1728
Martin Radev70866b82016-07-22 15:27:42 +03001729void TParseContext::checkInvariantVariableQualifier(bool invariant,
1730 const TQualifier qualifier,
1731 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001732{
Martin Radev70866b82016-07-22 15:27:42 +03001733 if (!invariant)
1734 return;
1735
1736 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001737 {
Martin Radev70866b82016-07-22 15:27:42 +03001738 // input variables in the fragment shader can be also qualified as invariant
1739 if (!sh::CanBeInvariantESSL1(qualifier))
1740 {
1741 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1742 }
1743 }
1744 else
1745 {
1746 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1747 {
1748 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1749 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001750 }
1751}
1752
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001753bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001754{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001755 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001756}
1757
Jamie Madillb98c3a82015-07-23 14:26:04 -04001758void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1759 const char *extName,
1760 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001761{
1762 pp::SourceLocation srcLoc;
1763 srcLoc.file = loc.first_file;
1764 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001765 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001766}
1767
Jamie Madillb98c3a82015-07-23 14:26:04 -04001768void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1769 const char *name,
1770 const char *value,
1771 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001772{
1773 pp::SourceLocation srcLoc;
1774 srcLoc.file = loc.first_file;
1775 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001776 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001777}
1778
Martin Radev4c4c8e72016-08-04 12:25:34 +03001779sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001780{
Jamie Madill2f294c92017-11-20 14:47:26 -05001781 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001782 for (size_t i = 0u; i < result.size(); ++i)
1783 {
1784 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1785 {
1786 result[i] = 1;
1787 }
1788 else
1789 {
1790 result[i] = mComputeShaderLocalSize[i];
1791 }
1792 }
1793 return result;
1794}
1795
Olli Etuaho56229f12017-07-10 14:16:33 +03001796TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1797 const TSourceLoc &line)
1798{
1799 TIntermConstantUnion *node = new TIntermConstantUnion(
1800 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1801 node->setLine(line);
1802 return node;
1803}
1804
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805/////////////////////////////////////////////////////////////////////////////////
1806//
1807// Non-Errors.
1808//
1809/////////////////////////////////////////////////////////////////////////////////
1810
Jamie Madill5c097022014-08-20 16:38:32 -04001811const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001812 const ImmutableString &name,
Jamie Madill5c097022014-08-20 16:38:32 -04001813 const TSymbol *symbol)
1814{
Jamie Madill5c097022014-08-20 16:38:32 -04001815 if (!symbol)
1816 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001817 error(location, "undeclared identifier", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001818 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001819 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001820
1821 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001822 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001823 error(location, "variable expected", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001824 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001825 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001826
1827 const TVariable *variable = static_cast<const TVariable *>(symbol);
1828
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001829 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001830 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001831 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001832 }
1833
Olli Etuaho0f684632017-07-13 12:42:15 +03001834 // Reject shaders using both gl_FragData and gl_FragColor
1835 TQualifier qualifier = variable->getType().getQualifier();
1836 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001837 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001838 mUsesFragData = true;
1839 }
1840 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1841 {
1842 mUsesFragColor = true;
1843 }
1844 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1845 {
1846 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001847 }
1848
Olli Etuaho0f684632017-07-13 12:42:15 +03001849 // This validation is not quite correct - it's only an error to write to
1850 // both FragData and FragColor. For simplicity, and because users shouldn't
1851 // be rewarded for reading from undefined varaibles, return an error
1852 // if they are both referenced, rather than assigned.
1853 if (mUsesFragData && mUsesFragColor)
1854 {
1855 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1856 if (mUsesSecondaryOutputs)
1857 {
1858 errorMessage =
1859 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1860 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1861 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02001862 error(location, errorMessage, name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001863 }
1864
1865 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1866 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1867 qualifier == EvqWorkGroupSize)
1868 {
1869 error(location,
1870 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1871 "gl_WorkGroupSize");
1872 }
Jamie Madill5c097022014-08-20 16:38:32 -04001873 return variable;
1874}
1875
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001876TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001877 const ImmutableString &name,
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001878 const TSymbol *symbol)
1879{
1880 const TVariable *variable = getNamedVariable(location, name, symbol);
1881
Olli Etuaho0f684632017-07-13 12:42:15 +03001882 if (!variable)
1883 {
1884 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1885 node->setLine(location);
1886 return node;
1887 }
1888
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001889 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001890 TIntermTyped *node = nullptr;
1891
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001892 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001893 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001894 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001895 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001896 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001897 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001898 {
1899 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1900 // needs to be added to the AST as a constant and not as a symbol.
1901 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1902 TConstantUnion *constArray = new TConstantUnion[3];
1903 for (size_t i = 0; i < 3; ++i)
1904 {
1905 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1906 }
1907
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001908 ASSERT(variableType.getBasicType() == EbtUInt);
1909 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001910
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001911 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001912 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001913 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001914 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001915 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1916 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001917 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02001918 ASSERT(mGlInVariableWithArraySize != nullptr);
1919 node = new TIntermSymbol(mGlInVariableWithArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001920 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001921 else
1922 {
Olli Etuaho195be942017-12-04 23:40:14 +02001923 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001924 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001925 ASSERT(node != nullptr);
1926 node->setLine(location);
1927 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001928}
1929
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001930// Initializers show up in several places in the grammar. Have one set of
1931// code to handle them here.
1932//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001933// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001934bool TParseContext::executeInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001935 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001936 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001937 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001938 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939{
Olli Etuaho13389b62016-10-16 11:48:18 +01001940 ASSERT(initNode != nullptr);
1941 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942
Olli Etuahob60d30f2018-01-16 12:31:06 +02001943 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001944 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001945 // In case initializer is not an array or type has more dimensions than initializer, this
1946 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1947 // actually is an array or not. Having a non-array initializer for an unsized array will
1948 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001949 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001950 type->sizeUnsizedArrays(arraySizes);
1951 }
1952
1953 const TQualifier qualifier = type->getQualifier();
1954
1955 bool constError = false;
1956 if (qualifier == EvqConst)
1957 {
1958 if (EvqConst != initializer->getType().getQualifier())
1959 {
1960 std::stringstream reasonStream;
1961 reasonStream << "assigning non-constant to '" << type->getCompleteString() << "'";
1962 std::string reason = reasonStream.str();
1963 error(line, reason.c_str(), "=");
1964
1965 // We're still going to declare the variable to avoid extra error messages.
1966 type->setQualifier(EvqTemporary);
1967 constError = true;
1968 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001969 }
Olli Etuaho195be942017-12-04 23:40:14 +02001970
1971 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001972 if (!declareVariable(line, identifier, type, &variable))
1973 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001974 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001975 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001976
Olli Etuahob60d30f2018-01-16 12:31:06 +02001977 if (constError)
1978 {
1979 return false;
1980 }
1981
Olli Etuahob0c645e2015-05-12 14:25:36 +03001982 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001983 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001984 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001985 {
1986 // Error message does not completely match behavior with ESSL 1.00, but
1987 // we want to steer developers towards only using constant expressions.
1988 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001989 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001990 }
1991 if (globalInitWarning)
1992 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001993 warning(
1994 line,
1995 "global variable initializers should be constant expressions "
1996 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1997 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001998 }
1999
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002000 // identifier must be of type constant, a global, or a temporary
Arun Patole7e7e68d2015-05-22 12:02:25 +05302001 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
2002 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002003 error(line, " cannot initialize this type of qualifier ",
2004 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002005 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002006 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02002007
2008 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
2009 intermSymbol->setLine(line);
2010
2011 if (!binaryOpCommonCheck(EOpInitialize, intermSymbol, initializer, line))
2012 {
2013 assignError(line, "=", variable->getType().getCompleteString(),
2014 initializer->getCompleteString());
2015 return false;
2016 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002017
Arun Patole7e7e68d2015-05-22 12:02:25 +05302018 if (qualifier == EvqConst)
2019 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002020 // Save the constant folded value to the variable if possible.
2021 const TConstantUnion *constArray = initializer->getConstantValue();
2022 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302023 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002024 variable->shareConstPointer(constArray);
2025 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002026 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03002027 ASSERT(*initNode == nullptr);
2028 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002029 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002030 }
2031 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002032
Olli Etuahob60d30f2018-01-16 12:31:06 +02002033 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
2034 (*initNode)->setLine(line);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002035 return true;
2036}
2037
2038TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002039 const ImmutableString &identifier,
Olli Etuaho914b79a2017-06-19 16:03:19 +03002040 TIntermTyped *initializer,
2041 const TSourceLoc &loc)
2042{
2043 checkIsScalarBool(loc, pType);
2044 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002045 TType *type = new TType(pType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002046 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002047 {
2048 // The initializer is valid. The init condition needs to have a node - either the
2049 // initializer node, or a constant node in case the initialized variable is const and won't
2050 // be recorded in the AST.
2051 if (initNode == nullptr)
2052 {
2053 return initializer;
2054 }
2055 else
2056 {
2057 TIntermDeclaration *declaration = new TIntermDeclaration();
2058 declaration->appendDeclarator(initNode);
2059 return declaration;
2060 }
2061 }
2062 return nullptr;
2063}
2064
2065TIntermNode *TParseContext::addLoop(TLoopType type,
2066 TIntermNode *init,
2067 TIntermNode *cond,
2068 TIntermTyped *expr,
2069 TIntermNode *body,
2070 const TSourceLoc &line)
2071{
2072 TIntermNode *node = nullptr;
2073 TIntermTyped *typedCond = nullptr;
2074 if (cond)
2075 {
2076 typedCond = cond->getAsTyped();
2077 }
2078 if (cond == nullptr || typedCond)
2079 {
Olli Etuahocce89652017-06-19 16:04:09 +03002080 if (type == ELoopDoWhile)
2081 {
2082 checkIsScalarBool(line, typedCond);
2083 }
2084 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2085 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2086 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2087 !typedCond->isVector()));
2088
Olli Etuaho3ec75682017-07-05 17:02:55 +03002089 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002090 node->setLine(line);
2091 return node;
2092 }
2093
Olli Etuahocce89652017-06-19 16:04:09 +03002094 ASSERT(type != ELoopDoWhile);
2095
Olli Etuaho914b79a2017-06-19 16:03:19 +03002096 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2097 ASSERT(declaration);
2098 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2099 ASSERT(declarator->getLeft()->getAsSymbolNode());
2100
2101 // The condition is a declaration. In the AST representation we don't support declarations as
2102 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2103 // the loop.
2104 TIntermBlock *block = new TIntermBlock();
2105
2106 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2107 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2108 block->appendStatement(declareCondition);
2109
2110 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2111 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002112 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002113 block->appendStatement(loop);
2114 loop->setLine(line);
2115 block->setLine(line);
2116 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117}
2118
Olli Etuahocce89652017-06-19 16:04:09 +03002119TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2120 TIntermNodePair code,
2121 const TSourceLoc &loc)
2122{
Olli Etuaho56229f12017-07-10 14:16:33 +03002123 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002124
2125 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002126 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002127 {
2128 if (cond->getAsConstantUnion()->getBConst(0) == true)
2129 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002130 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002131 }
2132 else
2133 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002134 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002135 }
2136 }
2137
Olli Etuaho3ec75682017-07-05 17:02:55 +03002138 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002139 node->setLine(loc);
2140
2141 return node;
2142}
2143
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002144void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2145{
2146 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2147 typeSpecifier->getBasicType());
2148
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002149 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002150 {
2151 error(typeSpecifier->getLine(), "not supported", "first-class array");
2152 typeSpecifier->clearArrayness();
2153 }
2154}
2155
Martin Radev70866b82016-07-22 15:27:42 +03002156TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302157 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002158{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002159 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002160
Martin Radev70866b82016-07-22 15:27:42 +03002161 TPublicType returnType = typeSpecifier;
2162 returnType.qualifier = typeQualifier.qualifier;
2163 returnType.invariant = typeQualifier.invariant;
2164 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002165 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002166 returnType.precision = typeSpecifier.precision;
2167
2168 if (typeQualifier.precision != EbpUndefined)
2169 {
2170 returnType.precision = typeQualifier.precision;
2171 }
2172
Martin Radev4a9cd802016-09-01 16:51:51 +03002173 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2174 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002175
Martin Radev4a9cd802016-09-01 16:51:51 +03002176 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2177 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002178
Martin Radev4a9cd802016-09-01 16:51:51 +03002179 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002180
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002181 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002182 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002183 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002184 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002185 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002186 returnType.clearArrayness();
2187 }
2188
Martin Radev70866b82016-07-22 15:27:42 +03002189 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002190 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002191 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002192 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002193 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002194 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002195
Martin Radev70866b82016-07-22 15:27:42 +03002196 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002197 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002198 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002199 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002200 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002201 }
2202 }
2203 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002204 {
Martin Radev70866b82016-07-22 15:27:42 +03002205 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002206 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002207 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002208 }
Martin Radev70866b82016-07-22 15:27:42 +03002209 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2210 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002211 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002212 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2213 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002214 }
Martin Radev70866b82016-07-22 15:27:42 +03002215 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002216 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002217 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002218 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002219 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002220 }
2221
2222 return returnType;
2223}
2224
Olli Etuaho856c4972016-08-08 11:38:39 +03002225void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2226 const TPublicType &type,
2227 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002228{
2229 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002230 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002231 {
2232 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002233 }
2234
2235 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2236 switch (qualifier)
2237 {
2238 case EvqVertexIn:
2239 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002240 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002241 {
2242 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002243 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002244 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002245 return;
2246 case EvqFragmentOut:
2247 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002248 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002249 {
2250 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002251 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002252 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002253 return;
2254 default:
2255 break;
2256 }
2257
2258 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2259 // restrictions.
2260 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002261 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2262 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002263 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2264 {
2265 error(qualifierLocation, "must use 'flat' interpolation here",
2266 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 }
2268
Martin Radev4a9cd802016-09-01 16:51:51 +03002269 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002270 {
2271 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2272 // These restrictions are only implied by the ESSL 3.00 spec, but
2273 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002274 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002275 {
2276 error(qualifierLocation, "cannot be an array of structures",
2277 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002278 }
2279 if (type.isStructureContainingArrays())
2280 {
2281 error(qualifierLocation, "cannot be a structure containing an array",
2282 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002283 }
2284 if (type.isStructureContainingType(EbtStruct))
2285 {
2286 error(qualifierLocation, "cannot be a structure containing a structure",
2287 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002288 }
2289 if (type.isStructureContainingType(EbtBool))
2290 {
2291 error(qualifierLocation, "cannot be a structure containing a bool",
2292 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002293 }
2294 }
2295}
2296
Martin Radev2cc85b32016-08-05 16:22:53 +03002297void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2298{
2299 if (qualifier.getType() == QtStorage)
2300 {
2301 const TStorageQualifierWrapper &storageQualifier =
2302 static_cast<const TStorageQualifierWrapper &>(qualifier);
2303 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2304 !symbolTable.atGlobalLevel())
2305 {
2306 error(storageQualifier.getLine(),
2307 "Local variables can only use the const storage qualifier.",
2308 storageQualifier.getQualifierString().c_str());
2309 }
2310 }
2311}
2312
Olli Etuaho43364892017-02-13 16:00:12 +00002313void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002314 const TSourceLoc &location)
2315{
Jiajia Qinbc585152017-06-23 15:42:17 +08002316 const std::string reason(
2317 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2318 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002319 if (memoryQualifier.readonly)
2320 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002321 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002322 }
2323 if (memoryQualifier.writeonly)
2324 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002325 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002326 }
Martin Radev049edfa2016-11-11 14:35:37 +02002327 if (memoryQualifier.coherent)
2328 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002329 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002330 }
2331 if (memoryQualifier.restrictQualifier)
2332 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002333 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002334 }
2335 if (memoryQualifier.volatileQualifier)
2336 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002337 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002338 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002339}
2340
jchen104cdac9e2017-05-08 11:01:20 +08002341// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2342// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002343void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2344 const TSourceLoc &loc,
2345 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002346{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002347 if (!IsAtomicCounter(type->getBasicType()))
2348 {
2349 return;
2350 }
2351
2352 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2353 : kAtomicCounterSize;
2354 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2355 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002356 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002357 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002358 {
2359 offset = bindingState.appendSpan(size);
2360 }
2361 else
2362 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002363 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002364 }
2365 if (offset == -1)
2366 {
2367 error(loc, "Offset overlapping", "atomic counter");
2368 return;
2369 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002370 layoutQualifier.offset = offset;
2371 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002372}
2373
Olli Etuaho454c34c2017-10-25 16:35:56 +03002374void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002375 const ImmutableString &token,
Olli Etuaho454c34c2017-10-25 16:35:56 +03002376 TType *type)
2377{
2378 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2379 {
2380 if (type->isArray() && type->getOutermostArraySize() == 0u)
2381 {
2382 // Set size for the unsized geometry shader inputs if they are declared after a valid
2383 // input primitive declaration.
2384 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2385 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002386 ASSERT(mGlInVariableWithArraySize != nullptr);
2387 type->sizeOutermostUnsizedArray(
2388 mGlInVariableWithArraySize->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002389 }
2390 else
2391 {
2392 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2393 // An input can be declared without an array size if there is a previous layout
2394 // which specifies the size.
2395 error(location,
2396 "Missing a valid input primitive declaration before declaring an unsized "
2397 "array input",
2398 token);
2399 }
2400 }
2401 else if (type->isArray())
2402 {
2403 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2404 }
2405 else
2406 {
2407 error(location, "Geometry shader input variable must be declared as an array", token);
2408 }
2409 }
2410}
2411
Olli Etuaho13389b62016-10-16 11:48:18 +01002412TIntermDeclaration *TParseContext::parseSingleDeclaration(
2413 TPublicType &publicType,
2414 const TSourceLoc &identifierOrTypeLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002415 const ImmutableString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002416{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002417 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002418 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2419 mDirectiveHandler.pragma().stdgl.invariantAll)
2420 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002421 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002422
2423 // The directive handler has already taken care of rejecting invalid uses of this pragma
2424 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2425 // affected variable declarations:
2426 //
2427 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2428 // elsewhere, in TranslatorGLSL.)
2429 //
2430 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2431 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2432 // the way this is currently implemented we have to enable this compiler option before
2433 // parsing the shader and determining the shading language version it uses. If this were
2434 // implemented as a post-pass, the workaround could be more targeted.
2435 //
2436 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2437 // the specification, but there are desktop OpenGL drivers that expect that this is the
2438 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2439 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2440 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002441 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002442 }
2443 }
2444
Olli Etuahofbb1c792018-01-19 16:26:59 +02002445 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier, type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002446
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002447 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2448 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002449
Olli Etuahobab4c082015-04-24 16:38:49 +03002450 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002451 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002452
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002453 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002454 if (emptyDeclaration)
2455 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002456 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002457 // In most cases we don't need to create a symbol node for an empty declaration.
2458 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002459 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002460 {
Olli Etuaho195be942017-12-04 23:40:14 +02002461 TVariable *emptyVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02002462 new TVariable(&symbolTable, ImmutableString(""), type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002463 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002464 }
jchen104cdac9e2017-05-08 11:01:20 +08002465 else if (IsAtomicCounter(publicType.getBasicType()))
2466 {
2467 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2468 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002469 }
2470 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002471 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002472 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002473
Olli Etuahob60d30f2018-01-16 12:31:06 +02002474 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002475
Olli Etuahob60d30f2018-01-16 12:31:06 +02002476 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002477
Olli Etuaho2935c582015-04-08 14:32:06 +03002478 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002479 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002480 {
Olli Etuaho195be942017-12-04 23:40:14 +02002481 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002482 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002483 }
2484
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002485 TIntermDeclaration *declaration = new TIntermDeclaration();
2486 declaration->setLine(identifierOrTypeLocation);
2487 if (symbol)
2488 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002489 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002490 declaration->appendDeclarator(symbol);
2491 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002492 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002493}
2494
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002495TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2496 TPublicType &elementType,
2497 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002498 const ImmutableString &identifier,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002499 const TSourceLoc &indexLocation,
2500 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002501{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002502 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002503
Olli Etuaho55bde912017-10-25 13:41:13 +03002504 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002505 identifierLocation);
2506
Olli Etuaho55bde912017-10-25 13:41:13 +03002507 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002508
Olli Etuaho55bde912017-10-25 13:41:13 +03002509 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002510
Olli Etuahob60d30f2018-01-16 12:31:06 +02002511 TType *arrayType = new TType(elementType);
2512 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002513
Olli Etuahofbb1c792018-01-19 16:26:59 +02002514 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002515
Olli Etuahob60d30f2018-01-16 12:31:06 +02002516 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002517
Olli Etuahob60d30f2018-01-16 12:31:06 +02002518 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002519
Olli Etuaho13389b62016-10-16 11:48:18 +01002520 TIntermDeclaration *declaration = new TIntermDeclaration();
2521 declaration->setLine(identifierLocation);
2522
Olli Etuaho195be942017-12-04 23:40:14 +02002523 TVariable *variable = nullptr;
2524 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002525 {
Olli Etuaho195be942017-12-04 23:40:14 +02002526 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002527 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002528 declaration->appendDeclarator(symbol);
2529 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002530
Olli Etuaho13389b62016-10-16 11:48:18 +01002531 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002532}
2533
Olli Etuaho13389b62016-10-16 11:48:18 +01002534TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2535 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002536 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002537 const TSourceLoc &initLocation,
2538 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002539{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002540 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002541
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002542 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2543 identifierLocation);
2544
2545 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002546
Olli Etuaho13389b62016-10-16 11:48:18 +01002547 TIntermDeclaration *declaration = new TIntermDeclaration();
2548 declaration->setLine(identifierLocation);
2549
2550 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002551 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002552 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002553 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002554 if (initNode)
2555 {
2556 declaration->appendDeclarator(initNode);
2557 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002558 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002559 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002560}
2561
Olli Etuaho13389b62016-10-16 11:48:18 +01002562TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002563 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002564 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002565 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002566 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002567 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002568 const TSourceLoc &initLocation,
2569 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002570{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002571 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572
Olli Etuaho55bde912017-10-25 13:41:13 +03002573 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002574 identifierLocation);
2575
Olli Etuaho55bde912017-10-25 13:41:13 +03002576 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002577
Olli Etuaho55bde912017-10-25 13:41:13 +03002578 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002579
Olli Etuahob60d30f2018-01-16 12:31:06 +02002580 TType *arrayType = new TType(elementType);
2581 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002582
Olli Etuaho13389b62016-10-16 11:48:18 +01002583 TIntermDeclaration *declaration = new TIntermDeclaration();
2584 declaration->setLine(identifierLocation);
2585
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002586 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002587 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002588 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002589 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002590 if (initNode)
2591 {
2592 declaration->appendDeclarator(initNode);
2593 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002594 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002595
2596 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002597}
2598
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002599TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002600 const TTypeQualifierBuilder &typeQualifierBuilder,
2601 const TSourceLoc &identifierLoc,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002602 const ImmutableString &identifier,
Martin Radev70866b82016-07-22 15:27:42 +03002603 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002604{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002605 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002606
Martin Radev70866b82016-07-22 15:27:42 +03002607 if (!typeQualifier.invariant)
2608 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002609 error(identifierLoc, "Expected invariant", identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002610 return nullptr;
2611 }
2612 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2613 {
2614 return nullptr;
2615 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002616 if (!symbol)
2617 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002618 error(identifierLoc, "undeclared identifier declared as invariant", identifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002619 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002620 }
Martin Radev70866b82016-07-22 15:27:42 +03002621 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002622 {
Martin Radev70866b82016-07-22 15:27:42 +03002623 error(identifierLoc, "invariant declaration specifies qualifier",
2624 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002625 }
Martin Radev70866b82016-07-22 15:27:42 +03002626 if (typeQualifier.precision != EbpUndefined)
2627 {
2628 error(identifierLoc, "invariant declaration specifies precision",
2629 getPrecisionString(typeQualifier.precision));
2630 }
2631 if (!typeQualifier.layoutQualifier.isEmpty())
2632 {
2633 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2634 }
2635
2636 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002637 if (!variable)
2638 {
2639 return nullptr;
2640 }
Martin Radev70866b82016-07-22 15:27:42 +03002641 const TType &type = variable->getType();
2642
2643 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2644 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002645 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002646
Olli Etuahodefe3932018-02-13 11:56:09 +02002647 symbolTable.addInvariantVarying(identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002648
Olli Etuaho195be942017-12-04 23:40:14 +02002649 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002650 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002651
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002652 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002653}
2654
Olli Etuaho13389b62016-10-16 11:48:18 +01002655void TParseContext::parseDeclarator(TPublicType &publicType,
2656 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002657 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002658 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002659{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002660 // If the declaration starting this declarator list was empty (example: int,), some checks were
2661 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002662 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002663 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002664 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2665 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002666 }
2667
Olli Etuaho856c4972016-08-08 11:38:39 +03002668 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002669
Olli Etuahob60d30f2018-01-16 12:31:06 +02002670 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002671
Olli Etuahofbb1c792018-01-19 16:26:59 +02002672 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002673
Olli Etuahob60d30f2018-01-16 12:31:06 +02002674 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002675
Olli Etuahob60d30f2018-01-16 12:31:06 +02002676 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002677
Olli Etuaho195be942017-12-04 23:40:14 +02002678 TVariable *variable = nullptr;
2679 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002680 {
Olli Etuaho195be942017-12-04 23:40:14 +02002681 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002682 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002683 declarationOut->appendDeclarator(symbol);
2684 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002685}
2686
Olli Etuaho55bde912017-10-25 13:41:13 +03002687void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002688 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002689 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002690 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002691 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002692 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002693{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002694 // If the declaration starting this declarator list was empty (example: int,), some checks were
2695 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002696 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002697 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002698 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002699 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002700 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002701
Olli Etuaho55bde912017-10-25 13:41:13 +03002702 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002703
Olli Etuaho55bde912017-10-25 13:41:13 +03002704 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002705 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002706 TType *arrayType = new TType(elementType);
2707 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002708
Olli Etuahofbb1c792018-01-19 16:26:59 +02002709 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002710
Olli Etuahob60d30f2018-01-16 12:31:06 +02002711 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002712
Olli Etuahob60d30f2018-01-16 12:31:06 +02002713 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002714
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002715 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002716 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002717 {
Olli Etuaho195be942017-12-04 23:40:14 +02002718 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002719 symbol->setLine(identifierLocation);
2720 declarationOut->appendDeclarator(symbol);
2721 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002722 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002723}
2724
Olli Etuaho13389b62016-10-16 11:48:18 +01002725void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2726 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002727 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002728 const TSourceLoc &initLocation,
2729 TIntermTyped *initializer,
2730 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002731{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002732 // If the declaration starting this declarator list was empty (example: int,), some checks were
2733 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002734 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002735 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002736 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2737 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002738 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002739
Olli Etuaho856c4972016-08-08 11:38:39 +03002740 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002741
Olli Etuaho13389b62016-10-16 11:48:18 +01002742 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002743 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002744 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002745 {
2746 //
2747 // build the intermediate representation
2748 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002749 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002750 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002751 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002752 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002753 }
2754}
2755
Olli Etuaho55bde912017-10-25 13:41:13 +03002756void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002757 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002758 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002759 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002760 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002761 const TSourceLoc &initLocation,
2762 TIntermTyped *initializer,
2763 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002764{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002765 // If the declaration starting this declarator list was empty (example: int,), some checks were
2766 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002767 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002768 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002769 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002770 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002771 }
2772
Olli Etuaho55bde912017-10-25 13:41:13 +03002773 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002774
Olli Etuaho55bde912017-10-25 13:41:13 +03002775 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002776
Olli Etuahob60d30f2018-01-16 12:31:06 +02002777 TType *arrayType = new TType(elementType);
2778 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002779
2780 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002781 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002782 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002783 {
2784 if (initNode)
2785 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002786 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002787 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002788 }
2789}
2790
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002791TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2792{
2793 // It's simpler to parse an empty statement as a constant expression rather than having a
2794 // different type of node just for empty statements, that will be pruned from the AST anyway.
2795 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2796 node->setLine(location);
2797 return node;
2798}
2799
jchen104cdac9e2017-05-08 11:01:20 +08002800void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2801 const TSourceLoc &location)
2802{
2803 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2804 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2805 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2806 {
2807 error(location, "Requires both binding and offset", "layout");
2808 return;
2809 }
2810 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2811}
2812
Olli Etuahocce89652017-06-19 16:04:09 +03002813void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2814 const TPublicType &type,
2815 const TSourceLoc &loc)
2816{
2817 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2818 !getFragmentPrecisionHigh())
2819 {
2820 error(loc, "precision is not supported in fragment shader", "highp");
2821 }
2822
2823 if (!CanSetDefaultPrecisionOnType(type))
2824 {
2825 error(loc, "illegal type argument for default precision qualifier",
2826 getBasicString(type.getBasicType()));
2827 return;
2828 }
2829 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2830}
2831
Shaob5cc1192017-07-06 10:47:20 +08002832bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2833{
2834 switch (typeQualifier.layoutQualifier.primitiveType)
2835 {
2836 case EptLines:
2837 case EptLinesAdjacency:
2838 case EptTriangles:
2839 case EptTrianglesAdjacency:
2840 return typeQualifier.qualifier == EvqGeometryIn;
2841
2842 case EptLineStrip:
2843 case EptTriangleStrip:
2844 return typeQualifier.qualifier == EvqGeometryOut;
2845
2846 case EptPoints:
2847 return true;
2848
2849 default:
2850 UNREACHABLE();
2851 return false;
2852 }
2853}
2854
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002855void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2856 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002857{
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002858 if (mGlInVariableWithArraySize == nullptr)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002859 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002860 const TSymbol *glPerVertex = symbolTable.findBuiltIn(ImmutableString("gl_PerVertex"), 310);
Olli Etuahodd21ecf2018-01-10 12:42:09 +02002861 const TInterfaceBlock *glPerVertexBlock = static_cast<const TInterfaceBlock *>(glPerVertex);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002862 TType *glInType = new TType(glPerVertexBlock, EvqPerVertexIn, TLayoutQualifier::Create());
2863 glInType->makeArray(inputArraySize);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002864 mGlInVariableWithArraySize =
Olli Etuahofbb1c792018-01-19 16:26:59 +02002865 new TVariable(&symbolTable, ImmutableString("gl_in"), glInType, SymbolType::BuiltIn,
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002866 TExtension::EXT_geometry_shader);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002867 }
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002868 else if (mGlInVariableWithArraySize->getType().getOutermostArraySize() != inputArraySize)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002869 {
2870 error(line,
2871 "Array size or input primitive declaration doesn't match the size of earlier sized "
2872 "array inputs.",
2873 "layout");
2874 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002875}
2876
Shaob5cc1192017-07-06 10:47:20 +08002877bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2878{
2879 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2880
2881 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2882
2883 if (layoutQualifier.maxVertices != -1)
2884 {
2885 error(typeQualifier.line,
2886 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2887 return false;
2888 }
2889
2890 // Set mGeometryInputPrimitiveType if exists
2891 if (layoutQualifier.primitiveType != EptUndefined)
2892 {
2893 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2894 {
2895 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2896 return false;
2897 }
2898
2899 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2900 {
2901 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002902 setGeometryShaderInputArraySize(
2903 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2904 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002905 }
2906 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2907 {
2908 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2909 "layout");
2910 return false;
2911 }
2912 }
2913
2914 // Set mGeometryInvocations if exists
2915 if (layoutQualifier.invocations > 0)
2916 {
2917 if (mGeometryShaderInvocations == 0)
2918 {
2919 mGeometryShaderInvocations = layoutQualifier.invocations;
2920 }
2921 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2922 {
2923 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2924 "layout");
2925 return false;
2926 }
2927 }
2928
2929 return true;
2930}
2931
2932bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2933{
2934 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2935
2936 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2937
2938 if (layoutQualifier.invocations > 0)
2939 {
2940 error(typeQualifier.line,
2941 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2942 return false;
2943 }
2944
2945 // Set mGeometryOutputPrimitiveType if exists
2946 if (layoutQualifier.primitiveType != EptUndefined)
2947 {
2948 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2949 {
2950 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2951 return false;
2952 }
2953
2954 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2955 {
2956 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2957 }
2958 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2959 {
2960 error(typeQualifier.line,
2961 "primitive doesn't match earlier output primitive declaration", "layout");
2962 return false;
2963 }
2964 }
2965
2966 // Set mGeometryMaxVertices if exists
2967 if (layoutQualifier.maxVertices > -1)
2968 {
2969 if (mGeometryShaderMaxVertices == -1)
2970 {
2971 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2972 }
2973 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2974 {
2975 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2976 "layout");
2977 return false;
2978 }
2979 }
2980
2981 return true;
2982}
2983
Martin Radev70866b82016-07-22 15:27:42 +03002984void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002985{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002986 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002987 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002988
Martin Radev70866b82016-07-22 15:27:42 +03002989 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2990 typeQualifier.line);
2991
Jamie Madillc2128ff2016-07-04 10:26:17 -04002992 // It should never be the case, but some strange parser errors can send us here.
2993 if (layoutQualifier.isEmpty())
2994 {
2995 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002996 return;
2997 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002998
Martin Radev802abe02016-08-04 17:48:32 +03002999 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04003000 {
Olli Etuaho43364892017-02-13 16:00:12 +00003001 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04003002 return;
3003 }
3004
Olli Etuaho43364892017-02-13 16:00:12 +00003005 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
3006
3007 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003008
3009 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3010
Andrei Volykhina5527072017-03-22 16:46:30 +03003011 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3012
jchen104cdac9e2017-05-08 11:01:20 +08003013 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3014
Qin Jiajiaca68d982017-09-18 16:41:56 +08003015 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3016 typeQualifier.qualifier);
3017
Martin Radev802abe02016-08-04 17:48:32 +03003018 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003019 {
Martin Radev802abe02016-08-04 17:48:32 +03003020 if (mComputeShaderLocalSizeDeclared &&
3021 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3022 {
3023 error(typeQualifier.line, "Work group size does not match the previous declaration",
3024 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003025 return;
3026 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003027
Martin Radev802abe02016-08-04 17:48:32 +03003028 if (mShaderVersion < 310)
3029 {
3030 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003031 return;
3032 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003033
Martin Radev4c4c8e72016-08-04 12:25:34 +03003034 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003035 {
3036 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003037 return;
3038 }
3039
3040 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003041 symbolTable.findBuiltIn(ImmutableString("gl_MaxComputeWorkGroupSize"), mShaderVersion));
Martin Radev802abe02016-08-04 17:48:32 +03003042
3043 const TConstantUnion *maxComputeWorkGroupSizeData =
3044 maxComputeWorkGroupSize->getConstPointer();
3045
3046 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3047 {
3048 if (layoutQualifier.localSize[i] != -1)
3049 {
3050 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3051 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3052 if (mComputeShaderLocalSize[i] < 1 ||
3053 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3054 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003055 std::stringstream reasonStream;
3056 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3057 << maxComputeWorkGroupSizeValue;
3058 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003059
Olli Etuaho4de340a2016-12-16 09:32:03 +00003060 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003061 return;
3062 }
3063 }
3064 }
3065
3066 mComputeShaderLocalSizeDeclared = true;
3067 }
Shaob5cc1192017-07-06 10:47:20 +08003068 else if (typeQualifier.qualifier == EvqGeometryIn)
3069 {
3070 if (mShaderVersion < 310)
3071 {
3072 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3073 return;
3074 }
3075
3076 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3077 {
3078 return;
3079 }
3080 }
3081 else if (typeQualifier.qualifier == EvqGeometryOut)
3082 {
3083 if (mShaderVersion < 310)
3084 {
3085 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3086 "layout");
3087 return;
3088 }
3089
3090 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3091 {
3092 return;
3093 }
3094 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003095 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3096 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003097 {
3098 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3099 // specification.
3100 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3101 {
3102 error(typeQualifier.line, "Number of views does not match the previous declaration",
3103 "layout");
3104 return;
3105 }
3106
3107 if (layoutQualifier.numViews == -1)
3108 {
3109 error(typeQualifier.line, "No num_views specified", "layout");
3110 return;
3111 }
3112
3113 if (layoutQualifier.numViews > mMaxNumViews)
3114 {
3115 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3116 "layout");
3117 return;
3118 }
3119
3120 mNumViews = layoutQualifier.numViews;
3121 }
Martin Radev802abe02016-08-04 17:48:32 +03003122 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003123 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003124 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003125 {
Martin Radev802abe02016-08-04 17:48:32 +03003126 return;
3127 }
3128
Jiajia Qinbc585152017-06-23 15:42:17 +08003129 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003130 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003131 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003132 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003133 return;
3134 }
3135
3136 if (mShaderVersion < 300)
3137 {
3138 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3139 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003140 return;
3141 }
3142
Olli Etuaho09b04a22016-12-15 13:30:26 +00003143 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003144
3145 if (layoutQualifier.matrixPacking != EmpUnspecified)
3146 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003147 if (typeQualifier.qualifier == EvqUniform)
3148 {
3149 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3150 }
3151 else if (typeQualifier.qualifier == EvqBuffer)
3152 {
3153 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3154 }
Martin Radev802abe02016-08-04 17:48:32 +03003155 }
3156
3157 if (layoutQualifier.blockStorage != EbsUnspecified)
3158 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003159 if (typeQualifier.qualifier == EvqUniform)
3160 {
3161 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3162 }
3163 else if (typeQualifier.qualifier == EvqBuffer)
3164 {
3165 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3166 }
Martin Radev802abe02016-08-04 17:48:32 +03003167 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003168 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003169}
3170
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003171TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3172 const TFunction &function,
3173 const TSourceLoc &location,
3174 bool insertParametersToSymbolTable)
3175{
Olli Etuahobed35d72017-12-20 16:36:26 +02003176 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003177
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003178 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003179 prototype->setLine(location);
3180
3181 for (size_t i = 0; i < function.getParamCount(); i++)
3182 {
3183 const TConstParameter &param = function.getParam(i);
3184
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003185 TIntermSymbol *symbol = nullptr;
3186
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003187 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3188 // be used for unused args).
3189 if (param.name != nullptr)
3190 {
Olli Etuaho195be942017-12-04 23:40:14 +02003191 TVariable *variable =
Olli Etuahob60d30f2018-01-16 12:31:06 +02003192 new TVariable(&symbolTable, param.name, param.type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003193 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003194 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003195 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003196 {
Olli Etuaho437664b2018-02-28 15:38:14 +02003197 if (!symbolTable.declare(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003198 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003199 error(location, "redefinition", param.name);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003200 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003201 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003202 // Unsized type of a named parameter should have already been checked and sanitized.
3203 ASSERT(!param.type->isUnsizedArray());
3204 }
3205 else
3206 {
3207 if (param.type->isUnsizedArray())
3208 {
3209 error(location, "function parameter array must be sized at compile time", "[]");
3210 // We don't need to size the arrays since the parameter is unnamed and hence
3211 // inaccessible.
3212 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003213 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003214 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003215 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003216 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3217 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003218 TVariable *emptyVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003219 new TVariable(&symbolTable, ImmutableString(""), param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003220 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003221 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003222 symbol->setLine(location);
3223 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003224 }
3225 return prototype;
3226}
3227
Olli Etuaho16c745a2017-01-16 17:02:27 +00003228TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3229 const TFunction &parsedFunction,
3230 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003231{
Olli Etuaho476197f2016-10-11 13:59:08 +01003232 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3233 // first declaration. Either way the instance in the symbol table is used to track whether the
3234 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003235 bool hadPrototypeDeclaration = false;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003236 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003237 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3238
3239 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003240 {
3241 // ESSL 1.00.17 section 4.2.7.
3242 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3243 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003244 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003245
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003246 TIntermFunctionPrototype *prototype =
3247 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003248
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003249 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003250
3251 if (!symbolTable.atGlobalLevel())
3252 {
3253 // ESSL 3.00.4 section 4.2.4.
3254 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003255 }
3256
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003257 return prototype;
3258}
3259
Olli Etuaho336b1472016-10-05 16:37:55 +01003260TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003261 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003262 TIntermBlock *functionBody,
3263 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003264{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003265 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003266 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3267 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003268 error(location,
3269 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003270 }
3271
Olli Etuahof51fdd22016-10-03 10:03:40 +01003272 if (functionBody == nullptr)
3273 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003274 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003275 functionBody->setLine(location);
3276 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003277 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003278 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003279 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003280
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003281 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003282 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003283}
3284
Olli Etuaho476197f2016-10-11 13:59:08 +01003285void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003286 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003287 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003288{
Olli Etuaho476197f2016-10-11 13:59:08 +01003289 ASSERT(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003290
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003291 bool wasDefined = false;
3292 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3293 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003294 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003295 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003296 }
Jamie Madill185fb402015-06-12 15:48:48 -04003297
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003298 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003299 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003300 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003301
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003302 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003303 setLoopNestingLevel(0);
3304}
3305
Jamie Madillb98c3a82015-07-23 14:26:04 -04003306TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003307{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003308 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003309 // We don't know at this point whether this is a function definition or a prototype.
3310 // The definition production code will check for redefinitions.
3311 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003312 //
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303313
Olli Etuahod80f2942017-11-06 12:44:45 +02003314 for (size_t i = 0u; i < function->getParamCount(); ++i)
3315 {
3316 auto &param = function->getParam(i);
3317 if (param.type->isStructSpecifier())
3318 {
3319 // ESSL 3.00.6 section 12.10.
3320 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003321 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003322 }
3323 }
3324
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003325 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303326 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003327 const UnmangledBuiltIn *builtIn =
3328 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3329 if (builtIn &&
3330 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3331 {
3332 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3333 // functions. Therefore overloading or redefining builtin functions is an error.
3334 error(location, "Name of a built-in function cannot be redeclared as function",
3335 function->name());
3336 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303337 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003338 else
3339 {
3340 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3341 // this applies to redeclarations as well.
3342 const TSymbol *builtIn =
3343 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3344 if (builtIn)
3345 {
3346 error(location, "built-in functions cannot be redefined", function->name());
3347 }
3348 }
3349
3350 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3351 // here.
3352 const TFunction *prevDec =
3353 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3354 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003355 {
3356 if (prevDec->getReturnType() != function->getReturnType())
3357 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003358 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003359 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003360 }
3361 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3362 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003363 if (prevDec->getParam(i).type->getQualifier() !=
3364 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003365 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003366 error(location,
3367 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003368 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003369 }
3370 }
3371 }
3372
Jamie Madill185fb402015-06-12 15:48:48 -04003373 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003374 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3375 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003376 if (prevSym)
3377 {
3378 if (!prevSym->isFunction())
3379 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003380 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003381 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003382 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003383 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003384 // Parsing is at the inner scope level of the function's arguments and body statement at this
3385 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3386 // scope.
3387 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003388
Olli Etuaho78d13742017-01-18 13:06:10 +00003389 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003390 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003391 {
3392 if (function->getParamCount() > 0)
3393 {
3394 error(location, "function cannot take any parameter(s)", "main");
3395 }
3396 if (function->getReturnType().getBasicType() != EbtVoid)
3397 {
3398 error(location, "main function cannot return a value",
3399 function->getReturnType().getBasicString());
3400 }
3401 }
3402
Jamie Madill185fb402015-06-12 15:48:48 -04003403 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003404 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3405 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003406 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3407 //
3408 return function;
3409}
3410
Olli Etuaho9de84a52016-06-14 17:36:01 +03003411TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003412 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003413 const TSourceLoc &location)
3414{
3415 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3416 {
3417 error(location, "no qualifiers allowed for function return",
3418 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003419 }
3420 if (!type.layoutQualifier.isEmpty())
3421 {
3422 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003423 }
jchen10cc2a10e2017-05-03 14:05:12 +08003424 // make sure an opaque type is not involved as well...
3425 std::string reason(getBasicString(type.getBasicType()));
3426 reason += "s can't be function return values";
3427 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003428 if (mShaderVersion < 300)
3429 {
3430 // Array return values are forbidden, but there's also no valid syntax for declaring array
3431 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003432 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003433
3434 if (type.isStructureContainingArrays())
3435 {
3436 // ESSL 1.00.17 section 6.1 Function Definitions
3437 error(location, "structures containing arrays can't be function return values",
3438 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003439 }
3440 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003441
3442 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003443 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003444}
3445
Olli Etuaho697bf652018-02-16 11:50:54 +02003446TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3447 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003448{
Olli Etuaho697bf652018-02-16 11:50:54 +02003449 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003450}
3451
Olli Etuaho95ed1942018-02-01 14:01:19 +02003452TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003453{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003454 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003455 {
3456 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3457 "[]");
3458 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003459 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003460 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003461 error(publicType.getLine(), "constructor can't be a structure definition",
3462 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003463 }
3464
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003465 TType *type = new TType(publicType);
3466 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003467 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003468 error(publicType.getLine(), "cannot construct this type",
3469 getBasicString(publicType.getBasicType()));
3470 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003471 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003472 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003473}
3474
Olli Etuaho55bde912017-10-25 13:41:13 +03003475void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3476 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003477 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003478 TType *arrayType)
3479{
3480 if (arrayType->isUnsizedArray())
3481 {
3482 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003483 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003484 }
3485}
3486
3487TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003488 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003489 const TSourceLoc &nameLoc)
3490{
Olli Etuaho55bde912017-10-25 13:41:13 +03003491 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003492 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003493 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003494 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003495 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003496 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003497 checkIsNotReserved(nameLoc, name);
3498 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003499 return param;
3500}
3501
Olli Etuaho55bde912017-10-25 13:41:13 +03003502TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003503 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003504 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003505{
Olli Etuaho55bde912017-10-25 13:41:13 +03003506 TType *type = new TType(publicType);
3507 return parseParameterDeclarator(type, name, nameLoc);
3508}
3509
Olli Etuahofbb1c792018-01-19 16:26:59 +02003510TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003511 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003512 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003513 const TSourceLoc &arrayLoc,
3514 TPublicType *elementType)
3515{
3516 checkArrayElementIsNotArray(arrayLoc, *elementType);
3517 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003518 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003519 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003520}
3521
Olli Etuaho95ed1942018-02-01 14:01:19 +02003522bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3523 const TIntermSequence &arguments,
3524 TType type,
3525 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003526{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003527 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003528 {
3529 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3530 return false;
3531 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003532 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003533 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003534 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003535 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003536 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3537 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003538 {
3539 error(line, "constructing from a non-dereferenced array", "constructor");
3540 return false;
3541 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003542 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003543 {
3544 if (dimensionalityFromElement == 1u)
3545 {
3546 error(line, "implicitly sized array of arrays constructor argument is not an array",
3547 "constructor");
3548 }
3549 else
3550 {
3551 error(line,
3552 "implicitly sized array of arrays constructor argument dimensionality is too "
3553 "low",
3554 "constructor");
3555 }
3556 return false;
3557 }
3558 }
3559 return true;
3560}
3561
Jamie Madillb98c3a82015-07-23 14:26:04 -04003562// This function is used to test for the correctness of the parameters passed to various constructor
3563// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003564//
Olli Etuaho856c4972016-08-08 11:38:39 +03003565// 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 +00003566//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003567TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003568{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003569 TType type = fnCall->constructorType();
3570 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003571 if (type.isUnsizedArray())
3572 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003573 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003574 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003575 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003576 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003577 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003578 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003579 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003580 if (type.getOutermostArraySize() == 0u)
3581 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003582 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003583 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003584 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003585 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003586 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003587 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003588 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003589 }
3590 }
3591 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003592 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003593
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003594 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003595 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003596 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003597 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003598
Olli Etuaho95ed1942018-02-01 14:01:19 +02003599 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003600 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003601
Olli Etuaho765924f2018-01-04 12:48:36 +02003602 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003603}
3604
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003605//
3606// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003607// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003608//
Olli Etuaho13389b62016-10-16 11:48:18 +01003609TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003610 const TTypeQualifierBuilder &typeQualifierBuilder,
3611 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003612 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003613 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003614 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003615 const TSourceLoc &instanceLine,
3616 TIntermTyped *arrayIndex,
3617 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003618{
Olli Etuaho856c4972016-08-08 11:38:39 +03003619 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003620
Olli Etuaho77ba4082016-12-16 12:01:18 +00003621 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003622
Jiajia Qinbc585152017-06-23 15:42:17 +08003623 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003624 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003625 error(typeQualifier.line,
3626 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3627 "3.10",
3628 getQualifierString(typeQualifier.qualifier));
3629 }
3630 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3631 {
3632 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003633 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003634 }
3635
Martin Radev70866b82016-07-22 15:27:42 +03003636 if (typeQualifier.invariant)
3637 {
3638 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3639 }
3640
Jiajia Qinbc585152017-06-23 15:42:17 +08003641 if (typeQualifier.qualifier != EvqBuffer)
3642 {
3643 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3644 }
Olli Etuaho43364892017-02-13 16:00:12 +00003645
jchen10af713a22017-04-19 09:10:56 +08003646 // add array index
3647 unsigned int arraySize = 0;
3648 if (arrayIndex != nullptr)
3649 {
3650 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3651 }
3652
3653 if (mShaderVersion < 310)
3654 {
3655 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3656 }
3657 else
3658 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003659 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3660 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003661 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003662
Andrei Volykhina5527072017-03-22 16:46:30 +03003663 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3664
Jamie Madill099c0f32013-06-20 11:55:52 -04003665 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003666 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003667 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3668 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003669
Jamie Madill099c0f32013-06-20 11:55:52 -04003670 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3671 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003672 if (typeQualifier.qualifier == EvqUniform)
3673 {
3674 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3675 }
3676 else if (typeQualifier.qualifier == EvqBuffer)
3677 {
3678 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3679 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003680 }
3681
Jamie Madill1566ef72013-06-20 11:55:54 -04003682 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3683 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003684 if (typeQualifier.qualifier == EvqUniform)
3685 {
3686 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3687 }
3688 else if (typeQualifier.qualifier == EvqBuffer)
3689 {
3690 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3691 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003692 }
3693
Olli Etuaho856c4972016-08-08 11:38:39 +03003694 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003695
Martin Radev2cc85b32016-08-05 16:22:53 +03003696 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3697
Jamie Madill98493dd2013-07-08 14:39:03 -04003698 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303699 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3700 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003701 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303702 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003703 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303704 {
jchen10cc2a10e2017-05-03 14:05:12 +08003705 std::string reason("unsupported type - ");
3706 reason += fieldType->getBasicString();
3707 reason += " types are not allowed in interface blocks";
3708 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003709 }
3710
Jamie Madill98493dd2013-07-08 14:39:03 -04003711 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003712 switch (qualifier)
3713 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003714 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003715 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003716 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003717 if (typeQualifier.qualifier == EvqBuffer)
3718 {
3719 error(field->line(), "invalid qualifier on shader storage block member",
3720 getQualifierString(qualifier));
3721 }
3722 break;
3723 case EvqBuffer:
3724 if (typeQualifier.qualifier == EvqUniform)
3725 {
3726 error(field->line(), "invalid qualifier on uniform block member",
3727 getQualifierString(qualifier));
3728 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003729 break;
3730 default:
3731 error(field->line(), "invalid qualifier on interface block member",
3732 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003733 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003734 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003735
Martin Radev70866b82016-07-22 15:27:42 +03003736 if (fieldType->isInvariant())
3737 {
3738 error(field->line(), "invalid qualifier on interface block member", "invariant");
3739 }
3740
Jamie Madilla5efff92013-06-06 11:56:47 -04003741 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003742 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003743 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003744 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003745
Jamie Madill98493dd2013-07-08 14:39:03 -04003746 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003747 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003748 error(field->line(), "invalid layout qualifier: cannot be used here",
3749 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003750 }
3751
Jamie Madill98493dd2013-07-08 14:39:03 -04003752 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003753 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003754 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003755 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003756 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003757 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003758 warning(field->line(),
3759 "extraneous layout qualifier: only has an effect on matrix types",
3760 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003761 }
3762
Jamie Madill98493dd2013-07-08 14:39:03 -04003763 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003764
Olli Etuahoebee5b32017-11-23 12:56:32 +02003765 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3766 typeQualifier.qualifier != EvqBuffer)
3767 {
3768 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3769 checkIsNotUnsizedArray(field->line(),
3770 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003771 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003772 }
3773
Jiajia Qinbc585152017-06-23 15:42:17 +08003774 if (typeQualifier.qualifier == EvqBuffer)
3775 {
3776 // set memory qualifiers
3777 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3778 // qualified with a memory qualifier, it is as if all of its members were declared with
3779 // the same memory qualifier.
3780 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3781 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3782 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3783 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3784 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3785 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3786 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3787 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3788 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3789 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3790 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003791 }
3792
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003793 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003794 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003795 if (!symbolTable.declare(interfaceBlock))
Olli Etuaho378c3a52017-12-04 11:32:13 +02003796 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003797 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003798 }
3799
Olli Etuahob60d30f2018-01-16 12:31:06 +02003800 TType *interfaceBlockType =
3801 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003802 if (arrayIndex != nullptr)
3803 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003804 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003805 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003806
Olli Etuaho195be942017-12-04 23:40:14 +02003807 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003808 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3809 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003810 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003811 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003812 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003813
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003814 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003815 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003816 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003817 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3818 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003819 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003820 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003821
3822 // set parent pointer of the field variable
3823 fieldType->setInterfaceBlock(interfaceBlock);
3824
Olli Etuahob60d30f2018-01-16 12:31:06 +02003825 fieldType->setQualifier(typeQualifier.qualifier);
3826
Olli Etuaho195be942017-12-04 23:40:14 +02003827 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003828 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003829 if (!symbolTable.declare(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303830 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003831 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003832 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003833 }
3834 }
3835 }
3836 else
3837 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003838 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003839
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003840 // add a symbol for this interface block
Olli Etuaho437664b2018-02-28 15:38:14 +02003841 if (!symbolTable.declare(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303842 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003843 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003844 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003845 }
3846
Olli Etuaho195be942017-12-04 23:40:14 +02003847 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3848 blockSymbol->setLine(typeQualifier.line);
3849 TIntermDeclaration *declaration = new TIntermDeclaration();
3850 declaration->appendDeclarator(blockSymbol);
3851 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003852
3853 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003854 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003855}
3856
Olli Etuahofbb1c792018-01-19 16:26:59 +02003857void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3858 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003859{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003860 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003861
3862 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003863 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303864 if (mStructNestingLevel > 1)
3865 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003866 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003867 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003868}
3869
3870void TParseContext::exitStructDeclaration()
3871{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003872 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003873}
3874
Olli Etuaho8a176262016-08-16 14:23:01 +03003875void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003876{
Jamie Madillacb4b812016-11-07 13:50:29 -05003877 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303878 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003879 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003880 }
3881
Arun Patole7e7e68d2015-05-22 12:02:25 +05303882 if (field.type()->getBasicType() != EbtStruct)
3883 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003884 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003885 }
3886
3887 // We're already inside a structure definition at this point, so add
3888 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303889 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3890 {
Jamie Madill41a49272014-03-18 16:10:13 -04003891 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003892 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3893 {
3894 // This may happen in case there are nested struct definitions. While they are also
3895 // invalid GLSL, they don't cause a syntax error.
3896 reasonStream << "Struct nesting";
3897 }
3898 else
3899 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003900 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003901 }
3902 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003903 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003904 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003905 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003906 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003907}
3908
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003909//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003910// Parse an array index expression
3911//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003912TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3913 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303914 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003915{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003916 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3917 {
3918 if (baseExpression->getAsSymbolNode())
3919 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303920 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003921 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003922 }
3923 else
3924 {
3925 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3926 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003927
Olli Etuaho3ec75682017-07-05 17:02:55 +03003928 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003929 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003930
Jiawei Shaod8105a02017-08-08 09:54:36 +08003931 if (baseExpression->getQualifier() == EvqPerVertexIn)
3932 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003933 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003934 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3935 {
3936 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3937 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3938 }
3939 }
3940
Jamie Madill21c1e452014-12-29 11:33:41 -05003941 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3942
Olli Etuaho36b05142015-11-12 13:10:42 +02003943 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3944 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3945 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3946 // index is a constant expression.
3947 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3948 {
3949 if (baseExpression->isInterfaceBlock())
3950 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003951 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003952 switch (baseExpression->getQualifier())
3953 {
3954 case EvqPerVertexIn:
3955 break;
3956 case EvqUniform:
3957 case EvqBuffer:
3958 error(location,
3959 "array indexes for uniform block arrays and shader storage block arrays "
3960 "must be constant integral expressions",
3961 "[");
3962 break;
3963 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003964 // We can reach here only in error cases.
3965 ASSERT(mDiagnostics->numErrors() > 0);
3966 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003967 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003968 }
3969 else if (baseExpression->getQualifier() == EvqFragmentOut)
3970 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003971 error(location,
3972 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003973 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003974 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3975 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003976 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003977 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003978 }
3979
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003980 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003981 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003982 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3983 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3984 // constant fold expressions that are not constant expressions). The most compatible way to
3985 // handle this case is to report a warning instead of an error and force the index to be in
3986 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003987 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003988 int index = 0;
3989 if (indexConstantUnion->getBasicType() == EbtInt)
3990 {
3991 index = indexConstantUnion->getIConst(0);
3992 }
3993 else if (indexConstantUnion->getBasicType() == EbtUInt)
3994 {
3995 index = static_cast<int>(indexConstantUnion->getUConst(0));
3996 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003997
3998 int safeIndex = -1;
3999
Olli Etuahoebee5b32017-11-23 12:56:32 +02004000 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004001 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004002 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4003 safeIndex = 0;
4004 }
4005
4006 if (!baseExpression->getType().isUnsizedArray())
4007 {
4008 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004009 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004010 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004011 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004012 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4013 {
4014 outOfRangeError(outOfRangeIndexIsError, location,
4015 "array index for gl_FragData must be zero when "
4016 "GL_EXT_draw_buffers is disabled",
4017 "[]");
4018 safeIndex = 0;
4019 }
4020 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004021 }
4022 // Only do generic out-of-range check if similar error hasn't already been reported.
4023 if (safeIndex < 0)
4024 {
4025 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004026 {
4027 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4028 baseExpression->getOutermostArraySize(),
4029 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004030 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004031 else if (baseExpression->isMatrix())
4032 {
4033 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4034 baseExpression->getType().getCols(),
4035 "matrix field selection out of range");
4036 }
4037 else
4038 {
4039 ASSERT(baseExpression->isVector());
4040 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4041 baseExpression->getType().getNominalSize(),
4042 "vector field selection out of range");
4043 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004044 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004045
Olli Etuahoebee5b32017-11-23 12:56:32 +02004046 ASSERT(safeIndex >= 0);
4047 // Data of constant unions can't be changed, because it may be shared with other
4048 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4049 // sanitized object.
4050 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4051 {
4052 TConstantUnion *safeConstantUnion = new TConstantUnion();
4053 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004054 indexExpression = new TIntermConstantUnion(
4055 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4056 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004057 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004058
Olli Etuahoebee5b32017-11-23 12:56:32 +02004059 TIntermBinary *node =
4060 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4061 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004062 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004063 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004064 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004065
4066 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4067 node->setLine(location);
4068 // Indirect indexing can never be constant folded.
4069 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004070}
4071
Olli Etuahoebee5b32017-11-23 12:56:32 +02004072int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4073 const TSourceLoc &location,
4074 int index,
4075 int arraySize,
4076 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004077{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004078 // Should not reach here with an unsized / runtime-sized array.
4079 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004080 // A negative index should already have been checked.
4081 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004082 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004083 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004084 std::stringstream reasonStream;
4085 reasonStream << reason << " '" << index << "'";
4086 std::string token = reasonStream.str();
4087 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004088 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004089 }
4090 return index;
4091}
4092
Jamie Madillb98c3a82015-07-23 14:26:04 -04004093TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4094 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004095 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004096 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004097{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004098 if (baseExpression->isArray())
4099 {
4100 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004101 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004102 }
4103
4104 if (baseExpression->isVector())
4105 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004106 TVector<int> fieldOffsets;
4107 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4108 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004109 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004110 fieldOffsets.resize(1);
4111 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004112 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004113 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4114 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004115
Olli Etuaho765924f2018-01-04 12:48:36 +02004116 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004117 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004118 else if (baseExpression->getBasicType() == EbtStruct)
4119 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304120 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004121 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004122 {
4123 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004124 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004125 }
4126 else
4127 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004128 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004129 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004130 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004131 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004132 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004133 {
4134 fieldFound = true;
4135 break;
4136 }
4137 }
4138 if (fieldFound)
4139 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004140 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004141 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004142 TIntermBinary *node =
4143 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4144 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004145 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004146 }
4147 else
4148 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004149 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004150 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004151 }
4152 }
4153 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004154 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004155 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304156 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004157 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004158 {
4159 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004160 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004161 }
4162 else
4163 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004164 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004165 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004166 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004167 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004168 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004169 {
4170 fieldFound = true;
4171 break;
4172 }
4173 }
4174 if (fieldFound)
4175 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004176 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004177 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004178 TIntermBinary *node =
4179 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4180 node->setLine(dotLocation);
4181 // Indexing interface blocks can never be constant folded.
4182 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004183 }
4184 else
4185 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004186 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004187 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004188 }
4189 }
4190 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004191 else
4192 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004193 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004194 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004195 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004196 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004197 }
4198 else
4199 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304200 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004201 " field selection requires structure, vector, or interface block on left hand "
4202 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004203 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004204 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004205 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004206 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004207}
4208
Olli Etuahofbb1c792018-01-19 16:26:59 +02004209TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004210 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004211{
Jamie Madill2f294c92017-11-20 14:47:26 -05004212 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004213
4214 if (qualifierType == "shared")
4215 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004216 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004217 {
4218 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4219 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004220 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004221 }
4222 else if (qualifierType == "packed")
4223 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004224 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004225 {
4226 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4227 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004228 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004229 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004230 else if (qualifierType == "std430")
4231 {
4232 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4233 qualifier.blockStorage = EbsStd430;
4234 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004235 else if (qualifierType == "std140")
4236 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004237 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004238 }
4239 else if (qualifierType == "row_major")
4240 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004241 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004242 }
4243 else if (qualifierType == "column_major")
4244 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004245 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004246 }
4247 else if (qualifierType == "location")
4248 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004249 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004250 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004251 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004252 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004253 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004254 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4255 {
4256 qualifier.yuv = true;
4257 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004258 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004259 else if (qualifierType == "rgba32f")
4260 {
4261 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4262 qualifier.imageInternalFormat = EiifRGBA32F;
4263 }
4264 else if (qualifierType == "rgba16f")
4265 {
4266 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4267 qualifier.imageInternalFormat = EiifRGBA16F;
4268 }
4269 else if (qualifierType == "r32f")
4270 {
4271 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4272 qualifier.imageInternalFormat = EiifR32F;
4273 }
4274 else if (qualifierType == "rgba8")
4275 {
4276 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4277 qualifier.imageInternalFormat = EiifRGBA8;
4278 }
4279 else if (qualifierType == "rgba8_snorm")
4280 {
4281 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4282 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4283 }
4284 else if (qualifierType == "rgba32i")
4285 {
4286 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4287 qualifier.imageInternalFormat = EiifRGBA32I;
4288 }
4289 else if (qualifierType == "rgba16i")
4290 {
4291 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4292 qualifier.imageInternalFormat = EiifRGBA16I;
4293 }
4294 else if (qualifierType == "rgba8i")
4295 {
4296 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4297 qualifier.imageInternalFormat = EiifRGBA8I;
4298 }
4299 else if (qualifierType == "r32i")
4300 {
4301 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4302 qualifier.imageInternalFormat = EiifR32I;
4303 }
4304 else if (qualifierType == "rgba32ui")
4305 {
4306 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4307 qualifier.imageInternalFormat = EiifRGBA32UI;
4308 }
4309 else if (qualifierType == "rgba16ui")
4310 {
4311 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4312 qualifier.imageInternalFormat = EiifRGBA16UI;
4313 }
4314 else if (qualifierType == "rgba8ui")
4315 {
4316 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4317 qualifier.imageInternalFormat = EiifRGBA8UI;
4318 }
4319 else if (qualifierType == "r32ui")
4320 {
4321 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4322 qualifier.imageInternalFormat = EiifR32UI;
4323 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004324 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4325 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004326 {
4327 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4328 qualifier.primitiveType = EptPoints;
4329 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004330 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4331 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004332 {
4333 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4334 qualifier.primitiveType = EptLines;
4335 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004336 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4337 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004338 {
4339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4340 qualifier.primitiveType = EptLinesAdjacency;
4341 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004342 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4343 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004344 {
4345 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4346 qualifier.primitiveType = EptTriangles;
4347 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004348 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4349 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004350 {
4351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4352 qualifier.primitiveType = EptTrianglesAdjacency;
4353 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004354 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4355 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004356 {
4357 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4358 qualifier.primitiveType = EptLineStrip;
4359 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004360 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4361 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004362 {
4363 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4364 qualifier.primitiveType = EptTriangleStrip;
4365 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004366
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004367 else
4368 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004369 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004370 }
4371
Jamie Madilla5efff92013-06-06 11:56:47 -04004372 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004373}
4374
Olli Etuahofbb1c792018-01-19 16:26:59 +02004375void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004376 const TSourceLoc &qualifierTypeLine,
4377 int intValue,
4378 const TSourceLoc &intValueLine,
4379 const std::string &intValueString,
4380 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004381 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004382{
Olli Etuaho856c4972016-08-08 11:38:39 +03004383 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004384 if (intValue < 1)
4385 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004386 std::stringstream reasonStream;
4387 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4388 std::string reason = reasonStream.str();
4389 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004390 }
4391 (*localSize)[index] = intValue;
4392}
4393
Olli Etuaho09b04a22016-12-15 13:30:26 +00004394void TParseContext::parseNumViews(int intValue,
4395 const TSourceLoc &intValueLine,
4396 const std::string &intValueString,
4397 int *numViews)
4398{
4399 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4400 // specification.
4401 if (intValue < 1)
4402 {
4403 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4404 }
4405 *numViews = intValue;
4406}
4407
Shaob5cc1192017-07-06 10:47:20 +08004408void TParseContext::parseInvocations(int intValue,
4409 const TSourceLoc &intValueLine,
4410 const std::string &intValueString,
4411 int *numInvocations)
4412{
4413 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4414 // it doesn't make sense to accept invocations <= 0.
4415 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4416 {
4417 error(intValueLine,
4418 "out of range: invocations must be in the range of [1, "
4419 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4420 intValueString.c_str());
4421 }
4422 else
4423 {
4424 *numInvocations = intValue;
4425 }
4426}
4427
4428void TParseContext::parseMaxVertices(int intValue,
4429 const TSourceLoc &intValueLine,
4430 const std::string &intValueString,
4431 int *maxVertices)
4432{
4433 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4434 // it doesn't make sense to accept max_vertices < 0.
4435 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4436 {
4437 error(
4438 intValueLine,
4439 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4440 intValueString.c_str());
4441 }
4442 else
4443 {
4444 *maxVertices = intValue;
4445 }
4446}
4447
Olli Etuahofbb1c792018-01-19 16:26:59 +02004448TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004449 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004450 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304451 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004452{
Jamie Madill2f294c92017-11-20 14:47:26 -05004453 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004454
Martin Radev802abe02016-08-04 17:48:32 +03004455 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004456
Martin Radev802abe02016-08-04 17:48:32 +03004457 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004458 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004459 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004460 if (intValue < 0)
4461 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004462 error(intValueLine, "out of range: location must be non-negative",
4463 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004464 }
4465 else
4466 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004467 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004468 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004469 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004470 }
Olli Etuaho43364892017-02-13 16:00:12 +00004471 else if (qualifierType == "binding")
4472 {
4473 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4474 if (intValue < 0)
4475 {
4476 error(intValueLine, "out of range: binding must be non-negative",
4477 intValueString.c_str());
4478 }
4479 else
4480 {
4481 qualifier.binding = intValue;
4482 }
4483 }
jchen104cdac9e2017-05-08 11:01:20 +08004484 else if (qualifierType == "offset")
4485 {
4486 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4487 if (intValue < 0)
4488 {
4489 error(intValueLine, "out of range: offset must be non-negative",
4490 intValueString.c_str());
4491 }
4492 else
4493 {
4494 qualifier.offset = intValue;
4495 }
4496 }
Martin Radev802abe02016-08-04 17:48:32 +03004497 else if (qualifierType == "local_size_x")
4498 {
4499 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4500 &qualifier.localSize);
4501 }
4502 else if (qualifierType == "local_size_y")
4503 {
4504 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4505 &qualifier.localSize);
4506 }
4507 else if (qualifierType == "local_size_z")
4508 {
4509 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4510 &qualifier.localSize);
4511 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004512 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004513 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004514 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4515 {
4516 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4517 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004518 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004519 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4520 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004521 {
4522 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4523 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004524 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4525 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004526 {
4527 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4528 }
4529
Martin Radev802abe02016-08-04 17:48:32 +03004530 else
4531 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004532 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004533 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004534
Jamie Madilla5efff92013-06-06 11:56:47 -04004535 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004536}
4537
Olli Etuaho613b9592016-09-05 12:05:53 +03004538TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4539{
4540 return new TTypeQualifierBuilder(
4541 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4542 mShaderVersion);
4543}
4544
Olli Etuahocce89652017-06-19 16:04:09 +03004545TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4546 const TSourceLoc &loc)
4547{
4548 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4549 return new TStorageQualifierWrapper(qualifier, loc);
4550}
4551
4552TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4553{
4554 if (getShaderType() == GL_VERTEX_SHADER)
4555 {
4556 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4557 }
4558 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4559}
4560
4561TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4562{
4563 if (declaringFunction())
4564 {
4565 return new TStorageQualifierWrapper(EvqIn, loc);
4566 }
Shaob5cc1192017-07-06 10:47:20 +08004567
4568 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004569 {
Shaob5cc1192017-07-06 10:47:20 +08004570 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004571 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004572 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004573 {
4574 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4575 }
4576 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004577 }
Shaob5cc1192017-07-06 10:47:20 +08004578 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004579 {
Shaob5cc1192017-07-06 10:47:20 +08004580 if (mShaderVersion < 300)
4581 {
4582 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4583 }
4584 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004585 }
Shaob5cc1192017-07-06 10:47:20 +08004586 case GL_COMPUTE_SHADER:
4587 {
4588 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4589 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004590 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004591 {
4592 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4593 }
4594 default:
4595 {
4596 UNREACHABLE();
4597 return new TStorageQualifierWrapper(EvqLast, loc);
4598 }
Olli Etuahocce89652017-06-19 16:04:09 +03004599 }
Olli Etuahocce89652017-06-19 16:04:09 +03004600}
4601
4602TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4603{
4604 if (declaringFunction())
4605 {
4606 return new TStorageQualifierWrapper(EvqOut, loc);
4607 }
Shaob5cc1192017-07-06 10:47:20 +08004608 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004609 {
Shaob5cc1192017-07-06 10:47:20 +08004610 case GL_VERTEX_SHADER:
4611 {
4612 if (mShaderVersion < 300)
4613 {
4614 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4615 }
4616 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4617 }
4618 case GL_FRAGMENT_SHADER:
4619 {
4620 if (mShaderVersion < 300)
4621 {
4622 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4623 }
4624 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4625 }
4626 case GL_COMPUTE_SHADER:
4627 {
4628 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4629 return new TStorageQualifierWrapper(EvqLast, loc);
4630 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004631 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004632 {
4633 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4634 }
4635 default:
4636 {
4637 UNREACHABLE();
4638 return new TStorageQualifierWrapper(EvqLast, loc);
4639 }
Olli Etuahocce89652017-06-19 16:04:09 +03004640 }
Olli Etuahocce89652017-06-19 16:04:09 +03004641}
4642
4643TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4644{
4645 if (!declaringFunction())
4646 {
4647 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4648 }
4649 return new TStorageQualifierWrapper(EvqInOut, loc);
4650}
4651
Jamie Madillb98c3a82015-07-23 14:26:04 -04004652TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004653 TLayoutQualifier rightQualifier,
4654 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004655{
Martin Radevc28888b2016-07-22 15:27:42 +03004656 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004657 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004658}
4659
Olli Etuahofbb1c792018-01-19 16:26:59 +02004660TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4661 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004662{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004663 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004664 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004665}
4666
Olli Etuahofbb1c792018-01-19 16:26:59 +02004667TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004668 const TSourceLoc &loc,
4669 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004670{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004671 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004672 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004673}
4674
Olli Etuaho722bfb52017-10-26 17:00:11 +03004675void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4676 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004677 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004678 const TSourceLoc &location)
4679{
4680 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4681 {
4682 if ((*fieldIter)->name() == name)
4683 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004684 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004685 }
4686 }
4687}
4688
4689TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4690{
4691 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4692 ++fieldIter)
4693 {
4694 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4695 location);
4696 }
4697 return fields;
4698}
4699
Olli Etuaho4de340a2016-12-16 09:32:03 +00004700TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4701 const TFieldList *newlyAddedFields,
4702 const TSourceLoc &location)
4703{
4704 for (TField *field : *newlyAddedFields)
4705 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004706 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4707 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004708 processedFields->push_back(field);
4709 }
4710 return processedFields;
4711}
4712
Martin Radev70866b82016-07-22 15:27:42 +03004713TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4714 const TTypeQualifierBuilder &typeQualifierBuilder,
4715 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004716 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004717{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004718 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004719
Martin Radev70866b82016-07-22 15:27:42 +03004720 typeSpecifier->qualifier = typeQualifier.qualifier;
4721 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004722 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004723 typeSpecifier->invariant = typeQualifier.invariant;
4724 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304725 {
Martin Radev70866b82016-07-22 15:27:42 +03004726 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004727 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004728 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004729}
4730
Jamie Madillb98c3a82015-07-23 14:26:04 -04004731TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004732 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004733{
Martin Radev4a9cd802016-09-01 16:51:51 +03004734 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4735 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004736
Olli Etuahofbb1c792018-01-19 16:26:59 +02004737 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004738 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004739
Martin Radev4a9cd802016-09-01 16:51:51 +03004740 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004741
Olli Etuahod5f44c92017-11-29 17:15:40 +02004742 TFieldList *fieldList = new TFieldList();
4743
4744 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304745 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004746 TType *type = new TType(typeSpecifier);
4747 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304748 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004749 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004750 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004751 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004752 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004753
Olli Etuahod5f44c92017-11-29 17:15:40 +02004754 TField *field = new TField(type, declarator->name(), declarator->line());
4755 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4756 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004757 }
4758
Olli Etuahod5f44c92017-11-29 17:15:40 +02004759 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004760}
4761
Martin Radev4a9cd802016-09-01 16:51:51 +03004762TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4763 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004764 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004765 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004766{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004767 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004768 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004769 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004770 structSymbolType = SymbolType::Empty;
4771 }
4772 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004773
Jamie Madill9b820842015-02-12 10:40:10 -05004774 // Store a bool in the struct if we're at global scope, to allow us to
4775 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004776 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004777
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004778 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004779 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004780 checkIsNotReserved(nameLine, structName);
Olli Etuaho437664b2018-02-28 15:38:14 +02004781 if (!symbolTable.declare(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304782 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004783 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004784 }
4785 }
4786
4787 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004788 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004789 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004790 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004791 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004792 switch (qualifier)
4793 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004794 case EvqGlobal:
4795 case EvqTemporary:
4796 break;
4797 default:
4798 error(field.line(), "invalid qualifier on struct member",
4799 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004800 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004801 }
Martin Radev70866b82016-07-22 15:27:42 +03004802 if (field.type()->isInvariant())
4803 {
4804 error(field.line(), "invalid qualifier on struct member", "invariant");
4805 }
jchen104cdac9e2017-05-08 11:01:20 +08004806 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4807 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004808 {
4809 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4810 }
4811
Olli Etuahoebee5b32017-11-23 12:56:32 +02004812 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004813 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004814
Olli Etuaho43364892017-02-13 16:00:12 +00004815 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4816
4817 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004818
4819 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004820 }
4821
Martin Radev4a9cd802016-09-01 16:51:51 +03004822 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004823 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004824 exitStructDeclaration();
4825
Martin Radev4a9cd802016-09-01 16:51:51 +03004826 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004827}
4828
Jamie Madillb98c3a82015-07-23 14:26:04 -04004829TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004830 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004831 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004832{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004833 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004834 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004835 init->isVector())
4836 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004837 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4838 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004839 return nullptr;
4840 }
4841
Olli Etuaho923ecef2017-10-11 12:01:38 +03004842 ASSERT(statementList);
Olli Etuahod05f9642018-03-05 12:13:26 +02004843 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004844 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004845 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004846 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004847 }
4848
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004849 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4850 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004851 return node;
4852}
4853
4854TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4855{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004856 if (mSwitchNestingLevel == 0)
4857 {
4858 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004859 return nullptr;
4860 }
4861 if (condition == nullptr)
4862 {
4863 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004864 return nullptr;
4865 }
4866 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004867 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004868 {
4869 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004870 }
4871 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004872 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4873 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4874 // fold in case labels.
4875 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004876 {
4877 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004878 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004879 TIntermCase *node = new TIntermCase(condition);
4880 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004881 return node;
4882}
4883
4884TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4885{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004886 if (mSwitchNestingLevel == 0)
4887 {
4888 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004889 return nullptr;
4890 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004891 TIntermCase *node = new TIntermCase(nullptr);
4892 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004893 return node;
4894}
4895
Jamie Madillb98c3a82015-07-23 14:26:04 -04004896TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4897 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004898 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004899{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004900 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004901
4902 switch (op)
4903 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004904 case EOpLogicalNot:
4905 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4906 child->isVector())
4907 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004908 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004909 return nullptr;
4910 }
4911 break;
4912 case EOpBitwiseNot:
4913 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4914 child->isMatrix() || child->isArray())
4915 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004916 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004917 return nullptr;
4918 }
4919 break;
4920 case EOpPostIncrement:
4921 case EOpPreIncrement:
4922 case EOpPostDecrement:
4923 case EOpPreDecrement:
4924 case EOpNegative:
4925 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004926 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4927 child->getBasicType() == EbtBool || child->isArray() ||
4928 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004929 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004930 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004931 return nullptr;
4932 }
Nico Weber41b072b2018-02-09 10:01:32 -05004933 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004934 // Operators for built-ins are already type checked against their prototype.
4935 default:
4936 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004937 }
4938
Jiajia Qinbc585152017-06-23 15:42:17 +08004939 if (child->getMemoryQualifier().writeonly)
4940 {
4941 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4942 return nullptr;
4943 }
4944
Olli Etuahof119a262016-08-19 15:54:22 +03004945 TIntermUnary *node = new TIntermUnary(op, child);
4946 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004947
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004948 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004949}
4950
Olli Etuaho09b22472015-02-11 11:47:26 +02004951TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4952{
Olli Etuahocce89652017-06-19 16:04:09 +03004953 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004954 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004955 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004956 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004957 return child;
4958 }
4959 return node;
4960}
4961
Jamie Madillb98c3a82015-07-23 14:26:04 -04004962TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4963 TIntermTyped *child,
4964 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004965{
Olli Etuaho856c4972016-08-08 11:38:39 +03004966 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004967 return addUnaryMath(op, child, loc);
4968}
4969
Olli Etuaho765924f2018-01-04 12:48:36 +02004970TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4971{
4972 // If we can, we should return the folded version of the expression for subsequent parsing. This
4973 // enables folding the containing expression during parsing as well, instead of the separate
4974 // FoldExpressions() step where folding nested expressions requires multiple full AST
4975 // traversals.
4976
4977 // Even if folding fails the fold() functions return some node representing the expression,
4978 // typically the original node. So "folded" can be assumed to be non-null.
4979 TIntermTyped *folded = expression->fold(mDiagnostics);
4980 ASSERT(folded != nullptr);
4981 if (folded->getQualifier() == expression->getQualifier())
4982 {
4983 // We need this expression to have the correct qualifier when validating the consuming
4984 // expression. So we can only return the folded node from here in case it has the same
4985 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4986 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4987 // 1. (true ? 1.0 : non_constant)
4988 // 2. (non_constant, 1.0)
4989 return folded;
4990 }
4991 return expression;
4992}
4993
Jamie Madillb98c3a82015-07-23 14:26:04 -04004994bool TParseContext::binaryOpCommonCheck(TOperator op,
4995 TIntermTyped *left,
4996 TIntermTyped *right,
4997 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004998{
jchen10b4cf5652017-05-05 18:51:17 +08004999 // Check opaque types are not allowed to be operands in expressions other than array indexing
5000 // and structure member selection.
5001 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
5002 {
5003 switch (op)
5004 {
5005 case EOpIndexDirect:
5006 case EOpIndexIndirect:
5007 break;
jchen10b4cf5652017-05-05 18:51:17 +08005008
5009 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05005010 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08005011 error(loc, "Invalid operation for variables with an opaque type",
5012 GetOperatorString(op));
5013 return false;
5014 }
5015 }
jchen10cc2a10e2017-05-03 14:05:12 +08005016
Jiajia Qinbc585152017-06-23 15:42:17 +08005017 if (right->getMemoryQualifier().writeonly)
5018 {
5019 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5020 return false;
5021 }
5022
5023 if (left->getMemoryQualifier().writeonly)
5024 {
5025 switch (op)
5026 {
5027 case EOpAssign:
5028 case EOpInitialize:
5029 case EOpIndexDirect:
5030 case EOpIndexIndirect:
5031 case EOpIndexDirectStruct:
5032 case EOpIndexDirectInterfaceBlock:
5033 break;
5034 default:
5035 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5036 return false;
5037 }
5038 }
5039
Olli Etuaho244be012016-08-18 15:26:02 +03005040 if (left->getType().getStruct() || right->getType().getStruct())
5041 {
5042 switch (op)
5043 {
5044 case EOpIndexDirectStruct:
5045 ASSERT(left->getType().getStruct());
5046 break;
5047 case EOpEqual:
5048 case EOpNotEqual:
5049 case EOpAssign:
5050 case EOpInitialize:
5051 if (left->getType() != right->getType())
5052 {
5053 return false;
5054 }
5055 break;
5056 default:
5057 error(loc, "Invalid operation for structs", GetOperatorString(op));
5058 return false;
5059 }
5060 }
5061
Olli Etuaho94050052017-05-08 14:17:44 +03005062 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5063 {
5064 switch (op)
5065 {
5066 case EOpIndexDirectInterfaceBlock:
5067 ASSERT(left->getType().getInterfaceBlock());
5068 break;
5069 default:
5070 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5071 return false;
5072 }
5073 }
5074
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005075 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005076 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005077 error(loc, "array / non-array mismatch", GetOperatorString(op));
5078 return false;
5079 }
5080
5081 if (left->isArray())
5082 {
5083 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005084 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005085 {
5086 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5087 return false;
5088 }
5089
Olli Etuahoe79904c2015-03-18 16:56:42 +02005090 switch (op)
5091 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005092 case EOpEqual:
5093 case EOpNotEqual:
5094 case EOpAssign:
5095 case EOpInitialize:
5096 break;
5097 default:
5098 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5099 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005100 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005101 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005102 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005103 {
5104 error(loc, "array size mismatch", GetOperatorString(op));
5105 return false;
5106 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005107 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005108
5109 // Check ops which require integer / ivec parameters
5110 bool isBitShift = false;
5111 switch (op)
5112 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005113 case EOpBitShiftLeft:
5114 case EOpBitShiftRight:
5115 case EOpBitShiftLeftAssign:
5116 case EOpBitShiftRightAssign:
5117 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5118 // check that the basic type is an integer type.
5119 isBitShift = true;
5120 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5121 {
5122 return false;
5123 }
5124 break;
5125 case EOpBitwiseAnd:
5126 case EOpBitwiseXor:
5127 case EOpBitwiseOr:
5128 case EOpBitwiseAndAssign:
5129 case EOpBitwiseXorAssign:
5130 case EOpBitwiseOrAssign:
5131 // It is enough to check the type of only one operand, since later it
5132 // is checked that the operand types match.
5133 if (!IsInteger(left->getBasicType()))
5134 {
5135 return false;
5136 }
5137 break;
5138 default:
5139 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005140 }
5141
5142 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5143 // So the basic type should usually match.
5144 if (!isBitShift && left->getBasicType() != right->getBasicType())
5145 {
5146 return false;
5147 }
5148
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005149 // Check that:
5150 // 1. Type sizes match exactly on ops that require that.
5151 // 2. Restrictions for structs that contain arrays or samplers are respected.
5152 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005153 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005154 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005155 case EOpAssign:
5156 case EOpInitialize:
5157 case EOpEqual:
5158 case EOpNotEqual:
5159 // ESSL 1.00 sections 5.7, 5.8, 5.9
5160 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5161 {
5162 error(loc, "undefined operation for structs containing arrays",
5163 GetOperatorString(op));
5164 return false;
5165 }
5166 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5167 // we interpret the spec so that this extends to structs containing samplers,
5168 // similarly to ESSL 1.00 spec.
5169 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5170 left->getType().isStructureContainingSamplers())
5171 {
5172 error(loc, "undefined operation for structs containing samplers",
5173 GetOperatorString(op));
5174 return false;
5175 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005176
Olli Etuahoe1805592017-01-02 16:41:20 +00005177 if ((left->getNominalSize() != right->getNominalSize()) ||
5178 (left->getSecondarySize() != right->getSecondarySize()))
5179 {
5180 error(loc, "dimension mismatch", GetOperatorString(op));
5181 return false;
5182 }
5183 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005184 case EOpLessThan:
5185 case EOpGreaterThan:
5186 case EOpLessThanEqual:
5187 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005188 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005189 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005190 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005191 return false;
5192 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005193 break;
5194 case EOpAdd:
5195 case EOpSub:
5196 case EOpDiv:
5197 case EOpIMod:
5198 case EOpBitShiftLeft:
5199 case EOpBitShiftRight:
5200 case EOpBitwiseAnd:
5201 case EOpBitwiseXor:
5202 case EOpBitwiseOr:
5203 case EOpAddAssign:
5204 case EOpSubAssign:
5205 case EOpDivAssign:
5206 case EOpIModAssign:
5207 case EOpBitShiftLeftAssign:
5208 case EOpBitShiftRightAssign:
5209 case EOpBitwiseAndAssign:
5210 case EOpBitwiseXorAssign:
5211 case EOpBitwiseOrAssign:
5212 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5213 {
5214 return false;
5215 }
5216
5217 // Are the sizes compatible?
5218 if (left->getNominalSize() != right->getNominalSize() ||
5219 left->getSecondarySize() != right->getSecondarySize())
5220 {
5221 // If the nominal sizes of operands do not match:
5222 // One of them must be a scalar.
5223 if (!left->isScalar() && !right->isScalar())
5224 return false;
5225
5226 // In the case of compound assignment other than multiply-assign,
5227 // the right side needs to be a scalar. Otherwise a vector/matrix
5228 // would be assigned to a scalar. A scalar can't be shifted by a
5229 // vector either.
5230 if (!right->isScalar() &&
5231 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5232 return false;
5233 }
5234 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005235 default:
5236 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005237 }
5238
Olli Etuahod6b14282015-03-17 14:31:35 +02005239 return true;
5240}
5241
Olli Etuaho1dded802016-08-18 18:13:13 +03005242bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5243 const TType &left,
5244 const TType &right)
5245{
5246 switch (op)
5247 {
5248 case EOpMul:
5249 case EOpMulAssign:
5250 return left.getNominalSize() == right.getNominalSize() &&
5251 left.getSecondarySize() == right.getSecondarySize();
5252 case EOpVectorTimesScalar:
5253 return true;
5254 case EOpVectorTimesScalarAssign:
5255 ASSERT(!left.isMatrix() && !right.isMatrix());
5256 return left.isVector() && !right.isVector();
5257 case EOpVectorTimesMatrix:
5258 return left.getNominalSize() == right.getRows();
5259 case EOpVectorTimesMatrixAssign:
5260 ASSERT(!left.isMatrix() && right.isMatrix());
5261 return left.isVector() && left.getNominalSize() == right.getRows() &&
5262 left.getNominalSize() == right.getCols();
5263 case EOpMatrixTimesVector:
5264 return left.getCols() == right.getNominalSize();
5265 case EOpMatrixTimesScalar:
5266 return true;
5267 case EOpMatrixTimesScalarAssign:
5268 ASSERT(left.isMatrix() && !right.isMatrix());
5269 return !right.isVector();
5270 case EOpMatrixTimesMatrix:
5271 return left.getCols() == right.getRows();
5272 case EOpMatrixTimesMatrixAssign:
5273 ASSERT(left.isMatrix() && right.isMatrix());
5274 // We need to check two things:
5275 // 1. The matrix multiplication step is valid.
5276 // 2. The result will have the same number of columns as the lvalue.
5277 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5278
5279 default:
5280 UNREACHABLE();
5281 return false;
5282 }
5283}
5284
Jamie Madillb98c3a82015-07-23 14:26:04 -04005285TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5286 TIntermTyped *left,
5287 TIntermTyped *right,
5288 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005289{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005290 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005291 return nullptr;
5292
Olli Etuahofc1806e2015-03-17 13:03:11 +02005293 switch (op)
5294 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005295 case EOpEqual:
5296 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005297 case EOpLessThan:
5298 case EOpGreaterThan:
5299 case EOpLessThanEqual:
5300 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005301 break;
5302 case EOpLogicalOr:
5303 case EOpLogicalXor:
5304 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005305 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5306 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005307 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005308 {
5309 return nullptr;
5310 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005311 // Basic types matching should have been already checked.
5312 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005313 break;
5314 case EOpAdd:
5315 case EOpSub:
5316 case EOpDiv:
5317 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005318 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5319 !right->getType().getStruct());
5320 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005321 {
5322 return nullptr;
5323 }
5324 break;
5325 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005326 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5327 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005328 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005329 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005330 {
5331 return nullptr;
5332 }
5333 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005334 default:
5335 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005336 }
5337
Olli Etuaho1dded802016-08-18 18:13:13 +03005338 if (op == EOpMul)
5339 {
5340 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5341 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5342 {
5343 return nullptr;
5344 }
5345 }
5346
Olli Etuaho3fdec912016-08-18 15:08:06 +03005347 TIntermBinary *node = new TIntermBinary(op, left, right);
5348 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005349 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005350}
5351
Jamie Madillb98c3a82015-07-23 14:26:04 -04005352TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5353 TIntermTyped *left,
5354 TIntermTyped *right,
5355 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005356{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005357 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005358 if (node == 0)
5359 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005360 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5361 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005362 return left;
5363 }
5364 return node;
5365}
5366
Jamie Madillb98c3a82015-07-23 14:26:04 -04005367TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5368 TIntermTyped *left,
5369 TIntermTyped *right,
5370 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005371{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005372 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005373 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005374 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005375 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5376 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005377 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005378 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005379 }
5380 return node;
5381}
5382
Olli Etuaho13389b62016-10-16 11:48:18 +01005383TIntermBinary *TParseContext::createAssign(TOperator op,
5384 TIntermTyped *left,
5385 TIntermTyped *right,
5386 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005387{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005388 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005389 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005390 if (op == EOpMulAssign)
5391 {
5392 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5393 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5394 {
5395 return nullptr;
5396 }
5397 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005398 TIntermBinary *node = new TIntermBinary(op, left, right);
5399 node->setLine(loc);
5400
Olli Etuaho3fdec912016-08-18 15:08:06 +03005401 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005402 }
5403 return nullptr;
5404}
5405
Jamie Madillb98c3a82015-07-23 14:26:04 -04005406TIntermTyped *TParseContext::addAssign(TOperator op,
5407 TIntermTyped *left,
5408 TIntermTyped *right,
5409 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005410{
Olli Etuahocce89652017-06-19 16:04:09 +03005411 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005412 TIntermTyped *node = createAssign(op, left, right, loc);
5413 if (node == nullptr)
5414 {
5415 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005416 return left;
5417 }
5418 return node;
5419}
5420
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005421TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5422 TIntermTyped *right,
5423 const TSourceLoc &loc)
5424{
Corentin Wallez0d959252016-07-12 17:26:32 -04005425 // WebGL2 section 5.26, the following results in an error:
5426 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005427 if (mShaderSpec == SH_WEBGL2_SPEC &&
5428 (left->isArray() || left->getBasicType() == EbtVoid ||
5429 left->getType().isStructureContainingArrays() || right->isArray() ||
5430 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005431 {
5432 error(loc,
5433 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5434 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005435 }
5436
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005437 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho765924f2018-01-04 12:48:36 +02005438
5439 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005440}
5441
Olli Etuaho49300862015-02-20 14:54:49 +02005442TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5443{
5444 switch (op)
5445 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005446 case EOpContinue:
5447 if (mLoopNestingLevel <= 0)
5448 {
5449 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005450 }
5451 break;
5452 case EOpBreak:
5453 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5454 {
5455 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005456 }
5457 break;
5458 case EOpReturn:
5459 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5460 {
5461 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005462 }
5463 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005464 case EOpKill:
5465 if (mShaderType != GL_FRAGMENT_SHADER)
5466 {
5467 error(loc, "discard supported in fragment shaders only", "discard");
5468 }
5469 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005470 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005471 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005472 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005473 }
Olli Etuahocce89652017-06-19 16:04:09 +03005474 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005475}
5476
Jamie Madillb98c3a82015-07-23 14:26:04 -04005477TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005478 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005479 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005480{
Olli Etuahocce89652017-06-19 16:04:09 +03005481 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005482 {
Olli Etuahocce89652017-06-19 16:04:09 +03005483 ASSERT(op == EOpReturn);
5484 mFunctionReturnsValue = true;
5485 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5486 {
5487 error(loc, "void function cannot return a value", "return");
5488 }
5489 else if (*mCurrentFunctionType != expression->getType())
5490 {
5491 error(loc, "function return is not matching type:", "return");
5492 }
Olli Etuaho49300862015-02-20 14:54:49 +02005493 }
Olli Etuahocce89652017-06-19 16:04:09 +03005494 TIntermBranch *node = new TIntermBranch(op, expression);
5495 node->setLine(loc);
5496 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005497}
5498
Martin Radev84aa2dc2017-09-11 15:51:02 +03005499void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5500{
5501 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005502 const ImmutableString &name = functionCall->getFunction()->name();
5503 bool isTextureGather = name == kTextureGatherName;
5504 bool isTextureGatherOffset = name == kTextureGatherOffsetName;
Martin Radev84aa2dc2017-09-11 15:51:02 +03005505 if (isTextureGather || isTextureGatherOffset)
5506 {
5507 TIntermNode *componentNode = nullptr;
5508 TIntermSequence *arguments = functionCall->getSequence();
5509 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5510 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5511 ASSERT(sampler != nullptr);
5512 switch (sampler->getBasicType())
5513 {
5514 case EbtSampler2D:
5515 case EbtISampler2D:
5516 case EbtUSampler2D:
5517 case EbtSampler2DArray:
5518 case EbtISampler2DArray:
5519 case EbtUSampler2DArray:
5520 if ((isTextureGather && arguments->size() == 3u) ||
5521 (isTextureGatherOffset && arguments->size() == 4u))
5522 {
5523 componentNode = arguments->back();
5524 }
5525 break;
5526 case EbtSamplerCube:
5527 case EbtISamplerCube:
5528 case EbtUSamplerCube:
5529 ASSERT(!isTextureGatherOffset);
5530 if (arguments->size() == 3u)
5531 {
5532 componentNode = arguments->back();
5533 }
5534 break;
5535 case EbtSampler2DShadow:
5536 case EbtSampler2DArrayShadow:
5537 case EbtSamplerCubeShadow:
5538 break;
5539 default:
5540 UNREACHABLE();
5541 break;
5542 }
5543 if (componentNode)
5544 {
5545 const TIntermConstantUnion *componentConstantUnion =
5546 componentNode->getAsConstantUnion();
5547 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5548 {
5549 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuahofbb1c792018-01-19 16:26:59 +02005550 name);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005551 }
5552 else
5553 {
5554 int component = componentConstantUnion->getIConst(0);
5555 if (component < 0 || component > 3)
5556 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005557 error(functionCall->getLine(), "Component must be in the range [0;3]", name);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005558 }
5559 }
5560 }
5561 }
5562}
5563
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005564void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5565{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005566 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005567 const ImmutableString &name = functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005568 TIntermNode *offset = nullptr;
5569 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005570 bool useTextureGatherOffsetConstraints = false;
Olli Etuahofbb1c792018-01-19 16:26:59 +02005571 if (name == kTexelFetchOffsetName || name == kTextureLodOffsetName ||
5572 name == kTextureProjLodOffsetName || name == kTextureGradOffsetName ||
5573 name == kTextureProjGradOffsetName)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005574 {
5575 offset = arguments->back();
5576 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005577 else if (name == kTextureOffsetName || name == kTextureProjOffsetName)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005578 {
5579 // A bias parameter might follow the offset parameter.
5580 ASSERT(arguments->size() >= 3);
5581 offset = (*arguments)[2];
5582 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005583 else if (name == kTextureGatherOffsetName)
Martin Radev84aa2dc2017-09-11 15:51:02 +03005584 {
5585 ASSERT(arguments->size() >= 3u);
5586 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5587 ASSERT(sampler != nullptr);
5588 switch (sampler->getBasicType())
5589 {
5590 case EbtSampler2D:
5591 case EbtISampler2D:
5592 case EbtUSampler2D:
5593 case EbtSampler2DArray:
5594 case EbtISampler2DArray:
5595 case EbtUSampler2DArray:
5596 offset = (*arguments)[2];
5597 break;
5598 case EbtSampler2DShadow:
5599 case EbtSampler2DArrayShadow:
5600 offset = (*arguments)[3];
5601 break;
5602 default:
5603 UNREACHABLE();
5604 break;
5605 }
5606 useTextureGatherOffsetConstraints = true;
5607 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005608 if (offset != nullptr)
5609 {
5610 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5611 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5612 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005613 error(functionCall->getLine(), "Texture offset must be a constant expression", name);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005614 }
5615 else
5616 {
5617 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5618 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005619 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005620 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5621 : mMinProgramTexelOffset;
5622 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5623 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005624 for (size_t i = 0u; i < size; ++i)
5625 {
5626 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005627 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005628 {
5629 std::stringstream tokenStream;
5630 tokenStream << offsetValue;
5631 std::string token = tokenStream.str();
5632 error(offset->getLine(), "Texture offset value out of valid range",
5633 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005634 }
5635 }
5636 }
5637 }
5638}
5639
Jiajia Qina3106c52017-11-03 09:39:39 +08005640void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5641{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005642 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahofbb1c792018-01-19 16:26:59 +02005643 const ImmutableString &functionName = functionCall->getFunction()->name();
Olli Etuahobed35d72017-12-20 16:36:26 +02005644 if (IsAtomicBuiltin(functionName))
Jiajia Qina3106c52017-11-03 09:39:39 +08005645 {
5646 TIntermSequence *arguments = functionCall->getSequence();
5647 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5648
5649 if (IsBufferOrSharedVariable(memNode))
5650 {
5651 return;
5652 }
5653
5654 while (memNode->getAsBinaryNode())
5655 {
5656 memNode = memNode->getAsBinaryNode()->getLeft();
5657 if (IsBufferOrSharedVariable(memNode))
5658 {
5659 return;
5660 }
5661 }
5662
5663 error(memNode->getLine(),
5664 "The value passed to the mem argument of an atomic memory function does not "
5665 "correspond to a buffer or shared variable.",
Olli Etuahofbb1c792018-01-19 16:26:59 +02005666 functionName);
Jiajia Qina3106c52017-11-03 09:39:39 +08005667 }
5668}
5669
Martin Radev2cc85b32016-08-05 16:22:53 +03005670// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5671void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5672{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005673 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005674
Olli Etuahofbb1c792018-01-19 16:26:59 +02005675 if (functionCall->getFunction()->isImageFunction())
Martin Radev2cc85b32016-08-05 16:22:53 +03005676 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005677 const ImmutableString &name = functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005678 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005679 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005680
Olli Etuaho485eefd2017-02-14 17:40:06 +00005681 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005682
Olli Etuahofbb1c792018-01-19 16:26:59 +02005683 if (strcmp(name.data() + 5u, "Store") == 0)
Martin Radev2cc85b32016-08-05 16:22:53 +03005684 {
5685 if (memoryQualifier.readonly)
5686 {
5687 error(imageNode->getLine(),
5688 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005689 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005690 }
5691 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02005692 else if (strcmp(name.data() + 5u, "Load") == 0)
Martin Radev2cc85b32016-08-05 16:22:53 +03005693 {
5694 if (memoryQualifier.writeonly)
5695 {
5696 error(imageNode->getLine(),
5697 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005698 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005699 }
5700 }
5701 }
5702}
5703
5704// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5705void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5706 const TFunction *functionDefinition,
5707 const TIntermAggregate *functionCall)
5708{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005709 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005710
5711 const TIntermSequence &arguments = *functionCall->getSequence();
5712
5713 ASSERT(functionDefinition->getParamCount() == arguments.size());
5714
5715 for (size_t i = 0; i < arguments.size(); ++i)
5716 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005717 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5718 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005719 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5720 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5721
5722 if (IsImage(functionArgumentType.getBasicType()))
5723 {
5724 const TMemoryQualifier &functionArgumentMemoryQualifier =
5725 functionArgumentType.getMemoryQualifier();
5726 const TMemoryQualifier &functionParameterMemoryQualifier =
5727 functionParameterType.getMemoryQualifier();
5728 if (functionArgumentMemoryQualifier.readonly &&
5729 !functionParameterMemoryQualifier.readonly)
5730 {
5731 error(functionCall->getLine(),
5732 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005733 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005734 }
5735
5736 if (functionArgumentMemoryQualifier.writeonly &&
5737 !functionParameterMemoryQualifier.writeonly)
5738 {
5739 error(functionCall->getLine(),
5740 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005741 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005742 }
Martin Radev049edfa2016-11-11 14:35:37 +02005743
5744 if (functionArgumentMemoryQualifier.coherent &&
5745 !functionParameterMemoryQualifier.coherent)
5746 {
5747 error(functionCall->getLine(),
5748 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005749 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005750 }
5751
5752 if (functionArgumentMemoryQualifier.volatileQualifier &&
5753 !functionParameterMemoryQualifier.volatileQualifier)
5754 {
5755 error(functionCall->getLine(),
5756 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005757 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005758 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005759 }
5760 }
5761}
5762
Olli Etuaho95ed1942018-02-01 14:01:19 +02005763TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005764{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005765 if (fnCall->thisNode() != nullptr)
5766 {
5767 return addMethod(fnCall, loc);
5768 }
5769 if (fnCall->isConstructor())
5770 {
5771 return addConstructor(fnCall, loc);
5772 }
5773 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005774}
5775
Olli Etuaho95ed1942018-02-01 14:01:19 +02005776TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005777{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005778 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005779 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5780 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5781 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005782 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005783 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005784 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005785 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005786 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005787 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005788 {
5789 error(loc, "method takes no parameters", "length");
5790 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005791 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005792 {
5793 error(loc, "length can only be called on arrays", "length");
5794 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005795 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005796 mGeometryShaderInputPrimitiveType == EptUndefined)
5797 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005798 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005799 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5800 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005801 else
5802 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005803 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005804 node->setLine(loc);
5805 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005806 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005807 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005808}
5809
Olli Etuaho95ed1942018-02-01 14:01:19 +02005810TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005811 const TSourceLoc &loc)
5812{
Olli Etuaho697bf652018-02-16 11:50:54 +02005813 // First check whether the function has been hidden by a variable name or struct typename by
5814 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5815 // with a matching argument list.
5816 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005817 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005818 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005819 }
5820 else
5821 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005822 // There are no inner functions, so it's enough to look for user-defined functions in the
5823 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005824 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005825 if (symbol != nullptr)
5826 {
5827 // A user-defined function - could be an overloaded built-in as well.
5828 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5829 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5830 TIntermAggregate *callNode =
5831 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5832 callNode->setLine(loc);
5833 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5834 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5835 return callNode;
5836 }
5837
5838 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005839 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005840 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005841 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005842 }
5843 else
5844 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005845 // A built-in function.
5846 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005847 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005848
Olli Etuaho37b697e2018-01-29 12:19:27 +02005849 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005850 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005851 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005852 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005853 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005854 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005855 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005856 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005857 if (fnCandidate->getParamCount() == 1)
5858 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005859 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005860 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005861 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005862 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005863 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005864 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005865 TIntermAggregate *callNode =
5866 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005867 callNode->setLine(loc);
5868
Olli Etuahoe80825e2018-02-16 10:24:53 +02005869 // Some built-in functions have out parameters too.
5870 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5871
5872 // See if we can constant fold a built-in. Note that this may be possible
5873 // even if it is not const-qualified.
5874 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005875 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005876
5877 // This is a built-in function with no op associated with it.
5878 TIntermAggregate *callNode =
5879 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5880 callNode->setLine(loc);
5881 checkTextureOffsetConst(callNode);
5882 checkTextureGather(callNode);
5883 checkImageMemoryAccessForBuiltinFunctions(callNode);
5884 checkAtomicMemoryBuiltinFunctions(callNode);
5885 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5886 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005887 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005888 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005889
5890 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005891 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005892}
5893
Jamie Madillb98c3a82015-07-23 14:26:04 -04005894TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005895 TIntermTyped *trueExpression,
5896 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005897 const TSourceLoc &loc)
5898{
Olli Etuaho56229f12017-07-10 14:16:33 +03005899 if (!checkIsScalarBool(loc, cond))
5900 {
5901 return falseExpression;
5902 }
Olli Etuaho52901742015-04-15 13:42:45 +03005903
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005904 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005905 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005906 std::stringstream reasonStream;
5907 reasonStream << "mismatching ternary operator operand types '"
5908 << trueExpression->getCompleteString() << " and '"
5909 << falseExpression->getCompleteString() << "'";
5910 std::string reason = reasonStream.str();
5911 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005912 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005913 }
Olli Etuahode318b22016-10-25 16:18:25 +01005914 if (IsOpaqueType(trueExpression->getBasicType()))
5915 {
5916 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005917 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005918 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5919 // Note that structs containing opaque types don't need to be checked as structs are
5920 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005921 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005922 return falseExpression;
5923 }
5924
Jiajia Qinbc585152017-06-23 15:42:17 +08005925 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5926 falseExpression->getMemoryQualifier().writeonly)
5927 {
5928 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5929 return falseExpression;
5930 }
5931
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005932 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005933 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005934 // ESSL 3.00.6 section 5.7:
5935 // Ternary operator support is optional for arrays. No certainty that it works across all
5936 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5937 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005938 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005939 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005940 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005941 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005942 }
Olli Etuaho94050052017-05-08 14:17:44 +03005943 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5944 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005945 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005946 return falseExpression;
5947 }
5948
Corentin Wallez0d959252016-07-12 17:26:32 -04005949 // WebGL2 section 5.26, the following results in an error:
5950 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005951 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005952 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005953 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005954 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005955 }
5956
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005957 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5958 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005959 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005960}
Olli Etuaho49300862015-02-20 14:54:49 +02005961
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005962//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005963// Parse an array of strings using yyparse.
5964//
5965// Returns 0 for success.
5966//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005967int PaParseStrings(size_t count,
5968 const char *const string[],
5969 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305970 TParseContext *context)
5971{
Yunchao He4f285442017-04-21 12:15:49 +08005972 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005973 return 1;
5974
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005975 if (glslang_initialize(context))
5976 return 1;
5977
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005978 int error = glslang_scan(count, string, length, context);
5979 if (!error)
5980 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005981
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005982 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005983
alokp@chromium.org6b495712012-06-29 00:06:58 +00005984 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005985}
Jamie Madill45bcc782016-11-07 13:58:48 -05005986
5987} // namespace sh