blob: bf04693f7bb28f2519ee94d9ebbf2504bd990781 [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
614 std::stringstream reasonStream;
615 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 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000905 std::stringstream reasonStream;
906 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 if (!IsAtomicCounter(type->getBasicType()))
2370 {
2371 return;
2372 }
2373
2374 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2375 : kAtomicCounterSize;
2376 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2377 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002378 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002379 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002380 {
2381 offset = bindingState.appendSpan(size);
2382 }
2383 else
2384 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002385 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002386 }
2387 if (offset == -1)
2388 {
2389 error(loc, "Offset overlapping", "atomic counter");
2390 return;
2391 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002392 layoutQualifier.offset = offset;
2393 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002394}
2395
Olli Etuaho454c34c2017-10-25 16:35:56 +03002396void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002397 const ImmutableString &token,
Olli Etuaho454c34c2017-10-25 16:35:56 +03002398 TType *type)
2399{
2400 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2401 {
2402 if (type->isArray() && type->getOutermostArraySize() == 0u)
2403 {
2404 // Set size for the unsized geometry shader inputs if they are declared after a valid
2405 // input primitive declaration.
2406 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2407 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002408 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002409 type->sizeOutermostUnsizedArray(
Olli Etuaho94bbed12018-03-20 14:44:53 +02002410 symbolTable.getGlInVariableWithArraySize()->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002411 }
2412 else
2413 {
2414 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2415 // An input can be declared without an array size if there is a previous layout
2416 // which specifies the size.
2417 error(location,
2418 "Missing a valid input primitive declaration before declaring an unsized "
2419 "array input",
2420 token);
2421 }
2422 }
2423 else if (type->isArray())
2424 {
2425 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2426 }
2427 else
2428 {
2429 error(location, "Geometry shader input variable must be declared as an array", token);
2430 }
2431 }
2432}
2433
Olli Etuaho13389b62016-10-16 11:48:18 +01002434TIntermDeclaration *TParseContext::parseSingleDeclaration(
2435 TPublicType &publicType,
2436 const TSourceLoc &identifierOrTypeLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002437 const ImmutableString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002438{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002439 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002440 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2441 mDirectiveHandler.pragma().stdgl.invariantAll)
2442 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002443 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002444
2445 // The directive handler has already taken care of rejecting invalid uses of this pragma
2446 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2447 // affected variable declarations:
2448 //
2449 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2450 // elsewhere, in TranslatorGLSL.)
2451 //
2452 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2453 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2454 // the way this is currently implemented we have to enable this compiler option before
2455 // parsing the shader and determining the shading language version it uses. If this were
2456 // implemented as a post-pass, the workaround could be more targeted.
2457 //
2458 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2459 // the specification, but there are desktop OpenGL drivers that expect that this is the
2460 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2461 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2462 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002463 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002464 }
2465 }
2466
Olli Etuahofbb1c792018-01-19 16:26:59 +02002467 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier, type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002468
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002469 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2470 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002471
Jamie Madill50cf2be2018-06-15 09:46:57 -04002472 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002473 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002474
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002475 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002476 if (emptyDeclaration)
2477 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002478 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002479 // In most cases we don't need to create a symbol node for an empty declaration.
2480 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002481 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002482 {
Olli Etuaho195be942017-12-04 23:40:14 +02002483 TVariable *emptyVariable =
Jamie Madillb779b122018-06-20 11:46:43 -04002484 new TVariable(&symbolTable, kEmptyImmutableString, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002485 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002486 }
jchen104cdac9e2017-05-08 11:01:20 +08002487 else if (IsAtomicCounter(publicType.getBasicType()))
2488 {
2489 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2490 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002491 }
2492 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002493 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002494 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002495
Olli Etuahob60d30f2018-01-16 12:31:06 +02002496 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002497
Olli Etuahob60d30f2018-01-16 12:31:06 +02002498 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002499
Olli Etuaho2935c582015-04-08 14:32:06 +03002500 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002501 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002502 {
Olli Etuaho195be942017-12-04 23:40:14 +02002503 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002504 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002505 }
2506
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002507 TIntermDeclaration *declaration = new TIntermDeclaration();
2508 declaration->setLine(identifierOrTypeLocation);
2509 if (symbol)
2510 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002511 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002512 declaration->appendDeclarator(symbol);
2513 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002514 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002515}
2516
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002517TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2518 TPublicType &elementType,
2519 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002520 const ImmutableString &identifier,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002521 const TSourceLoc &indexLocation,
2522 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002523{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002524 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002525
Olli Etuaho55bde912017-10-25 13:41:13 +03002526 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002527 identifierLocation);
2528
Olli Etuaho55bde912017-10-25 13:41:13 +03002529 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002530
Olli Etuaho55bde912017-10-25 13:41:13 +03002531 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002532
Olli Etuahob60d30f2018-01-16 12:31:06 +02002533 TType *arrayType = new TType(elementType);
2534 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002535
Olli Etuahofbb1c792018-01-19 16:26:59 +02002536 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002537
Olli Etuahob60d30f2018-01-16 12:31:06 +02002538 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002539
Olli Etuahob60d30f2018-01-16 12:31:06 +02002540 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002541
Olli Etuaho13389b62016-10-16 11:48:18 +01002542 TIntermDeclaration *declaration = new TIntermDeclaration();
2543 declaration->setLine(identifierLocation);
2544
Olli Etuaho195be942017-12-04 23:40:14 +02002545 TVariable *variable = nullptr;
2546 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002547 {
Olli Etuaho195be942017-12-04 23:40:14 +02002548 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002549 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002550 declaration->appendDeclarator(symbol);
2551 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002552
Olli Etuaho13389b62016-10-16 11:48:18 +01002553 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002554}
2555
Olli Etuaho13389b62016-10-16 11:48:18 +01002556TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2557 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002558 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002559 const TSourceLoc &initLocation,
2560 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002561{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002562 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002563
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002564 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2565 identifierLocation);
2566
2567 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002568
Olli Etuaho13389b62016-10-16 11:48:18 +01002569 TIntermDeclaration *declaration = new TIntermDeclaration();
2570 declaration->setLine(identifierLocation);
2571
2572 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002573 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002574 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002575 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002576 if (initNode)
2577 {
2578 declaration->appendDeclarator(initNode);
2579 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002580 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002581 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002582}
2583
Olli Etuaho13389b62016-10-16 11:48:18 +01002584TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002585 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002586 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002587 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002588 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002589 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002590 const TSourceLoc &initLocation,
2591 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002592{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002593 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002594
Olli Etuaho55bde912017-10-25 13:41:13 +03002595 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002596 identifierLocation);
2597
Olli Etuaho55bde912017-10-25 13:41:13 +03002598 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002599
Olli Etuaho55bde912017-10-25 13:41:13 +03002600 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002601
Olli Etuahob60d30f2018-01-16 12:31:06 +02002602 TType *arrayType = new TType(elementType);
2603 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002604
Olli Etuaho13389b62016-10-16 11:48:18 +01002605 TIntermDeclaration *declaration = new TIntermDeclaration();
2606 declaration->setLine(identifierLocation);
2607
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002608 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002609 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002610 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002611 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002612 if (initNode)
2613 {
2614 declaration->appendDeclarator(initNode);
2615 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002616 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002617
2618 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002619}
2620
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002621TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002622 const TTypeQualifierBuilder &typeQualifierBuilder,
2623 const TSourceLoc &identifierLoc,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002624 const ImmutableString &identifier,
Martin Radev70866b82016-07-22 15:27:42 +03002625 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002626{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002627 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002628
Martin Radev70866b82016-07-22 15:27:42 +03002629 if (!typeQualifier.invariant)
2630 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002631 error(identifierLoc, "Expected invariant", identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002632 return nullptr;
2633 }
2634 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2635 {
2636 return nullptr;
2637 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002638 if (!symbol)
2639 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002640 error(identifierLoc, "undeclared identifier declared as invariant", identifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002641 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002642 }
Martin Radev70866b82016-07-22 15:27:42 +03002643 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002644 {
Martin Radev70866b82016-07-22 15:27:42 +03002645 error(identifierLoc, "invariant declaration specifies qualifier",
2646 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002647 }
Martin Radev70866b82016-07-22 15:27:42 +03002648 if (typeQualifier.precision != EbpUndefined)
2649 {
2650 error(identifierLoc, "invariant declaration specifies precision",
2651 getPrecisionString(typeQualifier.precision));
2652 }
2653 if (!typeQualifier.layoutQualifier.isEmpty())
2654 {
2655 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2656 }
2657
2658 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002659 if (!variable)
2660 {
2661 return nullptr;
2662 }
Martin Radev70866b82016-07-22 15:27:42 +03002663 const TType &type = variable->getType();
2664
2665 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2666 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002667 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002668
Olli Etuaho76b2c382018-03-19 15:51:29 +02002669 symbolTable.addInvariantVarying(*variable);
Martin Radev70866b82016-07-22 15:27:42 +03002670
Olli Etuaho195be942017-12-04 23:40:14 +02002671 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002672 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002673
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002674 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002675}
2676
Olli Etuaho13389b62016-10-16 11:48:18 +01002677void TParseContext::parseDeclarator(TPublicType &publicType,
2678 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002679 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002680 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002681{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002682 // If the declaration starting this declarator list was empty (example: int,), some checks were
2683 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002684 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002685 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002686 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2687 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002688 }
2689
Olli Etuaho856c4972016-08-08 11:38:39 +03002690 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002691
Olli Etuahob60d30f2018-01-16 12:31:06 +02002692 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002693
Olli Etuahofbb1c792018-01-19 16:26:59 +02002694 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002695
Olli Etuahob60d30f2018-01-16 12:31:06 +02002696 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002697
Olli Etuahob60d30f2018-01-16 12:31:06 +02002698 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002699
Olli Etuaho195be942017-12-04 23:40:14 +02002700 TVariable *variable = nullptr;
2701 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002702 {
Olli Etuaho195be942017-12-04 23:40:14 +02002703 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002704 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002705 declarationOut->appendDeclarator(symbol);
2706 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002707}
2708
Olli Etuaho55bde912017-10-25 13:41:13 +03002709void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002710 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002711 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002712 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002713 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002714 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002715{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002716 // If the declaration starting this declarator list was empty (example: int,), some checks were
2717 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002718 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002719 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002720 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002721 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002722 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002723
Olli Etuaho55bde912017-10-25 13:41:13 +03002724 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002725
Olli Etuaho55bde912017-10-25 13:41:13 +03002726 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002727 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002728 TType *arrayType = new TType(elementType);
2729 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002730
Olli Etuahofbb1c792018-01-19 16:26:59 +02002731 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002732
Olli Etuahob60d30f2018-01-16 12:31:06 +02002733 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002734
Olli Etuahob60d30f2018-01-16 12:31:06 +02002735 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002736
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002737 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002738 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002739 {
Olli Etuaho195be942017-12-04 23:40:14 +02002740 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002741 symbol->setLine(identifierLocation);
2742 declarationOut->appendDeclarator(symbol);
2743 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002744 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002745}
2746
Olli Etuaho13389b62016-10-16 11:48:18 +01002747void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2748 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002749 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002750 const TSourceLoc &initLocation,
2751 TIntermTyped *initializer,
2752 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002753{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002754 // If the declaration starting this declarator list was empty (example: int,), some checks were
2755 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002756 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002757 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002758 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2759 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002760 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002761
Olli Etuaho856c4972016-08-08 11:38:39 +03002762 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002763
Olli Etuaho13389b62016-10-16 11:48:18 +01002764 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002765 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002766 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002767 {
2768 //
2769 // build the intermediate representation
2770 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002771 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002772 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002773 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002774 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002775 }
2776}
2777
Olli Etuaho55bde912017-10-25 13:41:13 +03002778void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002779 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002780 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002781 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002782 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002783 const TSourceLoc &initLocation,
2784 TIntermTyped *initializer,
2785 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002786{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002787 // If the declaration starting this declarator list was empty (example: int,), some checks were
2788 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002789 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002790 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002791 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002792 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002793 }
2794
Olli Etuaho55bde912017-10-25 13:41:13 +03002795 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002796
Olli Etuaho55bde912017-10-25 13:41:13 +03002797 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002798
Olli Etuahob60d30f2018-01-16 12:31:06 +02002799 TType *arrayType = new TType(elementType);
2800 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002801
2802 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002803 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002804 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002805 {
2806 if (initNode)
2807 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002808 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002809 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002810 }
2811}
2812
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002813TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2814{
2815 // It's simpler to parse an empty statement as a constant expression rather than having a
2816 // different type of node just for empty statements, that will be pruned from the AST anyway.
2817 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2818 node->setLine(location);
2819 return node;
2820}
2821
jchen104cdac9e2017-05-08 11:01:20 +08002822void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2823 const TSourceLoc &location)
2824{
2825 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2826 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2827 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2828 {
2829 error(location, "Requires both binding and offset", "layout");
2830 return;
2831 }
2832 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2833}
2834
Olli Etuahocce89652017-06-19 16:04:09 +03002835void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2836 const TPublicType &type,
2837 const TSourceLoc &loc)
2838{
2839 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2840 !getFragmentPrecisionHigh())
2841 {
2842 error(loc, "precision is not supported in fragment shader", "highp");
2843 }
2844
2845 if (!CanSetDefaultPrecisionOnType(type))
2846 {
2847 error(loc, "illegal type argument for default precision qualifier",
2848 getBasicString(type.getBasicType()));
2849 return;
2850 }
2851 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2852}
2853
Shaob5cc1192017-07-06 10:47:20 +08002854bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2855{
2856 switch (typeQualifier.layoutQualifier.primitiveType)
2857 {
2858 case EptLines:
2859 case EptLinesAdjacency:
2860 case EptTriangles:
2861 case EptTrianglesAdjacency:
2862 return typeQualifier.qualifier == EvqGeometryIn;
2863
2864 case EptLineStrip:
2865 case EptTriangleStrip:
2866 return typeQualifier.qualifier == EvqGeometryOut;
2867
2868 case EptPoints:
2869 return true;
2870
2871 default:
2872 UNREACHABLE();
2873 return false;
2874 }
2875}
2876
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002877void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2878 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002879{
Olli Etuaho94bbed12018-03-20 14:44:53 +02002880 if (!symbolTable.setGlInArraySize(inputArraySize))
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002881 {
2882 error(line,
2883 "Array size or input primitive declaration doesn't match the size of earlier sized "
2884 "array inputs.",
2885 "layout");
2886 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002887}
2888
Shaob5cc1192017-07-06 10:47:20 +08002889bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2890{
2891 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2892
2893 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2894
2895 if (layoutQualifier.maxVertices != -1)
2896 {
2897 error(typeQualifier.line,
2898 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2899 return false;
2900 }
2901
2902 // Set mGeometryInputPrimitiveType if exists
2903 if (layoutQualifier.primitiveType != EptUndefined)
2904 {
2905 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2906 {
2907 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2908 return false;
2909 }
2910
2911 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2912 {
2913 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002914 setGeometryShaderInputArraySize(
2915 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2916 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002917 }
2918 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2919 {
2920 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2921 "layout");
2922 return false;
2923 }
2924 }
2925
2926 // Set mGeometryInvocations if exists
2927 if (layoutQualifier.invocations > 0)
2928 {
2929 if (mGeometryShaderInvocations == 0)
2930 {
2931 mGeometryShaderInvocations = layoutQualifier.invocations;
2932 }
2933 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2934 {
2935 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2936 "layout");
2937 return false;
2938 }
2939 }
2940
2941 return true;
2942}
2943
2944bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2945{
2946 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2947
2948 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2949
2950 if (layoutQualifier.invocations > 0)
2951 {
2952 error(typeQualifier.line,
2953 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2954 return false;
2955 }
2956
2957 // Set mGeometryOutputPrimitiveType if exists
2958 if (layoutQualifier.primitiveType != EptUndefined)
2959 {
2960 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2961 {
2962 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2963 return false;
2964 }
2965
2966 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2967 {
2968 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2969 }
2970 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2971 {
2972 error(typeQualifier.line,
2973 "primitive doesn't match earlier output primitive declaration", "layout");
2974 return false;
2975 }
2976 }
2977
2978 // Set mGeometryMaxVertices if exists
2979 if (layoutQualifier.maxVertices > -1)
2980 {
2981 if (mGeometryShaderMaxVertices == -1)
2982 {
2983 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2984 }
2985 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2986 {
2987 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2988 "layout");
2989 return false;
2990 }
2991 }
2992
2993 return true;
2994}
2995
Martin Radev70866b82016-07-22 15:27:42 +03002996void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002997{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002998 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002999 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04003000
Martin Radev70866b82016-07-22 15:27:42 +03003001 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
3002 typeQualifier.line);
3003
Jamie Madillc2128ff2016-07-04 10:26:17 -04003004 // It should never be the case, but some strange parser errors can send us here.
3005 if (layoutQualifier.isEmpty())
3006 {
3007 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04003008 return;
3009 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003010
Martin Radev802abe02016-08-04 17:48:32 +03003011 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04003012 {
Olli Etuaho43364892017-02-13 16:00:12 +00003013 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04003014 return;
3015 }
3016
Olli Etuahoa78092c2018-09-26 14:16:13 +03003017 checkIndexIsNotSpecified(typeQualifier.line, layoutQualifier.index);
3018
Olli Etuaho43364892017-02-13 16:00:12 +00003019 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
3020
3021 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003022
3023 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3024
Andrei Volykhina5527072017-03-22 16:46:30 +03003025 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3026
jchen104cdac9e2017-05-08 11:01:20 +08003027 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3028
Qin Jiajiaca68d982017-09-18 16:41:56 +08003029 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3030 typeQualifier.qualifier);
3031
Martin Radev802abe02016-08-04 17:48:32 +03003032 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003033 {
Martin Radev802abe02016-08-04 17:48:32 +03003034 if (mComputeShaderLocalSizeDeclared &&
3035 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3036 {
3037 error(typeQualifier.line, "Work group size does not match the previous declaration",
3038 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003039 return;
3040 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003041
Martin Radev802abe02016-08-04 17:48:32 +03003042 if (mShaderVersion < 310)
3043 {
3044 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003045 return;
3046 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003047
Martin Radev4c4c8e72016-08-04 12:25:34 +03003048 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003049 {
3050 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003051 return;
3052 }
3053
3054 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003055 symbolTable.findBuiltIn(ImmutableString("gl_MaxComputeWorkGroupSize"), mShaderVersion));
Martin Radev802abe02016-08-04 17:48:32 +03003056
3057 const TConstantUnion *maxComputeWorkGroupSizeData =
3058 maxComputeWorkGroupSize->getConstPointer();
3059
3060 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3061 {
3062 if (layoutQualifier.localSize[i] != -1)
3063 {
3064 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3065 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3066 if (mComputeShaderLocalSize[i] < 1 ||
3067 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3068 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003069 std::stringstream reasonStream;
3070 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3071 << maxComputeWorkGroupSizeValue;
3072 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003073
Olli Etuaho4de340a2016-12-16 09:32:03 +00003074 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003075 return;
3076 }
3077 }
3078 }
3079
3080 mComputeShaderLocalSizeDeclared = true;
3081 }
Shaob5cc1192017-07-06 10:47:20 +08003082 else if (typeQualifier.qualifier == EvqGeometryIn)
3083 {
3084 if (mShaderVersion < 310)
3085 {
3086 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3087 return;
3088 }
3089
3090 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3091 {
3092 return;
3093 }
3094 }
3095 else if (typeQualifier.qualifier == EvqGeometryOut)
3096 {
3097 if (mShaderVersion < 310)
3098 {
3099 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3100 "layout");
3101 return;
3102 }
3103
3104 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3105 {
3106 return;
3107 }
3108 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003109 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3110 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003111 {
3112 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3113 // specification.
3114 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3115 {
3116 error(typeQualifier.line, "Number of views does not match the previous declaration",
3117 "layout");
3118 return;
3119 }
3120
3121 if (layoutQualifier.numViews == -1)
3122 {
3123 error(typeQualifier.line, "No num_views specified", "layout");
3124 return;
3125 }
3126
3127 if (layoutQualifier.numViews > mMaxNumViews)
3128 {
3129 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3130 "layout");
3131 return;
3132 }
3133
3134 mNumViews = layoutQualifier.numViews;
3135 }
Martin Radev802abe02016-08-04 17:48:32 +03003136 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003137 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003138 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003139 {
Martin Radev802abe02016-08-04 17:48:32 +03003140 return;
3141 }
3142
Jiajia Qinbc585152017-06-23 15:42:17 +08003143 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003144 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003145 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003146 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003147 return;
3148 }
3149
3150 if (mShaderVersion < 300)
3151 {
3152 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3153 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003154 return;
3155 }
3156
Olli Etuaho09b04a22016-12-15 13:30:26 +00003157 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003158
3159 if (layoutQualifier.matrixPacking != EmpUnspecified)
3160 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003161 if (typeQualifier.qualifier == EvqUniform)
3162 {
3163 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3164 }
3165 else if (typeQualifier.qualifier == EvqBuffer)
3166 {
3167 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3168 }
Martin Radev802abe02016-08-04 17:48:32 +03003169 }
3170
3171 if (layoutQualifier.blockStorage != EbsUnspecified)
3172 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003173 if (typeQualifier.qualifier == EvqUniform)
3174 {
3175 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3176 }
3177 else if (typeQualifier.qualifier == EvqBuffer)
3178 {
3179 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3180 }
Martin Radev802abe02016-08-04 17:48:32 +03003181 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003182 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003183}
3184
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003185TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3186 const TFunction &function,
3187 const TSourceLoc &location,
3188 bool insertParametersToSymbolTable)
3189{
Olli Etuahobed35d72017-12-20 16:36:26 +02003190 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003191
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003192 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003193 prototype->setLine(location);
3194
3195 for (size_t i = 0; i < function.getParamCount(); i++)
3196 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003197 const TVariable *param = function.getParam(i);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003198
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003199 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3200 // be used for unused args).
Olli Etuahod4bd9632018-03-08 16:32:44 +02003201 if (param->symbolType() != SymbolType::Empty)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003202 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003203 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003204 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003205 if (!symbolTable.declare(const_cast<TVariable *>(param)))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003206 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003207 error(location, "redefinition", param->name());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003208 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003209 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003210 // Unsized type of a named parameter should have already been checked and sanitized.
Olli Etuahod4bd9632018-03-08 16:32:44 +02003211 ASSERT(!param->getType().isUnsizedArray());
Olli Etuaho55bde912017-10-25 13:41:13 +03003212 }
3213 else
3214 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003215 if (param->getType().isUnsizedArray())
Olli Etuaho55bde912017-10-25 13:41:13 +03003216 {
3217 error(location, "function parameter array must be sized at compile time", "[]");
3218 // We don't need to size the arrays since the parameter is unnamed and hence
3219 // inaccessible.
3220 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003221 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003222 }
3223 return prototype;
3224}
3225
Olli Etuaho16c745a2017-01-16 17:02:27 +00003226TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3227 const TFunction &parsedFunction,
3228 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003229{
Olli Etuaho476197f2016-10-11 13:59:08 +01003230 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3231 // first declaration. Either way the instance in the symbol table is used to track whether the
3232 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003233 bool hadPrototypeDeclaration = false;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003234 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003235 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3236
3237 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003238 {
3239 // ESSL 1.00.17 section 4.2.7.
3240 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3241 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003242 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003243
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003244 TIntermFunctionPrototype *prototype =
3245 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003246
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003247 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003248
3249 if (!symbolTable.atGlobalLevel())
3250 {
3251 // ESSL 3.00.4 section 4.2.4.
3252 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003253 }
3254
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003255 return prototype;
3256}
3257
Olli Etuaho336b1472016-10-05 16:37:55 +01003258TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003259 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003260 TIntermBlock *functionBody,
3261 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003262{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003263 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003264 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3265 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003266 error(location,
3267 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003268 }
3269
Olli Etuahof51fdd22016-10-03 10:03:40 +01003270 if (functionBody == nullptr)
3271 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003272 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003273 functionBody->setLine(location);
3274 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003275 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003276 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003277 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003278
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003279 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003280 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003281}
3282
Olli Etuaho476197f2016-10-11 13:59:08 +01003283void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003284 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003285 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003286{
Olli Etuaho476197f2016-10-11 13:59:08 +01003287 ASSERT(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003288
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003289 bool wasDefined = false;
3290 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3291 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003292 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003293 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003294 }
Jamie Madill185fb402015-06-12 15:48:48 -04003295
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003296 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003297 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003298 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003299
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003300 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003301 setLoopNestingLevel(0);
3302}
3303
Jamie Madillb98c3a82015-07-23 14:26:04 -04003304TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003305{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003306 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003307 // We don't know at this point whether this is a function definition or a prototype.
3308 // The definition production code will check for redefinitions.
3309 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003310 //
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303311
Olli Etuahod80f2942017-11-06 12:44:45 +02003312 for (size_t i = 0u; i < function->getParamCount(); ++i)
3313 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003314 const TVariable *param = function->getParam(i);
3315 if (param->getType().isStructSpecifier())
Olli Etuahod80f2942017-11-06 12:44:45 +02003316 {
3317 // ESSL 3.00.6 section 12.10.
3318 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003319 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003320 }
3321 }
3322
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003323 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303324 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003325 const UnmangledBuiltIn *builtIn =
3326 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3327 if (builtIn &&
3328 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3329 {
3330 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3331 // functions. Therefore overloading or redefining builtin functions is an error.
3332 error(location, "Name of a built-in function cannot be redeclared as function",
3333 function->name());
3334 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303335 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003336 else
3337 {
3338 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3339 // this applies to redeclarations as well.
3340 const TSymbol *builtIn =
3341 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3342 if (builtIn)
3343 {
3344 error(location, "built-in functions cannot be redefined", function->name());
3345 }
3346 }
3347
3348 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3349 // here.
3350 const TFunction *prevDec =
3351 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3352 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003353 {
3354 if (prevDec->getReturnType() != function->getReturnType())
3355 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003356 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003357 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003358 }
3359 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3360 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003361 if (prevDec->getParam(i)->getType().getQualifier() !=
3362 function->getParam(i)->getType().getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003363 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003364 error(location,
3365 "function must have the same parameter qualifiers in all of its declarations",
Olli Etuahod4bd9632018-03-08 16:32:44 +02003366 function->getParam(i)->getType().getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003367 }
3368 }
3369 }
3370
Jamie Madill185fb402015-06-12 15:48:48 -04003371 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003372 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3373 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003374 if (prevSym)
3375 {
3376 if (!prevSym->isFunction())
3377 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003378 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003379 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003380 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003381 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003382 // Parsing is at the inner scope level of the function's arguments and body statement at this
3383 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3384 // scope.
3385 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003386
Olli Etuaho78d13742017-01-18 13:06:10 +00003387 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003388 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003389 {
3390 if (function->getParamCount() > 0)
3391 {
3392 error(location, "function cannot take any parameter(s)", "main");
3393 }
3394 if (function->getReturnType().getBasicType() != EbtVoid)
3395 {
3396 error(location, "main function cannot return a value",
3397 function->getReturnType().getBasicString());
3398 }
3399 }
3400
Jamie Madill185fb402015-06-12 15:48:48 -04003401 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003402 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3403 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003404 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3405 //
3406 return function;
3407}
3408
Olli Etuaho9de84a52016-06-14 17:36:01 +03003409TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003410 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003411 const TSourceLoc &location)
3412{
3413 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3414 {
3415 error(location, "no qualifiers allowed for function return",
3416 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003417 }
3418 if (!type.layoutQualifier.isEmpty())
3419 {
3420 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003421 }
jchen10cc2a10e2017-05-03 14:05:12 +08003422 // make sure an opaque type is not involved as well...
3423 std::string reason(getBasicString(type.getBasicType()));
3424 reason += "s can't be function return values";
3425 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003426 if (mShaderVersion < 300)
3427 {
3428 // Array return values are forbidden, but there's also no valid syntax for declaring array
3429 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003430 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003431
3432 if (type.isStructureContainingArrays())
3433 {
3434 // ESSL 1.00.17 section 6.1 Function Definitions
Olli Etuaho72e35892018-06-20 11:43:08 +03003435 TInfoSinkBase typeString;
3436 typeString << TType(type);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003437 error(location, "structures containing arrays can't be function return values",
Olli Etuaho72e35892018-06-20 11:43:08 +03003438 typeString.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003439 }
3440 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003441
3442 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003443 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003444}
3445
Olli Etuaho697bf652018-02-16 11:50:54 +02003446TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3447 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003448{
Olli Etuaho697bf652018-02-16 11:50:54 +02003449 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003450}
3451
Olli Etuaho95ed1942018-02-01 14:01:19 +02003452TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003453{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003454 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003455 {
3456 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3457 "[]");
3458 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003459 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003460 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003461 error(publicType.getLine(), "constructor can't be a structure definition",
3462 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003463 }
3464
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003465 TType *type = new TType(publicType);
3466 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003467 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003468 error(publicType.getLine(), "cannot construct this type",
3469 getBasicString(publicType.getBasicType()));
3470 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003471 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003472 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003473}
3474
Olli Etuaho55bde912017-10-25 13:41:13 +03003475void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3476 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003477 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003478 TType *arrayType)
3479{
3480 if (arrayType->isUnsizedArray())
3481 {
3482 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003483 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003484 }
3485}
3486
3487TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003488 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003489 const TSourceLoc &nameLoc)
3490{
Olli Etuaho55bde912017-10-25 13:41:13 +03003491 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003492 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003493 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003494 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003495 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003496 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003497 checkIsNotReserved(nameLoc, name);
3498 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003499 return param;
3500}
3501
Olli Etuaho55bde912017-10-25 13:41:13 +03003502TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003503 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003504 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003505{
Olli Etuaho55bde912017-10-25 13:41:13 +03003506 TType *type = new TType(publicType);
3507 return parseParameterDeclarator(type, name, nameLoc);
3508}
3509
Olli Etuahofbb1c792018-01-19 16:26:59 +02003510TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003511 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003512 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003513 const TSourceLoc &arrayLoc,
3514 TPublicType *elementType)
3515{
3516 checkArrayElementIsNotArray(arrayLoc, *elementType);
3517 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003518 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003519 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003520}
3521
Olli Etuaho95ed1942018-02-01 14:01:19 +02003522bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3523 const TIntermSequence &arguments,
3524 TType type,
3525 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003526{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003527 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003528 {
3529 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3530 return false;
3531 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003532 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003533 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003534 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003535 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003536 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3537 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003538 {
3539 error(line, "constructing from a non-dereferenced array", "constructor");
3540 return false;
3541 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003542 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003543 {
3544 if (dimensionalityFromElement == 1u)
3545 {
3546 error(line, "implicitly sized array of arrays constructor argument is not an array",
3547 "constructor");
3548 }
3549 else
3550 {
3551 error(line,
3552 "implicitly sized array of arrays constructor argument dimensionality is too "
3553 "low",
3554 "constructor");
3555 }
3556 return false;
3557 }
3558 }
3559 return true;
3560}
3561
Jamie Madillb98c3a82015-07-23 14:26:04 -04003562// This function is used to test for the correctness of the parameters passed to various constructor
3563// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003564//
Olli Etuaho856c4972016-08-08 11:38:39 +03003565// Returns a node to add to the tree regardless of if an error was generated or not.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003566//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003567TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003568{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003569 TType type = fnCall->constructorType();
3570 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003571 if (type.isUnsizedArray())
3572 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003573 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003574 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003575 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003576 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003577 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003578 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003579 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003580 if (type.getOutermostArraySize() == 0u)
3581 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003582 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003583 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003584 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003585 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003586 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003587 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003588 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003589 }
3590 }
3591 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003592 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003593
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003594 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003595 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003596 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003597 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003598
Olli Etuaho95ed1942018-02-01 14:01:19 +02003599 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003600 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003601
Olli Etuaho765924f2018-01-04 12:48:36 +02003602 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003603}
3604
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003605//
3606// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003607// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003608//
Olli Etuaho13389b62016-10-16 11:48:18 +01003609TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003610 const TTypeQualifierBuilder &typeQualifierBuilder,
3611 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003612 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003613 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003614 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003615 const TSourceLoc &instanceLine,
3616 TIntermTyped *arrayIndex,
3617 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003618{
Olli Etuaho856c4972016-08-08 11:38:39 +03003619 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003620
Olli Etuaho77ba4082016-12-16 12:01:18 +00003621 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003622
Jiajia Qinbc585152017-06-23 15:42:17 +08003623 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003624 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003625 error(typeQualifier.line,
3626 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3627 "3.10",
3628 getQualifierString(typeQualifier.qualifier));
3629 }
3630 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3631 {
3632 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003633 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003634 }
3635
Martin Radev70866b82016-07-22 15:27:42 +03003636 if (typeQualifier.invariant)
3637 {
3638 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3639 }
3640
Jiajia Qinbc585152017-06-23 15:42:17 +08003641 if (typeQualifier.qualifier != EvqBuffer)
3642 {
3643 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3644 }
Olli Etuaho43364892017-02-13 16:00:12 +00003645
jchen10af713a22017-04-19 09:10:56 +08003646 // add array index
3647 unsigned int arraySize = 0;
3648 if (arrayIndex != nullptr)
3649 {
3650 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3651 }
3652
Olli Etuahoa78092c2018-09-26 14:16:13 +03003653 checkIndexIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.index);
3654
jchen10af713a22017-04-19 09:10:56 +08003655 if (mShaderVersion < 310)
3656 {
3657 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3658 }
3659 else
3660 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003661 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3662 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003663 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003664
Andrei Volykhina5527072017-03-22 16:46:30 +03003665 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3666
Jamie Madill099c0f32013-06-20 11:55:52 -04003667 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003668 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003669 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3670 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003671
Jamie Madill099c0f32013-06-20 11:55:52 -04003672 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3673 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003674 if (typeQualifier.qualifier == EvqUniform)
3675 {
3676 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3677 }
3678 else if (typeQualifier.qualifier == EvqBuffer)
3679 {
3680 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3681 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003682 }
3683
Jamie Madill1566ef72013-06-20 11:55:54 -04003684 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3685 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003686 if (typeQualifier.qualifier == EvqUniform)
3687 {
3688 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3689 }
3690 else if (typeQualifier.qualifier == EvqBuffer)
3691 {
3692 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3693 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003694 }
3695
Olli Etuaho856c4972016-08-08 11:38:39 +03003696 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003697
Martin Radev2cc85b32016-08-05 16:22:53 +03003698 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3699
Jamie Madill98493dd2013-07-08 14:39:03 -04003700 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303701 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3702 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003703 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303704 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003705 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303706 {
jchen10cc2a10e2017-05-03 14:05:12 +08003707 std::string reason("unsupported type - ");
3708 reason += fieldType->getBasicString();
3709 reason += " types are not allowed in interface blocks";
3710 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003711 }
3712
Jamie Madill98493dd2013-07-08 14:39:03 -04003713 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003714 switch (qualifier)
3715 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003716 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003717 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003718 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003719 if (typeQualifier.qualifier == EvqBuffer)
3720 {
3721 error(field->line(), "invalid qualifier on shader storage block member",
3722 getQualifierString(qualifier));
3723 }
3724 break;
3725 case EvqBuffer:
3726 if (typeQualifier.qualifier == EvqUniform)
3727 {
3728 error(field->line(), "invalid qualifier on uniform block member",
3729 getQualifierString(qualifier));
3730 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003731 break;
3732 default:
3733 error(field->line(), "invalid qualifier on interface block member",
3734 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003735 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003736 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003737
Martin Radev70866b82016-07-22 15:27:42 +03003738 if (fieldType->isInvariant())
3739 {
3740 error(field->line(), "invalid qualifier on interface block member", "invariant");
3741 }
3742
Jamie Madilla5efff92013-06-06 11:56:47 -04003743 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003744 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003745 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Olli Etuahoa78092c2018-09-26 14:16:13 +03003746 checkIndexIsNotSpecified(field->line(), fieldLayoutQualifier.index);
jchen10af713a22017-04-19 09:10:56 +08003747 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003748
Jamie Madill98493dd2013-07-08 14:39:03 -04003749 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003750 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003751 error(field->line(), "invalid layout qualifier: cannot be used here",
3752 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003753 }
3754
Jamie Madill98493dd2013-07-08 14:39:03 -04003755 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003756 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003757 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003758 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003759 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003760 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003761 warning(field->line(),
3762 "extraneous layout qualifier: only has an effect on matrix types",
3763 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003764 }
3765
Jamie Madill98493dd2013-07-08 14:39:03 -04003766 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003767
Olli Etuahoebee5b32017-11-23 12:56:32 +02003768 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3769 typeQualifier.qualifier != EvqBuffer)
3770 {
3771 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3772 checkIsNotUnsizedArray(field->line(),
3773 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003774 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003775 }
3776
Jiajia Qinbc585152017-06-23 15:42:17 +08003777 if (typeQualifier.qualifier == EvqBuffer)
3778 {
3779 // set memory qualifiers
3780 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3781 // qualified with a memory qualifier, it is as if all of its members were declared with
3782 // the same memory qualifier.
3783 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3784 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3785 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3786 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3787 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3788 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3789 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3790 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3791 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3792 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3793 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003794 }
3795
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003796 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003797 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003798 if (!symbolTable.declare(interfaceBlock))
Olli Etuaho378c3a52017-12-04 11:32:13 +02003799 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003800 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003801 }
3802
Olli Etuahob60d30f2018-01-16 12:31:06 +02003803 TType *interfaceBlockType =
3804 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003805 if (arrayIndex != nullptr)
3806 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003807 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003808 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003809
Olli Etuaho195be942017-12-04 23:40:14 +02003810 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003811 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3812 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003813 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003814 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003815 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003816
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003817 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003818 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003819 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003820 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3821 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003822 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003823 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003824
3825 // set parent pointer of the field variable
3826 fieldType->setInterfaceBlock(interfaceBlock);
3827
Olli Etuahob60d30f2018-01-16 12:31:06 +02003828 fieldType->setQualifier(typeQualifier.qualifier);
3829
Olli Etuaho195be942017-12-04 23:40:14 +02003830 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003831 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003832 if (!symbolTable.declare(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303833 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003834 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003835 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003836 }
3837 }
3838 }
3839 else
3840 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003841 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003842
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003843 // add a symbol for this interface block
Olli Etuaho437664b2018-02-28 15:38:14 +02003844 if (!symbolTable.declare(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303845 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003846 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003847 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003848 }
3849
Olli Etuaho195be942017-12-04 23:40:14 +02003850 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3851 blockSymbol->setLine(typeQualifier.line);
3852 TIntermDeclaration *declaration = new TIntermDeclaration();
3853 declaration->appendDeclarator(blockSymbol);
3854 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003855
3856 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003857 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003858}
3859
Olli Etuahofbb1c792018-01-19 16:26:59 +02003860void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3861 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003862{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003863 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003864
3865 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003866 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303867 if (mStructNestingLevel > 1)
3868 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003869 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003870 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003871}
3872
3873void TParseContext::exitStructDeclaration()
3874{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003875 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003876}
3877
Olli Etuaho8a176262016-08-16 14:23:01 +03003878void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003879{
Jamie Madillacb4b812016-11-07 13:50:29 -05003880 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303881 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003882 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003883 }
3884
Arun Patole7e7e68d2015-05-22 12:02:25 +05303885 if (field.type()->getBasicType() != EbtStruct)
3886 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003887 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003888 }
3889
3890 // We're already inside a structure definition at this point, so add
3891 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303892 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3893 {
Jamie Madill41a49272014-03-18 16:10:13 -04003894 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003895 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3896 {
3897 // This may happen in case there are nested struct definitions. While they are also
3898 // invalid GLSL, they don't cause a syntax error.
3899 reasonStream << "Struct nesting";
3900 }
3901 else
3902 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003903 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003904 }
3905 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003906 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003907 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003908 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003909 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003910}
3911
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003912//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003913// Parse an array index expression
3914//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003915TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3916 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303917 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003918{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003919 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3920 {
3921 if (baseExpression->getAsSymbolNode())
3922 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303923 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003924 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003925 }
3926 else
3927 {
3928 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3929 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003930
Olli Etuaho3ec75682017-07-05 17:02:55 +03003931 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003932 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003933
Jiawei Shaod8105a02017-08-08 09:54:36 +08003934 if (baseExpression->getQualifier() == EvqPerVertexIn)
3935 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003936 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003937 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3938 {
3939 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3940 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3941 }
3942 }
3943
Jamie Madill21c1e452014-12-29 11:33:41 -05003944 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3945
Olli Etuaho1dfd8ae2018-10-01 15:59:59 +03003946 // ANGLE should be able to fold any constant expressions resulting in an integer - but to be
3947 // safe we don't treat "EvqConst" that's evaluated according to the spec as being sufficient
3948 // for constness. Some interpretations of the spec have allowed constant expressions with side
3949 // effects - like array length() method on a non-constant array.
Olli Etuaho36b05142015-11-12 13:10:42 +02003950 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3951 {
3952 if (baseExpression->isInterfaceBlock())
3953 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003954 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003955 switch (baseExpression->getQualifier())
3956 {
3957 case EvqPerVertexIn:
3958 break;
3959 case EvqUniform:
3960 case EvqBuffer:
3961 error(location,
3962 "array indexes for uniform block arrays and shader storage block arrays "
3963 "must be constant integral expressions",
3964 "[");
3965 break;
3966 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003967 // We can reach here only in error cases.
3968 ASSERT(mDiagnostics->numErrors() > 0);
3969 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003970 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003971 }
3972 else if (baseExpression->getQualifier() == EvqFragmentOut)
3973 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003974 error(location,
3975 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003976 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003977 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3978 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003979 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003980 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003981 }
3982
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003983 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003984 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003985 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3986 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3987 // constant fold expressions that are not constant expressions). The most compatible way to
3988 // handle this case is to report a warning instead of an error and force the index to be in
3989 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003990 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003991 int index = 0;
3992 if (indexConstantUnion->getBasicType() == EbtInt)
3993 {
3994 index = indexConstantUnion->getIConst(0);
3995 }
3996 else if (indexConstantUnion->getBasicType() == EbtUInt)
3997 {
3998 index = static_cast<int>(indexConstantUnion->getUConst(0));
3999 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004000
4001 int safeIndex = -1;
4002
Olli Etuahoebee5b32017-11-23 12:56:32 +02004003 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004004 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004005 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4006 safeIndex = 0;
4007 }
4008
4009 if (!baseExpression->getType().isUnsizedArray())
4010 {
4011 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004012 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004013 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004014 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004015 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4016 {
4017 outOfRangeError(outOfRangeIndexIsError, location,
4018 "array index for gl_FragData must be zero when "
4019 "GL_EXT_draw_buffers is disabled",
4020 "[]");
4021 safeIndex = 0;
4022 }
4023 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004024 }
4025 // Only do generic out-of-range check if similar error hasn't already been reported.
4026 if (safeIndex < 0)
4027 {
4028 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004029 {
4030 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4031 baseExpression->getOutermostArraySize(),
4032 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004033 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004034 else if (baseExpression->isMatrix())
4035 {
4036 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4037 baseExpression->getType().getCols(),
4038 "matrix field selection out of range");
4039 }
4040 else
4041 {
4042 ASSERT(baseExpression->isVector());
4043 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4044 baseExpression->getType().getNominalSize(),
4045 "vector field selection out of range");
4046 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004047 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004048
Olli Etuahoebee5b32017-11-23 12:56:32 +02004049 ASSERT(safeIndex >= 0);
4050 // Data of constant unions can't be changed, because it may be shared with other
4051 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4052 // sanitized object.
4053 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4054 {
4055 TConstantUnion *safeConstantUnion = new TConstantUnion();
4056 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004057 indexExpression = new TIntermConstantUnion(
4058 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4059 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004060 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004061
Olli Etuahoebee5b32017-11-23 12:56:32 +02004062 TIntermBinary *node =
4063 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4064 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004065 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004066 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004067 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004068
Olli Etuaho94bbed12018-03-20 14:44:53 +02004069 markStaticReadIfSymbol(indexExpression);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004070 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4071 node->setLine(location);
4072 // Indirect indexing can never be constant folded.
4073 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004074}
4075
Olli Etuahoebee5b32017-11-23 12:56:32 +02004076int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4077 const TSourceLoc &location,
4078 int index,
4079 int arraySize,
4080 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004081{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004082 // Should not reach here with an unsized / runtime-sized array.
4083 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004084 // A negative index should already have been checked.
4085 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004086 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004087 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004088 std::stringstream reasonStream;
4089 reasonStream << reason << " '" << index << "'";
4090 std::string token = reasonStream.str();
4091 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004092 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004093 }
4094 return index;
4095}
4096
Jamie Madillb98c3a82015-07-23 14:26:04 -04004097TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4098 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004099 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004100 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004101{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004102 if (baseExpression->isArray())
4103 {
4104 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004105 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004106 }
4107
4108 if (baseExpression->isVector())
4109 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004110 TVector<int> fieldOffsets;
4111 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4112 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004113 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004114 fieldOffsets.resize(1);
4115 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004116 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004117 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4118 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004119
Olli Etuaho765924f2018-01-04 12:48:36 +02004120 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004122 else if (baseExpression->getBasicType() == EbtStruct)
4123 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304124 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004125 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004126 {
4127 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004128 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004129 }
4130 else
4131 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004132 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004133 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004134 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004135 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004136 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004137 {
4138 fieldFound = true;
4139 break;
4140 }
4141 }
4142 if (fieldFound)
4143 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004144 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004145 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004146 TIntermBinary *node =
4147 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4148 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004149 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004150 }
4151 else
4152 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004153 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004154 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004155 }
4156 }
4157 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004158 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004159 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304160 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004161 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004162 {
4163 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004164 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004165 }
4166 else
4167 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004168 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004169 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004170 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004171 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004172 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004173 {
4174 fieldFound = true;
4175 break;
4176 }
4177 }
4178 if (fieldFound)
4179 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004180 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004181 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004182 TIntermBinary *node =
4183 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4184 node->setLine(dotLocation);
4185 // Indexing interface blocks can never be constant folded.
4186 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004187 }
4188 else
4189 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004190 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004191 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004192 }
4193 }
4194 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004195 else
4196 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004197 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004198 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004199 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004200 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004201 }
4202 else
4203 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304204 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004205 " field selection requires structure, vector, or interface block on left hand "
4206 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004207 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004208 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004209 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004210 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004211}
4212
Olli Etuahofbb1c792018-01-19 16:26:59 +02004213TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004214 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004215{
Jamie Madill2f294c92017-11-20 14:47:26 -05004216 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004217
4218 if (qualifierType == "shared")
4219 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004220 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004221 {
4222 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4223 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004224 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004225 }
4226 else if (qualifierType == "packed")
4227 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004228 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004229 {
4230 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4231 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004232 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004233 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004234 else if (qualifierType == "std430")
4235 {
4236 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4237 qualifier.blockStorage = EbsStd430;
4238 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004239 else if (qualifierType == "std140")
4240 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004241 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004242 }
4243 else if (qualifierType == "row_major")
4244 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004245 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004246 }
4247 else if (qualifierType == "column_major")
4248 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004249 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004250 }
4251 else if (qualifierType == "location")
4252 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004253 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004254 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004255 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004256 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004257 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004258 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4259 {
4260 qualifier.yuv = true;
4261 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004262 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004263 else if (qualifierType == "rgba32f")
4264 {
4265 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4266 qualifier.imageInternalFormat = EiifRGBA32F;
4267 }
4268 else if (qualifierType == "rgba16f")
4269 {
4270 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4271 qualifier.imageInternalFormat = EiifRGBA16F;
4272 }
4273 else if (qualifierType == "r32f")
4274 {
4275 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4276 qualifier.imageInternalFormat = EiifR32F;
4277 }
4278 else if (qualifierType == "rgba8")
4279 {
4280 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4281 qualifier.imageInternalFormat = EiifRGBA8;
4282 }
4283 else if (qualifierType == "rgba8_snorm")
4284 {
4285 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4286 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4287 }
4288 else if (qualifierType == "rgba32i")
4289 {
4290 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4291 qualifier.imageInternalFormat = EiifRGBA32I;
4292 }
4293 else if (qualifierType == "rgba16i")
4294 {
4295 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4296 qualifier.imageInternalFormat = EiifRGBA16I;
4297 }
4298 else if (qualifierType == "rgba8i")
4299 {
4300 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4301 qualifier.imageInternalFormat = EiifRGBA8I;
4302 }
4303 else if (qualifierType == "r32i")
4304 {
4305 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4306 qualifier.imageInternalFormat = EiifR32I;
4307 }
4308 else if (qualifierType == "rgba32ui")
4309 {
4310 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4311 qualifier.imageInternalFormat = EiifRGBA32UI;
4312 }
4313 else if (qualifierType == "rgba16ui")
4314 {
4315 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4316 qualifier.imageInternalFormat = EiifRGBA16UI;
4317 }
4318 else if (qualifierType == "rgba8ui")
4319 {
4320 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4321 qualifier.imageInternalFormat = EiifRGBA8UI;
4322 }
4323 else if (qualifierType == "r32ui")
4324 {
4325 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4326 qualifier.imageInternalFormat = EiifR32UI;
4327 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004328 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4329 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004330 {
4331 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4332 qualifier.primitiveType = EptPoints;
4333 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004334 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4335 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004336 {
4337 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4338 qualifier.primitiveType = EptLines;
4339 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004340 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4341 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004342 {
4343 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4344 qualifier.primitiveType = EptLinesAdjacency;
4345 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004346 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4347 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004348 {
4349 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4350 qualifier.primitiveType = EptTriangles;
4351 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004352 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4353 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004354 {
4355 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4356 qualifier.primitiveType = EptTrianglesAdjacency;
4357 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004358 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4359 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004360 {
4361 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4362 qualifier.primitiveType = EptLineStrip;
4363 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004364 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4365 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004366 {
4367 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4368 qualifier.primitiveType = EptTriangleStrip;
4369 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004370
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004371 else
4372 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004373 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004374 }
4375
Jamie Madilla5efff92013-06-06 11:56:47 -04004376 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004377}
4378
Olli Etuahofbb1c792018-01-19 16:26:59 +02004379void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004380 const TSourceLoc &qualifierTypeLine,
4381 int intValue,
4382 const TSourceLoc &intValueLine,
4383 const std::string &intValueString,
4384 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004385 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004386{
Olli Etuaho856c4972016-08-08 11:38:39 +03004387 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004388 if (intValue < 1)
4389 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004390 std::stringstream reasonStream;
4391 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4392 std::string reason = reasonStream.str();
4393 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004394 }
4395 (*localSize)[index] = intValue;
4396}
4397
Olli Etuaho09b04a22016-12-15 13:30:26 +00004398void TParseContext::parseNumViews(int intValue,
4399 const TSourceLoc &intValueLine,
4400 const std::string &intValueString,
4401 int *numViews)
4402{
4403 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4404 // specification.
4405 if (intValue < 1)
4406 {
4407 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4408 }
4409 *numViews = intValue;
4410}
4411
Shaob5cc1192017-07-06 10:47:20 +08004412void TParseContext::parseInvocations(int intValue,
4413 const TSourceLoc &intValueLine,
4414 const std::string &intValueString,
4415 int *numInvocations)
4416{
4417 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4418 // it doesn't make sense to accept invocations <= 0.
4419 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4420 {
4421 error(intValueLine,
4422 "out of range: invocations must be in the range of [1, "
4423 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4424 intValueString.c_str());
4425 }
4426 else
4427 {
4428 *numInvocations = intValue;
4429 }
4430}
4431
4432void TParseContext::parseMaxVertices(int intValue,
4433 const TSourceLoc &intValueLine,
4434 const std::string &intValueString,
4435 int *maxVertices)
4436{
4437 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4438 // it doesn't make sense to accept max_vertices < 0.
4439 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4440 {
4441 error(
4442 intValueLine,
4443 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4444 intValueString.c_str());
4445 }
4446 else
4447 {
4448 *maxVertices = intValue;
4449 }
4450}
4451
Olli Etuahoa78092c2018-09-26 14:16:13 +03004452void TParseContext::parseIndexLayoutQualifier(int intValue,
4453 const TSourceLoc &intValueLine,
4454 const std::string &intValueString,
4455 int *index)
4456{
4457 // EXT_blend_func_extended specifies that most validation should happen at link time, but since
4458 // we're validating output variable locations at compile time, it makes sense to validate that
4459 // index is 0 or 1 also at compile time. Also since we use "-1" as a placeholder for unspecified
4460 // index, we can't accept it here.
4461 if (intValue < 0 || intValue > 1)
4462 {
4463 error(intValueLine, "out of range: index layout qualifier can only be 0 or 1",
4464 intValueString.c_str());
4465 }
4466 else
4467 {
4468 *index = intValue;
4469 }
4470}
4471
Olli Etuahofbb1c792018-01-19 16:26:59 +02004472TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004473 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004474 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304475 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004476{
Jamie Madill2f294c92017-11-20 14:47:26 -05004477 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004478
Martin Radev802abe02016-08-04 17:48:32 +03004479 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004480
Martin Radev802abe02016-08-04 17:48:32 +03004481 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004482 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004483 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004484 if (intValue < 0)
4485 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004486 error(intValueLine, "out of range: location must be non-negative",
4487 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004488 }
4489 else
4490 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004491 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004492 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004493 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004494 }
Olli Etuaho43364892017-02-13 16:00:12 +00004495 else if (qualifierType == "binding")
4496 {
4497 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4498 if (intValue < 0)
4499 {
4500 error(intValueLine, "out of range: binding must be non-negative",
4501 intValueString.c_str());
4502 }
4503 else
4504 {
4505 qualifier.binding = intValue;
4506 }
4507 }
jchen104cdac9e2017-05-08 11:01:20 +08004508 else if (qualifierType == "offset")
4509 {
4510 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4511 if (intValue < 0)
4512 {
4513 error(intValueLine, "out of range: offset must be non-negative",
4514 intValueString.c_str());
4515 }
4516 else
4517 {
4518 qualifier.offset = intValue;
4519 }
4520 }
Martin Radev802abe02016-08-04 17:48:32 +03004521 else if (qualifierType == "local_size_x")
4522 {
4523 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4524 &qualifier.localSize);
4525 }
4526 else if (qualifierType == "local_size_y")
4527 {
4528 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4529 &qualifier.localSize);
4530 }
4531 else if (qualifierType == "local_size_z")
4532 {
4533 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4534 &qualifier.localSize);
4535 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004536 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004537 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004538 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4539 {
4540 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4541 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004542 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004543 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4544 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004545 {
4546 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4547 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004548 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4549 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004550 {
4551 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4552 }
Olli Etuahoa78092c2018-09-26 14:16:13 +03004553 else if (qualifierType == "index" && mShaderType == GL_FRAGMENT_SHADER &&
4554 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_blend_func_extended))
4555 {
4556 parseIndexLayoutQualifier(intValue, intValueLine, intValueString, &qualifier.index);
4557 }
Martin Radev802abe02016-08-04 17:48:32 +03004558 else
4559 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004560 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004561 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004562
Jamie Madilla5efff92013-06-06 11:56:47 -04004563 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004564}
4565
Olli Etuaho613b9592016-09-05 12:05:53 +03004566TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4567{
4568 return new TTypeQualifierBuilder(
4569 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4570 mShaderVersion);
4571}
4572
Olli Etuahocce89652017-06-19 16:04:09 +03004573TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4574 const TSourceLoc &loc)
4575{
4576 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4577 return new TStorageQualifierWrapper(qualifier, loc);
4578}
4579
4580TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4581{
4582 if (getShaderType() == GL_VERTEX_SHADER)
4583 {
4584 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4585 }
4586 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4587}
4588
4589TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4590{
4591 if (declaringFunction())
4592 {
4593 return new TStorageQualifierWrapper(EvqIn, loc);
4594 }
Shaob5cc1192017-07-06 10:47:20 +08004595
4596 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004597 {
Shaob5cc1192017-07-06 10:47:20 +08004598 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004599 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004600 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004601 {
4602 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4603 }
4604 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004605 }
Shaob5cc1192017-07-06 10:47:20 +08004606 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004607 {
Shaob5cc1192017-07-06 10:47:20 +08004608 if (mShaderVersion < 300)
4609 {
4610 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4611 }
4612 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004613 }
Shaob5cc1192017-07-06 10:47:20 +08004614 case GL_COMPUTE_SHADER:
4615 {
4616 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4617 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004618 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004619 {
4620 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4621 }
4622 default:
4623 {
4624 UNREACHABLE();
4625 return new TStorageQualifierWrapper(EvqLast, loc);
4626 }
Olli Etuahocce89652017-06-19 16:04:09 +03004627 }
Olli Etuahocce89652017-06-19 16:04:09 +03004628}
4629
4630TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4631{
4632 if (declaringFunction())
4633 {
4634 return new TStorageQualifierWrapper(EvqOut, loc);
4635 }
Shaob5cc1192017-07-06 10:47:20 +08004636 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004637 {
Shaob5cc1192017-07-06 10:47:20 +08004638 case GL_VERTEX_SHADER:
4639 {
4640 if (mShaderVersion < 300)
4641 {
4642 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4643 }
4644 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4645 }
4646 case GL_FRAGMENT_SHADER:
4647 {
4648 if (mShaderVersion < 300)
4649 {
4650 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4651 }
4652 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4653 }
4654 case GL_COMPUTE_SHADER:
4655 {
4656 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4657 return new TStorageQualifierWrapper(EvqLast, loc);
4658 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004659 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004660 {
4661 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4662 }
4663 default:
4664 {
4665 UNREACHABLE();
4666 return new TStorageQualifierWrapper(EvqLast, loc);
4667 }
Olli Etuahocce89652017-06-19 16:04:09 +03004668 }
Olli Etuahocce89652017-06-19 16:04:09 +03004669}
4670
4671TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4672{
4673 if (!declaringFunction())
4674 {
4675 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4676 }
4677 return new TStorageQualifierWrapper(EvqInOut, loc);
4678}
4679
Jamie Madillb98c3a82015-07-23 14:26:04 -04004680TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004681 TLayoutQualifier rightQualifier,
4682 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004683{
Martin Radevc28888b2016-07-22 15:27:42 +03004684 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004685 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004686}
4687
Olli Etuahofbb1c792018-01-19 16:26:59 +02004688TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4689 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004690{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004691 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004692 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004693}
4694
Olli Etuahofbb1c792018-01-19 16:26:59 +02004695TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004696 const TSourceLoc &loc,
4697 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004698{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004699 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004700 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004701}
4702
Olli Etuaho722bfb52017-10-26 17:00:11 +03004703void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4704 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004705 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004706 const TSourceLoc &location)
4707{
4708 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4709 {
4710 if ((*fieldIter)->name() == name)
4711 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004712 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004713 }
4714 }
4715}
4716
4717TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4718{
4719 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4720 ++fieldIter)
4721 {
4722 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4723 location);
4724 }
4725 return fields;
4726}
4727
Olli Etuaho4de340a2016-12-16 09:32:03 +00004728TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4729 const TFieldList *newlyAddedFields,
4730 const TSourceLoc &location)
4731{
4732 for (TField *field : *newlyAddedFields)
4733 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004734 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4735 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004736 processedFields->push_back(field);
4737 }
4738 return processedFields;
4739}
4740
Martin Radev70866b82016-07-22 15:27:42 +03004741TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4742 const TTypeQualifierBuilder &typeQualifierBuilder,
4743 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004744 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004745{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004746 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004747
Martin Radev70866b82016-07-22 15:27:42 +03004748 typeSpecifier->qualifier = typeQualifier.qualifier;
4749 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004750 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004751 typeSpecifier->invariant = typeQualifier.invariant;
4752 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304753 {
Martin Radev70866b82016-07-22 15:27:42 +03004754 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004755 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004756 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004757}
4758
Jamie Madillb98c3a82015-07-23 14:26:04 -04004759TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004760 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004761{
Martin Radev4a9cd802016-09-01 16:51:51 +03004762 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4763 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004764
Olli Etuahofbb1c792018-01-19 16:26:59 +02004765 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004766 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004767
Martin Radev4a9cd802016-09-01 16:51:51 +03004768 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004769
Olli Etuahod5f44c92017-11-29 17:15:40 +02004770 TFieldList *fieldList = new TFieldList();
4771
4772 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304773 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004774 TType *type = new TType(typeSpecifier);
4775 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304776 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004777 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004778 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004779 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004780 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004781
Jamie Madillf5557ac2018-06-15 09:46:58 -04004782 TField *field =
4783 new TField(type, declarator->name(), declarator->line(), SymbolType::UserDefined);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004784 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4785 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004786 }
4787
Olli Etuahod5f44c92017-11-29 17:15:40 +02004788 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004789}
4790
Martin Radev4a9cd802016-09-01 16:51:51 +03004791TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4792 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004793 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004794 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004795{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004796 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004797 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004798 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004799 structSymbolType = SymbolType::Empty;
4800 }
4801 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004802
Jamie Madill9b820842015-02-12 10:40:10 -05004803 // Store a bool in the struct if we're at global scope, to allow us to
4804 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004805 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004806
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004807 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004808 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004809 checkIsNotReserved(nameLine, structName);
Olli Etuaho437664b2018-02-28 15:38:14 +02004810 if (!symbolTable.declare(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304811 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004812 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004813 }
4814 }
4815
4816 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004817 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004818 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004819 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004820 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004821 switch (qualifier)
4822 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004823 case EvqGlobal:
4824 case EvqTemporary:
4825 break;
4826 default:
4827 error(field.line(), "invalid qualifier on struct member",
4828 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004829 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004830 }
Martin Radev70866b82016-07-22 15:27:42 +03004831 if (field.type()->isInvariant())
4832 {
4833 error(field.line(), "invalid qualifier on struct member", "invariant");
4834 }
jchen104cdac9e2017-05-08 11:01:20 +08004835 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4836 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004837 {
4838 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4839 }
4840
Olli Etuahoebee5b32017-11-23 12:56:32 +02004841 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004842 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004843
Olli Etuaho43364892017-02-13 16:00:12 +00004844 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4845
Olli Etuahoa78092c2018-09-26 14:16:13 +03004846 checkIndexIsNotSpecified(field.line(), field.type()->getLayoutQualifier().index);
4847
Olli Etuaho43364892017-02-13 16:00:12 +00004848 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004849
4850 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004851 }
4852
Martin Radev4a9cd802016-09-01 16:51:51 +03004853 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004854 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004855 exitStructDeclaration();
4856
Martin Radev4a9cd802016-09-01 16:51:51 +03004857 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004858}
4859
Jamie Madillb98c3a82015-07-23 14:26:04 -04004860TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004861 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004862 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004863{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004864 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004865 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004866 init->isVector())
4867 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004868 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4869 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004870 return nullptr;
4871 }
4872
Olli Etuaho923ecef2017-10-11 12:01:38 +03004873 ASSERT(statementList);
Olli Etuahod05f9642018-03-05 12:13:26 +02004874 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004875 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004876 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004877 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004878 }
4879
Olli Etuaho94bbed12018-03-20 14:44:53 +02004880 markStaticReadIfSymbol(init);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004881 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4882 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004883 return node;
4884}
4885
4886TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4887{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004888 if (mSwitchNestingLevel == 0)
4889 {
4890 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004891 return nullptr;
4892 }
4893 if (condition == nullptr)
4894 {
4895 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004896 return nullptr;
4897 }
4898 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004899 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004900 {
4901 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004902 }
4903 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho1dfd8ae2018-10-01 15:59:59 +03004904 // ANGLE should be able to fold any EvqConst expressions resulting in an integer - but to be
4905 // safe against corner cases we still check for conditionConst. Some interpretations of the
4906 // spec have allowed constant expressions with side effects - like array length() method on a
4907 // non-constant array.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004908 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004909 {
4910 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004911 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004912 TIntermCase *node = new TIntermCase(condition);
4913 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004914 return node;
4915}
4916
4917TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4918{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004919 if (mSwitchNestingLevel == 0)
4920 {
4921 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004922 return nullptr;
4923 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004924 TIntermCase *node = new TIntermCase(nullptr);
4925 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004926 return node;
4927}
4928
Jamie Madillb98c3a82015-07-23 14:26:04 -04004929TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4930 TIntermTyped *child,
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004931 const TSourceLoc &loc,
4932 const TFunction *func)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004933{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004934 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004935
4936 switch (op)
4937 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004938 case EOpLogicalNot:
4939 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4940 child->isVector())
4941 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004942 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004943 return nullptr;
4944 }
4945 break;
4946 case EOpBitwiseNot:
4947 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4948 child->isMatrix() || child->isArray())
4949 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004950 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004951 return nullptr;
4952 }
4953 break;
4954 case EOpPostIncrement:
4955 case EOpPreIncrement:
4956 case EOpPostDecrement:
4957 case EOpPreDecrement:
4958 case EOpNegative:
4959 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004960 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4961 child->getBasicType() == EbtBool || child->isArray() ||
Geoff Lang6aab06e2018-11-20 14:04:11 -05004962 child->getBasicType() == EbtVoid || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004963 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004964 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004965 return nullptr;
4966 }
Nico Weber41b072b2018-02-09 10:01:32 -05004967 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004968 // Operators for built-ins are already type checked against their prototype.
4969 default:
4970 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004971 }
4972
Jiajia Qinbc585152017-06-23 15:42:17 +08004973 if (child->getMemoryQualifier().writeonly)
4974 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004975 unaryOpError(loc, GetOperatorString(op), child->getType());
Jiajia Qinbc585152017-06-23 15:42:17 +08004976 return nullptr;
4977 }
4978
Olli Etuaho94bbed12018-03-20 14:44:53 +02004979 markStaticReadIfSymbol(child);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004980 TIntermUnary *node = new TIntermUnary(op, child, func);
Olli Etuahof119a262016-08-19 15:54:22 +03004981 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004982
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004983 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004984}
4985
Olli Etuaho09b22472015-02-11 11:47:26 +02004986TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4987{
Olli Etuahocce89652017-06-19 16:04:09 +03004988 ASSERT(op != EOpNull);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004989 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004990 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004991 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004992 return child;
4993 }
4994 return node;
4995}
4996
Jamie Madillb98c3a82015-07-23 14:26:04 -04004997TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4998 TIntermTyped *child,
4999 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005000{
Olli Etuaho856c4972016-08-08 11:38:39 +03005001 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02005002 return addUnaryMath(op, child, loc);
5003}
5004
Olli Etuaho765924f2018-01-04 12:48:36 +02005005TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
5006{
5007 // If we can, we should return the folded version of the expression for subsequent parsing. This
5008 // enables folding the containing expression during parsing as well, instead of the separate
5009 // FoldExpressions() step where folding nested expressions requires multiple full AST
5010 // traversals.
5011
5012 // Even if folding fails the fold() functions return some node representing the expression,
5013 // typically the original node. So "folded" can be assumed to be non-null.
5014 TIntermTyped *folded = expression->fold(mDiagnostics);
5015 ASSERT(folded != nullptr);
5016 if (folded->getQualifier() == expression->getQualifier())
5017 {
5018 // We need this expression to have the correct qualifier when validating the consuming
5019 // expression. So we can only return the folded node from here in case it has the same
5020 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
5021 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
5022 // 1. (true ? 1.0 : non_constant)
5023 // 2. (non_constant, 1.0)
5024 return folded;
5025 }
5026 return expression;
5027}
5028
Jamie Madillb98c3a82015-07-23 14:26:04 -04005029bool TParseContext::binaryOpCommonCheck(TOperator op,
5030 TIntermTyped *left,
5031 TIntermTyped *right,
5032 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005033{
jchen10b4cf5652017-05-05 18:51:17 +08005034 // Check opaque types are not allowed to be operands in expressions other than array indexing
5035 // and structure member selection.
5036 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
5037 {
5038 switch (op)
5039 {
5040 case EOpIndexDirect:
5041 case EOpIndexIndirect:
5042 break;
jchen10b4cf5652017-05-05 18:51:17 +08005043
5044 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05005045 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08005046 error(loc, "Invalid operation for variables with an opaque type",
5047 GetOperatorString(op));
5048 return false;
5049 }
5050 }
jchen10cc2a10e2017-05-03 14:05:12 +08005051
Jiajia Qinbc585152017-06-23 15:42:17 +08005052 if (right->getMemoryQualifier().writeonly)
5053 {
5054 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5055 return false;
5056 }
5057
5058 if (left->getMemoryQualifier().writeonly)
5059 {
5060 switch (op)
5061 {
5062 case EOpAssign:
5063 case EOpInitialize:
5064 case EOpIndexDirect:
5065 case EOpIndexIndirect:
5066 case EOpIndexDirectStruct:
5067 case EOpIndexDirectInterfaceBlock:
5068 break;
5069 default:
5070 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5071 return false;
5072 }
5073 }
5074
Olli Etuaho244be012016-08-18 15:26:02 +03005075 if (left->getType().getStruct() || right->getType().getStruct())
5076 {
5077 switch (op)
5078 {
5079 case EOpIndexDirectStruct:
5080 ASSERT(left->getType().getStruct());
5081 break;
5082 case EOpEqual:
5083 case EOpNotEqual:
5084 case EOpAssign:
5085 case EOpInitialize:
5086 if (left->getType() != right->getType())
5087 {
5088 return false;
5089 }
5090 break;
5091 default:
5092 error(loc, "Invalid operation for structs", GetOperatorString(op));
5093 return false;
5094 }
5095 }
5096
Olli Etuaho94050052017-05-08 14:17:44 +03005097 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5098 {
5099 switch (op)
5100 {
5101 case EOpIndexDirectInterfaceBlock:
5102 ASSERT(left->getType().getInterfaceBlock());
5103 break;
5104 default:
5105 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5106 return false;
5107 }
5108 }
5109
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005110 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005111 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005112 error(loc, "array / non-array mismatch", GetOperatorString(op));
5113 return false;
5114 }
5115
5116 if (left->isArray())
5117 {
5118 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005119 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005120 {
5121 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5122 return false;
5123 }
5124
Olli Etuahoe79904c2015-03-18 16:56:42 +02005125 switch (op)
5126 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005127 case EOpEqual:
5128 case EOpNotEqual:
5129 case EOpAssign:
5130 case EOpInitialize:
5131 break;
5132 default:
5133 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5134 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005135 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005136 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005137 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005138 {
5139 error(loc, "array size mismatch", GetOperatorString(op));
5140 return false;
5141 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005142 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005143
5144 // Check ops which require integer / ivec parameters
5145 bool isBitShift = false;
5146 switch (op)
5147 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005148 case EOpBitShiftLeft:
5149 case EOpBitShiftRight:
5150 case EOpBitShiftLeftAssign:
5151 case EOpBitShiftRightAssign:
5152 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5153 // check that the basic type is an integer type.
5154 isBitShift = true;
5155 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5156 {
5157 return false;
5158 }
5159 break;
5160 case EOpBitwiseAnd:
5161 case EOpBitwiseXor:
5162 case EOpBitwiseOr:
5163 case EOpBitwiseAndAssign:
5164 case EOpBitwiseXorAssign:
5165 case EOpBitwiseOrAssign:
5166 // It is enough to check the type of only one operand, since later it
5167 // is checked that the operand types match.
5168 if (!IsInteger(left->getBasicType()))
5169 {
5170 return false;
5171 }
5172 break;
5173 default:
5174 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005175 }
5176
5177 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5178 // So the basic type should usually match.
5179 if (!isBitShift && left->getBasicType() != right->getBasicType())
5180 {
5181 return false;
5182 }
5183
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005184 // Check that:
5185 // 1. Type sizes match exactly on ops that require that.
5186 // 2. Restrictions for structs that contain arrays or samplers are respected.
5187 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005188 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005189 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005190 case EOpAssign:
5191 case EOpInitialize:
5192 case EOpEqual:
5193 case EOpNotEqual:
5194 // ESSL 1.00 sections 5.7, 5.8, 5.9
5195 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5196 {
5197 error(loc, "undefined operation for structs containing arrays",
5198 GetOperatorString(op));
5199 return false;
5200 }
5201 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5202 // we interpret the spec so that this extends to structs containing samplers,
5203 // similarly to ESSL 1.00 spec.
5204 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5205 left->getType().isStructureContainingSamplers())
5206 {
5207 error(loc, "undefined operation for structs containing samplers",
5208 GetOperatorString(op));
5209 return false;
5210 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005211
Olli Etuahoe1805592017-01-02 16:41:20 +00005212 if ((left->getNominalSize() != right->getNominalSize()) ||
5213 (left->getSecondarySize() != right->getSecondarySize()))
5214 {
5215 error(loc, "dimension mismatch", GetOperatorString(op));
5216 return false;
5217 }
5218 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005219 case EOpLessThan:
5220 case EOpGreaterThan:
5221 case EOpLessThanEqual:
5222 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005223 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005224 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005225 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005226 return false;
5227 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005228 break;
5229 case EOpAdd:
5230 case EOpSub:
5231 case EOpDiv:
5232 case EOpIMod:
5233 case EOpBitShiftLeft:
5234 case EOpBitShiftRight:
5235 case EOpBitwiseAnd:
5236 case EOpBitwiseXor:
5237 case EOpBitwiseOr:
5238 case EOpAddAssign:
5239 case EOpSubAssign:
5240 case EOpDivAssign:
5241 case EOpIModAssign:
5242 case EOpBitShiftLeftAssign:
5243 case EOpBitShiftRightAssign:
5244 case EOpBitwiseAndAssign:
5245 case EOpBitwiseXorAssign:
5246 case EOpBitwiseOrAssign:
5247 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5248 {
5249 return false;
5250 }
5251
5252 // Are the sizes compatible?
5253 if (left->getNominalSize() != right->getNominalSize() ||
5254 left->getSecondarySize() != right->getSecondarySize())
5255 {
5256 // If the nominal sizes of operands do not match:
5257 // One of them must be a scalar.
5258 if (!left->isScalar() && !right->isScalar())
5259 return false;
5260
5261 // In the case of compound assignment other than multiply-assign,
5262 // the right side needs to be a scalar. Otherwise a vector/matrix
5263 // would be assigned to a scalar. A scalar can't be shifted by a
5264 // vector either.
5265 if (!right->isScalar() &&
5266 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5267 return false;
5268 }
5269 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005270 default:
5271 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005272 }
5273
Olli Etuahod6b14282015-03-17 14:31:35 +02005274 return true;
5275}
5276
Olli Etuaho1dded802016-08-18 18:13:13 +03005277bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5278 const TType &left,
5279 const TType &right)
5280{
5281 switch (op)
5282 {
5283 case EOpMul:
5284 case EOpMulAssign:
5285 return left.getNominalSize() == right.getNominalSize() &&
5286 left.getSecondarySize() == right.getSecondarySize();
5287 case EOpVectorTimesScalar:
5288 return true;
5289 case EOpVectorTimesScalarAssign:
5290 ASSERT(!left.isMatrix() && !right.isMatrix());
5291 return left.isVector() && !right.isVector();
5292 case EOpVectorTimesMatrix:
5293 return left.getNominalSize() == right.getRows();
5294 case EOpVectorTimesMatrixAssign:
5295 ASSERT(!left.isMatrix() && right.isMatrix());
5296 return left.isVector() && left.getNominalSize() == right.getRows() &&
5297 left.getNominalSize() == right.getCols();
5298 case EOpMatrixTimesVector:
5299 return left.getCols() == right.getNominalSize();
5300 case EOpMatrixTimesScalar:
5301 return true;
5302 case EOpMatrixTimesScalarAssign:
5303 ASSERT(left.isMatrix() && !right.isMatrix());
5304 return !right.isVector();
5305 case EOpMatrixTimesMatrix:
5306 return left.getCols() == right.getRows();
5307 case EOpMatrixTimesMatrixAssign:
5308 ASSERT(left.isMatrix() && right.isMatrix());
5309 // We need to check two things:
5310 // 1. The matrix multiplication step is valid.
5311 // 2. The result will have the same number of columns as the lvalue.
5312 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5313
5314 default:
5315 UNREACHABLE();
5316 return false;
5317 }
5318}
5319
Jamie Madillb98c3a82015-07-23 14:26:04 -04005320TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5321 TIntermTyped *left,
5322 TIntermTyped *right,
5323 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005324{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005325 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005326 return nullptr;
5327
Olli Etuahofc1806e2015-03-17 13:03:11 +02005328 switch (op)
5329 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005330 case EOpEqual:
5331 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005332 case EOpLessThan:
5333 case EOpGreaterThan:
5334 case EOpLessThanEqual:
5335 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005336 break;
5337 case EOpLogicalOr:
5338 case EOpLogicalXor:
5339 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005340 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5341 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005342 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005343 {
5344 return nullptr;
5345 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005346 // Basic types matching should have been already checked.
5347 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005348 break;
5349 case EOpAdd:
5350 case EOpSub:
5351 case EOpDiv:
5352 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005353 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5354 !right->getType().getStruct());
5355 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005356 {
5357 return nullptr;
5358 }
5359 break;
5360 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005361 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5362 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005363 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005364 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005365 {
5366 return nullptr;
5367 }
5368 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005369 default:
5370 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005371 }
5372
Olli Etuaho1dded802016-08-18 18:13:13 +03005373 if (op == EOpMul)
5374 {
5375 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5376 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5377 {
5378 return nullptr;
5379 }
5380 }
5381
Olli Etuaho3fdec912016-08-18 15:08:06 +03005382 TIntermBinary *node = new TIntermBinary(op, left, right);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005383 ASSERT(op != EOpAssign);
5384 markStaticReadIfSymbol(left);
5385 markStaticReadIfSymbol(right);
Olli Etuaho3fdec912016-08-18 15:08:06 +03005386 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005387 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005388}
5389
Jamie Madillb98c3a82015-07-23 14:26:04 -04005390TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5391 TIntermTyped *left,
5392 TIntermTyped *right,
5393 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005394{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005395 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005396 if (node == 0)
5397 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005398 binaryOpError(loc, GetOperatorString(op), left->getType(), right->getType());
Olli Etuaho09b22472015-02-11 11:47:26 +02005399 return left;
5400 }
5401 return node;
5402}
5403
Jamie Madillb98c3a82015-07-23 14:26:04 -04005404TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5405 TIntermTyped *left,
5406 TIntermTyped *right,
5407 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005408{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005409 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005410 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005411 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005412 binaryOpError(loc, GetOperatorString(op), left->getType(), right->getType());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005413 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005414 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005415 }
5416 return node;
5417}
5418
Jamie Madillb98c3a82015-07-23 14:26:04 -04005419TIntermTyped *TParseContext::addAssign(TOperator op,
5420 TIntermTyped *left,
5421 TIntermTyped *right,
5422 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005423{
Olli Etuahocce89652017-06-19 16:04:09 +03005424 checkCanBeLValue(loc, "assign", left);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005425 TIntermBinary *node = nullptr;
5426 if (binaryOpCommonCheck(op, left, right, loc))
5427 {
5428 if (op == EOpMulAssign)
5429 {
5430 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5431 if (isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5432 {
5433 node = new TIntermBinary(op, left, right);
5434 }
5435 }
5436 else
5437 {
5438 node = new TIntermBinary(op, left, right);
5439 }
5440 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005441 if (node == nullptr)
5442 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005443 assignError(loc, "assign", left->getType(), right->getType());
Olli Etuahod6b14282015-03-17 14:31:35 +02005444 return left;
5445 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02005446 if (op != EOpAssign)
5447 {
5448 markStaticReadIfSymbol(left);
5449 }
5450 markStaticReadIfSymbol(right);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005451 node->setLine(loc);
Olli Etuahod6b14282015-03-17 14:31:35 +02005452 return node;
5453}
5454
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005455TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5456 TIntermTyped *right,
5457 const TSourceLoc &loc)
5458{
Corentin Wallez0d959252016-07-12 17:26:32 -04005459 // WebGL2 section 5.26, the following results in an error:
5460 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005461 if (mShaderSpec == SH_WEBGL2_SPEC &&
5462 (left->isArray() || left->getBasicType() == EbtVoid ||
5463 left->getType().isStructureContainingArrays() || right->isArray() ||
5464 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005465 {
5466 error(loc,
5467 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5468 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005469 }
5470
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005471 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005472 markStaticReadIfSymbol(left);
5473 markStaticReadIfSymbol(right);
5474 commaNode->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005475
5476 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005477}
5478
Olli Etuaho49300862015-02-20 14:54:49 +02005479TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5480{
5481 switch (op)
5482 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005483 case EOpContinue:
5484 if (mLoopNestingLevel <= 0)
5485 {
5486 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005487 }
5488 break;
5489 case EOpBreak:
5490 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5491 {
5492 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005493 }
5494 break;
5495 case EOpReturn:
5496 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5497 {
5498 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005499 }
5500 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005501 case EOpKill:
5502 if (mShaderType != GL_FRAGMENT_SHADER)
5503 {
5504 error(loc, "discard supported in fragment shaders only", "discard");
5505 }
5506 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005507 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005508 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005509 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005510 }
Olli Etuahocce89652017-06-19 16:04:09 +03005511 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005512}
5513
Jamie Madillb98c3a82015-07-23 14:26:04 -04005514TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005515 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005516 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005517{
Olli Etuahocce89652017-06-19 16:04:09 +03005518 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005519 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02005520 markStaticReadIfSymbol(expression);
Olli Etuahocce89652017-06-19 16:04:09 +03005521 ASSERT(op == EOpReturn);
5522 mFunctionReturnsValue = true;
5523 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5524 {
5525 error(loc, "void function cannot return a value", "return");
5526 }
5527 else if (*mCurrentFunctionType != expression->getType())
5528 {
5529 error(loc, "function return is not matching type:", "return");
5530 }
Olli Etuaho49300862015-02-20 14:54:49 +02005531 }
Olli Etuahocce89652017-06-19 16:04:09 +03005532 TIntermBranch *node = new TIntermBranch(op, expression);
5533 node->setLine(loc);
5534 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005535}
5536
Olli Etuaho94bbed12018-03-20 14:44:53 +02005537void TParseContext::appendStatement(TIntermBlock *block, TIntermNode *statement)
5538{
5539 if (statement != nullptr)
5540 {
5541 markStaticReadIfSymbol(statement);
5542 block->appendStatement(statement);
5543 }
5544}
5545
Martin Radev84aa2dc2017-09-11 15:51:02 +03005546void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5547{
5548 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005549 const TFunction *func = functionCall->getFunction();
5550 if (BuiltInGroup::isTextureGather(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005551 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005552 bool isTextureGatherOffset = BuiltInGroup::isTextureGatherOffset(func);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005553 TIntermNode *componentNode = nullptr;
5554 TIntermSequence *arguments = functionCall->getSequence();
5555 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5556 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5557 ASSERT(sampler != nullptr);
5558 switch (sampler->getBasicType())
5559 {
5560 case EbtSampler2D:
5561 case EbtISampler2D:
5562 case EbtUSampler2D:
5563 case EbtSampler2DArray:
5564 case EbtISampler2DArray:
5565 case EbtUSampler2DArray:
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005566 if ((!isTextureGatherOffset && arguments->size() == 3u) ||
Martin Radev84aa2dc2017-09-11 15:51:02 +03005567 (isTextureGatherOffset && arguments->size() == 4u))
5568 {
5569 componentNode = arguments->back();
5570 }
5571 break;
5572 case EbtSamplerCube:
5573 case EbtISamplerCube:
5574 case EbtUSamplerCube:
5575 ASSERT(!isTextureGatherOffset);
5576 if (arguments->size() == 3u)
5577 {
5578 componentNode = arguments->back();
5579 }
5580 break;
5581 case EbtSampler2DShadow:
5582 case EbtSampler2DArrayShadow:
5583 case EbtSamplerCubeShadow:
5584 break;
5585 default:
5586 UNREACHABLE();
5587 break;
5588 }
5589 if (componentNode)
5590 {
5591 const TIntermConstantUnion *componentConstantUnion =
5592 componentNode->getAsConstantUnion();
5593 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5594 {
5595 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005596 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005597 }
5598 else
5599 {
5600 int component = componentConstantUnion->getIConst(0);
5601 if (component < 0 || component > 3)
5602 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005603 error(functionCall->getLine(), "Component must be in the range [0;3]",
5604 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005605 }
5606 }
5607 }
5608 }
5609}
5610
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005611void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5612{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005613 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005614 const TFunction *func = functionCall->getFunction();
Jamie Madill50cf2be2018-06-15 09:46:57 -04005615 TIntermNode *offset = nullptr;
5616 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005617 bool useTextureGatherOffsetConstraints = false;
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005618 if (BuiltInGroup::isTextureOffsetNoBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005619 {
5620 offset = arguments->back();
5621 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005622 else if (BuiltInGroup::isTextureOffsetBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005623 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005624 // A bias parameter follows the offset parameter.
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005625 ASSERT(arguments->size() >= 3);
5626 offset = (*arguments)[2];
5627 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005628 else if (BuiltInGroup::isTextureGatherOffset(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005629 {
5630 ASSERT(arguments->size() >= 3u);
5631 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5632 ASSERT(sampler != nullptr);
5633 switch (sampler->getBasicType())
5634 {
5635 case EbtSampler2D:
5636 case EbtISampler2D:
5637 case EbtUSampler2D:
5638 case EbtSampler2DArray:
5639 case EbtISampler2DArray:
5640 case EbtUSampler2DArray:
5641 offset = (*arguments)[2];
5642 break;
5643 case EbtSampler2DShadow:
5644 case EbtSampler2DArrayShadow:
5645 offset = (*arguments)[3];
5646 break;
5647 default:
5648 UNREACHABLE();
5649 break;
5650 }
5651 useTextureGatherOffsetConstraints = true;
5652 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005653 if (offset != nullptr)
5654 {
5655 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5656 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5657 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005658 error(functionCall->getLine(), "Texture offset must be a constant expression",
5659 func->name());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005660 }
5661 else
5662 {
5663 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5664 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005665 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005666 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5667 : mMinProgramTexelOffset;
5668 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5669 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005670 for (size_t i = 0u; i < size; ++i)
5671 {
5672 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005673 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005674 {
5675 std::stringstream tokenStream;
5676 tokenStream << offsetValue;
5677 std::string token = tokenStream.str();
5678 error(offset->getLine(), "Texture offset value out of valid range",
5679 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005680 }
5681 }
5682 }
5683 }
5684}
5685
Jiajia Qina3106c52017-11-03 09:39:39 +08005686void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5687{
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005688 const TFunction *func = functionCall->getFunction();
5689 if (BuiltInGroup::isAtomicMemory(func))
Jiajia Qina3106c52017-11-03 09:39:39 +08005690 {
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005691 ASSERT(IsAtomicFunction(functionCall->getOp()));
Jiajia Qina3106c52017-11-03 09:39:39 +08005692 TIntermSequence *arguments = functionCall->getSequence();
5693 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5694
5695 if (IsBufferOrSharedVariable(memNode))
5696 {
5697 return;
5698 }
5699
5700 while (memNode->getAsBinaryNode())
5701 {
5702 memNode = memNode->getAsBinaryNode()->getLeft();
5703 if (IsBufferOrSharedVariable(memNode))
5704 {
5705 return;
5706 }
5707 }
5708
5709 error(memNode->getLine(),
5710 "The value passed to the mem argument of an atomic memory function does not "
5711 "correspond to a buffer or shared variable.",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005712 func->name());
Jiajia Qina3106c52017-11-03 09:39:39 +08005713 }
5714}
5715
Martin Radev2cc85b32016-08-05 16:22:53 +03005716// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5717void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5718{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005719 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005720
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005721 const TFunction *func = functionCall->getFunction();
5722
5723 if (BuiltInGroup::isImage(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005724 {
5725 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005726 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005727
Olli Etuaho485eefd2017-02-14 17:40:06 +00005728 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005729
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005730 if (BuiltInGroup::isImageStore(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005731 {
5732 if (memoryQualifier.readonly)
5733 {
5734 error(imageNode->getLine(),
5735 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005736 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005737 }
5738 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005739 else if (BuiltInGroup::isImageLoad(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005740 {
5741 if (memoryQualifier.writeonly)
5742 {
5743 error(imageNode->getLine(),
5744 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005745 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005746 }
5747 }
5748 }
5749}
5750
5751// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5752void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5753 const TFunction *functionDefinition,
5754 const TIntermAggregate *functionCall)
5755{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005756 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005757
5758 const TIntermSequence &arguments = *functionCall->getSequence();
5759
5760 ASSERT(functionDefinition->getParamCount() == arguments.size());
5761
5762 for (size_t i = 0; i < arguments.size(); ++i)
5763 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005764 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5765 const TType &functionArgumentType = typedArgument->getType();
Olli Etuahod4bd9632018-03-08 16:32:44 +02005766 const TType &functionParameterType = functionDefinition->getParam(i)->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005767 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5768
5769 if (IsImage(functionArgumentType.getBasicType()))
5770 {
5771 const TMemoryQualifier &functionArgumentMemoryQualifier =
5772 functionArgumentType.getMemoryQualifier();
5773 const TMemoryQualifier &functionParameterMemoryQualifier =
5774 functionParameterType.getMemoryQualifier();
5775 if (functionArgumentMemoryQualifier.readonly &&
5776 !functionParameterMemoryQualifier.readonly)
5777 {
5778 error(functionCall->getLine(),
5779 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005780 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005781 }
5782
5783 if (functionArgumentMemoryQualifier.writeonly &&
5784 !functionParameterMemoryQualifier.writeonly)
5785 {
5786 error(functionCall->getLine(),
5787 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005788 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005789 }
Martin Radev049edfa2016-11-11 14:35:37 +02005790
5791 if (functionArgumentMemoryQualifier.coherent &&
5792 !functionParameterMemoryQualifier.coherent)
5793 {
5794 error(functionCall->getLine(),
5795 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005796 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005797 }
5798
5799 if (functionArgumentMemoryQualifier.volatileQualifier &&
5800 !functionParameterMemoryQualifier.volatileQualifier)
5801 {
5802 error(functionCall->getLine(),
5803 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005804 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005805 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005806 }
5807 }
5808}
5809
Olli Etuaho95ed1942018-02-01 14:01:19 +02005810TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005811{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005812 if (fnCall->thisNode() != nullptr)
5813 {
5814 return addMethod(fnCall, loc);
5815 }
5816 if (fnCall->isConstructor())
5817 {
5818 return addConstructor(fnCall, loc);
5819 }
5820 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005821}
5822
Olli Etuaho95ed1942018-02-01 14:01:19 +02005823TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005824{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005825 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005826 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5827 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5828 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005829 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005830 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005831 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005832 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005833 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005834 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005835 {
5836 error(loc, "method takes no parameters", "length");
5837 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005838 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005839 {
5840 error(loc, "length can only be called on arrays", "length");
5841 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005842 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005843 mGeometryShaderInputPrimitiveType == EptUndefined)
5844 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005845 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005846 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5847 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005848 else
5849 {
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005850 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode, nullptr);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005851 node->setLine(loc);
5852 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005853 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005854 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005855}
5856
Olli Etuaho95ed1942018-02-01 14:01:19 +02005857TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005858 const TSourceLoc &loc)
5859{
Olli Etuaho697bf652018-02-16 11:50:54 +02005860 // First check whether the function has been hidden by a variable name or struct typename by
5861 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5862 // with a matching argument list.
5863 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005864 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005865 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005866 }
5867 else
5868 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005869 // There are no inner functions, so it's enough to look for user-defined functions in the
5870 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005871 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005872 if (symbol != nullptr)
5873 {
5874 // A user-defined function - could be an overloaded built-in as well.
5875 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5876 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5877 TIntermAggregate *callNode =
5878 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5879 callNode->setLine(loc);
5880 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5881 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5882 return callNode;
5883 }
5884
5885 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005886 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005887 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005888 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005889 }
5890 else
5891 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005892 // A built-in function.
5893 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005894 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005895
Olli Etuaho37b697e2018-01-29 12:19:27 +02005896 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005897 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005898 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005899 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005900 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005901 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005902 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005903 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005904 if (fnCandidate->getParamCount() == 1)
5905 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005906 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005907 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005908 TIntermTyped *callNode =
5909 createUnaryMath(op, unaryParamNode->getAsTyped(), loc, fnCandidate);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005910 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005911 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005912 }
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005913
Olli Etuahoe80825e2018-02-16 10:24:53 +02005914 TIntermAggregate *callNode =
5915 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005916 callNode->setLine(loc);
5917
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005918 checkAtomicMemoryBuiltinFunctions(callNode);
5919
Olli Etuahoe80825e2018-02-16 10:24:53 +02005920 // Some built-in functions have out parameters too.
5921 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5922
5923 // See if we can constant fold a built-in. Note that this may be possible
5924 // even if it is not const-qualified.
5925 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005926 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005927
5928 // This is a built-in function with no op associated with it.
5929 TIntermAggregate *callNode =
5930 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5931 callNode->setLine(loc);
5932 checkTextureOffsetConst(callNode);
5933 checkTextureGather(callNode);
5934 checkImageMemoryAccessForBuiltinFunctions(callNode);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005935 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5936 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005937 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005938 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005939
5940 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005941 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005942}
5943
Jamie Madillb98c3a82015-07-23 14:26:04 -04005944TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005945 TIntermTyped *trueExpression,
5946 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005947 const TSourceLoc &loc)
5948{
Olli Etuaho56229f12017-07-10 14:16:33 +03005949 if (!checkIsScalarBool(loc, cond))
5950 {
5951 return falseExpression;
5952 }
Olli Etuaho52901742015-04-15 13:42:45 +03005953
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005954 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005955 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005956 TInfoSinkBase reasonStream;
5957 reasonStream << "mismatching ternary operator operand types '" << trueExpression->getType()
5958 << " and '" << falseExpression->getType() << "'";
5959 error(loc, reasonStream.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005960 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005961 }
Olli Etuahode318b22016-10-25 16:18:25 +01005962 if (IsOpaqueType(trueExpression->getBasicType()))
5963 {
5964 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005965 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005966 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5967 // Note that structs containing opaque types don't need to be checked as structs are
5968 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005969 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005970 return falseExpression;
5971 }
5972
Jiajia Qinbc585152017-06-23 15:42:17 +08005973 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5974 falseExpression->getMemoryQualifier().writeonly)
5975 {
5976 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5977 return falseExpression;
5978 }
5979
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005980 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005981 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005982 // ESSL 3.00.6 section 5.7:
5983 // Ternary operator support is optional for arrays. No certainty that it works across all
5984 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5985 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005986 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005987 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005988 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005989 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005990 }
Olli Etuaho94050052017-05-08 14:17:44 +03005991 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5992 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005993 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005994 return falseExpression;
5995 }
5996
Corentin Wallez0d959252016-07-12 17:26:32 -04005997 // WebGL2 section 5.26, the following results in an error:
5998 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005999 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04006000 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03006001 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03006002 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04006003 }
6004
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03006005 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
Olli Etuaho94bbed12018-03-20 14:44:53 +02006006 markStaticReadIfSymbol(cond);
6007 markStaticReadIfSymbol(trueExpression);
6008 markStaticReadIfSymbol(falseExpression);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03006009 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02006010 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03006011}
Olli Etuaho49300862015-02-20 14:54:49 +02006012
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00006013//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006014// Parse an array of strings using yyparse.
6015//
6016// Returns 0 for success.
6017//
Jamie Madillb98c3a82015-07-23 14:26:04 -04006018int PaParseStrings(size_t count,
6019 const char *const string[],
6020 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05306021 TParseContext *context)
6022{
Yunchao He4f285442017-04-21 12:15:49 +08006023 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006024 return 1;
6025
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006026 if (glslang_initialize(context))
6027 return 1;
6028
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006029 int error = glslang_scan(count, string, length, context);
6030 if (!error)
6031 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006032
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006033 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006034
alokp@chromium.org6b495712012-06-29 00:06:58 +00006035 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006036}
Jamie Madill45bcc782016-11-07 13:58:48 -05006037
6038} // namespace sh