blob: cf6a1d36a342a0d5505b26f0ca19cce587e9652f [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
jchen104cdac9e2017-05-08 11:01:20 +080012#include "common/mathutil.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
Olli Etuahod5f44c92017-11-29 17:15:40 +020014#include "compiler/translator/Declarator.h"
Olli Etuaho2bfe9f62018-03-02 16:53:29 +020015#include "compiler/translator/ParseContext_autogen.h"
Kai Ninomiya614dd0f2017-11-22 14:04:48 -080016#include "compiler/translator/StaticType.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030017#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080018#include "compiler/translator/ValidateSwitch.h"
19#include "compiler/translator/glslang.h"
Olli Etuahoc26214d2018-03-16 10:43:11 +020020#include "compiler/translator/tree_util/IntermNode_util.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030021#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000022
Jamie Madill45bcc782016-11-07 13:58:48 -050023namespace sh
24{
25
alokp@chromium.org8b851c62012-06-15 16:25:11 +000026///////////////////////////////////////////////////////////////////////
27//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028// Sub- vector and matrix fields
29//
30////////////////////////////////////////////////////////////////////////
31
Martin Radev2cc85b32016-08-05 16:22:53 +030032namespace
33{
34
35const int kWebGLMaxStructNesting = 4;
36
Olli Etuaho0f684632017-07-13 12:42:15 +030037bool ContainsSampler(const TStructure *structType);
38
Martin Radev2cc85b32016-08-05 16:22:53 +030039bool ContainsSampler(const TType &type)
40{
41 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030042 {
Martin Radev2cc85b32016-08-05 16:22:53 +030043 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030044 }
jchen10cc2a10e2017-05-03 14:05:12 +080045 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030046 {
Olli Etuaho0f684632017-07-13 12:42:15 +030047 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030048 }
49
50 return false;
51}
52
Olli Etuaho0f684632017-07-13 12:42:15 +030053bool ContainsSampler(const TStructure *structType)
54{
55 for (const auto &field : structType->fields())
56 {
57 if (ContainsSampler(*field->type()))
58 return true;
59 }
60 return false;
61}
62
Olli Etuaho485eefd2017-02-14 17:40:06 +000063// Get a token from an image argument to use as an error message token.
64const char *GetImageArgumentToken(TIntermTyped *imageNode)
65{
66 ASSERT(IsImage(imageNode->getBasicType()));
67 while (imageNode->getAsBinaryNode() &&
68 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
69 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
70 {
71 imageNode = imageNode->getAsBinaryNode()->getLeft();
72 }
73 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
74 if (imageSymbol)
75 {
Olli Etuahofbb1c792018-01-19 16:26:59 +020076 return imageSymbol->getName().data();
Olli Etuaho485eefd2017-02-14 17:40:06 +000077 }
78 return "image";
79}
80
Olli Etuahocce89652017-06-19 16:04:09 +030081bool CanSetDefaultPrecisionOnType(const TPublicType &type)
82{
83 if (!SupportsPrecision(type.getBasicType()))
84 {
85 return false;
86 }
87 if (type.getBasicType() == EbtUInt)
88 {
89 // ESSL 3.00.4 section 4.5.4
90 return false;
91 }
92 if (type.isAggregate())
93 {
94 // Not allowed to set for aggregate types
95 return false;
96 }
97 return true;
98}
99
Jiawei Shaod8105a02017-08-08 09:54:36 +0800100// Map input primitive types to input array sizes in a geometry shader.
101GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
102{
103 switch (primitiveType)
104 {
105 case EptPoints:
106 return 1u;
107 case EptLines:
108 return 2u;
109 case EptTriangles:
110 return 3u;
111 case EptLinesAdjacency:
112 return 4u;
113 case EptTrianglesAdjacency:
114 return 6u;
115 default:
116 UNREACHABLE();
117 return 0u;
118 }
119}
120
Jiajia Qina3106c52017-11-03 09:39:39 +0800121bool IsBufferOrSharedVariable(TIntermTyped *var)
122{
123 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
124 var->getQualifier() == EvqShared)
125 {
126 return true;
127 }
128 return false;
129}
130
Martin Radev2cc85b32016-08-05 16:22:53 +0300131} // namespace
132
jchen104cdac9e2017-05-08 11:01:20 +0800133// This tracks each binding point's current default offset for inheritance of subsequent
134// variables using the same binding, and keeps offsets unique and non overlapping.
135// See GLSL ES 3.1, section 4.4.6.
136class TParseContext::AtomicCounterBindingState
137{
138 public:
139 AtomicCounterBindingState() : mDefaultOffset(0) {}
140 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
141 // newly inserted span.
142 int insertSpan(int start, size_t length)
143 {
144 gl::RangeI newSpan(start, start + static_cast<int>(length));
145 for (const auto &span : mSpans)
146 {
147 if (newSpan.intersects(span))
148 {
149 return -1;
150 }
151 }
152 mSpans.push_back(newSpan);
153 mDefaultOffset = newSpan.high();
154 return start;
155 }
156 // Inserts a new span starting from the default offset.
157 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
158 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
159
160 private:
161 int mDefaultOffset;
162 std::vector<gl::RangeI> mSpans;
163};
164
Jamie Madillacb4b812016-11-07 13:50:29 -0500165TParseContext::TParseContext(TSymbolTable &symt,
166 TExtensionBehavior &ext,
167 sh::GLenum type,
168 ShShaderSpec spec,
169 ShCompileOptions options,
170 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000171 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500172 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300173 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300174 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500175 mShaderType(type),
176 mShaderSpec(spec),
177 mCompileOptions(options),
178 mShaderVersion(100),
179 mTreeRoot(nullptr),
180 mLoopNestingLevel(0),
181 mStructNestingLevel(0),
182 mSwitchNestingLevel(0),
183 mCurrentFunctionType(nullptr),
184 mFunctionReturnsValue(false),
185 mChecksPrecisionErrors(checksPrecErrors),
186 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800187 mDefaultUniformMatrixPacking(EmpColumnMajor),
188 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
189 mDefaultBufferMatrixPacking(EmpColumnMajor),
190 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000191 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500192 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000193 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500194 mShaderVersion,
195 mShaderType,
196 resources.WEBGL_debug_shader_precision == 1),
Jamie Madillc3bef3e2018-10-03 07:35:09 -0400197 mPreprocessor(mDiagnostics, &mDirectiveHandler, angle::pp::PreprocessorSettings(spec)),
Jamie Madillacb4b812016-11-07 13:50:29 -0500198 mScanner(nullptr),
Jamie Madillacb4b812016-11-07 13:50:29 -0500199 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
200 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300201 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
202 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500203 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500204 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000205 mNumViews(-1),
206 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000207 mMaxImageUnits(resources.MaxImageUnits),
208 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000209 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800210 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800211 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800212 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800213 mDeclaringFunction(false),
214 mGeometryShaderInputPrimitiveType(EptUndefined),
215 mGeometryShaderOutputPrimitiveType(EptUndefined),
216 mGeometryShaderInvocations(0),
217 mGeometryShaderMaxVertices(-1),
218 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Olli Etuaho94bbed12018-03-20 14:44:53 +0200219 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices)
Jamie Madillb980c562018-11-27 11:34:27 -0500220{}
Jamie Madillacb4b812016-11-07 13:50:29 -0500221
Jamie Madillb980c562018-11-27 11:34:27 -0500222TParseContext::~TParseContext() {}
jchen104cdac9e2017-05-08 11:01:20 +0800223
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300224bool TParseContext::parseVectorFields(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200225 const ImmutableString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400226 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300227 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300229 ASSERT(fieldOffsets);
Olli Etuahofbb1c792018-01-19 16:26:59 +0200230 size_t fieldCount = compString.length();
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300231 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530232 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200233 error(line, "illegal vector field selection", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000234 return false;
235 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300236 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000237
Jamie Madillb98c3a82015-07-23 14:26:04 -0400238 enum
239 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000240 exyzw,
241 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000242 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000243 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300245 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530246 {
247 switch (compString[i])
248 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400249 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300250 (*fieldOffsets)[i] = 0;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400251 fieldSet[i] = exyzw;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400252 break;
253 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300254 (*fieldOffsets)[i] = 0;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400255 fieldSet[i] = ergba;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400256 break;
257 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300258 (*fieldOffsets)[i] = 0;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400259 fieldSet[i] = estpq;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400260 break;
261 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300262 (*fieldOffsets)[i] = 1;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400263 fieldSet[i] = exyzw;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400264 break;
265 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300266 (*fieldOffsets)[i] = 1;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400267 fieldSet[i] = ergba;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268 break;
269 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300270 (*fieldOffsets)[i] = 1;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400271 fieldSet[i] = estpq;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400272 break;
273 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300274 (*fieldOffsets)[i] = 2;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400275 fieldSet[i] = exyzw;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400276 break;
277 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300278 (*fieldOffsets)[i] = 2;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400279 fieldSet[i] = ergba;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400280 break;
281 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300282 (*fieldOffsets)[i] = 2;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400283 fieldSet[i] = estpq;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400284 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530285
Jamie Madillb98c3a82015-07-23 14:26:04 -0400286 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300287 (*fieldOffsets)[i] = 3;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400288 fieldSet[i] = exyzw;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400289 break;
290 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300291 (*fieldOffsets)[i] = 3;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400292 fieldSet[i] = ergba;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400293 break;
294 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300295 (*fieldOffsets)[i] = 3;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400296 fieldSet[i] = estpq;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400297 break;
298 default:
Olli Etuahofbb1c792018-01-19 16:26:59 +0200299 error(line, "illegal vector field selection", compString);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400300 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000301 }
302 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300304 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530305 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300306 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200308 error(line, "vector field selection out of range", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000309 return false;
310 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
Arun Patole7e7e68d2015-05-22 12:02:25 +0530312 if (i > 0)
313 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400314 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530315 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200316 error(line, "illegal - vector component fields not from the same set", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000317 return false;
318 }
319 }
320 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000322 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323}
324
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325///////////////////////////////////////////////////////////////////////
326//
327// Errors
328//
329////////////////////////////////////////////////////////////////////////
330
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331//
332// Used by flex/bison to output all syntax and parsing errors.
333//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000334void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000336 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337}
338
Olli Etuahofbb1c792018-01-19 16:26:59 +0200339void TParseContext::error(const TSourceLoc &loc, const char *reason, const ImmutableString &token)
340{
341 mDiagnostics->error(loc, reason, token.data());
342}
343
Olli Etuaho4de340a2016-12-16 09:32:03 +0000344void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530345{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000346 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000347}
348
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200349void TParseContext::outOfRangeError(bool isError,
350 const TSourceLoc &loc,
351 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000352 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200353{
354 if (isError)
355 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000356 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200357 }
358 else
359 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000360 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200361 }
362}
363
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364//
365// Same error message for all places assignments don't work.
366//
Olli Etuaho72e35892018-06-20 11:43:08 +0300367void TParseContext::assignError(const TSourceLoc &line,
368 const char *op,
369 const TType &left,
370 const TType &right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371{
Olli Etuaho72e35892018-06-20 11:43:08 +0300372 TInfoSinkBase reasonStream;
Olli Etuaho4de340a2016-12-16 09:32:03 +0000373 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
Olli Etuaho72e35892018-06-20 11:43:08 +0300374 error(line, reasonStream.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000375}
376
377//
378// Same error message for all places unary operations don't work.
379//
Olli Etuaho72e35892018-06-20 11:43:08 +0300380void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, const TType &operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381{
Olli Etuaho72e35892018-06-20 11:43:08 +0300382 TInfoSinkBase reasonStream;
Olli Etuaho4de340a2016-12-16 09:32:03 +0000383 reasonStream << "wrong operand type - no operation '" << op
384 << "' exists that takes an operand of type " << operand
385 << " (or there is no acceptable conversion)";
Olli Etuaho72e35892018-06-20 11:43:08 +0300386 error(line, reasonStream.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387}
388
389//
390// Same error message for all binary operations don't work.
391//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400392void TParseContext::binaryOpError(const TSourceLoc &line,
393 const char *op,
Olli Etuaho72e35892018-06-20 11:43:08 +0300394 const TType &left,
395 const TType &right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396{
Olli Etuaho72e35892018-06-20 11:43:08 +0300397 TInfoSinkBase reasonStream;
Olli Etuaho4de340a2016-12-16 09:32:03 +0000398 reasonStream << "wrong operand types - no operation '" << op
399 << "' exists that takes a left-hand operand of type '" << left
400 << "' and a right operand of type '" << right
401 << "' (or there is no acceptable conversion)";
Olli Etuaho72e35892018-06-20 11:43:08 +0300402 error(line, reasonStream.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403}
404
Olli Etuaho856c4972016-08-08 11:38:39 +0300405void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
406 TPrecision precision,
407 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530408{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400409 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300410 return;
Martin Radev70866b82016-07-22 15:27:42 +0300411
412 if (precision != EbpUndefined && !SupportsPrecision(type))
413 {
414 error(line, "illegal type for precision qualifier", getBasicString(type));
415 }
416
Olli Etuaho183d7e22015-11-20 15:59:09 +0200417 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530418 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200419 switch (type)
420 {
421 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400422 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300423 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200424 case EbtInt:
425 case EbtUInt:
426 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400427 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300428 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200429 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800430 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200431 {
jchen10cc2a10e2017-05-03 14:05:12 +0800432 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300433 return;
434 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200435 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000436 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000437}
438
Olli Etuaho94bbed12018-03-20 14:44:53 +0200439void TParseContext::markStaticReadIfSymbol(TIntermNode *node)
440{
441 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
442 if (swizzleNode)
443 {
444 markStaticReadIfSymbol(swizzleNode->getOperand());
445 return;
446 }
447 TIntermBinary *binaryNode = node->getAsBinaryNode();
448 if (binaryNode)
449 {
450 switch (binaryNode->getOp())
451 {
452 case EOpIndexDirect:
453 case EOpIndexIndirect:
454 case EOpIndexDirectStruct:
455 case EOpIndexDirectInterfaceBlock:
456 markStaticReadIfSymbol(binaryNode->getLeft());
457 return;
458 default:
459 return;
460 }
461 }
462 TIntermSymbol *symbolNode = node->getAsSymbolNode();
463 if (symbolNode)
464 {
465 symbolTable.markStaticRead(symbolNode->variable());
466 }
467}
468
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469// Both test and if necessary, spit out an error, to see if the node is really
470// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300471bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000472{
Olli Etuahob6fa0432016-09-28 16:28:05 +0100473 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100474 if (swizzleNode)
475 {
476 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
477 if (ok && swizzleNode->hasDuplicateOffsets())
478 {
479 error(line, " l-value of swizzle cannot have duplicate components", op);
480 return false;
481 }
482 return ok;
483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
Olli Etuahodaf120b2018-03-20 14:21:10 +0200485 TIntermBinary *binaryNode = node->getAsBinaryNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530486 if (binaryNode)
487 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400488 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530489 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400490 case EOpIndexDirect:
491 case EOpIndexIndirect:
492 case EOpIndexDirectStruct:
493 case EOpIndexDirectInterfaceBlock:
Qin Jiajia76bf01d2018-02-22 14:11:34 +0800494 if (node->getMemoryQualifier().readonly)
495 {
496 error(line, "can't modify a readonly variable", op);
497 return false;
498 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300499 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400500 default:
501 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000502 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000503 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300504 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000505 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506
jchen10cc2a10e2017-05-03 14:05:12 +0800507 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530508 switch (node->getQualifier())
509 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400510 case EvqConst:
511 message = "can't modify a const";
512 break;
513 case EvqConstReadOnly:
514 message = "can't modify a const";
515 break;
516 case EvqAttribute:
517 message = "can't modify an attribute";
518 break;
519 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400520 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800521 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800522 case EvqFlatIn:
523 case EvqSmoothIn:
524 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400525 message = "can't modify an input";
526 break;
527 case EvqUniform:
528 message = "can't modify a uniform";
529 break;
530 case EvqVaryingIn:
531 message = "can't modify a varying";
532 break;
533 case EvqFragCoord:
534 message = "can't modify gl_FragCoord";
535 break;
536 case EvqFrontFacing:
537 message = "can't modify gl_FrontFacing";
538 break;
539 case EvqPointCoord:
540 message = "can't modify gl_PointCoord";
541 break;
Martin Radevb0883602016-08-04 17:48:58 +0300542 case EvqNumWorkGroups:
543 message = "can't modify gl_NumWorkGroups";
544 break;
545 case EvqWorkGroupSize:
546 message = "can't modify gl_WorkGroupSize";
547 break;
548 case EvqWorkGroupID:
549 message = "can't modify gl_WorkGroupID";
550 break;
551 case EvqLocalInvocationID:
552 message = "can't modify gl_LocalInvocationID";
553 break;
554 case EvqGlobalInvocationID:
555 message = "can't modify gl_GlobalInvocationID";
556 break;
557 case EvqLocalInvocationIndex:
558 message = "can't modify gl_LocalInvocationIndex";
559 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300560 case EvqViewIDOVR:
561 message = "can't modify gl_ViewID_OVR";
562 break;
Martin Radev802abe02016-08-04 17:48:32 +0300563 case EvqComputeIn:
564 message = "can't modify work group size variable";
565 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800566 case EvqPerVertexIn:
567 message = "can't modify any member in gl_in";
568 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800569 case EvqPrimitiveIDIn:
570 message = "can't modify gl_PrimitiveIDIn";
571 break;
572 case EvqInvocationID:
573 message = "can't modify gl_InvocationID";
574 break;
575 case EvqPrimitiveID:
576 if (mShaderType == GL_FRAGMENT_SHADER)
577 {
578 message = "can't modify gl_PrimitiveID in a fragment shader";
579 }
580 break;
581 case EvqLayer:
582 if (mShaderType == GL_FRAGMENT_SHADER)
583 {
584 message = "can't modify gl_Layer in a fragment shader";
585 }
586 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400587 default:
588 //
589 // Type that can't be written to?
590 //
591 if (node->getBasicType() == EbtVoid)
592 {
593 message = "can't modify void";
594 }
jchen10cc2a10e2017-05-03 14:05:12 +0800595 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400596 {
jchen10cc2a10e2017-05-03 14:05:12 +0800597 message = "can't modify a variable with type ";
598 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300599 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800600 else if (node->getMemoryQualifier().readonly)
601 {
602 message = "can't modify a readonly variable";
603 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605
Olli Etuahodaf120b2018-03-20 14:21:10 +0200606 ASSERT(binaryNode == nullptr && swizzleNode == nullptr);
607 TIntermSymbol *symNode = node->getAsSymbolNode();
608 if (message.empty() && symNode != nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530609 {
Olli Etuaho94bbed12018-03-20 14:44:53 +0200610 symbolTable.markStaticWrite(symNode->variable());
Olli Etuaho8a176262016-08-16 14:23:01 +0300611 return true;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000612 }
Olli Etuahodaf120b2018-03-20 14:21:10 +0200613
Jonah Ryan-Davisf563fdc2019-01-31 13:53:59 -0500614 std::stringstream reasonStream = sh::InitializeStream<std::stringstream>();
Olli Etuahodaf120b2018-03-20 14:21:10 +0200615 reasonStream << "l-value required";
616 if (!message.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530617 {
Olli Etuahodaf120b2018-03-20 14:21:10 +0200618 if (symNode)
619 {
620 // Symbol inside an expression can't be nameless.
621 ASSERT(symNode->variable().symbolType() != SymbolType::Empty);
622 const ImmutableString &symbol = symNode->getName();
623 reasonStream << " (" << message << " \"" << symbol << "\")";
624 }
625 else
626 {
627 reasonStream << " (" << message << ")";
628 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000629 }
Olli Etuahodaf120b2018-03-20 14:21:10 +0200630 std::string reason = reasonStream.str();
631 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632
Olli Etuaho8a176262016-08-16 14:23:01 +0300633 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634}
635
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636// Both test, and if necessary spit out an error, to see if the node is really
637// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300638void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639{
Olli Etuaho383b7912016-08-05 11:22:59 +0300640 if (node->getQualifier() != EvqConst)
641 {
642 error(node->getLine(), "constant expression required", "");
643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644}
645
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646// Both test, and if necessary spit out an error, to see if the node is really
647// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300648void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649{
Olli Etuaho383b7912016-08-05 11:22:59 +0300650 if (!node->isScalarInt())
651 {
652 error(node->getLine(), "integer expression required", token);
653 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654}
655
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000656// Both test, and if necessary spit out an error, to see if we are currently
657// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800658bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659{
Olli Etuaho856c4972016-08-08 11:38:39 +0300660 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300661 {
662 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800663 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300664 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800665 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666}
667
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300668// ESSL 3.00.5 sections 3.8 and 3.9.
669// If it starts "gl_" or contains two consecutive underscores, it's reserved.
670// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuahofbb1c792018-01-19 16:26:59 +0200671bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const ImmutableString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000672{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530673 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahofbb1c792018-01-19 16:26:59 +0200674 if (identifier.beginsWith("gl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530675 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300676 error(line, reservedErrMsg, "gl_");
677 return false;
678 }
679 if (sh::IsWebGLBasedSpec(mShaderSpec))
680 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200681 if (identifier.beginsWith("webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530682 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300683 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300684 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000685 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200686 if (identifier.beginsWith("_webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530687 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300688 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300689 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000690 }
691 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200692 if (identifier.contains("__"))
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300693 {
694 error(line,
695 "identifiers containing two consecutive underscores (__) are reserved as "
696 "possible future keywords",
Olli Etuahofbb1c792018-01-19 16:26:59 +0200697 identifier);
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300698 return false;
699 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300700 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701}
702
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300703// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300704bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuaho95ed1942018-02-01 14:01:19 +0200705 const TIntermSequence &arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300706 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000707{
Olli Etuaho95ed1942018-02-01 14:01:19 +0200708 if (arguments.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530709 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200710 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300711 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000712 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200713
Olli Etuaho95ed1942018-02-01 14:01:19 +0200714 for (TIntermNode *arg : arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530715 {
Olli Etuaho94bbed12018-03-20 14:44:53 +0200716 markStaticReadIfSymbol(arg);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300717 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200718 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300719 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200720 {
jchen10cc2a10e2017-05-03 14:05:12 +0800721 std::string reason("cannot convert a variable with type ");
722 reason += getBasicString(argTyped->getBasicType());
723 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300724 return false;
725 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800726 else if (argTyped->getMemoryQualifier().writeonly)
727 {
728 error(line, "cannot convert a variable with writeonly", "constructor");
729 return false;
730 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200731 if (argTyped->getBasicType() == EbtVoid)
732 {
733 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300734 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200735 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000736 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000737
Olli Etuaho856c4972016-08-08 11:38:39 +0300738 if (type.isArray())
739 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300740 // The size of an unsized constructor should already have been determined.
741 ASSERT(!type.isUnsizedArray());
Olli Etuaho95ed1942018-02-01 14:01:19 +0200742 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300743 {
744 error(line, "array constructor needs one argument per array element", "constructor");
745 return false;
746 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300747 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
748 // the array.
Olli Etuaho95ed1942018-02-01 14:01:19 +0200749 for (TIntermNode *const &argNode : arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300750 {
751 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300752 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500753 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300754 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500755 return false;
756 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300757 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300758 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000759 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300760 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300761 }
762 }
763 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300764 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300765 {
766 const TFieldList &fields = type.getStruct()->fields();
Olli Etuaho95ed1942018-02-01 14:01:19 +0200767 if (fields.size() != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300768 {
769 error(line,
770 "Number of constructor parameters does not match the number of structure fields",
771 "constructor");
772 return false;
773 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300774
775 for (size_t i = 0; i < fields.size(); i++)
776 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200777 if (i >= arguments.size() ||
778 arguments[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300779 {
780 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000781 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300782 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300783 }
784 }
785 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300786 else
787 {
788 // We're constructing a scalar, vector, or matrix.
789
790 // Note: It's okay to have too many components available, but not okay to have unused
791 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
792 // there is an extra argument, so 'overFull' will become true.
793
794 size_t size = 0;
795 bool full = false;
796 bool overFull = false;
797 bool matrixArg = false;
Olli Etuaho95ed1942018-02-01 14:01:19 +0200798 for (TIntermNode *arg : arguments)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300799 {
800 const TIntermTyped *argTyped = arg->getAsTyped();
801 ASSERT(argTyped != nullptr);
802
Olli Etuaho487b63a2017-05-23 15:55:09 +0300803 if (argTyped->getBasicType() == EbtStruct)
804 {
805 error(line, "a struct cannot be used as a constructor argument for this type",
806 "constructor");
807 return false;
808 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300809 if (argTyped->getType().isArray())
810 {
811 error(line, "constructing from a non-dereferenced array", "constructor");
812 return false;
813 }
814 if (argTyped->getType().isMatrix())
815 {
816 matrixArg = true;
817 }
818
819 size += argTyped->getType().getObjectSize();
820 if (full)
821 {
822 overFull = true;
823 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300824 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300825 {
826 full = true;
827 }
828 }
829
830 if (type.isMatrix() && matrixArg)
831 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200832 if (arguments.size() != 1)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300833 {
834 error(line, "constructing matrix from matrix can only take one argument",
835 "constructor");
836 return false;
837 }
838 }
839 else
840 {
841 if (size != 1 && size < type.getObjectSize())
842 {
843 error(line, "not enough data provided for construction", "constructor");
844 return false;
845 }
846 if (overFull)
847 {
848 error(line, "too many arguments", "constructor");
849 return false;
850 }
851 }
852 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300853
Olli Etuaho8a176262016-08-16 14:23:01 +0300854 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855}
856
Jamie Madillb98c3a82015-07-23 14:26:04 -0400857// This function checks to see if a void variable has been declared and raise an error message for
858// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859//
860// returns true in case of an error
861//
Olli Etuaho856c4972016-08-08 11:38:39 +0300862bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200863 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400864 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300866 if (type == EbtVoid)
867 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200868 error(line, "illegal use of type 'void'", identifier);
Olli Etuaho8a176262016-08-16 14:23:01 +0300869 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300870 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871
Olli Etuaho8a176262016-08-16 14:23:01 +0300872 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873}
874
Jamie Madillb98c3a82015-07-23 14:26:04 -0400875// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300876// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300877bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300879 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530880 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000881 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300882 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530883 }
Olli Etuaho56229f12017-07-10 14:16:33 +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 Etuaho856c4972016-08-08 11:38:39 +0300889void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890{
Martin Radev4a9cd802016-09-01 16:51:51 +0300891 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530892 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000893 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530894 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895}
896
jchen10cc2a10e2017-05-03 14:05:12 +0800897bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
898 const TTypeSpecifierNonArray &pType,
899 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530901 if (pType.type == EbtStruct)
902 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300903 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530904 {
Jonah Ryan-Davisf563fdc2019-01-31 13:53:59 -0500905 std::stringstream reasonStream = sh::InitializeStream<std::stringstream>();
Olli Etuaho4de340a2016-12-16 09:32:03 +0000906 reasonStream << reason << " (structure contains a sampler)";
907 std::string reasonStr = reasonStream.str();
908 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300909 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000910 }
jchen10cc2a10e2017-05-03 14:05:12 +0800911 // only samplers need to be checked from structs, since other opaque types can't be struct
912 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300913 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530914 }
jchen10cc2a10e2017-05-03 14:05:12 +0800915 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530916 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000917 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300918 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000919 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
Olli Etuaho8a176262016-08-16 14:23:01 +0300921 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922}
923
Olli Etuaho856c4972016-08-08 11:38:39 +0300924void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
925 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400926{
927 if (pType.layoutQualifier.location != -1)
928 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400929 error(line, "location must only be specified for a single input or output variable",
930 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400931 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400932}
933
Olli Etuaho856c4972016-08-08 11:38:39 +0300934void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
935 const TLayoutQualifier &layoutQualifier)
936{
937 if (layoutQualifier.location != -1)
938 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000939 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
940 if (mShaderVersion >= 310)
941 {
942 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800943 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000944 }
945 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300946 }
947}
948
Qin Jiajiaca68d982017-09-18 16:41:56 +0800949void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
950 const TLayoutBlockStorage &blockStorage,
951 const TQualifier &qualifier)
952{
953 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
954 {
955 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
956 }
957}
958
Martin Radev2cc85b32016-08-05 16:22:53 +0300959void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
960 TQualifier qualifier,
961 const TType &type)
962{
Martin Radev2cc85b32016-08-05 16:22:53 +0300963 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800964 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530965 {
jchen10cc2a10e2017-05-03 14:05:12 +0800966 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968}
969
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300971unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530973 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000974
Olli Etuaho1dfd8ae2018-10-01 15:59:59 +0300975 // ANGLE should be able to fold any EvqConst expressions resulting in an integer - but to be
976 // safe against corner cases we still check for constant folding. Some interpretations of the
977 // spec have allowed constant expressions with side effects - like array length() method on a
978 // non-constant array.
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200979 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000980 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000981 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300982 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000983 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984
Olli Etuaho856c4972016-08-08 11:38:39 +0300985 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400986
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000987 if (constant->getBasicType() == EbtUInt)
988 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300989 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000990 }
991 else
992 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300993 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000994
Olli Etuaho856c4972016-08-08 11:38:39 +0300995 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000996 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400997 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300998 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000999 }
Nicolas Capens906744a2014-06-06 15:18:07 -04001000
Olli Etuaho856c4972016-08-08 11:38:39 +03001001 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -04001002 }
1003
Olli Etuaho856c4972016-08-08 11:38:39 +03001004 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -04001005 {
1006 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001007 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -04001008 }
1009
1010 // The size of arrays is restricted here to prevent issues further down the
1011 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1012 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1013 const unsigned int sizeLimit = 65536;
1014
Olli Etuaho856c4972016-08-08 11:38:39 +03001015 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001016 {
1017 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001018 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001019 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001020
1021 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022}
1023
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001025bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1026 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027{
Olli Etuaho8a176262016-08-16 14:23:01 +03001028 if ((elementQualifier.qualifier == EvqAttribute) ||
1029 (elementQualifier.qualifier == EvqVertexIn) ||
1030 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001031 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001032 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001033 TType(elementQualifier).getQualifierString());
1034 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001035 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001036
Olli Etuaho8a176262016-08-16 14:23:01 +03001037 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038}
1039
Olli Etuaho8a176262016-08-16 14:23:01 +03001040// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001041bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1042 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001043{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001044 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001045 {
Olli Etuaho72e35892018-06-20 11:43:08 +03001046 TInfoSinkBase typeString;
1047 typeString << TType(elementType);
1048 error(line, "cannot declare arrays of arrays", typeString.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001049 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001050 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001051 return true;
1052}
1053
1054// Check if this qualified element type can be formed into an array. This is only called when array
1055// brackets are associated with an identifier in a declaration, like this:
1056// float a[2];
1057// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1058// are associated with the type, like this:
1059// float[2] a;
1060bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1061 const TPublicType &elementType)
1062{
1063 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1064 {
1065 return false;
1066 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001067 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1068 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1069 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001070 // Geometry shader requires each user-defined input be declared as arrays or inside input
1071 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1072 // interface matching, such variables and blocks are treated as though they were not declared
1073 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001074 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001075 sh::IsVarying(elementType.qualifier) &&
1076 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001077 {
Olli Etuaho72e35892018-06-20 11:43:08 +03001078 TInfoSinkBase typeString;
1079 typeString << TType(elementType);
Olli Etuahoe0803872017-08-23 15:30:23 +03001080 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho72e35892018-06-20 11:43:08 +03001081 typeString.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001082 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001083 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001084 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085}
1086
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001088void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001089 const ImmutableString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001090 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091{
Olli Etuaho3739d232015-04-08 12:23:44 +03001092 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001093 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001094 {
1095 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001096 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001097
1098 // Generate informative error messages for ESSL1.
1099 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001100 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001101 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301102 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001103 "structures containing arrays may not be declared constant since they cannot be "
1104 "initialized",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001105 identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001106 }
1107 else
1108 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001109 error(line, "variables with qualifier 'const' must be initialized", identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001110 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001111 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001112 // This will make the type sized if it isn't sized yet.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001113 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized", identifier,
1114 type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001115}
1116
Olli Etuaho2935c582015-04-08 14:32:06 +03001117// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118// and update the symbol table.
1119//
Olli Etuaho2935c582015-04-08 14:32:06 +03001120// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001122bool TParseContext::declareVariable(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001123 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001124 const TType *type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001125 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001126{
Olli Etuaho2935c582015-04-08 14:32:06 +03001127 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128
Olli Etuahofbb1c792018-01-19 16:26:59 +02001129 (*variable) = new TVariable(&symbolTable, identifier, type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02001130
Olli Etuahoa78092c2018-09-26 14:16:13 +03001131 ASSERT(type->getLayoutQualifier().index == -1 ||
1132 (isExtensionEnabled(TExtension::EXT_blend_func_extended) &&
1133 mShaderType == GL_FRAGMENT_SHADER && mShaderVersion >= 300));
1134 if (type->getQualifier() == EvqFragmentOut)
1135 {
1136 if (type->getLayoutQualifier().index != -1 && type->getLayoutQualifier().location == -1)
1137 {
1138 error(line,
1139 "If index layout qualifier is specified for a fragment output, location must "
1140 "also be specified.",
1141 "index");
1142 return false;
1143 }
1144 }
1145 else
1146 {
1147 checkIndexIsNotSpecified(line, type->getLayoutQualifier().index);
1148 }
1149
Olli Etuahob60d30f2018-01-16 12:31:06 +02001150 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001151
Olli Etuaho856c4972016-08-08 11:38:39 +03001152 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001153
Olli Etuaho2935c582015-04-08 14:32:06 +03001154 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahofbb1c792018-01-19 16:26:59 +02001155 if (type->isArray() && identifier.beginsWith("gl_LastFragData"))
Olli Etuaho2935c582015-04-08 14:32:06 +03001156 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001157 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02001158 symbolTable.findBuiltIn(ImmutableString("gl_MaxDrawBuffers"), mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001159 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001160 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001161 error(line, "redeclaration of gl_LastFragData as an array of arrays", identifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001162 return false;
1163 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001164 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001165 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001166 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001167 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001168 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001169 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001170 }
1171 }
1172 else
1173 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001174 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001175 identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001176 return false;
1177 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001178 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001179
Olli Etuaho8a176262016-08-16 14:23:01 +03001180 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001181 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001182
Olli Etuaho437664b2018-02-28 15:38:14 +02001183 if (!symbolTable.declare(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001184 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001185 error(line, "redefinition", identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001186 return false;
1187 }
1188
Olli Etuahob60d30f2018-01-16 12:31:06 +02001189 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001190 return false;
1191
1192 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001193}
1194
Martin Radev70866b82016-07-22 15:27:42 +03001195void TParseContext::checkIsParameterQualifierValid(
1196 const TSourceLoc &line,
1197 const TTypeQualifierBuilder &typeQualifierBuilder,
1198 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301199{
Olli Etuahocce89652017-06-19 16:04:09 +03001200 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001201 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001202
1203 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301204 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001205 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1206 }
1207
1208 if (!IsImage(type->getBasicType()))
1209 {
Olli Etuaho43364892017-02-13 16:00:12 +00001210 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001211 }
1212 else
1213 {
1214 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001215 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001216
Martin Radev70866b82016-07-22 15:27:42 +03001217 type->setQualifier(typeQualifier.qualifier);
1218
1219 if (typeQualifier.precision != EbpUndefined)
1220 {
1221 type->setPrecision(typeQualifier.precision);
1222 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001223}
1224
Olli Etuaho703671e2017-11-08 17:47:18 +02001225template <size_t size>
1226bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1227 const std::array<TExtension, size> &extensions)
1228{
1229 ASSERT(!extensions.empty());
1230 const TExtensionBehavior &extBehavior = extensionBehavior();
1231
1232 bool canUseWithWarning = false;
1233 bool canUseWithoutWarning = false;
1234
1235 const char *errorMsgString = "";
1236 TExtension errorMsgExtension = TExtension::UNDEFINED;
1237
1238 for (TExtension extension : extensions)
1239 {
1240 auto extIter = extBehavior.find(extension);
1241 if (canUseWithWarning)
1242 {
1243 // We already have an extension that we can use, but with a warning.
1244 // See if we can use the alternative extension without a warning.
1245 if (extIter == extBehavior.end())
1246 {
1247 continue;
1248 }
1249 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1250 {
1251 canUseWithoutWarning = true;
1252 break;
1253 }
1254 continue;
1255 }
1256 if (extIter == extBehavior.end())
1257 {
1258 errorMsgString = "extension is not supported";
1259 errorMsgExtension = extension;
1260 }
1261 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1262 {
1263 errorMsgString = "extension is disabled";
1264 errorMsgExtension = extension;
1265 }
1266 else if (extIter->second == EBhWarn)
1267 {
1268 errorMsgExtension = extension;
1269 canUseWithWarning = true;
1270 }
1271 else
1272 {
1273 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1274 canUseWithoutWarning = true;
1275 break;
1276 }
1277 }
1278
1279 if (canUseWithoutWarning)
1280 {
1281 return true;
1282 }
1283 if (canUseWithWarning)
1284 {
1285 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1286 return true;
1287 }
1288 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1289 return false;
1290}
1291
1292template bool TParseContext::checkCanUseOneOfExtensions(
1293 const TSourceLoc &line,
1294 const std::array<TExtension, 1> &extensions);
1295template bool TParseContext::checkCanUseOneOfExtensions(
1296 const TSourceLoc &line,
1297 const std::array<TExtension, 2> &extensions);
1298template bool TParseContext::checkCanUseOneOfExtensions(
1299 const TSourceLoc &line,
1300 const std::array<TExtension, 3> &extensions);
1301
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001302bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001303{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001304 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001305 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001306}
1307
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001308// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1309// compile-time or link-time errors are the same whether or not the declaration is empty".
1310// This function implements all the checks that are done on qualifiers regardless of if the
1311// declaration is empty.
1312void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1313 const sh::TLayoutQualifier &layoutQualifier,
1314 const TSourceLoc &location)
1315{
1316 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1317 {
1318 error(location, "Shared memory declarations cannot have layout specified", "layout");
1319 }
1320
1321 if (layoutQualifier.matrixPacking != EmpUnspecified)
1322 {
1323 error(location, "layout qualifier only valid for interface blocks",
1324 getMatrixPackingString(layoutQualifier.matrixPacking));
1325 return;
1326 }
1327
1328 if (layoutQualifier.blockStorage != EbsUnspecified)
1329 {
1330 error(location, "layout qualifier only valid for interface blocks",
1331 getBlockStorageString(layoutQualifier.blockStorage));
1332 return;
1333 }
1334
1335 if (qualifier == EvqFragmentOut)
1336 {
1337 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1338 {
1339 error(location, "invalid layout qualifier combination", "yuv");
1340 return;
1341 }
1342 }
1343 else
1344 {
1345 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1346 }
1347
Olli Etuaho95468d12017-05-04 11:14:34 +03001348 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1349 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001350 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1351 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001352 {
1353 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1354 }
1355
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001356 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001357 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001358 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001359 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001360 // We're not checking whether the uniform location is in range here since that depends on
1361 // the type of the variable.
1362 // The type can only be fully determined for non-empty declarations.
1363 }
1364 if (!canHaveLocation)
1365 {
1366 checkLocationIsNotSpecified(location, layoutQualifier);
1367 }
1368}
1369
jchen104cdac9e2017-05-08 11:01:20 +08001370void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1371 const TSourceLoc &location)
1372{
1373 if (publicType.precision != EbpHigh)
1374 {
1375 error(location, "Can only be highp", "atomic counter");
1376 }
1377 // dEQP enforces compile error if location is specified. See uniform_location.test.
1378 if (publicType.layoutQualifier.location != -1)
1379 {
1380 error(location, "location must not be set for atomic_uint", "layout");
1381 }
1382 if (publicType.layoutQualifier.binding == -1)
1383 {
1384 error(location, "no binding specified", "atomic counter");
1385 }
1386}
1387
Olli Etuaho55bde912017-10-25 13:41:13 +03001388void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001389{
Olli Etuaho55bde912017-10-25 13:41:13 +03001390 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001391 {
1392 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1393 // error. It is assumed that this applies to empty declarations as well.
1394 error(location, "empty array declaration needs to specify a size", "");
1395 }
Olli Etuahoa78092c2018-09-26 14:16:13 +03001396
1397 if (type.getQualifier() != EvqFragmentOut)
1398 {
1399 checkIndexIsNotSpecified(location, type.getLayoutQualifier().index);
1400 }
Martin Radevb8b01222016-11-20 23:25:53 +02001401}
1402
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001403// These checks are done for all declarations that are non-empty. They're done for non-empty
1404// declarations starting a declarator list, and declarators that follow an empty declaration.
1405void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1406 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001407{
Olli Etuahofa33d582015-04-09 14:33:12 +03001408 switch (publicType.qualifier)
1409 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001410 case EvqVaryingIn:
1411 case EvqVaryingOut:
1412 case EvqAttribute:
1413 case EvqVertexIn:
1414 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001415 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001416 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001417 {
1418 error(identifierLocation, "cannot be used with a structure",
1419 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001420 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001421 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001422 break;
1423 case EvqBuffer:
1424 if (publicType.getBasicType() != EbtInterfaceBlock)
1425 {
1426 error(identifierLocation,
1427 "cannot declare buffer variables at global scope(outside a block)",
1428 getQualifierString(publicType.qualifier));
1429 return;
1430 }
1431 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001432 default:
1433 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001434 }
jchen10cc2a10e2017-05-03 14:05:12 +08001435 std::string reason(getBasicString(publicType.getBasicType()));
1436 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001437 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001438 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001439 {
1440 return;
1441 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001442
Andrei Volykhina5527072017-03-22 16:46:30 +03001443 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1444 publicType.qualifier != EvqConst) &&
1445 publicType.getBasicType() == EbtYuvCscStandardEXT)
1446 {
1447 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1448 getQualifierString(publicType.qualifier));
1449 return;
1450 }
1451
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001452 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1453 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001454 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1455 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001456 TType type(publicType);
1457 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001458 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001459 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1460 publicType.layoutQualifier);
1461 }
1462 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001463
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001464 // check for layout qualifier issues
1465 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001466
Martin Radev2cc85b32016-08-05 16:22:53 +03001467 if (IsImage(publicType.getBasicType()))
1468 {
1469
1470 switch (layoutQualifier.imageInternalFormat)
1471 {
1472 case EiifRGBA32F:
1473 case EiifRGBA16F:
1474 case EiifR32F:
1475 case EiifRGBA8:
1476 case EiifRGBA8_SNORM:
1477 if (!IsFloatImage(publicType.getBasicType()))
1478 {
1479 error(identifierLocation,
1480 "internal image format requires a floating image type",
1481 getBasicString(publicType.getBasicType()));
1482 return;
1483 }
1484 break;
1485 case EiifRGBA32I:
1486 case EiifRGBA16I:
1487 case EiifRGBA8I:
1488 case EiifR32I:
1489 if (!IsIntegerImage(publicType.getBasicType()))
1490 {
1491 error(identifierLocation,
1492 "internal image format requires an integer image type",
1493 getBasicString(publicType.getBasicType()));
1494 return;
1495 }
1496 break;
1497 case EiifRGBA32UI:
1498 case EiifRGBA16UI:
1499 case EiifRGBA8UI:
1500 case EiifR32UI:
1501 if (!IsUnsignedImage(publicType.getBasicType()))
1502 {
1503 error(identifierLocation,
1504 "internal image format requires an unsigned image type",
1505 getBasicString(publicType.getBasicType()));
1506 return;
1507 }
1508 break;
1509 case EiifUnspecified:
1510 error(identifierLocation, "layout qualifier", "No image internal format specified");
1511 return;
1512 default:
1513 error(identifierLocation, "layout qualifier", "unrecognized token");
1514 return;
1515 }
1516
1517 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1518 switch (layoutQualifier.imageInternalFormat)
1519 {
1520 case EiifR32F:
1521 case EiifR32I:
1522 case EiifR32UI:
1523 break;
1524 default:
1525 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1526 {
1527 error(identifierLocation, "layout qualifier",
1528 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1529 "image variables must be qualified readonly and/or writeonly");
1530 return;
1531 }
1532 break;
1533 }
1534 }
1535 else
1536 {
Olli Etuaho43364892017-02-13 16:00:12 +00001537 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001538 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1539 }
jchen104cdac9e2017-05-08 11:01:20 +08001540
1541 if (IsAtomicCounter(publicType.getBasicType()))
1542 {
1543 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1544 }
1545 else
1546 {
1547 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1548 }
Olli Etuaho43364892017-02-13 16:00:12 +00001549}
Martin Radev2cc85b32016-08-05 16:22:53 +03001550
Olli Etuaho43364892017-02-13 16:00:12 +00001551void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1552{
1553 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001554 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1555 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1556 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1557 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1558 // when it comes to which shaders are accepted by the compiler.
1559 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001560 if (IsImage(type.getBasicType()))
1561 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001562 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1563 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001564 }
1565 else if (IsSampler(type.getBasicType()))
1566 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001567 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1568 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001569 }
jchen104cdac9e2017-05-08 11:01:20 +08001570 else if (IsAtomicCounter(type.getBasicType()))
1571 {
1572 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1573 }
Olli Etuaho43364892017-02-13 16:00:12 +00001574 else
1575 {
1576 ASSERT(!IsOpaqueType(type.getBasicType()));
1577 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001578 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001579}
1580
Olli Etuaho856c4972016-08-08 11:38:39 +03001581void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001582 const ImmutableString &layoutQualifierName,
Olli Etuaho856c4972016-08-08 11:38:39 +03001583 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001584{
1585
1586 if (mShaderVersion < versionRequired)
1587 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001588 error(location, "invalid layout qualifier: not supported", layoutQualifierName);
Martin Radev802abe02016-08-04 17:48:32 +03001589 }
1590}
1591
Olli Etuaho856c4972016-08-08 11:38:39 +03001592bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1593 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001594{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001595 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001596 for (size_t i = 0u; i < localSize.size(); ++i)
1597 {
1598 if (localSize[i] != -1)
1599 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001600 error(location,
1601 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1602 "global layout declaration",
1603 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001604 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001605 }
1606 }
1607
Olli Etuaho8a176262016-08-16 14:23:01 +03001608 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001609}
1610
Olli Etuaho43364892017-02-13 16:00:12 +00001611void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001612 TLayoutImageInternalFormat internalFormat)
1613{
1614 if (internalFormat != EiifUnspecified)
1615 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001616 error(location, "invalid layout qualifier: only valid when used with images",
1617 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001618 }
Olli Etuaho43364892017-02-13 16:00:12 +00001619}
1620
Olli Etuahoa78092c2018-09-26 14:16:13 +03001621void TParseContext::checkIndexIsNotSpecified(const TSourceLoc &location, int index)
1622{
1623 if (index != -1)
1624 {
1625 error(location,
1626 "invalid layout qualifier: only valid when used with a fragment shader output in "
1627 "ESSL version >= 3.00 and EXT_blend_func_extended is enabled",
1628 "index");
1629 }
1630}
1631
Olli Etuaho43364892017-02-13 16:00:12 +00001632void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1633{
1634 if (binding != -1)
1635 {
1636 error(location,
1637 "invalid layout qualifier: only valid when used with opaque types or blocks",
1638 "binding");
1639 }
1640}
1641
jchen104cdac9e2017-05-08 11:01:20 +08001642void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1643{
1644 if (offset != -1)
1645 {
1646 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1647 "offset");
1648 }
1649}
1650
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001651void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1652 int binding,
1653 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001654{
1655 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001656 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001657 {
1658 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1659 }
1660}
1661
1662void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1663 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001664 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001665{
1666 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001667 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001668 {
1669 error(location, "sampler binding greater than maximum texture units", "binding");
1670 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001671}
1672
Jiajia Qinbc585152017-06-23 15:42:17 +08001673void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1674 const TQualifier &qualifier,
1675 int binding,
1676 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001677{
1678 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001679 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001680 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001681 if (binding + size > mMaxUniformBufferBindings)
1682 {
1683 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1684 "binding");
1685 }
1686 }
1687 else if (qualifier == EvqBuffer)
1688 {
1689 if (binding + size > mMaxShaderStorageBufferBindings)
1690 {
1691 error(location,
1692 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1693 "binding");
1694 }
jchen10af713a22017-04-19 09:10:56 +08001695 }
1696}
jchen104cdac9e2017-05-08 11:01:20 +08001697void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1698{
1699 if (binding >= mMaxAtomicCounterBindings)
1700 {
1701 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1702 "binding");
1703 }
1704}
jchen10af713a22017-04-19 09:10:56 +08001705
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001706void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1707 int objectLocationCount,
1708 const TLayoutQualifier &layoutQualifier)
1709{
1710 int loc = layoutQualifier.location;
1711 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1712 {
1713 error(location, "Uniform location out of range", "location");
1714 }
1715}
1716
Andrei Volykhina5527072017-03-22 16:46:30 +03001717void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1718{
1719 if (yuv != false)
1720 {
1721 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1722 }
1723}
1724
Jiajia Qinbc585152017-06-23 15:42:17 +08001725void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1726 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001727{
1728 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1729 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001730 TQualifier qual = fnCandidate->getParam(i)->getType().getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001731 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho94bbed12018-03-20 14:44:53 +02001732 bool argumentIsRead = (IsQualifierUnspecified(qual) || qual == EvqIn || qual == EvqInOut ||
1733 qual == EvqConstReadOnly);
1734 if (argumentIsRead)
Jiajia Qinbc585152017-06-23 15:42:17 +08001735 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001736 markStaticReadIfSymbol(argument);
1737 if (!IsImage(argument->getBasicType()))
Jiajia Qinbc585152017-06-23 15:42:17 +08001738 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001739 if (argument->getMemoryQualifier().writeonly)
1740 {
1741 error(argument->getLine(),
1742 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1743 fnCall->functionName());
1744 return;
1745 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001746 }
1747 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001748 if (qual == EvqOut || qual == EvqInOut)
1749 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001750 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001751 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001752 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001753 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001754 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001755 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001756 }
1757 }
1758 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001759}
1760
Martin Radev70866b82016-07-22 15:27:42 +03001761void TParseContext::checkInvariantVariableQualifier(bool invariant,
1762 const TQualifier qualifier,
1763 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001764{
Martin Radev70866b82016-07-22 15:27:42 +03001765 if (!invariant)
1766 return;
1767
1768 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001769 {
Martin Radev70866b82016-07-22 15:27:42 +03001770 // input variables in the fragment shader can be also qualified as invariant
1771 if (!sh::CanBeInvariantESSL1(qualifier))
1772 {
1773 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1774 }
1775 }
1776 else
1777 {
1778 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1779 {
1780 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1781 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001782 }
1783}
1784
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001785bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001786{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001787 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001788}
1789
Jamie Madillb98c3a82015-07-23 14:26:04 -04001790void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1791 const char *extName,
1792 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001793{
Geoff Lang197d5292018-04-25 14:29:00 -04001794 angle::pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -04001795 srcLoc.file = loc.first_file;
1796 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001797 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001798}
1799
Jamie Madillb98c3a82015-07-23 14:26:04 -04001800void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1801 const char *name,
1802 const char *value,
1803 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001804{
Geoff Lang197d5292018-04-25 14:29:00 -04001805 angle::pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -04001806 srcLoc.file = loc.first_file;
1807 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001808 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001809}
1810
Martin Radev4c4c8e72016-08-04 12:25:34 +03001811sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001812{
Jamie Madill2f294c92017-11-20 14:47:26 -05001813 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001814 for (size_t i = 0u; i < result.size(); ++i)
1815 {
1816 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1817 {
1818 result[i] = 1;
1819 }
1820 else
1821 {
1822 result[i] = mComputeShaderLocalSize[i];
1823 }
1824 }
1825 return result;
1826}
1827
Olli Etuaho56229f12017-07-10 14:16:33 +03001828TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1829 const TSourceLoc &line)
1830{
1831 TIntermConstantUnion *node = new TIntermConstantUnion(
1832 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1833 node->setLine(line);
1834 return node;
1835}
1836
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837/////////////////////////////////////////////////////////////////////////////////
1838//
1839// Non-Errors.
1840//
1841/////////////////////////////////////////////////////////////////////////////////
1842
Jamie Madill5c097022014-08-20 16:38:32 -04001843const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001844 const ImmutableString &name,
Jamie Madill5c097022014-08-20 16:38:32 -04001845 const TSymbol *symbol)
1846{
Jamie Madill5c097022014-08-20 16:38:32 -04001847 if (!symbol)
1848 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001849 error(location, "undeclared identifier", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001850 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001851 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001852
1853 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001854 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001855 error(location, "variable expected", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001856 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001857 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001858
1859 const TVariable *variable = static_cast<const TVariable *>(symbol);
1860
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001861 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001862 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001863 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001864 }
1865
Olli Etuaho0f684632017-07-13 12:42:15 +03001866 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1867 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
Olli Etuaho59c5b892018-04-03 11:44:50 +03001868 variable->getType().getQualifier() == EvqWorkGroupSize)
Olli Etuaho0f684632017-07-13 12:42:15 +03001869 {
1870 error(location,
1871 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1872 "gl_WorkGroupSize");
1873 }
Jamie Madill5c097022014-08-20 16:38:32 -04001874 return variable;
1875}
1876
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001877TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001878 const ImmutableString &name,
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001879 const TSymbol *symbol)
1880{
1881 const TVariable *variable = getNamedVariable(location, name, symbol);
1882
Olli Etuaho0f684632017-07-13 12:42:15 +03001883 if (!variable)
1884 {
1885 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1886 node->setLine(location);
1887 return node;
1888 }
1889
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001890 const TType &variableType = variable->getType();
Jamie Madill50cf2be2018-06-15 09:46:57 -04001891 TIntermTyped *node = nullptr;
Olli Etuaho56229f12017-07-10 14:16:33 +03001892
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001893 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001894 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001895 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001896 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001897 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001898 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001899 {
1900 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1901 // needs to be added to the AST as a constant and not as a symbol.
1902 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1903 TConstantUnion *constArray = new TConstantUnion[3];
1904 for (size_t i = 0; i < 3; ++i)
1905 {
1906 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1907 }
1908
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001909 ASSERT(variableType.getBasicType() == EbtUInt);
1910 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001911
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001912 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001913 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001914 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001915 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001916 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1917 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001918 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001919 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
1920 node = new TIntermSymbol(symbolTable.getGlInVariableWithArraySize());
Jiawei Shaod8105a02017-08-08 09:54:36 +08001921 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001922 else
1923 {
Olli Etuaho195be942017-12-04 23:40:14 +02001924 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001925 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001926 ASSERT(node != nullptr);
1927 node->setLine(location);
1928 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001929}
1930
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001931// Initializers show up in several places in the grammar. Have one set of
1932// code to handle them here.
1933//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001934// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001935bool TParseContext::executeInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001936 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001937 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001938 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001939 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001940{
Olli Etuaho13389b62016-10-16 11:48:18 +01001941 ASSERT(initNode != nullptr);
1942 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943
Olli Etuahob60d30f2018-01-16 12:31:06 +02001944 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001945 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001946 // In case initializer is not an array or type has more dimensions than initializer, this
1947 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1948 // actually is an array or not. Having a non-array initializer for an unsized array will
1949 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001950 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001951 type->sizeUnsizedArrays(arraySizes);
1952 }
1953
1954 const TQualifier qualifier = type->getQualifier();
1955
1956 bool constError = false;
1957 if (qualifier == EvqConst)
1958 {
1959 if (EvqConst != initializer->getType().getQualifier())
1960 {
Olli Etuaho72e35892018-06-20 11:43:08 +03001961 TInfoSinkBase reasonStream;
1962 reasonStream << "assigning non-constant to '" << *type << "'";
1963 error(line, reasonStream.c_str(), "=");
Olli Etuahob60d30f2018-01-16 12:31:06 +02001964
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 {
Olli Etuaho72e35892018-06-20 11:43:08 +03002013 assignError(line, "=", variable->getType(), initializer->getType());
Olli Etuahob60d30f2018-01-16 12:31:06 +02002014 return false;
2015 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016
Arun Patole7e7e68d2015-05-22 12:02:25 +05302017 if (qualifier == EvqConst)
2018 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002019 // Save the constant folded value to the variable if possible.
2020 const TConstantUnion *constArray = initializer->getConstantValue();
2021 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302022 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002023 variable->shareConstPointer(constArray);
2024 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002025 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03002026 ASSERT(*initNode == nullptr);
2027 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002028 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002029 }
2030 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002031
Olli Etuahob60d30f2018-01-16 12:31:06 +02002032 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
Olli Etuaho94bbed12018-03-20 14:44:53 +02002033 markStaticReadIfSymbol(initializer);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002034 (*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 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002076 markStaticReadIfSymbol(cond);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002077 typedCond = cond->getAsTyped();
2078 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02002079 if (expr)
2080 {
2081 markStaticReadIfSymbol(expr);
2082 }
2083 // In case the loop body was not parsed as a block and contains a statement that simply refers
2084 // to a variable, we need to mark it as statically used.
2085 if (body)
2086 {
2087 markStaticReadIfSymbol(body);
2088 }
Olli Etuaho914b79a2017-06-19 16:03:19 +03002089 if (cond == nullptr || typedCond)
2090 {
Olli Etuahocce89652017-06-19 16:04:09 +03002091 if (type == ELoopDoWhile)
2092 {
2093 checkIsScalarBool(line, typedCond);
2094 }
2095 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2096 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2097 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2098 !typedCond->isVector()));
2099
Olli Etuaho3ec75682017-07-05 17:02:55 +03002100 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002101 node->setLine(line);
2102 return node;
2103 }
2104
Olli Etuahocce89652017-06-19 16:04:09 +03002105 ASSERT(type != ELoopDoWhile);
2106
Olli Etuaho914b79a2017-06-19 16:03:19 +03002107 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2108 ASSERT(declaration);
2109 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2110 ASSERT(declarator->getLeft()->getAsSymbolNode());
2111
2112 // The condition is a declaration. In the AST representation we don't support declarations as
2113 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2114 // the loop.
2115 TIntermBlock *block = new TIntermBlock();
2116
2117 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2118 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2119 block->appendStatement(declareCondition);
2120
2121 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2122 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002123 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002124 block->appendStatement(loop);
2125 loop->setLine(line);
2126 block->setLine(line);
2127 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128}
2129
Olli Etuahocce89652017-06-19 16:04:09 +03002130TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2131 TIntermNodePair code,
2132 const TSourceLoc &loc)
2133{
Olli Etuaho56229f12017-07-10 14:16:33 +03002134 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuaho94bbed12018-03-20 14:44:53 +02002135 // In case the conditional statements were not parsed as blocks and contain a statement that
2136 // simply refers to a variable, we need to mark them as statically used.
2137 if (code.node1)
2138 {
2139 markStaticReadIfSymbol(code.node1);
2140 }
2141 if (code.node2)
2142 {
2143 markStaticReadIfSymbol(code.node2);
2144 }
Olli Etuahocce89652017-06-19 16:04:09 +03002145
2146 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002147 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002148 {
2149 if (cond->getAsConstantUnion()->getBConst(0) == true)
2150 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002151 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002152 }
2153 else
2154 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002155 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002156 }
2157 }
2158
Olli Etuaho3ec75682017-07-05 17:02:55 +03002159 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuaho94bbed12018-03-20 14:44:53 +02002160 markStaticReadIfSymbol(cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002161 node->setLine(loc);
2162
2163 return node;
2164}
2165
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002166void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2167{
2168 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2169 typeSpecifier->getBasicType());
2170
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002171 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002172 {
2173 error(typeSpecifier->getLine(), "not supported", "first-class array");
2174 typeSpecifier->clearArrayness();
2175 }
2176}
2177
Martin Radev70866b82016-07-22 15:27:42 +03002178TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302179 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002180{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002181 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002182
Martin Radev70866b82016-07-22 15:27:42 +03002183 TPublicType returnType = typeSpecifier;
2184 returnType.qualifier = typeQualifier.qualifier;
2185 returnType.invariant = typeQualifier.invariant;
2186 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002187 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002188 returnType.precision = typeSpecifier.precision;
2189
2190 if (typeQualifier.precision != EbpUndefined)
2191 {
2192 returnType.precision = typeQualifier.precision;
2193 }
2194
Martin Radev4a9cd802016-09-01 16:51:51 +03002195 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2196 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002197
Martin Radev4a9cd802016-09-01 16:51:51 +03002198 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2199 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002200
Martin Radev4a9cd802016-09-01 16:51:51 +03002201 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002202
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002203 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002204 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002205 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002206 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002207 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002208 returnType.clearArrayness();
2209 }
2210
Martin Radev70866b82016-07-22 15:27:42 +03002211 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002212 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002213 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002214 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002215 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002216 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002217
Martin Radev70866b82016-07-22 15:27:42 +03002218 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002219 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002220 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002221 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002222 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002223 }
2224 }
2225 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002226 {
Martin Radev70866b82016-07-22 15:27:42 +03002227 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002228 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002229 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002230 }
Martin Radev70866b82016-07-22 15:27:42 +03002231 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2232 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002233 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002234 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2235 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002236 }
Martin Radev70866b82016-07-22 15:27:42 +03002237 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002238 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002239 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002240 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002241 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002242 }
2243
2244 return returnType;
2245}
2246
Olli Etuaho856c4972016-08-08 11:38:39 +03002247void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2248 const TPublicType &type,
2249 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002250{
2251 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002252 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002253 {
2254 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002255 }
2256
2257 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2258 switch (qualifier)
2259 {
2260 case EvqVertexIn:
2261 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002262 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002263 {
2264 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002265 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002266 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 return;
2268 case EvqFragmentOut:
2269 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002270 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002271 {
2272 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002273 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002274 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002275 return;
2276 default:
2277 break;
2278 }
2279
2280 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2281 // restrictions.
2282 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002283 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2284 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002285 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2286 {
2287 error(qualifierLocation, "must use 'flat' interpolation here",
2288 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002289 }
2290
Martin Radev4a9cd802016-09-01 16:51:51 +03002291 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002292 {
2293 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2294 // These restrictions are only implied by the ESSL 3.00 spec, but
2295 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002296 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002297 {
2298 error(qualifierLocation, "cannot be an array of structures",
2299 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002300 }
2301 if (type.isStructureContainingArrays())
2302 {
2303 error(qualifierLocation, "cannot be a structure containing an array",
2304 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002305 }
2306 if (type.isStructureContainingType(EbtStruct))
2307 {
2308 error(qualifierLocation, "cannot be a structure containing a structure",
2309 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002310 }
2311 if (type.isStructureContainingType(EbtBool))
2312 {
2313 error(qualifierLocation, "cannot be a structure containing a bool",
2314 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002315 }
2316 }
2317}
2318
Martin Radev2cc85b32016-08-05 16:22:53 +03002319void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2320{
2321 if (qualifier.getType() == QtStorage)
2322 {
2323 const TStorageQualifierWrapper &storageQualifier =
2324 static_cast<const TStorageQualifierWrapper &>(qualifier);
2325 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2326 !symbolTable.atGlobalLevel())
2327 {
2328 error(storageQualifier.getLine(),
2329 "Local variables can only use the const storage qualifier.",
Olli Etuaho1a3bbaa2018-01-25 11:41:31 +02002330 storageQualifier.getQualifierString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002331 }
2332 }
2333}
2334
Olli Etuaho43364892017-02-13 16:00:12 +00002335void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002336 const TSourceLoc &location)
2337{
Jiajia Qinbc585152017-06-23 15:42:17 +08002338 const std::string reason(
2339 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2340 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002341 if (memoryQualifier.readonly)
2342 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002343 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002344 }
2345 if (memoryQualifier.writeonly)
2346 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002347 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002348 }
Martin Radev049edfa2016-11-11 14:35:37 +02002349 if (memoryQualifier.coherent)
2350 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002351 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002352 }
2353 if (memoryQualifier.restrictQualifier)
2354 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002355 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002356 }
2357 if (memoryQualifier.volatileQualifier)
2358 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002359 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002360 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002361}
2362
jchen104cdac9e2017-05-08 11:01:20 +08002363// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2364// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002365void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2366 const TSourceLoc &loc,
2367 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002368{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002369 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2370 : kAtomicCounterSize;
2371 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2372 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002373 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002374 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002375 {
2376 offset = bindingState.appendSpan(size);
2377 }
2378 else
2379 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002380 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002381 }
2382 if (offset == -1)
2383 {
2384 error(loc, "Offset overlapping", "atomic counter");
2385 return;
2386 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002387 layoutQualifier.offset = offset;
2388 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002389}
2390
Enrico Galliee7ffd92018-12-13 14:07:52 -08002391void TParseContext::checkAtomicCounterOffsetAlignment(const TSourceLoc &location, const TType &type)
2392{
2393 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
2394
2395 // OpenGL ES 3.1 Table 6.5, Atomic counter offset must be a multiple of 4
2396 if (layoutQualifier.offset % 4 != 0)
2397 {
2398 error(location, "Offset must be multiple of 4", "atomic counter");
2399 }
2400}
2401
Olli Etuaho454c34c2017-10-25 16:35:56 +03002402void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002403 const ImmutableString &token,
Olli Etuaho454c34c2017-10-25 16:35:56 +03002404 TType *type)
2405{
2406 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2407 {
2408 if (type->isArray() && type->getOutermostArraySize() == 0u)
2409 {
2410 // Set size for the unsized geometry shader inputs if they are declared after a valid
2411 // input primitive declaration.
2412 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2413 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002414 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002415 type->sizeOutermostUnsizedArray(
Olli Etuaho94bbed12018-03-20 14:44:53 +02002416 symbolTable.getGlInVariableWithArraySize()->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002417 }
2418 else
2419 {
2420 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2421 // An input can be declared without an array size if there is a previous layout
2422 // which specifies the size.
2423 error(location,
2424 "Missing a valid input primitive declaration before declaring an unsized "
2425 "array input",
2426 token);
2427 }
2428 }
2429 else if (type->isArray())
2430 {
2431 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2432 }
2433 else
2434 {
2435 error(location, "Geometry shader input variable must be declared as an array", token);
2436 }
2437 }
2438}
2439
Olli Etuaho13389b62016-10-16 11:48:18 +01002440TIntermDeclaration *TParseContext::parseSingleDeclaration(
2441 TPublicType &publicType,
2442 const TSourceLoc &identifierOrTypeLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002443 const ImmutableString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002444{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002445 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002446 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2447 mDirectiveHandler.pragma().stdgl.invariantAll)
2448 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002449 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002450
2451 // The directive handler has already taken care of rejecting invalid uses of this pragma
2452 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2453 // affected variable declarations:
2454 //
2455 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2456 // elsewhere, in TranslatorGLSL.)
2457 //
2458 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2459 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2460 // the way this is currently implemented we have to enable this compiler option before
2461 // parsing the shader and determining the shading language version it uses. If this were
2462 // implemented as a post-pass, the workaround could be more targeted.
2463 //
2464 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2465 // the specification, but there are desktop OpenGL drivers that expect that this is the
2466 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2467 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2468 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002469 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002470 }
2471 }
2472
Olli Etuahofbb1c792018-01-19 16:26:59 +02002473 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier, type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002474
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002475 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2476 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002477
Jamie Madill50cf2be2018-06-15 09:46:57 -04002478 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002479 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002480
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002481 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002482 if (emptyDeclaration)
2483 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002484 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002485 // In most cases we don't need to create a symbol node for an empty declaration.
2486 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002487 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002488 {
Olli Etuaho195be942017-12-04 23:40:14 +02002489 TVariable *emptyVariable =
Jamie Madillb779b122018-06-20 11:46:43 -04002490 new TVariable(&symbolTable, kEmptyImmutableString, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002491 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002492 }
jchen104cdac9e2017-05-08 11:01:20 +08002493 else if (IsAtomicCounter(publicType.getBasicType()))
2494 {
2495 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2496 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002497 }
2498 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002499 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002500 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002501
Olli Etuahob60d30f2018-01-16 12:31:06 +02002502 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002503
Enrico Galliee7ffd92018-12-13 14:07:52 -08002504 if (IsAtomicCounter(type->getBasicType()))
2505 {
2506 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
2507
2508 checkAtomicCounterOffsetAlignment(identifierOrTypeLocation, *type);
2509 }
jchen104cdac9e2017-05-08 11:01:20 +08002510
Olli Etuaho2935c582015-04-08 14:32:06 +03002511 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002512 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002513 {
Olli Etuaho195be942017-12-04 23:40:14 +02002514 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002515 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002516 }
2517
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002518 TIntermDeclaration *declaration = new TIntermDeclaration();
2519 declaration->setLine(identifierOrTypeLocation);
2520 if (symbol)
2521 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002522 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002523 declaration->appendDeclarator(symbol);
2524 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002525 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002526}
2527
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002528TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2529 TPublicType &elementType,
2530 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002531 const ImmutableString &identifier,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002532 const TSourceLoc &indexLocation,
2533 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002534{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002535 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002536
Olli Etuaho55bde912017-10-25 13:41:13 +03002537 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002538 identifierLocation);
2539
Olli Etuaho55bde912017-10-25 13:41:13 +03002540 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002541
Olli Etuaho55bde912017-10-25 13:41:13 +03002542 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002543
Olli Etuahob60d30f2018-01-16 12:31:06 +02002544 TType *arrayType = new TType(elementType);
2545 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002546
Olli Etuahofbb1c792018-01-19 16:26:59 +02002547 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002548
Olli Etuahob60d30f2018-01-16 12:31:06 +02002549 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002550
Enrico Galliee7ffd92018-12-13 14:07:52 -08002551 if (IsAtomicCounter(arrayType->getBasicType()))
2552 {
2553 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
2554
2555 checkAtomicCounterOffsetAlignment(identifierLocation, *arrayType);
2556 }
jchen104cdac9e2017-05-08 11:01:20 +08002557
Olli Etuaho13389b62016-10-16 11:48:18 +01002558 TIntermDeclaration *declaration = new TIntermDeclaration();
2559 declaration->setLine(identifierLocation);
2560
Olli Etuaho195be942017-12-04 23:40:14 +02002561 TVariable *variable = nullptr;
2562 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002563 {
Olli Etuaho195be942017-12-04 23:40:14 +02002564 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002565 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002566 declaration->appendDeclarator(symbol);
2567 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002568
Olli Etuaho13389b62016-10-16 11:48:18 +01002569 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002570}
2571
Olli Etuaho13389b62016-10-16 11:48:18 +01002572TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2573 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002574 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002575 const TSourceLoc &initLocation,
2576 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002577{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002578 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002579
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002580 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2581 identifierLocation);
2582
2583 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002584
Olli Etuaho13389b62016-10-16 11:48:18 +01002585 TIntermDeclaration *declaration = new TIntermDeclaration();
2586 declaration->setLine(identifierLocation);
2587
2588 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002589 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002590 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002591 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002592 if (initNode)
2593 {
2594 declaration->appendDeclarator(initNode);
2595 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002596 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002597 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002598}
2599
Olli Etuaho13389b62016-10-16 11:48:18 +01002600TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002601 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002602 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002603 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002604 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002605 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002606 const TSourceLoc &initLocation,
2607 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002608{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002609 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002610
Olli Etuaho55bde912017-10-25 13:41:13 +03002611 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002612 identifierLocation);
2613
Olli Etuaho55bde912017-10-25 13:41:13 +03002614 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002615
Olli Etuaho55bde912017-10-25 13:41:13 +03002616 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002617
Olli Etuahob60d30f2018-01-16 12:31:06 +02002618 TType *arrayType = new TType(elementType);
2619 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002620
Olli Etuaho13389b62016-10-16 11:48:18 +01002621 TIntermDeclaration *declaration = new TIntermDeclaration();
2622 declaration->setLine(identifierLocation);
2623
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002624 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002625 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002626 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002627 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002628 if (initNode)
2629 {
2630 declaration->appendDeclarator(initNode);
2631 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002632 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002633
2634 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002635}
2636
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002637TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002638 const TTypeQualifierBuilder &typeQualifierBuilder,
2639 const TSourceLoc &identifierLoc,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002640 const ImmutableString &identifier,
Martin Radev70866b82016-07-22 15:27:42 +03002641 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002642{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002643 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002644
Martin Radev70866b82016-07-22 15:27:42 +03002645 if (!typeQualifier.invariant)
2646 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002647 error(identifierLoc, "Expected invariant", identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002648 return nullptr;
2649 }
2650 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2651 {
2652 return nullptr;
2653 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002654 if (!symbol)
2655 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002656 error(identifierLoc, "undeclared identifier declared as invariant", identifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002657 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002658 }
Martin Radev70866b82016-07-22 15:27:42 +03002659 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002660 {
Martin Radev70866b82016-07-22 15:27:42 +03002661 error(identifierLoc, "invariant declaration specifies qualifier",
2662 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002663 }
Martin Radev70866b82016-07-22 15:27:42 +03002664 if (typeQualifier.precision != EbpUndefined)
2665 {
2666 error(identifierLoc, "invariant declaration specifies precision",
2667 getPrecisionString(typeQualifier.precision));
2668 }
2669 if (!typeQualifier.layoutQualifier.isEmpty())
2670 {
2671 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2672 }
2673
2674 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002675 if (!variable)
2676 {
2677 return nullptr;
2678 }
Martin Radev70866b82016-07-22 15:27:42 +03002679 const TType &type = variable->getType();
2680
2681 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2682 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002683 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002684
Olli Etuaho76b2c382018-03-19 15:51:29 +02002685 symbolTable.addInvariantVarying(*variable);
Martin Radev70866b82016-07-22 15:27:42 +03002686
Olli Etuaho195be942017-12-04 23:40:14 +02002687 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002688 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002689
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002690 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002691}
2692
Olli Etuaho13389b62016-10-16 11:48:18 +01002693void TParseContext::parseDeclarator(TPublicType &publicType,
2694 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002695 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002696 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002697{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002698 // If the declaration starting this declarator list was empty (example: int,), some checks were
2699 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002700 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002701 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002702 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2703 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002704 }
2705
Olli Etuaho856c4972016-08-08 11:38:39 +03002706 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002707
Olli Etuahob60d30f2018-01-16 12:31:06 +02002708 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002709
Olli Etuahofbb1c792018-01-19 16:26:59 +02002710 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002711
Olli Etuahob60d30f2018-01-16 12:31:06 +02002712 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002713
Enrico Galliee7ffd92018-12-13 14:07:52 -08002714 if (IsAtomicCounter(type->getBasicType()))
2715 {
2716 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
2717
2718 checkAtomicCounterOffsetAlignment(identifierLocation, *type);
2719 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002720
Olli Etuaho195be942017-12-04 23:40:14 +02002721 TVariable *variable = nullptr;
2722 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002723 {
Olli Etuaho195be942017-12-04 23:40:14 +02002724 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002725 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002726 declarationOut->appendDeclarator(symbol);
2727 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002728}
2729
Olli Etuaho55bde912017-10-25 13:41:13 +03002730void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002731 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002732 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002733 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002734 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002735 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002736{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002737 // If the declaration starting this declarator list was empty (example: int,), some checks were
2738 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002739 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002740 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002741 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002742 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002743 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002744
Olli Etuaho55bde912017-10-25 13:41:13 +03002745 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002746
Olli Etuaho55bde912017-10-25 13:41:13 +03002747 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002748 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002749 TType *arrayType = new TType(elementType);
2750 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002751
Olli Etuahofbb1c792018-01-19 16:26:59 +02002752 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002753
Olli Etuahob60d30f2018-01-16 12:31:06 +02002754 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002755
Enrico Galliee7ffd92018-12-13 14:07:52 -08002756 if (IsAtomicCounter(arrayType->getBasicType()))
2757 {
2758 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
2759
2760 checkAtomicCounterOffsetAlignment(identifierLocation, *arrayType);
2761 }
jchen104cdac9e2017-05-08 11:01:20 +08002762
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002763 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002764 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002765 {
Olli Etuaho195be942017-12-04 23:40:14 +02002766 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002767 symbol->setLine(identifierLocation);
2768 declarationOut->appendDeclarator(symbol);
2769 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002770 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002771}
2772
Olli Etuaho13389b62016-10-16 11:48:18 +01002773void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2774 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002775 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002776 const TSourceLoc &initLocation,
2777 TIntermTyped *initializer,
2778 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002779{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002780 // If the declaration starting this declarator list was empty (example: int,), some checks were
2781 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002782 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002783 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002784 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2785 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002786 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002787
Olli Etuaho856c4972016-08-08 11:38:39 +03002788 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002789
Olli Etuaho13389b62016-10-16 11:48:18 +01002790 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002791 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002792 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002793 {
2794 //
2795 // build the intermediate representation
2796 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002797 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002798 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002799 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002800 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002801 }
2802}
2803
Olli Etuaho55bde912017-10-25 13:41:13 +03002804void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002805 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002806 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002807 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002808 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002809 const TSourceLoc &initLocation,
2810 TIntermTyped *initializer,
2811 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002812{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002813 // If the declaration starting this declarator list was empty (example: int,), some checks were
2814 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002815 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002816 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002817 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002818 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002819 }
2820
Olli Etuaho55bde912017-10-25 13:41:13 +03002821 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002822
Olli Etuaho55bde912017-10-25 13:41:13 +03002823 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002824
Olli Etuahob60d30f2018-01-16 12:31:06 +02002825 TType *arrayType = new TType(elementType);
2826 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002827
2828 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002829 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002830 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002831 {
2832 if (initNode)
2833 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002834 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002835 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002836 }
2837}
2838
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002839TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2840{
2841 // It's simpler to parse an empty statement as a constant expression rather than having a
2842 // different type of node just for empty statements, that will be pruned from the AST anyway.
2843 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2844 node->setLine(location);
2845 return node;
2846}
2847
jchen104cdac9e2017-05-08 11:01:20 +08002848void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2849 const TSourceLoc &location)
2850{
2851 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2852 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2853 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2854 {
2855 error(location, "Requires both binding and offset", "layout");
2856 return;
2857 }
2858 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2859}
2860
Olli Etuahocce89652017-06-19 16:04:09 +03002861void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2862 const TPublicType &type,
2863 const TSourceLoc &loc)
2864{
2865 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2866 !getFragmentPrecisionHigh())
2867 {
2868 error(loc, "precision is not supported in fragment shader", "highp");
2869 }
2870
2871 if (!CanSetDefaultPrecisionOnType(type))
2872 {
2873 error(loc, "illegal type argument for default precision qualifier",
2874 getBasicString(type.getBasicType()));
2875 return;
2876 }
2877 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2878}
2879
Shaob5cc1192017-07-06 10:47:20 +08002880bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2881{
2882 switch (typeQualifier.layoutQualifier.primitiveType)
2883 {
2884 case EptLines:
2885 case EptLinesAdjacency:
2886 case EptTriangles:
2887 case EptTrianglesAdjacency:
2888 return typeQualifier.qualifier == EvqGeometryIn;
2889
2890 case EptLineStrip:
2891 case EptTriangleStrip:
2892 return typeQualifier.qualifier == EvqGeometryOut;
2893
2894 case EptPoints:
2895 return true;
2896
2897 default:
2898 UNREACHABLE();
2899 return false;
2900 }
2901}
2902
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002903void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2904 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002905{
Olli Etuaho94bbed12018-03-20 14:44:53 +02002906 if (!symbolTable.setGlInArraySize(inputArraySize))
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002907 {
2908 error(line,
2909 "Array size or input primitive declaration doesn't match the size of earlier sized "
2910 "array inputs.",
2911 "layout");
2912 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002913}
2914
Shaob5cc1192017-07-06 10:47:20 +08002915bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2916{
2917 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2918
2919 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2920
2921 if (layoutQualifier.maxVertices != -1)
2922 {
2923 error(typeQualifier.line,
2924 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2925 return false;
2926 }
2927
2928 // Set mGeometryInputPrimitiveType if exists
2929 if (layoutQualifier.primitiveType != EptUndefined)
2930 {
2931 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2932 {
2933 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2934 return false;
2935 }
2936
2937 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2938 {
2939 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002940 setGeometryShaderInputArraySize(
2941 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2942 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002943 }
2944 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2945 {
2946 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2947 "layout");
2948 return false;
2949 }
2950 }
2951
2952 // Set mGeometryInvocations if exists
2953 if (layoutQualifier.invocations > 0)
2954 {
2955 if (mGeometryShaderInvocations == 0)
2956 {
2957 mGeometryShaderInvocations = layoutQualifier.invocations;
2958 }
2959 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2960 {
2961 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2962 "layout");
2963 return false;
2964 }
2965 }
2966
2967 return true;
2968}
2969
2970bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2971{
2972 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2973
2974 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2975
2976 if (layoutQualifier.invocations > 0)
2977 {
2978 error(typeQualifier.line,
2979 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2980 return false;
2981 }
2982
2983 // Set mGeometryOutputPrimitiveType if exists
2984 if (layoutQualifier.primitiveType != EptUndefined)
2985 {
2986 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2987 {
2988 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2989 return false;
2990 }
2991
2992 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2993 {
2994 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2995 }
2996 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2997 {
2998 error(typeQualifier.line,
2999 "primitive doesn't match earlier output primitive declaration", "layout");
3000 return false;
3001 }
3002 }
3003
3004 // Set mGeometryMaxVertices if exists
3005 if (layoutQualifier.maxVertices > -1)
3006 {
3007 if (mGeometryShaderMaxVertices == -1)
3008 {
3009 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
3010 }
3011 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
3012 {
3013 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
3014 "layout");
3015 return false;
3016 }
3017 }
3018
3019 return true;
3020}
3021
Martin Radev70866b82016-07-22 15:27:42 +03003022void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04003023{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003024 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04003025 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04003026
Martin Radev70866b82016-07-22 15:27:42 +03003027 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
3028 typeQualifier.line);
3029
Jamie Madillc2128ff2016-07-04 10:26:17 -04003030 // It should never be the case, but some strange parser errors can send us here.
3031 if (layoutQualifier.isEmpty())
3032 {
3033 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04003034 return;
3035 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003036
Martin Radev802abe02016-08-04 17:48:32 +03003037 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04003038 {
Olli Etuaho43364892017-02-13 16:00:12 +00003039 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04003040 return;
3041 }
3042
Olli Etuahoa78092c2018-09-26 14:16:13 +03003043 checkIndexIsNotSpecified(typeQualifier.line, layoutQualifier.index);
3044
Olli Etuaho43364892017-02-13 16:00:12 +00003045 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
3046
3047 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003048
3049 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3050
Andrei Volykhina5527072017-03-22 16:46:30 +03003051 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3052
jchen104cdac9e2017-05-08 11:01:20 +08003053 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3054
Qin Jiajiaca68d982017-09-18 16:41:56 +08003055 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3056 typeQualifier.qualifier);
3057
Martin Radev802abe02016-08-04 17:48:32 +03003058 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003059 {
Martin Radev802abe02016-08-04 17:48:32 +03003060 if (mComputeShaderLocalSizeDeclared &&
3061 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3062 {
3063 error(typeQualifier.line, "Work group size does not match the previous declaration",
3064 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003065 return;
3066 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003067
Martin Radev802abe02016-08-04 17:48:32 +03003068 if (mShaderVersion < 310)
3069 {
3070 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003071 return;
3072 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003073
Martin Radev4c4c8e72016-08-04 12:25:34 +03003074 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003075 {
3076 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003077 return;
3078 }
3079
3080 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003081 symbolTable.findBuiltIn(ImmutableString("gl_MaxComputeWorkGroupSize"), mShaderVersion));
Martin Radev802abe02016-08-04 17:48:32 +03003082
3083 const TConstantUnion *maxComputeWorkGroupSizeData =
3084 maxComputeWorkGroupSize->getConstPointer();
3085
3086 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3087 {
3088 if (layoutQualifier.localSize[i] != -1)
3089 {
3090 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3091 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3092 if (mComputeShaderLocalSize[i] < 1 ||
3093 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3094 {
Jonah Ryan-Davisf563fdc2019-01-31 13:53:59 -05003095 std::stringstream reasonStream = sh::InitializeStream<std::stringstream>();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003096 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3097 << maxComputeWorkGroupSizeValue;
3098 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003099
Olli Etuaho4de340a2016-12-16 09:32:03 +00003100 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003101 return;
3102 }
3103 }
3104 }
3105
3106 mComputeShaderLocalSizeDeclared = true;
3107 }
Shaob5cc1192017-07-06 10:47:20 +08003108 else if (typeQualifier.qualifier == EvqGeometryIn)
3109 {
3110 if (mShaderVersion < 310)
3111 {
3112 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3113 return;
3114 }
3115
3116 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3117 {
3118 return;
3119 }
3120 }
3121 else if (typeQualifier.qualifier == EvqGeometryOut)
3122 {
3123 if (mShaderVersion < 310)
3124 {
3125 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3126 "layout");
3127 return;
3128 }
3129
3130 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3131 {
3132 return;
3133 }
3134 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003135 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3136 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003137 {
3138 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3139 // specification.
3140 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3141 {
3142 error(typeQualifier.line, "Number of views does not match the previous declaration",
3143 "layout");
3144 return;
3145 }
3146
3147 if (layoutQualifier.numViews == -1)
3148 {
3149 error(typeQualifier.line, "No num_views specified", "layout");
3150 return;
3151 }
3152
3153 if (layoutQualifier.numViews > mMaxNumViews)
3154 {
3155 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3156 "layout");
3157 return;
3158 }
3159
3160 mNumViews = layoutQualifier.numViews;
3161 }
Martin Radev802abe02016-08-04 17:48:32 +03003162 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003163 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003164 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003165 {
Martin Radev802abe02016-08-04 17:48:32 +03003166 return;
3167 }
3168
Jiajia Qinbc585152017-06-23 15:42:17 +08003169 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003170 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003171 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003172 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003173 return;
3174 }
3175
3176 if (mShaderVersion < 300)
3177 {
3178 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3179 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003180 return;
3181 }
3182
Olli Etuaho09b04a22016-12-15 13:30:26 +00003183 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003184
3185 if (layoutQualifier.matrixPacking != EmpUnspecified)
3186 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003187 if (typeQualifier.qualifier == EvqUniform)
3188 {
3189 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3190 }
3191 else if (typeQualifier.qualifier == EvqBuffer)
3192 {
3193 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3194 }
Martin Radev802abe02016-08-04 17:48:32 +03003195 }
3196
3197 if (layoutQualifier.blockStorage != EbsUnspecified)
3198 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003199 if (typeQualifier.qualifier == EvqUniform)
3200 {
3201 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3202 }
3203 else if (typeQualifier.qualifier == EvqBuffer)
3204 {
3205 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3206 }
Martin Radev802abe02016-08-04 17:48:32 +03003207 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003208 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003209}
3210
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003211TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3212 const TFunction &function,
3213 const TSourceLoc &location,
3214 bool insertParametersToSymbolTable)
3215{
Olli Etuahobed35d72017-12-20 16:36:26 +02003216 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003217
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003218 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003219 prototype->setLine(location);
3220
3221 for (size_t i = 0; i < function.getParamCount(); i++)
3222 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003223 const TVariable *param = function.getParam(i);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003224
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003225 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3226 // be used for unused args).
Olli Etuahod4bd9632018-03-08 16:32:44 +02003227 if (param->symbolType() != SymbolType::Empty)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003228 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003229 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003230 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003231 if (!symbolTable.declare(const_cast<TVariable *>(param)))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003232 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003233 error(location, "redefinition", param->name());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003234 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003235 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003236 // Unsized type of a named parameter should have already been checked and sanitized.
Olli Etuahod4bd9632018-03-08 16:32:44 +02003237 ASSERT(!param->getType().isUnsizedArray());
Olli Etuaho55bde912017-10-25 13:41:13 +03003238 }
3239 else
3240 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003241 if (param->getType().isUnsizedArray())
Olli Etuaho55bde912017-10-25 13:41:13 +03003242 {
3243 error(location, "function parameter array must be sized at compile time", "[]");
3244 // We don't need to size the arrays since the parameter is unnamed and hence
3245 // inaccessible.
3246 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003247 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003248 }
3249 return prototype;
3250}
3251
Olli Etuaho16c745a2017-01-16 17:02:27 +00003252TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3253 const TFunction &parsedFunction,
3254 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003255{
Olli Etuaho476197f2016-10-11 13:59:08 +01003256 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3257 // first declaration. Either way the instance in the symbol table is used to track whether the
3258 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003259 bool hadPrototypeDeclaration = false;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003260 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003261 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3262
3263 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003264 {
3265 // ESSL 1.00.17 section 4.2.7.
3266 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3267 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003268 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003269
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003270 TIntermFunctionPrototype *prototype =
3271 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003272
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003273 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003274
3275 if (!symbolTable.atGlobalLevel())
3276 {
3277 // ESSL 3.00.4 section 4.2.4.
3278 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003279 }
3280
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003281 return prototype;
3282}
3283
Olli Etuaho336b1472016-10-05 16:37:55 +01003284TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003285 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003286 TIntermBlock *functionBody,
3287 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003288{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003289 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003290 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3291 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003292 error(location,
3293 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003294 }
3295
Olli Etuahof51fdd22016-10-03 10:03:40 +01003296 if (functionBody == nullptr)
3297 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003298 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003299 functionBody->setLine(location);
3300 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003301 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003302 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003303 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003304
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003305 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003306 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003307}
3308
Olli Etuaho476197f2016-10-11 13:59:08 +01003309void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003310 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003311 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003312{
Olli Etuaho476197f2016-10-11 13:59:08 +01003313 ASSERT(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003314
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003315 bool wasDefined = false;
3316 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3317 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003318 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003319 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003320 }
Jamie Madill185fb402015-06-12 15:48:48 -04003321
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003322 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003323 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003324 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003325
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003326 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003327 setLoopNestingLevel(0);
3328}
3329
Jamie Madillb98c3a82015-07-23 14:26:04 -04003330TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003331{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003332 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003333 // We don't know at this point whether this is a function definition or a prototype.
3334 // The definition production code will check for redefinitions.
3335 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003336 //
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303337
Olli Etuahod80f2942017-11-06 12:44:45 +02003338 for (size_t i = 0u; i < function->getParamCount(); ++i)
3339 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003340 const TVariable *param = function->getParam(i);
3341 if (param->getType().isStructSpecifier())
Olli Etuahod80f2942017-11-06 12:44:45 +02003342 {
3343 // ESSL 3.00.6 section 12.10.
3344 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003345 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003346 }
3347 }
3348
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003349 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303350 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003351 const UnmangledBuiltIn *builtIn =
3352 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3353 if (builtIn &&
3354 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3355 {
3356 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3357 // functions. Therefore overloading or redefining builtin functions is an error.
3358 error(location, "Name of a built-in function cannot be redeclared as function",
3359 function->name());
3360 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303361 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003362 else
3363 {
3364 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3365 // this applies to redeclarations as well.
3366 const TSymbol *builtIn =
3367 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3368 if (builtIn)
3369 {
3370 error(location, "built-in functions cannot be redefined", function->name());
3371 }
3372 }
3373
3374 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3375 // here.
3376 const TFunction *prevDec =
3377 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3378 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003379 {
3380 if (prevDec->getReturnType() != function->getReturnType())
3381 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003382 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003383 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003384 }
3385 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3386 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003387 if (prevDec->getParam(i)->getType().getQualifier() !=
3388 function->getParam(i)->getType().getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003389 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003390 error(location,
3391 "function must have the same parameter qualifiers in all of its declarations",
Olli Etuahod4bd9632018-03-08 16:32:44 +02003392 function->getParam(i)->getType().getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003393 }
3394 }
3395 }
3396
Jamie Madill185fb402015-06-12 15:48:48 -04003397 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003398 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3399 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003400 if (prevSym)
3401 {
3402 if (!prevSym->isFunction())
3403 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003404 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003405 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003406 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003407 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003408 // Parsing is at the inner scope level of the function's arguments and body statement at this
3409 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3410 // scope.
3411 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003412
Olli Etuaho78d13742017-01-18 13:06:10 +00003413 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003414 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003415 {
3416 if (function->getParamCount() > 0)
3417 {
3418 error(location, "function cannot take any parameter(s)", "main");
3419 }
3420 if (function->getReturnType().getBasicType() != EbtVoid)
3421 {
3422 error(location, "main function cannot return a value",
3423 function->getReturnType().getBasicString());
3424 }
3425 }
3426
Jamie Madill185fb402015-06-12 15:48:48 -04003427 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003428 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3429 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003430 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3431 //
3432 return function;
3433}
3434
Olli Etuaho9de84a52016-06-14 17:36:01 +03003435TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003436 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003437 const TSourceLoc &location)
3438{
3439 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3440 {
3441 error(location, "no qualifiers allowed for function return",
3442 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003443 }
3444 if (!type.layoutQualifier.isEmpty())
3445 {
3446 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003447 }
jchen10cc2a10e2017-05-03 14:05:12 +08003448 // make sure an opaque type is not involved as well...
3449 std::string reason(getBasicString(type.getBasicType()));
3450 reason += "s can't be function return values";
3451 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003452 if (mShaderVersion < 300)
3453 {
3454 // Array return values are forbidden, but there's also no valid syntax for declaring array
3455 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003456 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003457
3458 if (type.isStructureContainingArrays())
3459 {
3460 // ESSL 1.00.17 section 6.1 Function Definitions
Olli Etuaho72e35892018-06-20 11:43:08 +03003461 TInfoSinkBase typeString;
3462 typeString << TType(type);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003463 error(location, "structures containing arrays can't be function return values",
Olli Etuaho72e35892018-06-20 11:43:08 +03003464 typeString.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003465 }
3466 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003467
3468 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003469 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003470}
3471
Olli Etuaho697bf652018-02-16 11:50:54 +02003472TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3473 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003474{
Olli Etuaho697bf652018-02-16 11:50:54 +02003475 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003476}
3477
Olli Etuaho95ed1942018-02-01 14:01:19 +02003478TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003479{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003480 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003481 {
3482 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3483 "[]");
3484 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003485 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003486 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003487 error(publicType.getLine(), "constructor can't be a structure definition",
3488 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003489 }
3490
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003491 TType *type = new TType(publicType);
3492 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003493 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003494 error(publicType.getLine(), "cannot construct this type",
3495 getBasicString(publicType.getBasicType()));
3496 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003497 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003498 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003499}
3500
Olli Etuaho55bde912017-10-25 13:41:13 +03003501void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3502 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003503 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003504 TType *arrayType)
3505{
3506 if (arrayType->isUnsizedArray())
3507 {
3508 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003509 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003510 }
3511}
3512
3513TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003514 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003515 const TSourceLoc &nameLoc)
3516{
Olli Etuaho55bde912017-10-25 13:41:13 +03003517 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003518 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003519 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003520 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003521 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003522 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003523 checkIsNotReserved(nameLoc, name);
3524 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003525 return param;
3526}
3527
Olli Etuaho55bde912017-10-25 13:41:13 +03003528TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003529 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003530 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003531{
Olli Etuaho55bde912017-10-25 13:41:13 +03003532 TType *type = new TType(publicType);
3533 return parseParameterDeclarator(type, name, nameLoc);
3534}
3535
Olli Etuahofbb1c792018-01-19 16:26:59 +02003536TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003537 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003538 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003539 const TSourceLoc &arrayLoc,
3540 TPublicType *elementType)
3541{
3542 checkArrayElementIsNotArray(arrayLoc, *elementType);
3543 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003544 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003545 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003546}
3547
Olli Etuaho95ed1942018-02-01 14:01:19 +02003548bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3549 const TIntermSequence &arguments,
3550 TType type,
3551 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003552{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003553 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003554 {
3555 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3556 return false;
3557 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003558 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003559 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003560 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003561 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003562 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3563 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003564 {
3565 error(line, "constructing from a non-dereferenced array", "constructor");
3566 return false;
3567 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003568 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003569 {
3570 if (dimensionalityFromElement == 1u)
3571 {
3572 error(line, "implicitly sized array of arrays constructor argument is not an array",
3573 "constructor");
3574 }
3575 else
3576 {
3577 error(line,
3578 "implicitly sized array of arrays constructor argument dimensionality is too "
3579 "low",
3580 "constructor");
3581 }
3582 return false;
3583 }
3584 }
3585 return true;
3586}
3587
Jamie Madillb98c3a82015-07-23 14:26:04 -04003588// This function is used to test for the correctness of the parameters passed to various constructor
3589// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003590//
Olli Etuaho856c4972016-08-08 11:38:39 +03003591// 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 +00003592//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003593TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003594{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003595 TType type = fnCall->constructorType();
3596 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003597 if (type.isUnsizedArray())
3598 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003599 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003600 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003601 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003602 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003603 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003604 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003605 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003606 if (type.getOutermostArraySize() == 0u)
3607 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003608 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003609 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003610 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003611 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003612 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003613 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003614 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003615 }
3616 }
3617 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003618 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003619
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003620 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003621 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003622 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003623 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003624
Olli Etuaho95ed1942018-02-01 14:01:19 +02003625 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003626 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003627
Olli Etuaho765924f2018-01-04 12:48:36 +02003628 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003629}
3630
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003631//
3632// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003633// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003634//
Olli Etuaho13389b62016-10-16 11:48:18 +01003635TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003636 const TTypeQualifierBuilder &typeQualifierBuilder,
3637 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003638 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003639 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003640 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003641 const TSourceLoc &instanceLine,
3642 TIntermTyped *arrayIndex,
3643 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003644{
Olli Etuaho856c4972016-08-08 11:38:39 +03003645 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003646
Olli Etuaho77ba4082016-12-16 12:01:18 +00003647 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003648
Jiajia Qinbc585152017-06-23 15:42:17 +08003649 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003650 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003651 error(typeQualifier.line,
3652 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3653 "3.10",
3654 getQualifierString(typeQualifier.qualifier));
3655 }
3656 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3657 {
3658 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003659 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003660 }
3661
Martin Radev70866b82016-07-22 15:27:42 +03003662 if (typeQualifier.invariant)
3663 {
3664 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3665 }
3666
Jiajia Qinbc585152017-06-23 15:42:17 +08003667 if (typeQualifier.qualifier != EvqBuffer)
3668 {
3669 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3670 }
Olli Etuaho43364892017-02-13 16:00:12 +00003671
jchen10af713a22017-04-19 09:10:56 +08003672 // add array index
3673 unsigned int arraySize = 0;
3674 if (arrayIndex != nullptr)
3675 {
3676 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3677 }
3678
Olli Etuahoa78092c2018-09-26 14:16:13 +03003679 checkIndexIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.index);
3680
jchen10af713a22017-04-19 09:10:56 +08003681 if (mShaderVersion < 310)
3682 {
3683 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3684 }
3685 else
3686 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003687 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3688 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003689 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003690
Andrei Volykhina5527072017-03-22 16:46:30 +03003691 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3692
Jamie Madill099c0f32013-06-20 11:55:52 -04003693 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003694 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003695 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3696 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003697
Jamie Madill099c0f32013-06-20 11:55:52 -04003698 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3699 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003700 if (typeQualifier.qualifier == EvqUniform)
3701 {
3702 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3703 }
3704 else if (typeQualifier.qualifier == EvqBuffer)
3705 {
3706 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3707 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003708 }
3709
Jamie Madill1566ef72013-06-20 11:55:54 -04003710 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3711 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003712 if (typeQualifier.qualifier == EvqUniform)
3713 {
3714 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3715 }
3716 else if (typeQualifier.qualifier == EvqBuffer)
3717 {
3718 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3719 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003720 }
3721
Olli Etuaho856c4972016-08-08 11:38:39 +03003722 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003723
Martin Radev2cc85b32016-08-05 16:22:53 +03003724 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3725
Jamie Madill98493dd2013-07-08 14:39:03 -04003726 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303727 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3728 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003729 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303730 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003731 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303732 {
jchen10cc2a10e2017-05-03 14:05:12 +08003733 std::string reason("unsupported type - ");
3734 reason += fieldType->getBasicString();
3735 reason += " types are not allowed in interface blocks";
3736 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003737 }
3738
Jamie Madill98493dd2013-07-08 14:39:03 -04003739 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003740 switch (qualifier)
3741 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003742 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003743 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003744 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003745 if (typeQualifier.qualifier == EvqBuffer)
3746 {
3747 error(field->line(), "invalid qualifier on shader storage block member",
3748 getQualifierString(qualifier));
3749 }
3750 break;
3751 case EvqBuffer:
3752 if (typeQualifier.qualifier == EvqUniform)
3753 {
3754 error(field->line(), "invalid qualifier on uniform block member",
3755 getQualifierString(qualifier));
3756 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003757 break;
3758 default:
3759 error(field->line(), "invalid qualifier on interface block member",
3760 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003761 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003762 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003763
Martin Radev70866b82016-07-22 15:27:42 +03003764 if (fieldType->isInvariant())
3765 {
3766 error(field->line(), "invalid qualifier on interface block member", "invariant");
3767 }
3768
Jamie Madilla5efff92013-06-06 11:56:47 -04003769 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003770 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003771 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Olli Etuahoa78092c2018-09-26 14:16:13 +03003772 checkIndexIsNotSpecified(field->line(), fieldLayoutQualifier.index);
jchen10af713a22017-04-19 09:10:56 +08003773 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003774
Jamie Madill98493dd2013-07-08 14:39:03 -04003775 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003776 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003777 error(field->line(), "invalid layout qualifier: cannot be used here",
3778 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003779 }
3780
Jamie Madill98493dd2013-07-08 14:39:03 -04003781 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003782 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003783 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003784 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003785 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003786 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003787 warning(field->line(),
3788 "extraneous layout qualifier: only has an effect on matrix types",
3789 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003790 }
3791
Jamie Madill98493dd2013-07-08 14:39:03 -04003792 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003793
Olli Etuahoebee5b32017-11-23 12:56:32 +02003794 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3795 typeQualifier.qualifier != EvqBuffer)
3796 {
3797 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3798 checkIsNotUnsizedArray(field->line(),
3799 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003800 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003801 }
3802
Jiajia Qinbc585152017-06-23 15:42:17 +08003803 if (typeQualifier.qualifier == EvqBuffer)
3804 {
3805 // set memory qualifiers
3806 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3807 // qualified with a memory qualifier, it is as if all of its members were declared with
3808 // the same memory qualifier.
3809 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3810 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3811 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3812 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3813 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3814 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3815 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3816 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3817 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3818 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3819 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003820 }
3821
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003822 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003823 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003824 if (!symbolTable.declare(interfaceBlock))
Olli Etuaho378c3a52017-12-04 11:32:13 +02003825 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003826 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003827 }
3828
Olli Etuahob60d30f2018-01-16 12:31:06 +02003829 TType *interfaceBlockType =
3830 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003831 if (arrayIndex != nullptr)
3832 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003833 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003834 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003835
Olli Etuaho195be942017-12-04 23:40:14 +02003836 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003837 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3838 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003839 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003840 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003841 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003842
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003843 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003844 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003845 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003846 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3847 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003848 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003849 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003850
3851 // set parent pointer of the field variable
3852 fieldType->setInterfaceBlock(interfaceBlock);
3853
Olli Etuahob60d30f2018-01-16 12:31:06 +02003854 fieldType->setQualifier(typeQualifier.qualifier);
3855
Olli Etuaho195be942017-12-04 23:40:14 +02003856 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003857 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003858 if (!symbolTable.declare(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303859 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003860 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003861 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003862 }
3863 }
3864 }
3865 else
3866 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003867 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003868
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003869 // add a symbol for this interface block
Olli Etuaho437664b2018-02-28 15:38:14 +02003870 if (!symbolTable.declare(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303871 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003872 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003873 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003874 }
3875
Olli Etuaho195be942017-12-04 23:40:14 +02003876 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3877 blockSymbol->setLine(typeQualifier.line);
3878 TIntermDeclaration *declaration = new TIntermDeclaration();
3879 declaration->appendDeclarator(blockSymbol);
3880 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003881
3882 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003883 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003884}
3885
Olli Etuahofbb1c792018-01-19 16:26:59 +02003886void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3887 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003888{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003889 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003890
3891 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003892 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303893 if (mStructNestingLevel > 1)
3894 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003895 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003896 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003897}
3898
3899void TParseContext::exitStructDeclaration()
3900{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003901 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003902}
3903
Olli Etuaho8a176262016-08-16 14:23:01 +03003904void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003905{
Jamie Madillacb4b812016-11-07 13:50:29 -05003906 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303907 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003908 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003909 }
3910
Arun Patole7e7e68d2015-05-22 12:02:25 +05303911 if (field.type()->getBasicType() != EbtStruct)
3912 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003913 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003914 }
3915
3916 // We're already inside a structure definition at this point, so add
3917 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303918 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3919 {
Jonah Ryan-Davisf563fdc2019-01-31 13:53:59 -05003920 std::stringstream reasonStream = sh::InitializeStream<std::stringstream>();
Olli Etuahof0957992017-12-22 11:10:04 +02003921 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3922 {
3923 // This may happen in case there are nested struct definitions. While they are also
3924 // invalid GLSL, they don't cause a syntax error.
3925 reasonStream << "Struct nesting";
3926 }
3927 else
3928 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003929 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003930 }
3931 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003932 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003933 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003934 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003935 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003936}
3937
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003938//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003939// Parse an array index expression
3940//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003941TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3942 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303943 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003944{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003945 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3946 {
3947 if (baseExpression->getAsSymbolNode())
3948 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303949 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003950 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003951 }
3952 else
3953 {
3954 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3955 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003956
Olli Etuaho3ec75682017-07-05 17:02:55 +03003957 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003958 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003959
Jiawei Shaod8105a02017-08-08 09:54:36 +08003960 if (baseExpression->getQualifier() == EvqPerVertexIn)
3961 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003962 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003963 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3964 {
3965 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3966 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3967 }
3968 }
3969
Jamie Madill21c1e452014-12-29 11:33:41 -05003970 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3971
Olli Etuaho1dfd8ae2018-10-01 15:59:59 +03003972 // ANGLE should be able to fold any constant expressions resulting in an integer - but to be
3973 // safe we don't treat "EvqConst" that's evaluated according to the spec as being sufficient
3974 // for constness. Some interpretations of the spec have allowed constant expressions with side
3975 // effects - like array length() method on a non-constant array.
Olli Etuaho36b05142015-11-12 13:10:42 +02003976 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3977 {
3978 if (baseExpression->isInterfaceBlock())
3979 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003980 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003981 switch (baseExpression->getQualifier())
3982 {
3983 case EvqPerVertexIn:
3984 break;
3985 case EvqUniform:
3986 case EvqBuffer:
3987 error(location,
3988 "array indexes for uniform block arrays and shader storage block arrays "
3989 "must be constant integral expressions",
3990 "[");
3991 break;
3992 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003993 // We can reach here only in error cases.
3994 ASSERT(mDiagnostics->numErrors() > 0);
3995 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003996 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003997 }
3998 else if (baseExpression->getQualifier() == EvqFragmentOut)
3999 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004000 error(location,
4001 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02004002 }
Olli Etuaho3e960462015-11-12 15:58:39 +02004003 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
4004 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004005 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02004006 }
Olli Etuaho36b05142015-11-12 13:10:42 +02004007 }
4008
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004009 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04004010 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004011 // If an out-of-range index is not qualified as constant, the behavior in the spec is
4012 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
4013 // constant fold expressions that are not constant expressions). The most compatible way to
4014 // handle this case is to report a warning instead of an error and force the index to be in
4015 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004016 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03004017 int index = 0;
4018 if (indexConstantUnion->getBasicType() == EbtInt)
4019 {
4020 index = indexConstantUnion->getIConst(0);
4021 }
4022 else if (indexConstantUnion->getBasicType() == EbtUInt)
4023 {
4024 index = static_cast<int>(indexConstantUnion->getUConst(0));
4025 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004026
4027 int safeIndex = -1;
4028
Olli Etuahoebee5b32017-11-23 12:56:32 +02004029 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004030 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004031 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4032 safeIndex = 0;
4033 }
4034
4035 if (!baseExpression->getType().isUnsizedArray())
4036 {
4037 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004038 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004039 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004040 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004041 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4042 {
4043 outOfRangeError(outOfRangeIndexIsError, location,
4044 "array index for gl_FragData must be zero when "
4045 "GL_EXT_draw_buffers is disabled",
4046 "[]");
4047 safeIndex = 0;
4048 }
4049 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004050 }
4051 // Only do generic out-of-range check if similar error hasn't already been reported.
4052 if (safeIndex < 0)
4053 {
4054 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004055 {
4056 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4057 baseExpression->getOutermostArraySize(),
4058 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004059 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004060 else if (baseExpression->isMatrix())
4061 {
4062 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4063 baseExpression->getType().getCols(),
4064 "matrix field selection out of range");
4065 }
4066 else
4067 {
4068 ASSERT(baseExpression->isVector());
4069 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4070 baseExpression->getType().getNominalSize(),
4071 "vector field selection out of range");
4072 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004073 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004074
Olli Etuahoebee5b32017-11-23 12:56:32 +02004075 ASSERT(safeIndex >= 0);
4076 // Data of constant unions can't be changed, because it may be shared with other
4077 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4078 // sanitized object.
4079 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4080 {
4081 TConstantUnion *safeConstantUnion = new TConstantUnion();
4082 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004083 indexExpression = new TIntermConstantUnion(
4084 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4085 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004086 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004087
Olli Etuahoebee5b32017-11-23 12:56:32 +02004088 TIntermBinary *node =
4089 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4090 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004091 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004092 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004093 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004094
Olli Etuaho94bbed12018-03-20 14:44:53 +02004095 markStaticReadIfSymbol(indexExpression);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004096 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4097 node->setLine(location);
4098 // Indirect indexing can never be constant folded.
4099 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004100}
4101
Olli Etuahoebee5b32017-11-23 12:56:32 +02004102int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4103 const TSourceLoc &location,
4104 int index,
4105 int arraySize,
4106 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004107{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004108 // Should not reach here with an unsized / runtime-sized array.
4109 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004110 // A negative index should already have been checked.
4111 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004112 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004113 {
Jonah Ryan-Davisf563fdc2019-01-31 13:53:59 -05004114 std::stringstream reasonStream = sh::InitializeStream<std::stringstream>();
Olli Etuaho4de340a2016-12-16 09:32:03 +00004115 reasonStream << reason << " '" << index << "'";
4116 std::string token = reasonStream.str();
4117 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004118 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004119 }
4120 return index;
4121}
4122
Jamie Madillb98c3a82015-07-23 14:26:04 -04004123TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4124 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004125 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004126 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004127{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004128 if (baseExpression->isArray())
4129 {
4130 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004131 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004132 }
4133
4134 if (baseExpression->isVector())
4135 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004136 TVector<int> fieldOffsets;
4137 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4138 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004139 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004140 fieldOffsets.resize(1);
4141 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004142 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004143 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4144 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004145
Olli Etuaho765924f2018-01-04 12:48:36 +02004146 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004147 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004148 else if (baseExpression->getBasicType() == EbtStruct)
4149 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304150 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004151 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004152 {
4153 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004154 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004155 }
4156 else
4157 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004158 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004159 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004160 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004161 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004162 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004163 {
4164 fieldFound = true;
4165 break;
4166 }
4167 }
4168 if (fieldFound)
4169 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004170 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004171 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004172 TIntermBinary *node =
4173 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4174 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004175 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004176 }
4177 else
4178 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004179 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004180 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004181 }
4182 }
4183 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004184 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004185 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304186 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004187 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004188 {
4189 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004190 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004191 }
4192 else
4193 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004194 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004195 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004196 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004197 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004198 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004199 {
4200 fieldFound = true;
4201 break;
4202 }
4203 }
4204 if (fieldFound)
4205 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004206 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004207 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004208 TIntermBinary *node =
4209 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4210 node->setLine(dotLocation);
4211 // Indexing interface blocks can never be constant folded.
4212 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004213 }
4214 else
4215 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004216 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004217 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004218 }
4219 }
4220 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004221 else
4222 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004223 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004224 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004225 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004226 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004227 }
4228 else
4229 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304230 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004231 " field selection requires structure, vector, or interface block on left hand "
4232 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004233 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004234 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004235 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004236 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004237}
4238
Olli Etuahofbb1c792018-01-19 16:26:59 +02004239TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004240 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004241{
Jamie Madill2f294c92017-11-20 14:47:26 -05004242 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004243
4244 if (qualifierType == "shared")
4245 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004246 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004247 {
4248 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4249 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004250 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004251 }
4252 else if (qualifierType == "packed")
4253 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004254 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004255 {
4256 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4257 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004258 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004259 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004260 else if (qualifierType == "std430")
4261 {
4262 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4263 qualifier.blockStorage = EbsStd430;
4264 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004265 else if (qualifierType == "std140")
4266 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004267 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004268 }
4269 else if (qualifierType == "row_major")
4270 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004271 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004272 }
4273 else if (qualifierType == "column_major")
4274 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004275 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004276 }
4277 else if (qualifierType == "location")
4278 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004279 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004280 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004281 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004282 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004283 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004284 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4285 {
4286 qualifier.yuv = true;
4287 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004288 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004289 else if (qualifierType == "rgba32f")
4290 {
4291 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4292 qualifier.imageInternalFormat = EiifRGBA32F;
4293 }
4294 else if (qualifierType == "rgba16f")
4295 {
4296 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4297 qualifier.imageInternalFormat = EiifRGBA16F;
4298 }
4299 else if (qualifierType == "r32f")
4300 {
4301 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4302 qualifier.imageInternalFormat = EiifR32F;
4303 }
4304 else if (qualifierType == "rgba8")
4305 {
4306 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4307 qualifier.imageInternalFormat = EiifRGBA8;
4308 }
4309 else if (qualifierType == "rgba8_snorm")
4310 {
4311 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4312 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4313 }
4314 else if (qualifierType == "rgba32i")
4315 {
4316 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4317 qualifier.imageInternalFormat = EiifRGBA32I;
4318 }
4319 else if (qualifierType == "rgba16i")
4320 {
4321 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4322 qualifier.imageInternalFormat = EiifRGBA16I;
4323 }
4324 else if (qualifierType == "rgba8i")
4325 {
4326 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4327 qualifier.imageInternalFormat = EiifRGBA8I;
4328 }
4329 else if (qualifierType == "r32i")
4330 {
4331 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4332 qualifier.imageInternalFormat = EiifR32I;
4333 }
4334 else if (qualifierType == "rgba32ui")
4335 {
4336 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4337 qualifier.imageInternalFormat = EiifRGBA32UI;
4338 }
4339 else if (qualifierType == "rgba16ui")
4340 {
4341 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4342 qualifier.imageInternalFormat = EiifRGBA16UI;
4343 }
4344 else if (qualifierType == "rgba8ui")
4345 {
4346 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4347 qualifier.imageInternalFormat = EiifRGBA8UI;
4348 }
4349 else if (qualifierType == "r32ui")
4350 {
4351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4352 qualifier.imageInternalFormat = EiifR32UI;
4353 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004354 else if (qualifierType == "points" && 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 = EptPoints;
4359 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004360 else if (qualifierType == "lines" && 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 = EptLines;
4365 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004366 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4367 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004368 {
4369 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4370 qualifier.primitiveType = EptLinesAdjacency;
4371 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004372 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4373 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004374 {
4375 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4376 qualifier.primitiveType = EptTriangles;
4377 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004378 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4379 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004380 {
4381 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4382 qualifier.primitiveType = EptTrianglesAdjacency;
4383 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004384 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4385 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004386 {
4387 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4388 qualifier.primitiveType = EptLineStrip;
4389 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004390 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4391 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004392 {
4393 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4394 qualifier.primitiveType = EptTriangleStrip;
4395 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004396
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004397 else
4398 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004399 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004400 }
4401
Jamie Madilla5efff92013-06-06 11:56:47 -04004402 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004403}
4404
Olli Etuahofbb1c792018-01-19 16:26:59 +02004405void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004406 const TSourceLoc &qualifierTypeLine,
4407 int intValue,
4408 const TSourceLoc &intValueLine,
4409 const std::string &intValueString,
4410 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004411 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004412{
Olli Etuaho856c4972016-08-08 11:38:39 +03004413 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004414 if (intValue < 1)
4415 {
Jonah Ryan-Davisf563fdc2019-01-31 13:53:59 -05004416 std::stringstream reasonStream = sh::InitializeStream<std::stringstream>();
Olli Etuaho4de340a2016-12-16 09:32:03 +00004417 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4418 std::string reason = reasonStream.str();
4419 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004420 }
4421 (*localSize)[index] = intValue;
4422}
4423
Olli Etuaho09b04a22016-12-15 13:30:26 +00004424void TParseContext::parseNumViews(int intValue,
4425 const TSourceLoc &intValueLine,
4426 const std::string &intValueString,
4427 int *numViews)
4428{
4429 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4430 // specification.
4431 if (intValue < 1)
4432 {
4433 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4434 }
4435 *numViews = intValue;
4436}
4437
Shaob5cc1192017-07-06 10:47:20 +08004438void TParseContext::parseInvocations(int intValue,
4439 const TSourceLoc &intValueLine,
4440 const std::string &intValueString,
4441 int *numInvocations)
4442{
4443 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4444 // it doesn't make sense to accept invocations <= 0.
4445 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4446 {
4447 error(intValueLine,
4448 "out of range: invocations must be in the range of [1, "
4449 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4450 intValueString.c_str());
4451 }
4452 else
4453 {
4454 *numInvocations = intValue;
4455 }
4456}
4457
4458void TParseContext::parseMaxVertices(int intValue,
4459 const TSourceLoc &intValueLine,
4460 const std::string &intValueString,
4461 int *maxVertices)
4462{
4463 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4464 // it doesn't make sense to accept max_vertices < 0.
4465 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4466 {
4467 error(
4468 intValueLine,
4469 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4470 intValueString.c_str());
4471 }
4472 else
4473 {
4474 *maxVertices = intValue;
4475 }
4476}
4477
Olli Etuahoa78092c2018-09-26 14:16:13 +03004478void TParseContext::parseIndexLayoutQualifier(int intValue,
4479 const TSourceLoc &intValueLine,
4480 const std::string &intValueString,
4481 int *index)
4482{
4483 // EXT_blend_func_extended specifies that most validation should happen at link time, but since
4484 // we're validating output variable locations at compile time, it makes sense to validate that
4485 // index is 0 or 1 also at compile time. Also since we use "-1" as a placeholder for unspecified
4486 // index, we can't accept it here.
4487 if (intValue < 0 || intValue > 1)
4488 {
4489 error(intValueLine, "out of range: index layout qualifier can only be 0 or 1",
4490 intValueString.c_str());
4491 }
4492 else
4493 {
4494 *index = intValue;
4495 }
4496}
4497
Olli Etuahofbb1c792018-01-19 16:26:59 +02004498TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004499 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004500 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304501 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004502{
Jamie Madill2f294c92017-11-20 14:47:26 -05004503 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004504
Martin Radev802abe02016-08-04 17:48:32 +03004505 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004506
Martin Radev802abe02016-08-04 17:48:32 +03004507 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004508 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004509 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004510 if (intValue < 0)
4511 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004512 error(intValueLine, "out of range: location must be non-negative",
4513 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004514 }
4515 else
4516 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004517 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004518 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004519 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004520 }
Olli Etuaho43364892017-02-13 16:00:12 +00004521 else if (qualifierType == "binding")
4522 {
4523 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4524 if (intValue < 0)
4525 {
4526 error(intValueLine, "out of range: binding must be non-negative",
4527 intValueString.c_str());
4528 }
4529 else
4530 {
4531 qualifier.binding = intValue;
4532 }
4533 }
jchen104cdac9e2017-05-08 11:01:20 +08004534 else if (qualifierType == "offset")
4535 {
4536 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4537 if (intValue < 0)
4538 {
4539 error(intValueLine, "out of range: offset must be non-negative",
4540 intValueString.c_str());
4541 }
4542 else
4543 {
4544 qualifier.offset = intValue;
4545 }
4546 }
Martin Radev802abe02016-08-04 17:48:32 +03004547 else if (qualifierType == "local_size_x")
4548 {
4549 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4550 &qualifier.localSize);
4551 }
4552 else if (qualifierType == "local_size_y")
4553 {
4554 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4555 &qualifier.localSize);
4556 }
4557 else if (qualifierType == "local_size_z")
4558 {
4559 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4560 &qualifier.localSize);
4561 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004562 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004563 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004564 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4565 {
4566 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4567 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004568 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004569 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4570 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004571 {
4572 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4573 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004574 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4575 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004576 {
4577 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4578 }
Olli Etuahoa78092c2018-09-26 14:16:13 +03004579 else if (qualifierType == "index" && mShaderType == GL_FRAGMENT_SHADER &&
4580 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_blend_func_extended))
4581 {
4582 parseIndexLayoutQualifier(intValue, intValueLine, intValueString, &qualifier.index);
4583 }
Martin Radev802abe02016-08-04 17:48:32 +03004584 else
4585 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004586 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004587 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004588
Jamie Madilla5efff92013-06-06 11:56:47 -04004589 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004590}
4591
Olli Etuaho613b9592016-09-05 12:05:53 +03004592TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4593{
4594 return new TTypeQualifierBuilder(
4595 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4596 mShaderVersion);
4597}
4598
Olli Etuahocce89652017-06-19 16:04:09 +03004599TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4600 const TSourceLoc &loc)
4601{
4602 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4603 return new TStorageQualifierWrapper(qualifier, loc);
4604}
4605
4606TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4607{
4608 if (getShaderType() == GL_VERTEX_SHADER)
4609 {
4610 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4611 }
4612 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4613}
4614
4615TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4616{
4617 if (declaringFunction())
4618 {
4619 return new TStorageQualifierWrapper(EvqIn, loc);
4620 }
Shaob5cc1192017-07-06 10:47:20 +08004621
4622 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004623 {
Shaob5cc1192017-07-06 10:47:20 +08004624 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004625 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004626 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004627 {
4628 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4629 }
4630 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004631 }
Shaob5cc1192017-07-06 10:47:20 +08004632 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004633 {
Shaob5cc1192017-07-06 10:47:20 +08004634 if (mShaderVersion < 300)
4635 {
4636 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4637 }
4638 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004639 }
Shaob5cc1192017-07-06 10:47:20 +08004640 case GL_COMPUTE_SHADER:
4641 {
4642 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4643 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004644 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004645 {
4646 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4647 }
4648 default:
4649 {
4650 UNREACHABLE();
4651 return new TStorageQualifierWrapper(EvqLast, loc);
4652 }
Olli Etuahocce89652017-06-19 16:04:09 +03004653 }
Olli Etuahocce89652017-06-19 16:04:09 +03004654}
4655
4656TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4657{
4658 if (declaringFunction())
4659 {
4660 return new TStorageQualifierWrapper(EvqOut, loc);
4661 }
Shaob5cc1192017-07-06 10:47:20 +08004662 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004663 {
Shaob5cc1192017-07-06 10:47:20 +08004664 case GL_VERTEX_SHADER:
4665 {
4666 if (mShaderVersion < 300)
4667 {
4668 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4669 }
4670 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4671 }
4672 case GL_FRAGMENT_SHADER:
4673 {
4674 if (mShaderVersion < 300)
4675 {
4676 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4677 }
4678 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4679 }
4680 case GL_COMPUTE_SHADER:
4681 {
4682 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4683 return new TStorageQualifierWrapper(EvqLast, loc);
4684 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004685 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004686 {
4687 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4688 }
4689 default:
4690 {
4691 UNREACHABLE();
4692 return new TStorageQualifierWrapper(EvqLast, loc);
4693 }
Olli Etuahocce89652017-06-19 16:04:09 +03004694 }
Olli Etuahocce89652017-06-19 16:04:09 +03004695}
4696
4697TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4698{
4699 if (!declaringFunction())
4700 {
4701 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4702 }
4703 return new TStorageQualifierWrapper(EvqInOut, loc);
4704}
4705
Jamie Madillb98c3a82015-07-23 14:26:04 -04004706TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004707 TLayoutQualifier rightQualifier,
4708 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004709{
Martin Radevc28888b2016-07-22 15:27:42 +03004710 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004711 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004712}
4713
Olli Etuahofbb1c792018-01-19 16:26:59 +02004714TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4715 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004716{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004717 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004718 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004719}
4720
Olli Etuahofbb1c792018-01-19 16:26:59 +02004721TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004722 const TSourceLoc &loc,
4723 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004724{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004725 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004726 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004727}
4728
Olli Etuaho722bfb52017-10-26 17:00:11 +03004729void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4730 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004731 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004732 const TSourceLoc &location)
4733{
4734 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4735 {
4736 if ((*fieldIter)->name() == name)
4737 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004738 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004739 }
4740 }
4741}
4742
4743TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4744{
4745 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4746 ++fieldIter)
4747 {
4748 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4749 location);
4750 }
4751 return fields;
4752}
4753
Olli Etuaho4de340a2016-12-16 09:32:03 +00004754TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4755 const TFieldList *newlyAddedFields,
4756 const TSourceLoc &location)
4757{
4758 for (TField *field : *newlyAddedFields)
4759 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004760 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4761 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004762 processedFields->push_back(field);
4763 }
4764 return processedFields;
4765}
4766
Martin Radev70866b82016-07-22 15:27:42 +03004767TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4768 const TTypeQualifierBuilder &typeQualifierBuilder,
4769 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004770 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004771{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004772 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004773
Martin Radev70866b82016-07-22 15:27:42 +03004774 typeSpecifier->qualifier = typeQualifier.qualifier;
4775 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004776 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004777 typeSpecifier->invariant = typeQualifier.invariant;
4778 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304779 {
Martin Radev70866b82016-07-22 15:27:42 +03004780 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004781 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004782 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004783}
4784
Jamie Madillb98c3a82015-07-23 14:26:04 -04004785TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004786 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004787{
Martin Radev4a9cd802016-09-01 16:51:51 +03004788 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4789 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004790
Olli Etuahofbb1c792018-01-19 16:26:59 +02004791 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004792 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004793
Martin Radev4a9cd802016-09-01 16:51:51 +03004794 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004795
Olli Etuahod5f44c92017-11-29 17:15:40 +02004796 TFieldList *fieldList = new TFieldList();
4797
4798 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304799 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004800 TType *type = new TType(typeSpecifier);
4801 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304802 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004803 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004804 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004805 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004806 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004807
Jamie Madillf5557ac2018-06-15 09:46:58 -04004808 TField *field =
4809 new TField(type, declarator->name(), declarator->line(), SymbolType::UserDefined);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004810 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4811 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004812 }
4813
Olli Etuahod5f44c92017-11-29 17:15:40 +02004814 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004815}
4816
Martin Radev4a9cd802016-09-01 16:51:51 +03004817TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4818 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004819 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004820 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004821{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004822 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004823 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004824 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004825 structSymbolType = SymbolType::Empty;
4826 }
4827 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004828
Jamie Madill9b820842015-02-12 10:40:10 -05004829 // Store a bool in the struct if we're at global scope, to allow us to
4830 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004831 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004832
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004833 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004834 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004835 checkIsNotReserved(nameLine, structName);
Olli Etuaho437664b2018-02-28 15:38:14 +02004836 if (!symbolTable.declare(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304837 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004838 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004839 }
4840 }
4841
4842 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004843 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004844 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004845 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004846 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004847 switch (qualifier)
4848 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004849 case EvqGlobal:
4850 case EvqTemporary:
4851 break;
4852 default:
4853 error(field.line(), "invalid qualifier on struct member",
4854 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004855 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004856 }
Martin Radev70866b82016-07-22 15:27:42 +03004857 if (field.type()->isInvariant())
4858 {
4859 error(field.line(), "invalid qualifier on struct member", "invariant");
4860 }
jchen104cdac9e2017-05-08 11:01:20 +08004861 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4862 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004863 {
4864 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4865 }
4866
Olli Etuahoebee5b32017-11-23 12:56:32 +02004867 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004868 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004869
Olli Etuaho43364892017-02-13 16:00:12 +00004870 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4871
Olli Etuahoa78092c2018-09-26 14:16:13 +03004872 checkIndexIsNotSpecified(field.line(), field.type()->getLayoutQualifier().index);
4873
Olli Etuaho43364892017-02-13 16:00:12 +00004874 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004875
4876 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004877 }
4878
Martin Radev4a9cd802016-09-01 16:51:51 +03004879 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004880 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004881 exitStructDeclaration();
4882
Martin Radev4a9cd802016-09-01 16:51:51 +03004883 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004884}
4885
Jamie Madillb98c3a82015-07-23 14:26:04 -04004886TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004887 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004888 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004889{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004890 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004891 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004892 init->isVector())
4893 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004894 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4895 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004896 return nullptr;
4897 }
4898
Olli Etuaho923ecef2017-10-11 12:01:38 +03004899 ASSERT(statementList);
Olli Etuahod05f9642018-03-05 12:13:26 +02004900 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004901 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004902 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004903 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004904 }
4905
Olli Etuaho94bbed12018-03-20 14:44:53 +02004906 markStaticReadIfSymbol(init);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004907 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4908 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004909 return node;
4910}
4911
4912TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4913{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004914 if (mSwitchNestingLevel == 0)
4915 {
4916 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004917 return nullptr;
4918 }
4919 if (condition == nullptr)
4920 {
4921 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004922 return nullptr;
4923 }
4924 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004925 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004926 {
4927 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004928 }
4929 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho1dfd8ae2018-10-01 15:59:59 +03004930 // ANGLE should be able to fold any EvqConst expressions resulting in an integer - but to be
4931 // safe against corner cases we still check for conditionConst. Some interpretations of the
4932 // spec have allowed constant expressions with side effects - like array length() method on a
4933 // non-constant array.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004934 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004935 {
4936 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004937 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004938 TIntermCase *node = new TIntermCase(condition);
4939 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004940 return node;
4941}
4942
4943TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4944{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004945 if (mSwitchNestingLevel == 0)
4946 {
4947 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004948 return nullptr;
4949 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004950 TIntermCase *node = new TIntermCase(nullptr);
4951 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004952 return node;
4953}
4954
Jamie Madillb98c3a82015-07-23 14:26:04 -04004955TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4956 TIntermTyped *child,
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004957 const TSourceLoc &loc,
4958 const TFunction *func)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004959{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004960 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004961
4962 switch (op)
4963 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004964 case EOpLogicalNot:
4965 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4966 child->isVector())
4967 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004968 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004969 return nullptr;
4970 }
4971 break;
4972 case EOpBitwiseNot:
4973 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4974 child->isMatrix() || child->isArray())
4975 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004976 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004977 return nullptr;
4978 }
4979 break;
4980 case EOpPostIncrement:
4981 case EOpPreIncrement:
4982 case EOpPostDecrement:
4983 case EOpPreDecrement:
4984 case EOpNegative:
4985 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004986 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4987 child->getBasicType() == EbtBool || child->isArray() ||
Geoff Lang6aab06e2018-11-20 14:04:11 -05004988 child->getBasicType() == EbtVoid || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004989 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004990 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004991 return nullptr;
4992 }
Nico Weber41b072b2018-02-09 10:01:32 -05004993 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004994 // Operators for built-ins are already type checked against their prototype.
4995 default:
4996 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004997 }
4998
Jiajia Qinbc585152017-06-23 15:42:17 +08004999 if (child->getMemoryQualifier().writeonly)
5000 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005001 unaryOpError(loc, GetOperatorString(op), child->getType());
Jiajia Qinbc585152017-06-23 15:42:17 +08005002 return nullptr;
5003 }
5004
Olli Etuaho94bbed12018-03-20 14:44:53 +02005005 markStaticReadIfSymbol(child);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005006 TIntermUnary *node = new TIntermUnary(op, child, func);
Olli Etuahof119a262016-08-19 15:54:22 +03005007 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03005008
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005009 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02005010}
5011
Olli Etuaho09b22472015-02-11 11:47:26 +02005012TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
5013{
Olli Etuahocce89652017-06-19 16:04:09 +03005014 ASSERT(op != EOpNull);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005015 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02005016 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005017 {
Olli Etuaho09b22472015-02-11 11:47:26 +02005018 return child;
5019 }
5020 return node;
5021}
5022
Jamie Madillb98c3a82015-07-23 14:26:04 -04005023TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
5024 TIntermTyped *child,
5025 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005026{
Olli Etuaho856c4972016-08-08 11:38:39 +03005027 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02005028 return addUnaryMath(op, child, loc);
5029}
5030
Olli Etuaho765924f2018-01-04 12:48:36 +02005031TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
5032{
5033 // If we can, we should return the folded version of the expression for subsequent parsing. This
5034 // enables folding the containing expression during parsing as well, instead of the separate
5035 // FoldExpressions() step where folding nested expressions requires multiple full AST
5036 // traversals.
5037
5038 // Even if folding fails the fold() functions return some node representing the expression,
5039 // typically the original node. So "folded" can be assumed to be non-null.
5040 TIntermTyped *folded = expression->fold(mDiagnostics);
5041 ASSERT(folded != nullptr);
5042 if (folded->getQualifier() == expression->getQualifier())
5043 {
5044 // We need this expression to have the correct qualifier when validating the consuming
5045 // expression. So we can only return the folded node from here in case it has the same
5046 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
5047 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
5048 // 1. (true ? 1.0 : non_constant)
5049 // 2. (non_constant, 1.0)
5050 return folded;
5051 }
5052 return expression;
5053}
5054
Jamie Madillb98c3a82015-07-23 14:26:04 -04005055bool TParseContext::binaryOpCommonCheck(TOperator op,
5056 TIntermTyped *left,
5057 TIntermTyped *right,
5058 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005059{
jchen10b4cf5652017-05-05 18:51:17 +08005060 // Check opaque types are not allowed to be operands in expressions other than array indexing
5061 // and structure member selection.
5062 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
5063 {
5064 switch (op)
5065 {
5066 case EOpIndexDirect:
5067 case EOpIndexIndirect:
5068 break;
jchen10b4cf5652017-05-05 18:51:17 +08005069
5070 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05005071 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08005072 error(loc, "Invalid operation for variables with an opaque type",
5073 GetOperatorString(op));
5074 return false;
5075 }
5076 }
jchen10cc2a10e2017-05-03 14:05:12 +08005077
Jiajia Qinbc585152017-06-23 15:42:17 +08005078 if (right->getMemoryQualifier().writeonly)
5079 {
5080 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5081 return false;
5082 }
5083
5084 if (left->getMemoryQualifier().writeonly)
5085 {
5086 switch (op)
5087 {
5088 case EOpAssign:
5089 case EOpInitialize:
5090 case EOpIndexDirect:
5091 case EOpIndexIndirect:
5092 case EOpIndexDirectStruct:
5093 case EOpIndexDirectInterfaceBlock:
5094 break;
5095 default:
5096 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5097 return false;
5098 }
5099 }
5100
Olli Etuaho244be012016-08-18 15:26:02 +03005101 if (left->getType().getStruct() || right->getType().getStruct())
5102 {
5103 switch (op)
5104 {
5105 case EOpIndexDirectStruct:
5106 ASSERT(left->getType().getStruct());
5107 break;
5108 case EOpEqual:
5109 case EOpNotEqual:
5110 case EOpAssign:
5111 case EOpInitialize:
5112 if (left->getType() != right->getType())
5113 {
5114 return false;
5115 }
5116 break;
5117 default:
5118 error(loc, "Invalid operation for structs", GetOperatorString(op));
5119 return false;
5120 }
5121 }
5122
Olli Etuaho94050052017-05-08 14:17:44 +03005123 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5124 {
5125 switch (op)
5126 {
5127 case EOpIndexDirectInterfaceBlock:
5128 ASSERT(left->getType().getInterfaceBlock());
5129 break;
5130 default:
5131 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5132 return false;
5133 }
5134 }
5135
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005136 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005137 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005138 error(loc, "array / non-array mismatch", GetOperatorString(op));
5139 return false;
5140 }
5141
5142 if (left->isArray())
5143 {
5144 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005145 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005146 {
5147 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5148 return false;
5149 }
5150
Olli Etuahoe79904c2015-03-18 16:56:42 +02005151 switch (op)
5152 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005153 case EOpEqual:
5154 case EOpNotEqual:
5155 case EOpAssign:
5156 case EOpInitialize:
5157 break;
5158 default:
5159 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5160 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005161 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005162 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005163 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005164 {
5165 error(loc, "array size mismatch", GetOperatorString(op));
5166 return false;
5167 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005168 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005169
5170 // Check ops which require integer / ivec parameters
5171 bool isBitShift = false;
5172 switch (op)
5173 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005174 case EOpBitShiftLeft:
5175 case EOpBitShiftRight:
5176 case EOpBitShiftLeftAssign:
5177 case EOpBitShiftRightAssign:
5178 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5179 // check that the basic type is an integer type.
5180 isBitShift = true;
5181 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5182 {
5183 return false;
5184 }
5185 break;
5186 case EOpBitwiseAnd:
5187 case EOpBitwiseXor:
5188 case EOpBitwiseOr:
5189 case EOpBitwiseAndAssign:
5190 case EOpBitwiseXorAssign:
5191 case EOpBitwiseOrAssign:
5192 // It is enough to check the type of only one operand, since later it
5193 // is checked that the operand types match.
5194 if (!IsInteger(left->getBasicType()))
5195 {
5196 return false;
5197 }
5198 break;
5199 default:
5200 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005201 }
5202
5203 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5204 // So the basic type should usually match.
5205 if (!isBitShift && left->getBasicType() != right->getBasicType())
5206 {
5207 return false;
5208 }
5209
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005210 // Check that:
5211 // 1. Type sizes match exactly on ops that require that.
5212 // 2. Restrictions for structs that contain arrays or samplers are respected.
5213 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005214 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005215 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005216 case EOpAssign:
5217 case EOpInitialize:
5218 case EOpEqual:
5219 case EOpNotEqual:
5220 // ESSL 1.00 sections 5.7, 5.8, 5.9
5221 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5222 {
5223 error(loc, "undefined operation for structs containing arrays",
5224 GetOperatorString(op));
5225 return false;
5226 }
5227 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5228 // we interpret the spec so that this extends to structs containing samplers,
5229 // similarly to ESSL 1.00 spec.
5230 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5231 left->getType().isStructureContainingSamplers())
5232 {
5233 error(loc, "undefined operation for structs containing samplers",
5234 GetOperatorString(op));
5235 return false;
5236 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005237
Olli Etuahoe1805592017-01-02 16:41:20 +00005238 if ((left->getNominalSize() != right->getNominalSize()) ||
5239 (left->getSecondarySize() != right->getSecondarySize()))
5240 {
5241 error(loc, "dimension mismatch", GetOperatorString(op));
5242 return false;
5243 }
5244 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005245 case EOpLessThan:
5246 case EOpGreaterThan:
5247 case EOpLessThanEqual:
5248 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005249 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005250 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005251 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005252 return false;
5253 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005254 break;
5255 case EOpAdd:
5256 case EOpSub:
5257 case EOpDiv:
5258 case EOpIMod:
5259 case EOpBitShiftLeft:
5260 case EOpBitShiftRight:
5261 case EOpBitwiseAnd:
5262 case EOpBitwiseXor:
5263 case EOpBitwiseOr:
5264 case EOpAddAssign:
5265 case EOpSubAssign:
5266 case EOpDivAssign:
5267 case EOpIModAssign:
5268 case EOpBitShiftLeftAssign:
5269 case EOpBitShiftRightAssign:
5270 case EOpBitwiseAndAssign:
5271 case EOpBitwiseXorAssign:
5272 case EOpBitwiseOrAssign:
5273 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5274 {
5275 return false;
5276 }
5277
5278 // Are the sizes compatible?
5279 if (left->getNominalSize() != right->getNominalSize() ||
5280 left->getSecondarySize() != right->getSecondarySize())
5281 {
5282 // If the nominal sizes of operands do not match:
5283 // One of them must be a scalar.
5284 if (!left->isScalar() && !right->isScalar())
5285 return false;
5286
5287 // In the case of compound assignment other than multiply-assign,
5288 // the right side needs to be a scalar. Otherwise a vector/matrix
5289 // would be assigned to a scalar. A scalar can't be shifted by a
5290 // vector either.
5291 if (!right->isScalar() &&
5292 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5293 return false;
5294 }
5295 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005296 default:
5297 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005298 }
5299
Olli Etuahod6b14282015-03-17 14:31:35 +02005300 return true;
5301}
5302
Olli Etuaho1dded802016-08-18 18:13:13 +03005303bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5304 const TType &left,
5305 const TType &right)
5306{
5307 switch (op)
5308 {
5309 case EOpMul:
5310 case EOpMulAssign:
5311 return left.getNominalSize() == right.getNominalSize() &&
5312 left.getSecondarySize() == right.getSecondarySize();
5313 case EOpVectorTimesScalar:
5314 return true;
5315 case EOpVectorTimesScalarAssign:
5316 ASSERT(!left.isMatrix() && !right.isMatrix());
5317 return left.isVector() && !right.isVector();
5318 case EOpVectorTimesMatrix:
5319 return left.getNominalSize() == right.getRows();
5320 case EOpVectorTimesMatrixAssign:
5321 ASSERT(!left.isMatrix() && right.isMatrix());
5322 return left.isVector() && left.getNominalSize() == right.getRows() &&
5323 left.getNominalSize() == right.getCols();
5324 case EOpMatrixTimesVector:
5325 return left.getCols() == right.getNominalSize();
5326 case EOpMatrixTimesScalar:
5327 return true;
5328 case EOpMatrixTimesScalarAssign:
5329 ASSERT(left.isMatrix() && !right.isMatrix());
5330 return !right.isVector();
5331 case EOpMatrixTimesMatrix:
5332 return left.getCols() == right.getRows();
5333 case EOpMatrixTimesMatrixAssign:
5334 ASSERT(left.isMatrix() && right.isMatrix());
5335 // We need to check two things:
5336 // 1. The matrix multiplication step is valid.
5337 // 2. The result will have the same number of columns as the lvalue.
5338 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5339
5340 default:
5341 UNREACHABLE();
5342 return false;
5343 }
5344}
5345
Jamie Madillb98c3a82015-07-23 14:26:04 -04005346TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5347 TIntermTyped *left,
5348 TIntermTyped *right,
5349 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005350{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005351 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005352 return nullptr;
5353
Olli Etuahofc1806e2015-03-17 13:03:11 +02005354 switch (op)
5355 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005356 case EOpEqual:
5357 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005358 case EOpLessThan:
5359 case EOpGreaterThan:
5360 case EOpLessThanEqual:
5361 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005362 break;
5363 case EOpLogicalOr:
5364 case EOpLogicalXor:
5365 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005366 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5367 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005368 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005369 {
5370 return nullptr;
5371 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005372 // Basic types matching should have been already checked.
5373 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005374 break;
5375 case EOpAdd:
5376 case EOpSub:
5377 case EOpDiv:
5378 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005379 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5380 !right->getType().getStruct());
5381 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005382 {
5383 return nullptr;
5384 }
5385 break;
5386 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005387 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5388 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005389 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005390 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005391 {
5392 return nullptr;
5393 }
5394 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005395 default:
5396 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005397 }
5398
Olli Etuaho1dded802016-08-18 18:13:13 +03005399 if (op == EOpMul)
5400 {
5401 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5402 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5403 {
5404 return nullptr;
5405 }
5406 }
5407
Olli Etuaho3fdec912016-08-18 15:08:06 +03005408 TIntermBinary *node = new TIntermBinary(op, left, right);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005409 ASSERT(op != EOpAssign);
5410 markStaticReadIfSymbol(left);
5411 markStaticReadIfSymbol(right);
Olli Etuaho3fdec912016-08-18 15:08:06 +03005412 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005413 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005414}
5415
Jamie Madillb98c3a82015-07-23 14:26:04 -04005416TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5417 TIntermTyped *left,
5418 TIntermTyped *right,
5419 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005420{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005421 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005422 if (node == 0)
5423 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005424 binaryOpError(loc, GetOperatorString(op), left->getType(), right->getType());
Olli Etuaho09b22472015-02-11 11:47:26 +02005425 return left;
5426 }
5427 return node;
5428}
5429
Jamie Madillb98c3a82015-07-23 14:26:04 -04005430TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5431 TIntermTyped *left,
5432 TIntermTyped *right,
5433 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005434{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005435 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005436 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005437 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005438 binaryOpError(loc, GetOperatorString(op), left->getType(), right->getType());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005439 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005440 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005441 }
5442 return node;
5443}
5444
Jamie Madillb98c3a82015-07-23 14:26:04 -04005445TIntermTyped *TParseContext::addAssign(TOperator op,
5446 TIntermTyped *left,
5447 TIntermTyped *right,
5448 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005449{
Olli Etuahocce89652017-06-19 16:04:09 +03005450 checkCanBeLValue(loc, "assign", left);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005451 TIntermBinary *node = nullptr;
5452 if (binaryOpCommonCheck(op, left, right, loc))
5453 {
5454 if (op == EOpMulAssign)
5455 {
5456 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5457 if (isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5458 {
5459 node = new TIntermBinary(op, left, right);
5460 }
5461 }
5462 else
5463 {
5464 node = new TIntermBinary(op, left, right);
5465 }
5466 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005467 if (node == nullptr)
5468 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005469 assignError(loc, "assign", left->getType(), right->getType());
Olli Etuahod6b14282015-03-17 14:31:35 +02005470 return left;
5471 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02005472 if (op != EOpAssign)
5473 {
5474 markStaticReadIfSymbol(left);
5475 }
5476 markStaticReadIfSymbol(right);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005477 node->setLine(loc);
Olli Etuahod6b14282015-03-17 14:31:35 +02005478 return node;
5479}
5480
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005481TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5482 TIntermTyped *right,
5483 const TSourceLoc &loc)
5484{
Corentin Wallez0d959252016-07-12 17:26:32 -04005485 // WebGL2 section 5.26, the following results in an error:
5486 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005487 if (mShaderSpec == SH_WEBGL2_SPEC &&
5488 (left->isArray() || left->getBasicType() == EbtVoid ||
5489 left->getType().isStructureContainingArrays() || right->isArray() ||
5490 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005491 {
5492 error(loc,
5493 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5494 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005495 }
5496
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005497 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005498 markStaticReadIfSymbol(left);
5499 markStaticReadIfSymbol(right);
5500 commaNode->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005501
5502 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005503}
5504
Olli Etuaho49300862015-02-20 14:54:49 +02005505TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5506{
5507 switch (op)
5508 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005509 case EOpContinue:
5510 if (mLoopNestingLevel <= 0)
5511 {
5512 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005513 }
5514 break;
5515 case EOpBreak:
5516 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5517 {
5518 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005519 }
5520 break;
5521 case EOpReturn:
5522 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5523 {
5524 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005525 }
5526 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005527 case EOpKill:
5528 if (mShaderType != GL_FRAGMENT_SHADER)
5529 {
5530 error(loc, "discard supported in fragment shaders only", "discard");
5531 }
5532 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005533 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005534 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005535 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005536 }
Olli Etuahocce89652017-06-19 16:04:09 +03005537 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005538}
5539
Jamie Madillb98c3a82015-07-23 14:26:04 -04005540TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005541 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005542 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005543{
Olli Etuahocce89652017-06-19 16:04:09 +03005544 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005545 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02005546 markStaticReadIfSymbol(expression);
Olli Etuahocce89652017-06-19 16:04:09 +03005547 ASSERT(op == EOpReturn);
5548 mFunctionReturnsValue = true;
5549 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5550 {
5551 error(loc, "void function cannot return a value", "return");
5552 }
5553 else if (*mCurrentFunctionType != expression->getType())
5554 {
5555 error(loc, "function return is not matching type:", "return");
5556 }
Olli Etuaho49300862015-02-20 14:54:49 +02005557 }
Olli Etuahocce89652017-06-19 16:04:09 +03005558 TIntermBranch *node = new TIntermBranch(op, expression);
5559 node->setLine(loc);
5560 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005561}
5562
Olli Etuaho94bbed12018-03-20 14:44:53 +02005563void TParseContext::appendStatement(TIntermBlock *block, TIntermNode *statement)
5564{
5565 if (statement != nullptr)
5566 {
5567 markStaticReadIfSymbol(statement);
5568 block->appendStatement(statement);
5569 }
5570}
5571
Martin Radev84aa2dc2017-09-11 15:51:02 +03005572void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5573{
5574 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005575 const TFunction *func = functionCall->getFunction();
5576 if (BuiltInGroup::isTextureGather(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005577 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005578 bool isTextureGatherOffset = BuiltInGroup::isTextureGatherOffset(func);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005579 TIntermNode *componentNode = nullptr;
5580 TIntermSequence *arguments = functionCall->getSequence();
5581 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5582 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5583 ASSERT(sampler != nullptr);
5584 switch (sampler->getBasicType())
5585 {
5586 case EbtSampler2D:
5587 case EbtISampler2D:
5588 case EbtUSampler2D:
5589 case EbtSampler2DArray:
5590 case EbtISampler2DArray:
5591 case EbtUSampler2DArray:
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005592 if ((!isTextureGatherOffset && arguments->size() == 3u) ||
Martin Radev84aa2dc2017-09-11 15:51:02 +03005593 (isTextureGatherOffset && arguments->size() == 4u))
5594 {
5595 componentNode = arguments->back();
5596 }
5597 break;
5598 case EbtSamplerCube:
5599 case EbtISamplerCube:
5600 case EbtUSamplerCube:
5601 ASSERT(!isTextureGatherOffset);
5602 if (arguments->size() == 3u)
5603 {
5604 componentNode = arguments->back();
5605 }
5606 break;
5607 case EbtSampler2DShadow:
5608 case EbtSampler2DArrayShadow:
5609 case EbtSamplerCubeShadow:
5610 break;
5611 default:
5612 UNREACHABLE();
5613 break;
5614 }
5615 if (componentNode)
5616 {
5617 const TIntermConstantUnion *componentConstantUnion =
5618 componentNode->getAsConstantUnion();
5619 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5620 {
5621 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005622 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005623 }
5624 else
5625 {
5626 int component = componentConstantUnion->getIConst(0);
5627 if (component < 0 || component > 3)
5628 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005629 error(functionCall->getLine(), "Component must be in the range [0;3]",
5630 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005631 }
5632 }
5633 }
5634 }
5635}
5636
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005637void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5638{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005639 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005640 const TFunction *func = functionCall->getFunction();
Jamie Madill50cf2be2018-06-15 09:46:57 -04005641 TIntermNode *offset = nullptr;
5642 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005643 bool useTextureGatherOffsetConstraints = false;
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005644 if (BuiltInGroup::isTextureOffsetNoBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005645 {
5646 offset = arguments->back();
5647 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005648 else if (BuiltInGroup::isTextureOffsetBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005649 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005650 // A bias parameter follows the offset parameter.
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005651 ASSERT(arguments->size() >= 3);
5652 offset = (*arguments)[2];
5653 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005654 else if (BuiltInGroup::isTextureGatherOffset(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005655 {
5656 ASSERT(arguments->size() >= 3u);
5657 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5658 ASSERT(sampler != nullptr);
5659 switch (sampler->getBasicType())
5660 {
5661 case EbtSampler2D:
5662 case EbtISampler2D:
5663 case EbtUSampler2D:
5664 case EbtSampler2DArray:
5665 case EbtISampler2DArray:
5666 case EbtUSampler2DArray:
5667 offset = (*arguments)[2];
5668 break;
5669 case EbtSampler2DShadow:
5670 case EbtSampler2DArrayShadow:
5671 offset = (*arguments)[3];
5672 break;
5673 default:
5674 UNREACHABLE();
5675 break;
5676 }
5677 useTextureGatherOffsetConstraints = true;
5678 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005679 if (offset != nullptr)
5680 {
5681 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5682 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5683 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005684 error(functionCall->getLine(), "Texture offset must be a constant expression",
5685 func->name());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005686 }
5687 else
5688 {
5689 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5690 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005691 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005692 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5693 : mMinProgramTexelOffset;
5694 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5695 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005696 for (size_t i = 0u; i < size; ++i)
5697 {
5698 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005699 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005700 {
Jonah Ryan-Davisf563fdc2019-01-31 13:53:59 -05005701 std::stringstream tokenStream = sh::InitializeStream<std::stringstream>();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005702 tokenStream << offsetValue;
5703 std::string token = tokenStream.str();
5704 error(offset->getLine(), "Texture offset value out of valid range",
5705 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005706 }
5707 }
5708 }
5709 }
5710}
5711
Jiajia Qina3106c52017-11-03 09:39:39 +08005712void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5713{
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005714 const TFunction *func = functionCall->getFunction();
5715 if (BuiltInGroup::isAtomicMemory(func))
Jiajia Qina3106c52017-11-03 09:39:39 +08005716 {
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005717 ASSERT(IsAtomicFunction(functionCall->getOp()));
Jiajia Qina3106c52017-11-03 09:39:39 +08005718 TIntermSequence *arguments = functionCall->getSequence();
5719 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5720
5721 if (IsBufferOrSharedVariable(memNode))
5722 {
5723 return;
5724 }
5725
5726 while (memNode->getAsBinaryNode())
5727 {
5728 memNode = memNode->getAsBinaryNode()->getLeft();
5729 if (IsBufferOrSharedVariable(memNode))
5730 {
5731 return;
5732 }
5733 }
5734
5735 error(memNode->getLine(),
5736 "The value passed to the mem argument of an atomic memory function does not "
5737 "correspond to a buffer or shared variable.",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005738 func->name());
Jiajia Qina3106c52017-11-03 09:39:39 +08005739 }
5740}
5741
Martin Radev2cc85b32016-08-05 16:22:53 +03005742// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5743void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5744{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005745 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005746
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005747 const TFunction *func = functionCall->getFunction();
5748
5749 if (BuiltInGroup::isImage(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005750 {
5751 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005752 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005753
Olli Etuaho485eefd2017-02-14 17:40:06 +00005754 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005755
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005756 if (BuiltInGroup::isImageStore(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005757 {
5758 if (memoryQualifier.readonly)
5759 {
5760 error(imageNode->getLine(),
5761 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005762 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005763 }
5764 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005765 else if (BuiltInGroup::isImageLoad(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005766 {
5767 if (memoryQualifier.writeonly)
5768 {
5769 error(imageNode->getLine(),
5770 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005771 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005772 }
5773 }
5774 }
5775}
5776
5777// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5778void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5779 const TFunction *functionDefinition,
5780 const TIntermAggregate *functionCall)
5781{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005782 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005783
5784 const TIntermSequence &arguments = *functionCall->getSequence();
5785
5786 ASSERT(functionDefinition->getParamCount() == arguments.size());
5787
5788 for (size_t i = 0; i < arguments.size(); ++i)
5789 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005790 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5791 const TType &functionArgumentType = typedArgument->getType();
Olli Etuahod4bd9632018-03-08 16:32:44 +02005792 const TType &functionParameterType = functionDefinition->getParam(i)->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005793 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5794
5795 if (IsImage(functionArgumentType.getBasicType()))
5796 {
5797 const TMemoryQualifier &functionArgumentMemoryQualifier =
5798 functionArgumentType.getMemoryQualifier();
5799 const TMemoryQualifier &functionParameterMemoryQualifier =
5800 functionParameterType.getMemoryQualifier();
5801 if (functionArgumentMemoryQualifier.readonly &&
5802 !functionParameterMemoryQualifier.readonly)
5803 {
5804 error(functionCall->getLine(),
5805 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005806 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005807 }
5808
5809 if (functionArgumentMemoryQualifier.writeonly &&
5810 !functionParameterMemoryQualifier.writeonly)
5811 {
5812 error(functionCall->getLine(),
5813 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005814 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005815 }
Martin Radev049edfa2016-11-11 14:35:37 +02005816
5817 if (functionArgumentMemoryQualifier.coherent &&
5818 !functionParameterMemoryQualifier.coherent)
5819 {
5820 error(functionCall->getLine(),
5821 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005822 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005823 }
5824
5825 if (functionArgumentMemoryQualifier.volatileQualifier &&
5826 !functionParameterMemoryQualifier.volatileQualifier)
5827 {
5828 error(functionCall->getLine(),
5829 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005830 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005831 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005832 }
5833 }
5834}
5835
Olli Etuaho95ed1942018-02-01 14:01:19 +02005836TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005837{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005838 if (fnCall->thisNode() != nullptr)
5839 {
5840 return addMethod(fnCall, loc);
5841 }
5842 if (fnCall->isConstructor())
5843 {
5844 return addConstructor(fnCall, loc);
5845 }
5846 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005847}
5848
Olli Etuaho95ed1942018-02-01 14:01:19 +02005849TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005850{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005851 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005852 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5853 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5854 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005855 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005856 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005857 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005858 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005859 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005860 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005861 {
5862 error(loc, "method takes no parameters", "length");
5863 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005864 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005865 {
5866 error(loc, "length can only be called on arrays", "length");
5867 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005868 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005869 mGeometryShaderInputPrimitiveType == EptUndefined)
5870 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005871 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005872 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5873 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005874 else
5875 {
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005876 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode, nullptr);
Qin Jiajia19f2f9e2018-12-07 15:10:48 +08005877 markStaticReadIfSymbol(thisNode);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005878 node->setLine(loc);
5879 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005880 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005881 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005882}
5883
Olli Etuaho95ed1942018-02-01 14:01:19 +02005884TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005885 const TSourceLoc &loc)
5886{
Olli Etuaho697bf652018-02-16 11:50:54 +02005887 // First check whether the function has been hidden by a variable name or struct typename by
5888 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5889 // with a matching argument list.
5890 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005891 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005892 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005893 }
5894 else
5895 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005896 // There are no inner functions, so it's enough to look for user-defined functions in the
5897 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005898 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005899 if (symbol != nullptr)
5900 {
5901 // A user-defined function - could be an overloaded built-in as well.
5902 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5903 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5904 TIntermAggregate *callNode =
5905 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5906 callNode->setLine(loc);
5907 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5908 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5909 return callNode;
5910 }
5911
5912 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005913 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005914 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005915 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005916 }
5917 else
5918 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005919 // A built-in function.
5920 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005921 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005922
Olli Etuaho37b697e2018-01-29 12:19:27 +02005923 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005924 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005925 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005926 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005927 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005928 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005929 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005930 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005931 if (fnCandidate->getParamCount() == 1)
5932 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005933 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005934 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005935 TIntermTyped *callNode =
5936 createUnaryMath(op, unaryParamNode->getAsTyped(), loc, fnCandidate);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005937 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005938 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005939 }
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005940
Olli Etuahoe80825e2018-02-16 10:24:53 +02005941 TIntermAggregate *callNode =
5942 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005943 callNode->setLine(loc);
5944
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005945 checkAtomicMemoryBuiltinFunctions(callNode);
5946
Olli Etuahoe80825e2018-02-16 10:24:53 +02005947 // Some built-in functions have out parameters too.
5948 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5949
5950 // See if we can constant fold a built-in. Note that this may be possible
5951 // even if it is not const-qualified.
5952 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005953 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005954
5955 // This is a built-in function with no op associated with it.
5956 TIntermAggregate *callNode =
5957 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5958 callNode->setLine(loc);
5959 checkTextureOffsetConst(callNode);
5960 checkTextureGather(callNode);
5961 checkImageMemoryAccessForBuiltinFunctions(callNode);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005962 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5963 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005964 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005965 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005966
5967 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005968 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005969}
5970
Jamie Madillb98c3a82015-07-23 14:26:04 -04005971TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005972 TIntermTyped *trueExpression,
5973 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005974 const TSourceLoc &loc)
5975{
Olli Etuaho56229f12017-07-10 14:16:33 +03005976 if (!checkIsScalarBool(loc, cond))
5977 {
5978 return falseExpression;
5979 }
Olli Etuaho52901742015-04-15 13:42:45 +03005980
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005981 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005982 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005983 TInfoSinkBase reasonStream;
5984 reasonStream << "mismatching ternary operator operand types '" << trueExpression->getType()
5985 << " and '" << falseExpression->getType() << "'";
5986 error(loc, reasonStream.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005987 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005988 }
Olli Etuahode318b22016-10-25 16:18:25 +01005989 if (IsOpaqueType(trueExpression->getBasicType()))
5990 {
5991 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005992 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005993 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5994 // Note that structs containing opaque types don't need to be checked as structs are
5995 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005996 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005997 return falseExpression;
5998 }
5999
Jiajia Qinbc585152017-06-23 15:42:17 +08006000 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
6001 falseExpression->getMemoryQualifier().writeonly)
6002 {
6003 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
6004 return falseExpression;
6005 }
6006
Olli Etuahoe01c02b2017-05-08 14:41:49 +03006007 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03006008 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03006009 // ESSL 3.00.6 section 5.7:
6010 // Ternary operator support is optional for arrays. No certainty that it works across all
6011 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
6012 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03006013 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03006014 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03006015 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03006016 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03006017 }
Olli Etuaho94050052017-05-08 14:17:44 +03006018 if (trueExpression->getBasicType() == EbtInterfaceBlock)
6019 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03006020 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03006021 return falseExpression;
6022 }
6023
Corentin Wallez0d959252016-07-12 17:26:32 -04006024 // WebGL2 section 5.26, the following results in an error:
6025 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03006026 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04006027 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03006028 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03006029 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04006030 }
6031
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03006032 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
Olli Etuaho94bbed12018-03-20 14:44:53 +02006033 markStaticReadIfSymbol(cond);
6034 markStaticReadIfSymbol(trueExpression);
6035 markStaticReadIfSymbol(falseExpression);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03006036 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02006037 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03006038}
Olli Etuaho49300862015-02-20 14:54:49 +02006039
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00006040//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006041// Parse an array of strings using yyparse.
6042//
6043// Returns 0 for success.
6044//
Jamie Madillb98c3a82015-07-23 14:26:04 -04006045int PaParseStrings(size_t count,
6046 const char *const string[],
6047 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05306048 TParseContext *context)
6049{
Yunchao He4f285442017-04-21 12:15:49 +08006050 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006051 return 1;
6052
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006053 if (glslang_initialize(context))
6054 return 1;
6055
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006056 int error = glslang_scan(count, string, length, context);
6057 if (!error)
6058 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006059
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006060 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006061
alokp@chromium.org6b495712012-06-29 00:06:58 +00006062 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006063}
Jamie Madill45bcc782016-11-07 13:58:48 -05006064
6065} // namespace sh