blob: 778d9bf3d6d57242ca11646fc7a07086e1e21cb2 [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"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080017#include "compiler/translator/ValidateSwitch.h"
18#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030019#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000020
Jamie Madill45bcc782016-11-07 13:58:48 -050021namespace sh
22{
23
alokp@chromium.org8b851c62012-06-15 16:25:11 +000024///////////////////////////////////////////////////////////////////////
25//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026// Sub- vector and matrix fields
27//
28////////////////////////////////////////////////////////////////////////
29
Martin Radev2cc85b32016-08-05 16:22:53 +030030namespace
31{
32
33const int kWebGLMaxStructNesting = 4;
34
Olli Etuaho0f684632017-07-13 12:42:15 +030035bool ContainsSampler(const TStructure *structType);
36
Martin Radev2cc85b32016-08-05 16:22:53 +030037bool ContainsSampler(const TType &type)
38{
39 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030040 {
Martin Radev2cc85b32016-08-05 16:22:53 +030041 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030042 }
jchen10cc2a10e2017-05-03 14:05:12 +080043 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030044 {
Olli Etuaho0f684632017-07-13 12:42:15 +030045 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030046 }
47
48 return false;
49}
50
Olli Etuaho0f684632017-07-13 12:42:15 +030051bool ContainsSampler(const TStructure *structType)
52{
53 for (const auto &field : structType->fields())
54 {
55 if (ContainsSampler(*field->type()))
56 return true;
57 }
58 return false;
59}
60
Olli Etuaho485eefd2017-02-14 17:40:06 +000061// Get a token from an image argument to use as an error message token.
62const char *GetImageArgumentToken(TIntermTyped *imageNode)
63{
64 ASSERT(IsImage(imageNode->getBasicType()));
65 while (imageNode->getAsBinaryNode() &&
66 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
67 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
68 {
69 imageNode = imageNode->getAsBinaryNode()->getLeft();
70 }
71 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
72 if (imageSymbol)
73 {
74 return imageSymbol->getSymbol().c_str();
75 }
76 return "image";
77}
78
Olli Etuahocce89652017-06-19 16:04:09 +030079bool CanSetDefaultPrecisionOnType(const TPublicType &type)
80{
81 if (!SupportsPrecision(type.getBasicType()))
82 {
83 return false;
84 }
85 if (type.getBasicType() == EbtUInt)
86 {
87 // ESSL 3.00.4 section 4.5.4
88 return false;
89 }
90 if (type.isAggregate())
91 {
92 // Not allowed to set for aggregate types
93 return false;
94 }
95 return true;
96}
97
Jiawei Shaod8105a02017-08-08 09:54:36 +080098// Map input primitive types to input array sizes in a geometry shader.
99GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
100{
101 switch (primitiveType)
102 {
103 case EptPoints:
104 return 1u;
105 case EptLines:
106 return 2u;
107 case EptTriangles:
108 return 3u;
109 case EptLinesAdjacency:
110 return 4u;
111 case EptTrianglesAdjacency:
112 return 6u;
113 default:
114 UNREACHABLE();
115 return 0u;
116 }
117}
118
Martin Radev2cc85b32016-08-05 16:22:53 +0300119} // namespace
120
jchen104cdac9e2017-05-08 11:01:20 +0800121// This tracks each binding point's current default offset for inheritance of subsequent
122// variables using the same binding, and keeps offsets unique and non overlapping.
123// See GLSL ES 3.1, section 4.4.6.
124class TParseContext::AtomicCounterBindingState
125{
126 public:
127 AtomicCounterBindingState() : mDefaultOffset(0) {}
128 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
129 // newly inserted span.
130 int insertSpan(int start, size_t length)
131 {
132 gl::RangeI newSpan(start, start + static_cast<int>(length));
133 for (const auto &span : mSpans)
134 {
135 if (newSpan.intersects(span))
136 {
137 return -1;
138 }
139 }
140 mSpans.push_back(newSpan);
141 mDefaultOffset = newSpan.high();
142 return start;
143 }
144 // Inserts a new span starting from the default offset.
145 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
146 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
147
148 private:
149 int mDefaultOffset;
150 std::vector<gl::RangeI> mSpans;
151};
152
Jamie Madillacb4b812016-11-07 13:50:29 -0500153TParseContext::TParseContext(TSymbolTable &symt,
154 TExtensionBehavior &ext,
155 sh::GLenum type,
156 ShShaderSpec spec,
157 ShCompileOptions options,
158 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000159 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500160 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300161 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300162 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500163 mShaderType(type),
164 mShaderSpec(spec),
165 mCompileOptions(options),
166 mShaderVersion(100),
167 mTreeRoot(nullptr),
168 mLoopNestingLevel(0),
169 mStructNestingLevel(0),
170 mSwitchNestingLevel(0),
171 mCurrentFunctionType(nullptr),
172 mFunctionReturnsValue(false),
173 mChecksPrecisionErrors(checksPrecErrors),
174 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800175 mDefaultUniformMatrixPacking(EmpColumnMajor),
176 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
177 mDefaultBufferMatrixPacking(EmpColumnMajor),
178 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000179 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500180 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000181 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500182 mShaderVersion,
183 mShaderType,
184 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000185 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500186 mScanner(nullptr),
187 mUsesFragData(false),
188 mUsesFragColor(false),
189 mUsesSecondaryOutputs(false),
190 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
191 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300192 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
193 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500194 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000195 mNumViews(-1),
196 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000197 mMaxImageUnits(resources.MaxImageUnits),
198 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000199 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800200 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800201 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800202 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800203 mDeclaringFunction(false),
204 mGeometryShaderInputPrimitiveType(EptUndefined),
205 mGeometryShaderOutputPrimitiveType(EptUndefined),
206 mGeometryShaderInvocations(0),
207 mGeometryShaderMaxVertices(-1),
208 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800209 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800210 mGeometryShaderInputArraySize(0u)
Jamie Madillacb4b812016-11-07 13:50:29 -0500211{
212 mComputeShaderLocalSize.fill(-1);
213}
214
jchen104cdac9e2017-05-08 11:01:20 +0800215TParseContext::~TParseContext()
216{
217}
218
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300219bool TParseContext::parseVectorFields(const TSourceLoc &line,
220 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400221 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300222 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300224 ASSERT(fieldOffsets);
225 size_t fieldCount = compString.size();
226 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530227 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000228 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000229 return false;
230 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300231 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232
Jamie Madillb98c3a82015-07-23 14:26:04 -0400233 enum
234 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000235 exyzw,
236 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000237 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000238 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300240 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530241 {
242 switch (compString[i])
243 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400244 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300245 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400246 fieldSet[i] = exyzw;
247 break;
248 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300249 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400250 fieldSet[i] = ergba;
251 break;
252 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300253 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400254 fieldSet[i] = estpq;
255 break;
256 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300257 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400258 fieldSet[i] = exyzw;
259 break;
260 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300261 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400262 fieldSet[i] = ergba;
263 break;
264 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300265 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 fieldSet[i] = estpq;
267 break;
268 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300269 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400270 fieldSet[i] = exyzw;
271 break;
272 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300273 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400274 fieldSet[i] = ergba;
275 break;
276 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300277 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400278 fieldSet[i] = estpq;
279 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530280
Jamie Madillb98c3a82015-07-23 14:26:04 -0400281 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300282 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400283 fieldSet[i] = exyzw;
284 break;
285 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300286 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400287 fieldSet[i] = ergba;
288 break;
289 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300290 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400291 fieldSet[i] = estpq;
292 break;
293 default:
294 error(line, "illegal vector field selection", compString.c_str());
295 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000296 }
297 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300299 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530300 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300301 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530302 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400303 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000304 return false;
305 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307 if (i > 0)
308 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400309 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530310 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400311 error(line, "illegal - vector component fields not from the same set",
312 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000313 return false;
314 }
315 }
316 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000318 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319}
320
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321///////////////////////////////////////////////////////////////////////
322//
323// Errors
324//
325////////////////////////////////////////////////////////////////////////
326
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327//
328// Used by flex/bison to output all syntax and parsing errors.
329//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000330void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000332 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333}
334
Olli Etuaho4de340a2016-12-16 09:32:03 +0000335void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530336{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000337 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000338}
339
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200340void TParseContext::outOfRangeError(bool isError,
341 const TSourceLoc &loc,
342 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000343 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200344{
345 if (isError)
346 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000347 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200348 }
349 else
350 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000351 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200352 }
353}
354
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355//
356// Same error message for all places assignments don't work.
357//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530358void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000360 std::stringstream reasonStream;
361 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
362 std::string reason = reasonStream.str();
363 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364}
365
366//
367// Same error message for all places unary operations don't work.
368//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530369void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000371 std::stringstream reasonStream;
372 reasonStream << "wrong operand type - no operation '" << op
373 << "' exists that takes an operand of type " << operand
374 << " (or there is no acceptable conversion)";
375 std::string reason = reasonStream.str();
376 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377}
378
379//
380// Same error message for all binary operations don't work.
381//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400382void TParseContext::binaryOpError(const TSourceLoc &line,
383 const char *op,
384 TString left,
385 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000387 std::stringstream reasonStream;
388 reasonStream << "wrong operand types - no operation '" << op
389 << "' exists that takes a left-hand operand of type '" << left
390 << "' and a right operand of type '" << right
391 << "' (or there is no acceptable conversion)";
392 std::string reason = reasonStream.str();
393 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000394}
395
Olli Etuaho856c4972016-08-08 11:38:39 +0300396void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
397 TPrecision precision,
398 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530399{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400400 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300401 return;
Martin Radev70866b82016-07-22 15:27:42 +0300402
403 if (precision != EbpUndefined && !SupportsPrecision(type))
404 {
405 error(line, "illegal type for precision qualifier", getBasicString(type));
406 }
407
Olli Etuaho183d7e22015-11-20 15:59:09 +0200408 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530409 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200410 switch (type)
411 {
412 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400413 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300414 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200415 case EbtInt:
416 case EbtUInt:
417 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400418 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300419 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200420 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800421 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200422 {
jchen10cc2a10e2017-05-03 14:05:12 +0800423 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300424 return;
425 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200426 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000427 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000428}
429
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430// Both test and if necessary, spit out an error, to see if the node is really
431// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300432bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500434 TIntermSymbol *symNode = node->getAsSymbolNode();
435 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100436 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
437
438 if (swizzleNode)
439 {
440 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
441 if (ok && swizzleNode->hasDuplicateOffsets())
442 {
443 error(line, " l-value of swizzle cannot have duplicate components", op);
444 return false;
445 }
446 return ok;
447 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448
Arun Patole7e7e68d2015-05-22 12:02:25 +0530449 if (binaryNode)
450 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400451 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530452 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400453 case EOpIndexDirect:
454 case EOpIndexIndirect:
455 case EOpIndexDirectStruct:
456 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300457 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400458 default:
459 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000460 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000461 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300462 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000463 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000464
jchen10cc2a10e2017-05-03 14:05:12 +0800465 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530466 switch (node->getQualifier())
467 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400468 case EvqConst:
469 message = "can't modify a const";
470 break;
471 case EvqConstReadOnly:
472 message = "can't modify a const";
473 break;
474 case EvqAttribute:
475 message = "can't modify an attribute";
476 break;
477 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800479 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800480 case EvqFlatIn:
481 case EvqSmoothIn:
482 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400483 message = "can't modify an input";
484 break;
485 case EvqUniform:
486 message = "can't modify a uniform";
487 break;
488 case EvqVaryingIn:
489 message = "can't modify a varying";
490 break;
491 case EvqFragCoord:
492 message = "can't modify gl_FragCoord";
493 break;
494 case EvqFrontFacing:
495 message = "can't modify gl_FrontFacing";
496 break;
497 case EvqPointCoord:
498 message = "can't modify gl_PointCoord";
499 break;
Martin Radevb0883602016-08-04 17:48:58 +0300500 case EvqNumWorkGroups:
501 message = "can't modify gl_NumWorkGroups";
502 break;
503 case EvqWorkGroupSize:
504 message = "can't modify gl_WorkGroupSize";
505 break;
506 case EvqWorkGroupID:
507 message = "can't modify gl_WorkGroupID";
508 break;
509 case EvqLocalInvocationID:
510 message = "can't modify gl_LocalInvocationID";
511 break;
512 case EvqGlobalInvocationID:
513 message = "can't modify gl_GlobalInvocationID";
514 break;
515 case EvqLocalInvocationIndex:
516 message = "can't modify gl_LocalInvocationIndex";
517 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300518 case EvqViewIDOVR:
519 message = "can't modify gl_ViewID_OVR";
520 break;
Martin Radev802abe02016-08-04 17:48:32 +0300521 case EvqComputeIn:
522 message = "can't modify work group size variable";
523 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800524 case EvqPerVertexIn:
525 message = "can't modify any member in gl_in";
526 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800527 case EvqPrimitiveIDIn:
528 message = "can't modify gl_PrimitiveIDIn";
529 break;
530 case EvqInvocationID:
531 message = "can't modify gl_InvocationID";
532 break;
533 case EvqPrimitiveID:
534 if (mShaderType == GL_FRAGMENT_SHADER)
535 {
536 message = "can't modify gl_PrimitiveID in a fragment shader";
537 }
538 break;
539 case EvqLayer:
540 if (mShaderType == GL_FRAGMENT_SHADER)
541 {
542 message = "can't modify gl_Layer in a fragment shader";
543 }
544 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400545 default:
546 //
547 // Type that can't be written to?
548 //
549 if (node->getBasicType() == EbtVoid)
550 {
551 message = "can't modify void";
552 }
jchen10cc2a10e2017-05-03 14:05:12 +0800553 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400554 {
jchen10cc2a10e2017-05-03 14:05:12 +0800555 message = "can't modify a variable with type ";
556 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300557 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800558 else if (node->getMemoryQualifier().readonly)
559 {
560 message = "can't modify a readonly variable";
561 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000562 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563
jchen10cc2a10e2017-05-03 14:05:12 +0800564 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530565 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000566 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567
Olli Etuaho8a176262016-08-16 14:23:01 +0300568 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000571 //
572 // Everything else is okay, no error.
573 //
jchen10cc2a10e2017-05-03 14:05:12 +0800574 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300575 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 //
578 // If we get here, we have an error and a message.
579 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530580 if (symNode)
581 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000582 const char *symbol = symNode->getSymbol().c_str();
583 std::stringstream reasonStream;
584 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
585 std::string reason = reasonStream.str();
586 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000587 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530588 else
589 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000590 std::stringstream reasonStream;
591 reasonStream << "l-value required (" << message << ")";
592 std::string reason = reasonStream.str();
593 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000594 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595
Olli Etuaho8a176262016-08-16 14:23:01 +0300596 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597}
598
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000599// Both test, and if necessary spit out an error, to see if the node is really
600// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300601void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602{
Olli Etuaho383b7912016-08-05 11:22:59 +0300603 if (node->getQualifier() != EvqConst)
604 {
605 error(node->getLine(), "constant expression required", "");
606 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607}
608
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609// Both test, and if necessary spit out an error, to see if the node is really
610// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300611void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612{
Olli Etuaho383b7912016-08-05 11:22:59 +0300613 if (!node->isScalarInt())
614 {
615 error(node->getLine(), "integer expression required", token);
616 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617}
618
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619// Both test, and if necessary spit out an error, to see if we are currently
620// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800621bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622{
Olli Etuaho856c4972016-08-08 11:38:39 +0300623 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300624 {
625 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800626 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300627 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800628 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629}
630
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300631// ESSL 3.00.5 sections 3.8 and 3.9.
632// If it starts "gl_" or contains two consecutive underscores, it's reserved.
633// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300634bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530636 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300637 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530638 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300639 error(line, reservedErrMsg, "gl_");
640 return false;
641 }
642 if (sh::IsWebGLBasedSpec(mShaderSpec))
643 {
644 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530645 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300646 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300647 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300649 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530650 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300651 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300652 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000653 }
654 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300655 if (identifier.find("__") != TString::npos)
656 {
657 error(line,
658 "identifiers containing two consecutive underscores (__) are reserved as "
659 "possible future keywords",
660 identifier.c_str());
661 return false;
662 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300663 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664}
665
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300666// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300667bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800668 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300669 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800671 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200673 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300674 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200676
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300677 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530678 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300679 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200680 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300681 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200682 {
jchen10cc2a10e2017-05-03 14:05:12 +0800683 std::string reason("cannot convert a variable with type ");
684 reason += getBasicString(argTyped->getBasicType());
685 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300686 return false;
687 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800688 else if (argTyped->getMemoryQualifier().writeonly)
689 {
690 error(line, "cannot convert a variable with writeonly", "constructor");
691 return false;
692 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200693 if (argTyped->getBasicType() == EbtVoid)
694 {
695 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300696 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200697 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000698 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000699
Olli Etuaho856c4972016-08-08 11:38:39 +0300700 if (type.isArray())
701 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300702 // The size of an unsized constructor should already have been determined.
703 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300704 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300705 {
706 error(line, "array constructor needs one argument per array element", "constructor");
707 return false;
708 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300709 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
710 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800711 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300712 {
713 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500714 if (argType.isArray())
715 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300716 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500717 return false;
718 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300719 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300720 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000721 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300722 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300723 }
724 }
725 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300726 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300727 {
728 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300729 if (fields.size() != arguments->size())
730 {
731 error(line,
732 "Number of constructor parameters does not match the number of structure fields",
733 "constructor");
734 return false;
735 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300736
737 for (size_t i = 0; i < fields.size(); i++)
738 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800739 if (i >= arguments->size() ||
740 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300741 {
742 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000743 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300744 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300745 }
746 }
747 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300748 else
749 {
750 // We're constructing a scalar, vector, or matrix.
751
752 // Note: It's okay to have too many components available, but not okay to have unused
753 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
754 // there is an extra argument, so 'overFull' will become true.
755
756 size_t size = 0;
757 bool full = false;
758 bool overFull = false;
759 bool matrixArg = false;
760 for (TIntermNode *arg : *arguments)
761 {
762 const TIntermTyped *argTyped = arg->getAsTyped();
763 ASSERT(argTyped != nullptr);
764
Olli Etuaho487b63a2017-05-23 15:55:09 +0300765 if (argTyped->getBasicType() == EbtStruct)
766 {
767 error(line, "a struct cannot be used as a constructor argument for this type",
768 "constructor");
769 return false;
770 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300771 if (argTyped->getType().isArray())
772 {
773 error(line, "constructing from a non-dereferenced array", "constructor");
774 return false;
775 }
776 if (argTyped->getType().isMatrix())
777 {
778 matrixArg = true;
779 }
780
781 size += argTyped->getType().getObjectSize();
782 if (full)
783 {
784 overFull = true;
785 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300786 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300787 {
788 full = true;
789 }
790 }
791
792 if (type.isMatrix() && matrixArg)
793 {
794 if (arguments->size() != 1)
795 {
796 error(line, "constructing matrix from matrix can only take one argument",
797 "constructor");
798 return false;
799 }
800 }
801 else
802 {
803 if (size != 1 && size < type.getObjectSize())
804 {
805 error(line, "not enough data provided for construction", "constructor");
806 return false;
807 }
808 if (overFull)
809 {
810 error(line, "too many arguments", "constructor");
811 return false;
812 }
813 }
814 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300815
Olli Etuaho8a176262016-08-16 14:23:01 +0300816 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817}
818
Jamie Madillb98c3a82015-07-23 14:26:04 -0400819// This function checks to see if a void variable has been declared and raise an error message for
820// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821//
822// returns true in case of an error
823//
Olli Etuaho856c4972016-08-08 11:38:39 +0300824bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400825 const TString &identifier,
826 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300828 if (type == EbtVoid)
829 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000830 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300831 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300832 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833
Olli Etuaho8a176262016-08-16 14:23:01 +0300834 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835}
836
Jamie Madillb98c3a82015-07-23 14:26:04 -0400837// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300838// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300839bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300841 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530842 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000843 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300844 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530845 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300846 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
Jamie Madillb98c3a82015-07-23 14:26:04 -0400849// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300850// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300851void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852{
Martin Radev4a9cd802016-09-01 16:51:51 +0300853 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530854 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000855 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530856 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
jchen10cc2a10e2017-05-03 14:05:12 +0800859bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
860 const TTypeSpecifierNonArray &pType,
861 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530863 if (pType.type == EbtStruct)
864 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300865 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530866 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000867 std::stringstream reasonStream;
868 reasonStream << reason << " (structure contains a sampler)";
869 std::string reasonStr = reasonStream.str();
870 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300871 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000872 }
jchen10cc2a10e2017-05-03 14:05:12 +0800873 // only samplers need to be checked from structs, since other opaque types can't be struct
874 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300875 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530876 }
jchen10cc2a10e2017-05-03 14:05:12 +0800877 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530878 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000879 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300880 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000881 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882
Olli Etuaho8a176262016-08-16 14:23:01 +0300883 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884}
885
Olli Etuaho856c4972016-08-08 11:38:39 +0300886void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
887 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400888{
889 if (pType.layoutQualifier.location != -1)
890 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400891 error(line, "location must only be specified for a single input or output variable",
892 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400893 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400894}
895
Olli Etuaho856c4972016-08-08 11:38:39 +0300896void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
897 const TLayoutQualifier &layoutQualifier)
898{
899 if (layoutQualifier.location != -1)
900 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000901 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
902 if (mShaderVersion >= 310)
903 {
904 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800905 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000906 }
907 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300908 }
909}
910
Qin Jiajiaca68d982017-09-18 16:41:56 +0800911void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
912 const TLayoutBlockStorage &blockStorage,
913 const TQualifier &qualifier)
914{
915 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
916 {
917 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
918 }
919}
920
Martin Radev2cc85b32016-08-05 16:22:53 +0300921void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
922 TQualifier qualifier,
923 const TType &type)
924{
Martin Radev2cc85b32016-08-05 16:22:53 +0300925 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800926 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530927 {
jchen10cc2a10e2017-05-03 14:05:12 +0800928 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000929 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930}
931
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300933unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530935 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000936
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200937 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
938 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
939 // fold as array size.
940 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000941 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000942 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300943 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000944 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945
Olli Etuaho856c4972016-08-08 11:38:39 +0300946 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400947
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000948 if (constant->getBasicType() == EbtUInt)
949 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300950 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000951 }
952 else
953 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300954 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000955
Olli Etuaho856c4972016-08-08 11:38:39 +0300956 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000957 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400958 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300959 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000960 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400961
Olli Etuaho856c4972016-08-08 11:38:39 +0300962 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400963 }
964
Olli Etuaho856c4972016-08-08 11:38:39 +0300965 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400966 {
967 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300968 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400969 }
970
971 // The size of arrays is restricted here to prevent issues further down the
972 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
973 // 4096 registers so this should be reasonable even for aggressively optimizable code.
974 const unsigned int sizeLimit = 65536;
975
Olli Etuaho856c4972016-08-08 11:38:39 +0300976 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400977 {
978 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300979 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000980 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300981
982 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983}
984
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300986bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
987 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988{
Olli Etuaho8a176262016-08-16 14:23:01 +0300989 if ((elementQualifier.qualifier == EvqAttribute) ||
990 (elementQualifier.qualifier == EvqVertexIn) ||
991 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300992 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400993 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300994 TType(elementQualifier).getQualifierString());
995 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000996 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997
Olli Etuaho8a176262016-08-16 14:23:01 +0300998 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999}
1000
Olli Etuaho8a176262016-08-16 14:23:01 +03001001// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001002bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1003 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004{
Olli Etuaho8a176262016-08-16 14:23:01 +03001005 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -04001006 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001007 error(line, "cannot declare arrays of arrays",
1008 TType(elementType).getCompleteString().c_str());
1009 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001010 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001011 return true;
1012}
1013
1014// Check if this qualified element type can be formed into an array. This is only called when array
1015// brackets are associated with an identifier in a declaration, like this:
1016// float a[2];
1017// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1018// are associated with the type, like this:
1019// float[2] a;
1020bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1021 const TPublicType &elementType)
1022{
1023 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1024 {
1025 return false;
1026 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001027 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1028 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1029 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001030 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001031 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001032 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001033 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001034 TType(elementType).getCompleteString().c_str());
1035 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001036 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001037 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038}
1039
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001041void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1042 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001043 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044{
Olli Etuaho3739d232015-04-08 12:23:44 +03001045 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001046 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001047 {
1048 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001049 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001050
1051 // Generate informative error messages for ESSL1.
1052 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001053 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001054 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301055 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001056 "structures containing arrays may not be declared constant since they cannot be "
1057 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301058 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001059 }
1060 else
1061 {
1062 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1063 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001064 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001065 // This will make the type sized if it isn't sized yet.
1066 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1067 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068}
1069
Olli Etuaho2935c582015-04-08 14:32:06 +03001070// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071// and update the symbol table.
1072//
Olli Etuaho2935c582015-04-08 14:32:06 +03001073// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001075bool TParseContext::declareVariable(const TSourceLoc &line,
1076 const TString &identifier,
1077 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001078 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079{
Olli Etuaho2935c582015-04-08 14:32:06 +03001080 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081
Olli Etuaho43364892017-02-13 16:00:12 +00001082 checkBindingIsValid(line, type);
1083
Olli Etuaho856c4972016-08-08 11:38:39 +03001084 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085
Olli Etuaho2935c582015-04-08 14:32:06 +03001086 // gl_LastFragData may be redeclared with a new precision qualifier
1087 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1088 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001089 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1090 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001091 if (type.isArrayOfArrays())
1092 {
1093 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1094 identifier.c_str());
1095 return false;
1096 }
1097 else if (static_cast<int>(type.getOutermostArraySize()) ==
1098 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001099 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001100 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001101 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001102 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001103 }
1104 }
1105 else
1106 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001107 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1108 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001109 return false;
1110 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001111 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112
Olli Etuaho8a176262016-08-16 14:23:01 +03001113 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001114 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001115
Olli Etuaho0f684632017-07-13 12:42:15 +03001116 (*variable) = symbolTable.declareVariable(&identifier, type);
1117 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001118 {
1119 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001120 return false;
1121 }
1122
Olli Etuaho8a176262016-08-16 14:23:01 +03001123 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001124 return false;
1125
1126 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001127}
1128
Martin Radev70866b82016-07-22 15:27:42 +03001129void TParseContext::checkIsParameterQualifierValid(
1130 const TSourceLoc &line,
1131 const TTypeQualifierBuilder &typeQualifierBuilder,
1132 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301133{
Olli Etuahocce89652017-06-19 16:04:09 +03001134 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001135 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001136
1137 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301138 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001139 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1140 }
1141
1142 if (!IsImage(type->getBasicType()))
1143 {
Olli Etuaho43364892017-02-13 16:00:12 +00001144 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001145 }
1146 else
1147 {
1148 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001149 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001150
Martin Radev70866b82016-07-22 15:27:42 +03001151 type->setQualifier(typeQualifier.qualifier);
1152
1153 if (typeQualifier.precision != EbpUndefined)
1154 {
1155 type->setPrecision(typeQualifier.precision);
1156 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001157}
1158
Olli Etuaho703671e2017-11-08 17:47:18 +02001159template <size_t size>
1160bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1161 const std::array<TExtension, size> &extensions)
1162{
1163 ASSERT(!extensions.empty());
1164 const TExtensionBehavior &extBehavior = extensionBehavior();
1165
1166 bool canUseWithWarning = false;
1167 bool canUseWithoutWarning = false;
1168
1169 const char *errorMsgString = "";
1170 TExtension errorMsgExtension = TExtension::UNDEFINED;
1171
1172 for (TExtension extension : extensions)
1173 {
1174 auto extIter = extBehavior.find(extension);
1175 if (canUseWithWarning)
1176 {
1177 // We already have an extension that we can use, but with a warning.
1178 // See if we can use the alternative extension without a warning.
1179 if (extIter == extBehavior.end())
1180 {
1181 continue;
1182 }
1183 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1184 {
1185 canUseWithoutWarning = true;
1186 break;
1187 }
1188 continue;
1189 }
1190 if (extIter == extBehavior.end())
1191 {
1192 errorMsgString = "extension is not supported";
1193 errorMsgExtension = extension;
1194 }
1195 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1196 {
1197 errorMsgString = "extension is disabled";
1198 errorMsgExtension = extension;
1199 }
1200 else if (extIter->second == EBhWarn)
1201 {
1202 errorMsgExtension = extension;
1203 canUseWithWarning = true;
1204 }
1205 else
1206 {
1207 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1208 canUseWithoutWarning = true;
1209 break;
1210 }
1211 }
1212
1213 if (canUseWithoutWarning)
1214 {
1215 return true;
1216 }
1217 if (canUseWithWarning)
1218 {
1219 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1220 return true;
1221 }
1222 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1223 return false;
1224}
1225
1226template bool TParseContext::checkCanUseOneOfExtensions(
1227 const TSourceLoc &line,
1228 const std::array<TExtension, 1> &extensions);
1229template bool TParseContext::checkCanUseOneOfExtensions(
1230 const TSourceLoc &line,
1231 const std::array<TExtension, 2> &extensions);
1232template bool TParseContext::checkCanUseOneOfExtensions(
1233 const TSourceLoc &line,
1234 const std::array<TExtension, 3> &extensions);
1235
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001236bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001237{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001238 ASSERT(extension != TExtension::UNDEFINED);
Jiawei Shao0e883132017-10-26 09:53:50 +08001239 ASSERT(extension != TExtension::EXT_geometry_shader);
Olli Etuaho703671e2017-11-08 17:47:18 +02001240 if (extension == TExtension::OES_geometry_shader)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301241 {
Olli Etuaho703671e2017-11-08 17:47:18 +02001242 // OES_geometry_shader and EXT_geometry_shader are always interchangeable.
1243 constexpr std::array<TExtension, 2u> extensions{
1244 {TExtension::EXT_geometry_shader, TExtension::OES_geometry_shader}};
1245 return checkCanUseOneOfExtensions(line, extensions);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001246 }
Olli Etuaho703671e2017-11-08 17:47:18 +02001247 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{extension});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001248}
1249
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001250// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1251// compile-time or link-time errors are the same whether or not the declaration is empty".
1252// This function implements all the checks that are done on qualifiers regardless of if the
1253// declaration is empty.
1254void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1255 const sh::TLayoutQualifier &layoutQualifier,
1256 const TSourceLoc &location)
1257{
1258 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1259 {
1260 error(location, "Shared memory declarations cannot have layout specified", "layout");
1261 }
1262
1263 if (layoutQualifier.matrixPacking != EmpUnspecified)
1264 {
1265 error(location, "layout qualifier only valid for interface blocks",
1266 getMatrixPackingString(layoutQualifier.matrixPacking));
1267 return;
1268 }
1269
1270 if (layoutQualifier.blockStorage != EbsUnspecified)
1271 {
1272 error(location, "layout qualifier only valid for interface blocks",
1273 getBlockStorageString(layoutQualifier.blockStorage));
1274 return;
1275 }
1276
1277 if (qualifier == EvqFragmentOut)
1278 {
1279 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1280 {
1281 error(location, "invalid layout qualifier combination", "yuv");
1282 return;
1283 }
1284 }
1285 else
1286 {
1287 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1288 }
1289
Olli Etuaho95468d12017-05-04 11:14:34 +03001290 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1291 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001292 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1293 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001294 {
1295 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1296 }
1297
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001298 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001299 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001300 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001301 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001302 // We're not checking whether the uniform location is in range here since that depends on
1303 // the type of the variable.
1304 // The type can only be fully determined for non-empty declarations.
1305 }
1306 if (!canHaveLocation)
1307 {
1308 checkLocationIsNotSpecified(location, layoutQualifier);
1309 }
1310}
1311
jchen104cdac9e2017-05-08 11:01:20 +08001312void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1313 const TSourceLoc &location)
1314{
1315 if (publicType.precision != EbpHigh)
1316 {
1317 error(location, "Can only be highp", "atomic counter");
1318 }
1319 // dEQP enforces compile error if location is specified. See uniform_location.test.
1320 if (publicType.layoutQualifier.location != -1)
1321 {
1322 error(location, "location must not be set for atomic_uint", "layout");
1323 }
1324 if (publicType.layoutQualifier.binding == -1)
1325 {
1326 error(location, "no binding specified", "atomic counter");
1327 }
1328}
1329
Olli Etuaho55bde912017-10-25 13:41:13 +03001330void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001331{
Olli Etuaho55bde912017-10-25 13:41:13 +03001332 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001333 {
1334 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1335 // error. It is assumed that this applies to empty declarations as well.
1336 error(location, "empty array declaration needs to specify a size", "");
1337 }
Martin Radevb8b01222016-11-20 23:25:53 +02001338}
1339
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001340// These checks are done for all declarations that are non-empty. They're done for non-empty
1341// declarations starting a declarator list, and declarators that follow an empty declaration.
1342void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1343 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001344{
Olli Etuahofa33d582015-04-09 14:33:12 +03001345 switch (publicType.qualifier)
1346 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001347 case EvqVaryingIn:
1348 case EvqVaryingOut:
1349 case EvqAttribute:
1350 case EvqVertexIn:
1351 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001352 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001353 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001354 {
1355 error(identifierLocation, "cannot be used with a structure",
1356 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001357 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001358 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001359 break;
1360 case EvqBuffer:
1361 if (publicType.getBasicType() != EbtInterfaceBlock)
1362 {
1363 error(identifierLocation,
1364 "cannot declare buffer variables at global scope(outside a block)",
1365 getQualifierString(publicType.qualifier));
1366 return;
1367 }
1368 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001369 default:
1370 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001371 }
jchen10cc2a10e2017-05-03 14:05:12 +08001372 std::string reason(getBasicString(publicType.getBasicType()));
1373 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001374 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001375 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001376 {
1377 return;
1378 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001379
Andrei Volykhina5527072017-03-22 16:46:30 +03001380 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1381 publicType.qualifier != EvqConst) &&
1382 publicType.getBasicType() == EbtYuvCscStandardEXT)
1383 {
1384 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1385 getQualifierString(publicType.qualifier));
1386 return;
1387 }
1388
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001389 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1390 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001391 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1392 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001393 TType type(publicType);
1394 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001395 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001396 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1397 publicType.layoutQualifier);
1398 }
1399 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001400
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001401 // check for layout qualifier issues
1402 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001403
Martin Radev2cc85b32016-08-05 16:22:53 +03001404 if (IsImage(publicType.getBasicType()))
1405 {
1406
1407 switch (layoutQualifier.imageInternalFormat)
1408 {
1409 case EiifRGBA32F:
1410 case EiifRGBA16F:
1411 case EiifR32F:
1412 case EiifRGBA8:
1413 case EiifRGBA8_SNORM:
1414 if (!IsFloatImage(publicType.getBasicType()))
1415 {
1416 error(identifierLocation,
1417 "internal image format requires a floating image type",
1418 getBasicString(publicType.getBasicType()));
1419 return;
1420 }
1421 break;
1422 case EiifRGBA32I:
1423 case EiifRGBA16I:
1424 case EiifRGBA8I:
1425 case EiifR32I:
1426 if (!IsIntegerImage(publicType.getBasicType()))
1427 {
1428 error(identifierLocation,
1429 "internal image format requires an integer image type",
1430 getBasicString(publicType.getBasicType()));
1431 return;
1432 }
1433 break;
1434 case EiifRGBA32UI:
1435 case EiifRGBA16UI:
1436 case EiifRGBA8UI:
1437 case EiifR32UI:
1438 if (!IsUnsignedImage(publicType.getBasicType()))
1439 {
1440 error(identifierLocation,
1441 "internal image format requires an unsigned image type",
1442 getBasicString(publicType.getBasicType()));
1443 return;
1444 }
1445 break;
1446 case EiifUnspecified:
1447 error(identifierLocation, "layout qualifier", "No image internal format specified");
1448 return;
1449 default:
1450 error(identifierLocation, "layout qualifier", "unrecognized token");
1451 return;
1452 }
1453
1454 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1455 switch (layoutQualifier.imageInternalFormat)
1456 {
1457 case EiifR32F:
1458 case EiifR32I:
1459 case EiifR32UI:
1460 break;
1461 default:
1462 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1463 {
1464 error(identifierLocation, "layout qualifier",
1465 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1466 "image variables must be qualified readonly and/or writeonly");
1467 return;
1468 }
1469 break;
1470 }
1471 }
1472 else
1473 {
Olli Etuaho43364892017-02-13 16:00:12 +00001474 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001475 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1476 }
jchen104cdac9e2017-05-08 11:01:20 +08001477
1478 if (IsAtomicCounter(publicType.getBasicType()))
1479 {
1480 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1481 }
1482 else
1483 {
1484 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1485 }
Olli Etuaho43364892017-02-13 16:00:12 +00001486}
Martin Radev2cc85b32016-08-05 16:22:53 +03001487
Olli Etuaho43364892017-02-13 16:00:12 +00001488void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1489{
1490 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001491 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1492 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1493 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1494 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1495 // when it comes to which shaders are accepted by the compiler.
1496 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001497 if (IsImage(type.getBasicType()))
1498 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001499 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1500 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001501 }
1502 else if (IsSampler(type.getBasicType()))
1503 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001504 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1505 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001506 }
jchen104cdac9e2017-05-08 11:01:20 +08001507 else if (IsAtomicCounter(type.getBasicType()))
1508 {
1509 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1510 }
Olli Etuaho43364892017-02-13 16:00:12 +00001511 else
1512 {
1513 ASSERT(!IsOpaqueType(type.getBasicType()));
1514 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001515 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001516}
1517
Olli Etuaho856c4972016-08-08 11:38:39 +03001518void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1519 const TString &layoutQualifierName,
1520 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001521{
1522
1523 if (mShaderVersion < versionRequired)
1524 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001525 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001526 }
1527}
1528
Olli Etuaho856c4972016-08-08 11:38:39 +03001529bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1530 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001531{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001532 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001533 for (size_t i = 0u; i < localSize.size(); ++i)
1534 {
1535 if (localSize[i] != -1)
1536 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001537 error(location,
1538 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1539 "global layout declaration",
1540 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001541 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001542 }
1543 }
1544
Olli Etuaho8a176262016-08-16 14:23:01 +03001545 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001546}
1547
Olli Etuaho43364892017-02-13 16:00:12 +00001548void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001549 TLayoutImageInternalFormat internalFormat)
1550{
1551 if (internalFormat != EiifUnspecified)
1552 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001553 error(location, "invalid layout qualifier: only valid when used with images",
1554 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001555 }
Olli Etuaho43364892017-02-13 16:00:12 +00001556}
1557
1558void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1559{
1560 if (binding != -1)
1561 {
1562 error(location,
1563 "invalid layout qualifier: only valid when used with opaque types or blocks",
1564 "binding");
1565 }
1566}
1567
jchen104cdac9e2017-05-08 11:01:20 +08001568void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1569{
1570 if (offset != -1)
1571 {
1572 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1573 "offset");
1574 }
1575}
1576
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001577void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1578 int binding,
1579 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001580{
1581 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001582 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001583 {
1584 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1585 }
1586}
1587
1588void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1589 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001590 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001591{
1592 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001593 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001594 {
1595 error(location, "sampler binding greater than maximum texture units", "binding");
1596 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001597}
1598
Jiajia Qinbc585152017-06-23 15:42:17 +08001599void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1600 const TQualifier &qualifier,
1601 int binding,
1602 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001603{
1604 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001605 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001606 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001607 if (binding + size > mMaxUniformBufferBindings)
1608 {
1609 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1610 "binding");
1611 }
1612 }
1613 else if (qualifier == EvqBuffer)
1614 {
1615 if (binding + size > mMaxShaderStorageBufferBindings)
1616 {
1617 error(location,
1618 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1619 "binding");
1620 }
jchen10af713a22017-04-19 09:10:56 +08001621 }
1622}
jchen104cdac9e2017-05-08 11:01:20 +08001623void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1624{
1625 if (binding >= mMaxAtomicCounterBindings)
1626 {
1627 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1628 "binding");
1629 }
1630}
jchen10af713a22017-04-19 09:10:56 +08001631
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001632void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1633 int objectLocationCount,
1634 const TLayoutQualifier &layoutQualifier)
1635{
1636 int loc = layoutQualifier.location;
1637 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1638 {
1639 error(location, "Uniform location out of range", "location");
1640 }
1641}
1642
Andrei Volykhina5527072017-03-22 16:46:30 +03001643void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1644{
1645 if (yuv != false)
1646 {
1647 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1648 }
1649}
1650
Jiajia Qinbc585152017-06-23 15:42:17 +08001651void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1652 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001653{
1654 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1655 {
1656 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001657 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1658 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1659 qual == EvqInOut || qual == EvqConstReadOnly))
1660 {
1661 if (argument->getMemoryQualifier().writeonly)
1662 {
1663 error(argument->getLine(),
1664 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1665 fnCall->getFunctionSymbolInfo()->getName().c_str());
1666 return;
1667 }
1668 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001669 if (qual == EvqOut || qual == EvqInOut)
1670 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001671 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001672 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001673 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001674 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001675 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001676 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001677 }
1678 }
1679 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001680}
1681
Martin Radev70866b82016-07-22 15:27:42 +03001682void TParseContext::checkInvariantVariableQualifier(bool invariant,
1683 const TQualifier qualifier,
1684 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001685{
Martin Radev70866b82016-07-22 15:27:42 +03001686 if (!invariant)
1687 return;
1688
1689 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001690 {
Martin Radev70866b82016-07-22 15:27:42 +03001691 // input variables in the fragment shader can be also qualified as invariant
1692 if (!sh::CanBeInvariantESSL1(qualifier))
1693 {
1694 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1695 }
1696 }
1697 else
1698 {
1699 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1700 {
1701 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1702 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001703 }
1704}
1705
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001706bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001707{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001708 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001709}
1710
Jamie Madillb98c3a82015-07-23 14:26:04 -04001711void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1712 const char *extName,
1713 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001714{
1715 pp::SourceLocation srcLoc;
1716 srcLoc.file = loc.first_file;
1717 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001718 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001719}
1720
Jamie Madillb98c3a82015-07-23 14:26:04 -04001721void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1722 const char *name,
1723 const char *value,
1724 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001725{
1726 pp::SourceLocation srcLoc;
1727 srcLoc.file = loc.first_file;
1728 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001729 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001730}
1731
Martin Radev4c4c8e72016-08-04 12:25:34 +03001732sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001733{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001734 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001735 for (size_t i = 0u; i < result.size(); ++i)
1736 {
1737 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1738 {
1739 result[i] = 1;
1740 }
1741 else
1742 {
1743 result[i] = mComputeShaderLocalSize[i];
1744 }
1745 }
1746 return result;
1747}
1748
Olli Etuaho56229f12017-07-10 14:16:33 +03001749TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1750 const TSourceLoc &line)
1751{
1752 TIntermConstantUnion *node = new TIntermConstantUnion(
1753 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1754 node->setLine(line);
1755 return node;
1756}
1757
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758/////////////////////////////////////////////////////////////////////////////////
1759//
1760// Non-Errors.
1761//
1762/////////////////////////////////////////////////////////////////////////////////
1763
Jamie Madill5c097022014-08-20 16:38:32 -04001764const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1765 const TString *name,
1766 const TSymbol *symbol)
1767{
Jamie Madill5c097022014-08-20 16:38:32 -04001768 if (!symbol)
1769 {
1770 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001771 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001772 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001773
1774 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001775 {
1776 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001777 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001778 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001779
1780 const TVariable *variable = static_cast<const TVariable *>(symbol);
1781
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001782 if (variable->getExtension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001783 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001784 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001785 }
1786
Olli Etuaho0f684632017-07-13 12:42:15 +03001787 // Reject shaders using both gl_FragData and gl_FragColor
1788 TQualifier qualifier = variable->getType().getQualifier();
1789 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001790 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001791 mUsesFragData = true;
1792 }
1793 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1794 {
1795 mUsesFragColor = true;
1796 }
1797 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1798 {
1799 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001800 }
1801
Olli Etuaho0f684632017-07-13 12:42:15 +03001802 // This validation is not quite correct - it's only an error to write to
1803 // both FragData and FragColor. For simplicity, and because users shouldn't
1804 // be rewarded for reading from undefined varaibles, return an error
1805 // if they are both referenced, rather than assigned.
1806 if (mUsesFragData && mUsesFragColor)
1807 {
1808 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1809 if (mUsesSecondaryOutputs)
1810 {
1811 errorMessage =
1812 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1813 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1814 }
1815 error(location, errorMessage, name->c_str());
1816 }
1817
1818 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1819 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1820 qualifier == EvqWorkGroupSize)
1821 {
1822 error(location,
1823 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1824 "gl_WorkGroupSize");
1825 }
Jamie Madill5c097022014-08-20 16:38:32 -04001826 return variable;
1827}
1828
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001829TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1830 const TString *name,
1831 const TSymbol *symbol)
1832{
1833 const TVariable *variable = getNamedVariable(location, name, symbol);
1834
Olli Etuaho0f684632017-07-13 12:42:15 +03001835 if (!variable)
1836 {
1837 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1838 node->setLine(location);
1839 return node;
1840 }
1841
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001842 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001843 TIntermTyped *node = nullptr;
1844
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001845 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001846 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001847 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001848 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001849 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001850 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001851 {
1852 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1853 // needs to be added to the AST as a constant and not as a symbol.
1854 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1855 TConstantUnion *constArray = new TConstantUnion[3];
1856 for (size_t i = 0; i < 3; ++i)
1857 {
1858 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1859 }
1860
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001861 ASSERT(variableType.getBasicType() == EbtUInt);
1862 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001863
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001864 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001865 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001866 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001867 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001868 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1869 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001870 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001871 ASSERT(mGeometryShaderInputArraySize > 0u);
1872
1873 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho55bde912017-10-25 13:41:13 +03001874 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001875 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001876 else
1877 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001878 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001879 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001880 ASSERT(node != nullptr);
1881 node->setLine(location);
1882 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001883}
1884
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885// Initializers show up in several places in the grammar. Have one set of
1886// code to handle them here.
1887//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001888// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001889bool TParseContext::executeInitializer(const TSourceLoc &line,
1890 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001891 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001892 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001893 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894{
Olli Etuaho13389b62016-10-16 11:48:18 +01001895 ASSERT(initNode != nullptr);
1896 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897
Olli Etuaho2935c582015-04-08 14:32:06 +03001898 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001899 if (type.isUnsizedArray())
1900 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001901 // In case initializer is not an array or type has more dimensions than initializer, this
1902 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1903 // actually is an array or not. Having a non-array initializer for an unsized array will
1904 // result in an error later, so we don't generate an error message here.
1905 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001906 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001907 if (!declareVariable(line, identifier, type, &variable))
1908 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001909 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001910 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911
Olli Etuahob0c645e2015-05-12 14:25:36 +03001912 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001913 if (symbolTable.atGlobalLevel() &&
1914 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001915 {
1916 // Error message does not completely match behavior with ESSL 1.00, but
1917 // we want to steer developers towards only using constant expressions.
1918 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001919 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001920 }
1921 if (globalInitWarning)
1922 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001923 warning(
1924 line,
1925 "global variable initializers should be constant expressions "
1926 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1927 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001928 }
1929
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001930 //
1931 // identifier must be of type constant, a global, or a temporary
1932 //
1933 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301934 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1935 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001936 error(line, " cannot initialize this type of qualifier ",
1937 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001938 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001939 }
1940 //
1941 // test for and propagate constant
1942 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943
Arun Patole7e7e68d2015-05-22 12:02:25 +05301944 if (qualifier == EvqConst)
1945 {
1946 if (qualifier != initializer->getType().getQualifier())
1947 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001948 std::stringstream reasonStream;
1949 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1950 << "'";
1951 std::string reason = reasonStream.str();
1952 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001953 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001954 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001955 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301956 if (type != initializer->getType())
1957 {
1958 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001959 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001960 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001961 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001962 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001963
1964 // Save the constant folded value to the variable if possible. For example array
1965 // initializers are not folded, since that way copying the array literal to multiple places
1966 // in the shader is avoided.
1967 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1968 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301969 if (initializer->getAsConstantUnion())
1970 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001971 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001972 ASSERT(*initNode == nullptr);
1973 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301974 }
1975 else if (initializer->getAsSymbolNode())
1976 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001977 const TSymbol *symbol =
1978 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1979 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001981 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001982 if (constArray)
1983 {
1984 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001985 ASSERT(*initNode == nullptr);
1986 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001987 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001988 }
1989 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001990
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001991 TIntermSymbol *intermSymbol =
1992 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1993 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001994 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1995 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001996 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001997 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001998 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001999 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000
Olli Etuaho914b79a2017-06-19 16:03:19 +03002001 return true;
2002}
2003
2004TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2005 const TString &identifier,
2006 TIntermTyped *initializer,
2007 const TSourceLoc &loc)
2008{
2009 checkIsScalarBool(loc, pType);
2010 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002011 TType type(pType);
2012 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002013 {
2014 // The initializer is valid. The init condition needs to have a node - either the
2015 // initializer node, or a constant node in case the initialized variable is const and won't
2016 // be recorded in the AST.
2017 if (initNode == nullptr)
2018 {
2019 return initializer;
2020 }
2021 else
2022 {
2023 TIntermDeclaration *declaration = new TIntermDeclaration();
2024 declaration->appendDeclarator(initNode);
2025 return declaration;
2026 }
2027 }
2028 return nullptr;
2029}
2030
2031TIntermNode *TParseContext::addLoop(TLoopType type,
2032 TIntermNode *init,
2033 TIntermNode *cond,
2034 TIntermTyped *expr,
2035 TIntermNode *body,
2036 const TSourceLoc &line)
2037{
2038 TIntermNode *node = nullptr;
2039 TIntermTyped *typedCond = nullptr;
2040 if (cond)
2041 {
2042 typedCond = cond->getAsTyped();
2043 }
2044 if (cond == nullptr || typedCond)
2045 {
Olli Etuahocce89652017-06-19 16:04:09 +03002046 if (type == ELoopDoWhile)
2047 {
2048 checkIsScalarBool(line, typedCond);
2049 }
2050 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2051 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2052 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2053 !typedCond->isVector()));
2054
Olli Etuaho3ec75682017-07-05 17:02:55 +03002055 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002056 node->setLine(line);
2057 return node;
2058 }
2059
Olli Etuahocce89652017-06-19 16:04:09 +03002060 ASSERT(type != ELoopDoWhile);
2061
Olli Etuaho914b79a2017-06-19 16:03:19 +03002062 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2063 ASSERT(declaration);
2064 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2065 ASSERT(declarator->getLeft()->getAsSymbolNode());
2066
2067 // The condition is a declaration. In the AST representation we don't support declarations as
2068 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2069 // the loop.
2070 TIntermBlock *block = new TIntermBlock();
2071
2072 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2073 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2074 block->appendStatement(declareCondition);
2075
2076 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2077 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002078 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002079 block->appendStatement(loop);
2080 loop->setLine(line);
2081 block->setLine(line);
2082 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083}
2084
Olli Etuahocce89652017-06-19 16:04:09 +03002085TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2086 TIntermNodePair code,
2087 const TSourceLoc &loc)
2088{
Olli Etuaho56229f12017-07-10 14:16:33 +03002089 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002090
2091 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002092 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002093 {
2094 if (cond->getAsConstantUnion()->getBConst(0) == true)
2095 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002096 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002097 }
2098 else
2099 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002100 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002101 }
2102 }
2103
Olli Etuaho3ec75682017-07-05 17:02:55 +03002104 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002105 node->setLine(loc);
2106
2107 return node;
2108}
2109
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002110void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2111{
2112 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2113 typeSpecifier->getBasicType());
2114
2115 if (mShaderVersion < 300 && typeSpecifier->array)
2116 {
2117 error(typeSpecifier->getLine(), "not supported", "first-class array");
2118 typeSpecifier->clearArrayness();
2119 }
2120}
2121
Martin Radev70866b82016-07-22 15:27:42 +03002122TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302123 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002124{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002125 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002126
Martin Radev70866b82016-07-22 15:27:42 +03002127 TPublicType returnType = typeSpecifier;
2128 returnType.qualifier = typeQualifier.qualifier;
2129 returnType.invariant = typeQualifier.invariant;
2130 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002131 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002132 returnType.precision = typeSpecifier.precision;
2133
2134 if (typeQualifier.precision != EbpUndefined)
2135 {
2136 returnType.precision = typeQualifier.precision;
2137 }
2138
Martin Radev4a9cd802016-09-01 16:51:51 +03002139 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2140 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002141
Martin Radev4a9cd802016-09-01 16:51:51 +03002142 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2143 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002144
Martin Radev4a9cd802016-09-01 16:51:51 +03002145 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002146
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002147 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002148 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002149 if (typeSpecifier.array)
2150 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002151 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002152 returnType.clearArrayness();
2153 }
2154
Martin Radev70866b82016-07-22 15:27:42 +03002155 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002156 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002157 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002158 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002159 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002160 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002161
Martin Radev70866b82016-07-22 15:27:42 +03002162 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002163 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002164 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002165 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002166 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002167 }
2168 }
2169 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002170 {
Martin Radev70866b82016-07-22 15:27:42 +03002171 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002172 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002173 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002174 }
Martin Radev70866b82016-07-22 15:27:42 +03002175 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2176 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002177 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002178 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2179 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002180 }
Martin Radev70866b82016-07-22 15:27:42 +03002181 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002182 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002183 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002184 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002185 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002186 }
2187
2188 return returnType;
2189}
2190
Olli Etuaho856c4972016-08-08 11:38:39 +03002191void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2192 const TPublicType &type,
2193 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002194{
2195 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002196 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002197 {
2198 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002199 }
2200
2201 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2202 switch (qualifier)
2203 {
2204 case EvqVertexIn:
2205 // ESSL 3.00 section 4.3.4
2206 if (type.array)
2207 {
2208 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002209 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002210 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002211 return;
2212 case EvqFragmentOut:
2213 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002214 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002215 {
2216 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002217 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002218 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002219 return;
2220 default:
2221 break;
2222 }
2223
2224 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2225 // restrictions.
2226 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002227 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2228 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002229 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2230 {
2231 error(qualifierLocation, "must use 'flat' interpolation here",
2232 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002233 }
2234
Martin Radev4a9cd802016-09-01 16:51:51 +03002235 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002236 {
2237 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2238 // These restrictions are only implied by the ESSL 3.00 spec, but
2239 // the ESSL 3.10 spec lists these restrictions explicitly.
2240 if (type.array)
2241 {
2242 error(qualifierLocation, "cannot be an array of structures",
2243 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002244 }
2245 if (type.isStructureContainingArrays())
2246 {
2247 error(qualifierLocation, "cannot be a structure containing an array",
2248 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002249 }
2250 if (type.isStructureContainingType(EbtStruct))
2251 {
2252 error(qualifierLocation, "cannot be a structure containing a structure",
2253 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002254 }
2255 if (type.isStructureContainingType(EbtBool))
2256 {
2257 error(qualifierLocation, "cannot be a structure containing a bool",
2258 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002259 }
2260 }
2261}
2262
Martin Radev2cc85b32016-08-05 16:22:53 +03002263void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2264{
2265 if (qualifier.getType() == QtStorage)
2266 {
2267 const TStorageQualifierWrapper &storageQualifier =
2268 static_cast<const TStorageQualifierWrapper &>(qualifier);
2269 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2270 !symbolTable.atGlobalLevel())
2271 {
2272 error(storageQualifier.getLine(),
2273 "Local variables can only use the const storage qualifier.",
2274 storageQualifier.getQualifierString().c_str());
2275 }
2276 }
2277}
2278
Olli Etuaho43364892017-02-13 16:00:12 +00002279void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002280 const TSourceLoc &location)
2281{
Jiajia Qinbc585152017-06-23 15:42:17 +08002282 const std::string reason(
2283 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2284 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002285 if (memoryQualifier.readonly)
2286 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002287 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002288 }
2289 if (memoryQualifier.writeonly)
2290 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002291 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002292 }
Martin Radev049edfa2016-11-11 14:35:37 +02002293 if (memoryQualifier.coherent)
2294 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002295 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002296 }
2297 if (memoryQualifier.restrictQualifier)
2298 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002299 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002300 }
2301 if (memoryQualifier.volatileQualifier)
2302 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002303 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002304 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002305}
2306
jchen104cdac9e2017-05-08 11:01:20 +08002307// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2308// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002309void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2310 const TSourceLoc &loc,
2311 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002312{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002313 if (!IsAtomicCounter(type->getBasicType()))
2314 {
2315 return;
2316 }
2317
2318 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2319 : kAtomicCounterSize;
2320 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2321 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002322 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002323 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002324 {
2325 offset = bindingState.appendSpan(size);
2326 }
2327 else
2328 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002329 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002330 }
2331 if (offset == -1)
2332 {
2333 error(loc, "Offset overlapping", "atomic counter");
2334 return;
2335 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002336 layoutQualifier.offset = offset;
2337 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002338}
2339
Olli Etuaho454c34c2017-10-25 16:35:56 +03002340void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2341 const char *token,
2342 TType *type)
2343{
2344 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2345 {
2346 if (type->isArray() && type->getOutermostArraySize() == 0u)
2347 {
2348 // Set size for the unsized geometry shader inputs if they are declared after a valid
2349 // input primitive declaration.
2350 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2351 {
2352 ASSERT(mGeometryShaderInputArraySize > 0u);
2353 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2354 }
2355 else
2356 {
2357 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2358 // An input can be declared without an array size if there is a previous layout
2359 // which specifies the size.
2360 error(location,
2361 "Missing a valid input primitive declaration before declaring an unsized "
2362 "array input",
2363 token);
2364 }
2365 }
2366 else if (type->isArray())
2367 {
2368 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2369 }
2370 else
2371 {
2372 error(location, "Geometry shader input variable must be declared as an array", token);
2373 }
2374 }
2375}
2376
Olli Etuaho13389b62016-10-16 11:48:18 +01002377TIntermDeclaration *TParseContext::parseSingleDeclaration(
2378 TPublicType &publicType,
2379 const TSourceLoc &identifierOrTypeLocation,
2380 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002381{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002382 TType type(publicType);
2383 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2384 mDirectiveHandler.pragma().stdgl.invariantAll)
2385 {
2386 TQualifier qualifier = type.getQualifier();
2387
2388 // The directive handler has already taken care of rejecting invalid uses of this pragma
2389 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2390 // affected variable declarations:
2391 //
2392 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2393 // elsewhere, in TranslatorGLSL.)
2394 //
2395 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2396 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2397 // the way this is currently implemented we have to enable this compiler option before
2398 // parsing the shader and determining the shading language version it uses. If this were
2399 // implemented as a post-pass, the workaround could be more targeted.
2400 //
2401 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2402 // the specification, but there are desktop OpenGL drivers that expect that this is the
2403 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2404 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2405 {
2406 type.setInvariant(true);
2407 }
2408 }
2409
Olli Etuaho454c34c2017-10-25 16:35:56 +03002410 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002411
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002412 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2413 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002414
Olli Etuahobab4c082015-04-24 16:38:49 +03002415 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002416 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002417
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002418 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002419 if (emptyDeclaration)
2420 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002421 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002422 // In most cases we don't need to create a symbol node for an empty declaration.
2423 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2424 if (type.getBasicType() == EbtStruct)
2425 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002426 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002427 }
jchen104cdac9e2017-05-08 11:01:20 +08002428 else if (IsAtomicCounter(publicType.getBasicType()))
2429 {
2430 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2431 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002432 }
2433 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002434 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002435 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002436
Olli Etuaho55bde912017-10-25 13:41:13 +03002437 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002438
Olli Etuaho55bc9052017-10-25 17:33:06 +03002439 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002440
Olli Etuaho2935c582015-04-08 14:32:06 +03002441 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002442 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002443
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002444 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002445 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002446 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002447 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002448 }
2449
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002450 TIntermDeclaration *declaration = new TIntermDeclaration();
2451 declaration->setLine(identifierOrTypeLocation);
2452 if (symbol)
2453 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002454 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002455 declaration->appendDeclarator(symbol);
2456 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002457 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002458}
2459
Olli Etuaho55bde912017-10-25 13:41:13 +03002460TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002461 const TSourceLoc &identifierLocation,
2462 const TString &identifier,
2463 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002464 unsigned int arraySize)
Jamie Madill60ed9812013-06-06 11:56:46 -04002465{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002466 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002467
Olli Etuaho55bde912017-10-25 13:41:13 +03002468 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002469 identifierLocation);
2470
Olli Etuaho55bde912017-10-25 13:41:13 +03002471 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002472
Olli Etuaho55bde912017-10-25 13:41:13 +03002473 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002474
Olli Etuaho55bde912017-10-25 13:41:13 +03002475 TType arrayType(elementType);
2476 arrayType.makeArray(arraySize);
Jamie Madill60ed9812013-06-06 11:56:46 -04002477
Olli Etuaho454c34c2017-10-25 16:35:56 +03002478 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002479
2480 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2481
Olli Etuaho55bc9052017-10-25 17:33:06 +03002482 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002483
Olli Etuaho2935c582015-04-08 14:32:06 +03002484 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002485 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002486
Olli Etuaho13389b62016-10-16 11:48:18 +01002487 TIntermDeclaration *declaration = new TIntermDeclaration();
2488 declaration->setLine(identifierLocation);
2489
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002490 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002491 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002492 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2493 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002494 declaration->appendDeclarator(symbol);
2495 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002496
Olli Etuaho13389b62016-10-16 11:48:18 +01002497 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002498}
2499
Olli Etuaho13389b62016-10-16 11:48:18 +01002500TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2501 const TSourceLoc &identifierLocation,
2502 const TString &identifier,
2503 const TSourceLoc &initLocation,
2504 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002505{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002506 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002507
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002508 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2509 identifierLocation);
2510
2511 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002512
Olli Etuaho13389b62016-10-16 11:48:18 +01002513 TIntermDeclaration *declaration = new TIntermDeclaration();
2514 declaration->setLine(identifierLocation);
2515
2516 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002517 TType type(publicType);
2518 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002519 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002520 if (initNode)
2521 {
2522 declaration->appendDeclarator(initNode);
2523 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002524 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002525 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002526}
2527
Olli Etuaho13389b62016-10-16 11:48:18 +01002528TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002529 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002530 const TSourceLoc &identifierLocation,
2531 const TString &identifier,
2532 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002533 unsigned int arraySize,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002534 const TSourceLoc &initLocation,
2535 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002536{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002537 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002538
Olli Etuaho55bde912017-10-25 13:41:13 +03002539 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002540 identifierLocation);
2541
Olli Etuaho55bde912017-10-25 13:41:13 +03002542 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002543
Olli Etuaho55bde912017-10-25 13:41:13 +03002544 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002545
Olli Etuaho55bde912017-10-25 13:41:13 +03002546 TType arrayType(elementType);
2547 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002548
Olli Etuaho13389b62016-10-16 11:48:18 +01002549 TIntermDeclaration *declaration = new TIntermDeclaration();
2550 declaration->setLine(identifierLocation);
2551
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002552 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002553 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002554 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002555 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002556 if (initNode)
2557 {
2558 declaration->appendDeclarator(initNode);
2559 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002560 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002561
2562 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002563}
2564
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002565TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002566 const TTypeQualifierBuilder &typeQualifierBuilder,
2567 const TSourceLoc &identifierLoc,
2568 const TString *identifier,
2569 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002570{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002571 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002572
Martin Radev70866b82016-07-22 15:27:42 +03002573 if (!typeQualifier.invariant)
2574 {
2575 error(identifierLoc, "Expected invariant", identifier->c_str());
2576 return nullptr;
2577 }
2578 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2579 {
2580 return nullptr;
2581 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002582 if (!symbol)
2583 {
2584 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002585 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002586 }
Martin Radev70866b82016-07-22 15:27:42 +03002587 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002588 {
Martin Radev70866b82016-07-22 15:27:42 +03002589 error(identifierLoc, "invariant declaration specifies qualifier",
2590 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002591 }
Martin Radev70866b82016-07-22 15:27:42 +03002592 if (typeQualifier.precision != EbpUndefined)
2593 {
2594 error(identifierLoc, "invariant declaration specifies precision",
2595 getPrecisionString(typeQualifier.precision));
2596 }
2597 if (!typeQualifier.layoutQualifier.isEmpty())
2598 {
2599 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2600 }
2601
2602 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002603 if (!variable)
2604 {
2605 return nullptr;
2606 }
Martin Radev70866b82016-07-22 15:27:42 +03002607 const TType &type = variable->getType();
2608
2609 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2610 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002611 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002612
2613 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2614
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002615 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2616 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002617
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002618 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002619}
2620
Olli Etuaho13389b62016-10-16 11:48:18 +01002621void TParseContext::parseDeclarator(TPublicType &publicType,
2622 const TSourceLoc &identifierLocation,
2623 const TString &identifier,
2624 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002625{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002626 // If the declaration starting this declarator list was empty (example: int,), some checks were
2627 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002628 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002629 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002630 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2631 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002632 }
2633
Olli Etuaho856c4972016-08-08 11:38:39 +03002634 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002635
Olli Etuaho2935c582015-04-08 14:32:06 +03002636 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002637 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002638
2639 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2640
Olli Etuaho55bde912017-10-25 13:41:13 +03002641 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2642
Olli Etuaho55bc9052017-10-25 17:33:06 +03002643 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2644
Olli Etuaho43364892017-02-13 16:00:12 +00002645 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002646
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002647 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002648 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002649 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2650 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002651 declarationOut->appendDeclarator(symbol);
2652 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002653}
2654
Olli Etuaho55bde912017-10-25 13:41:13 +03002655void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002656 const TSourceLoc &identifierLocation,
2657 const TString &identifier,
2658 const TSourceLoc &arrayLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002659 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002660 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002661{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002662 // If the declaration starting this declarator list was empty (example: int,), some checks were
2663 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002664 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002665 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002666 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002667 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002668 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002669
Olli Etuaho55bde912017-10-25 13:41:13 +03002670 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002671
Olli Etuaho55bde912017-10-25 13:41:13 +03002672 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002673 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002674 TType arrayType(elementType);
2675 arrayType.makeArray(arraySize);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002676
Olli Etuaho454c34c2017-10-25 16:35:56 +03002677 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2678
Olli Etuaho55bde912017-10-25 13:41:13 +03002679 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2680
Olli Etuaho55bc9052017-10-25 17:33:06 +03002681 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002682
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002683 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002684 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002685
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002686 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002687 {
2688 TIntermSymbol *symbol =
2689 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2690 symbol->setLine(identifierLocation);
2691 declarationOut->appendDeclarator(symbol);
2692 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002693 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002694}
2695
Olli Etuaho13389b62016-10-16 11:48:18 +01002696void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2697 const TSourceLoc &identifierLocation,
2698 const TString &identifier,
2699 const TSourceLoc &initLocation,
2700 TIntermTyped *initializer,
2701 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002702{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002703 // If the declaration starting this declarator list was empty (example: int,), some checks were
2704 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002705 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002706 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002707 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2708 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002709 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002710
Olli Etuaho856c4972016-08-08 11:38:39 +03002711 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002712
Olli Etuaho13389b62016-10-16 11:48:18 +01002713 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002714 TType type(publicType);
2715 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002716 {
2717 //
2718 // build the intermediate representation
2719 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002720 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002721 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002722 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002723 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002724 }
2725}
2726
Olli Etuaho55bde912017-10-25 13:41:13 +03002727void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002728 const TSourceLoc &identifierLocation,
2729 const TString &identifier,
2730 const TSourceLoc &indexLocation,
Olli Etuaho55bde912017-10-25 13:41:13 +03002731 unsigned int arraySize,
Olli Etuaho13389b62016-10-16 11:48:18 +01002732 const TSourceLoc &initLocation,
2733 TIntermTyped *initializer,
2734 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002735{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002736 // If the declaration starting this declarator list was empty (example: int,), some checks were
2737 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002738 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002739 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002740 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002741 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002742 }
2743
Olli Etuaho55bde912017-10-25 13:41:13 +03002744 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002745
Olli Etuaho55bde912017-10-25 13:41:13 +03002746 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002747
Olli Etuaho55bde912017-10-25 13:41:13 +03002748 TType arrayType(elementType);
2749 arrayType.makeArray(arraySize);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002750
2751 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002752 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002753 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002754 {
2755 if (initNode)
2756 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002757 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002758 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002759 }
2760}
2761
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002762TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2763{
2764 // It's simpler to parse an empty statement as a constant expression rather than having a
2765 // different type of node just for empty statements, that will be pruned from the AST anyway.
2766 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2767 node->setLine(location);
2768 return node;
2769}
2770
jchen104cdac9e2017-05-08 11:01:20 +08002771void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2772 const TSourceLoc &location)
2773{
2774 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2775 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2776 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2777 {
2778 error(location, "Requires both binding and offset", "layout");
2779 return;
2780 }
2781 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2782}
2783
Olli Etuahocce89652017-06-19 16:04:09 +03002784void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2785 const TPublicType &type,
2786 const TSourceLoc &loc)
2787{
2788 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2789 !getFragmentPrecisionHigh())
2790 {
2791 error(loc, "precision is not supported in fragment shader", "highp");
2792 }
2793
2794 if (!CanSetDefaultPrecisionOnType(type))
2795 {
2796 error(loc, "illegal type argument for default precision qualifier",
2797 getBasicString(type.getBasicType()));
2798 return;
2799 }
2800 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2801}
2802
Shaob5cc1192017-07-06 10:47:20 +08002803bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2804{
2805 switch (typeQualifier.layoutQualifier.primitiveType)
2806 {
2807 case EptLines:
2808 case EptLinesAdjacency:
2809 case EptTriangles:
2810 case EptTrianglesAdjacency:
2811 return typeQualifier.qualifier == EvqGeometryIn;
2812
2813 case EptLineStrip:
2814 case EptTriangleStrip:
2815 return typeQualifier.qualifier == EvqGeometryOut;
2816
2817 case EptPoints:
2818 return true;
2819
2820 default:
2821 UNREACHABLE();
2822 return false;
2823 }
2824}
2825
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002826void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2827 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002828{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002829 if (mGeometryShaderInputArraySize == 0u)
2830 {
2831 mGeometryShaderInputArraySize = inputArraySize;
2832 }
2833 else if (mGeometryShaderInputArraySize != inputArraySize)
2834 {
2835 error(line,
2836 "Array size or input primitive declaration doesn't match the size of earlier sized "
2837 "array inputs.",
2838 "layout");
2839 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002840}
2841
Shaob5cc1192017-07-06 10:47:20 +08002842bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2843{
2844 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2845
2846 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2847
2848 if (layoutQualifier.maxVertices != -1)
2849 {
2850 error(typeQualifier.line,
2851 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2852 return false;
2853 }
2854
2855 // Set mGeometryInputPrimitiveType if exists
2856 if (layoutQualifier.primitiveType != EptUndefined)
2857 {
2858 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2859 {
2860 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2861 return false;
2862 }
2863
2864 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2865 {
2866 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002867 setGeometryShaderInputArraySize(
2868 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2869 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002870 }
2871 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2872 {
2873 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2874 "layout");
2875 return false;
2876 }
2877 }
2878
2879 // Set mGeometryInvocations if exists
2880 if (layoutQualifier.invocations > 0)
2881 {
2882 if (mGeometryShaderInvocations == 0)
2883 {
2884 mGeometryShaderInvocations = layoutQualifier.invocations;
2885 }
2886 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2887 {
2888 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2889 "layout");
2890 return false;
2891 }
2892 }
2893
2894 return true;
2895}
2896
2897bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2898{
2899 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2900
2901 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2902
2903 if (layoutQualifier.invocations > 0)
2904 {
2905 error(typeQualifier.line,
2906 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2907 return false;
2908 }
2909
2910 // Set mGeometryOutputPrimitiveType if exists
2911 if (layoutQualifier.primitiveType != EptUndefined)
2912 {
2913 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2914 {
2915 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2916 return false;
2917 }
2918
2919 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2920 {
2921 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2922 }
2923 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2924 {
2925 error(typeQualifier.line,
2926 "primitive doesn't match earlier output primitive declaration", "layout");
2927 return false;
2928 }
2929 }
2930
2931 // Set mGeometryMaxVertices if exists
2932 if (layoutQualifier.maxVertices > -1)
2933 {
2934 if (mGeometryShaderMaxVertices == -1)
2935 {
2936 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2937 }
2938 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2939 {
2940 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2941 "layout");
2942 return false;
2943 }
2944 }
2945
2946 return true;
2947}
2948
Martin Radev70866b82016-07-22 15:27:42 +03002949void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002950{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002951 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002952 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002953
Martin Radev70866b82016-07-22 15:27:42 +03002954 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2955 typeQualifier.line);
2956
Jamie Madillc2128ff2016-07-04 10:26:17 -04002957 // It should never be the case, but some strange parser errors can send us here.
2958 if (layoutQualifier.isEmpty())
2959 {
2960 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002961 return;
2962 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002963
Martin Radev802abe02016-08-04 17:48:32 +03002964 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002965 {
Olli Etuaho43364892017-02-13 16:00:12 +00002966 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002967 return;
2968 }
2969
Olli Etuaho43364892017-02-13 16:00:12 +00002970 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2971
2972 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002973
2974 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2975
Andrei Volykhina5527072017-03-22 16:46:30 +03002976 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2977
jchen104cdac9e2017-05-08 11:01:20 +08002978 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2979
Qin Jiajiaca68d982017-09-18 16:41:56 +08002980 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2981 typeQualifier.qualifier);
2982
Martin Radev802abe02016-08-04 17:48:32 +03002983 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002984 {
Martin Radev802abe02016-08-04 17:48:32 +03002985 if (mComputeShaderLocalSizeDeclared &&
2986 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2987 {
2988 error(typeQualifier.line, "Work group size does not match the previous declaration",
2989 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002990 return;
2991 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002992
Martin Radev802abe02016-08-04 17:48:32 +03002993 if (mShaderVersion < 310)
2994 {
2995 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002996 return;
2997 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002998
Martin Radev4c4c8e72016-08-04 12:25:34 +03002999 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003000 {
3001 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003002 return;
3003 }
3004
3005 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3006 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3007
3008 const TConstantUnion *maxComputeWorkGroupSizeData =
3009 maxComputeWorkGroupSize->getConstPointer();
3010
3011 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3012 {
3013 if (layoutQualifier.localSize[i] != -1)
3014 {
3015 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3016 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3017 if (mComputeShaderLocalSize[i] < 1 ||
3018 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3019 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003020 std::stringstream reasonStream;
3021 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3022 << maxComputeWorkGroupSizeValue;
3023 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003024
Olli Etuaho4de340a2016-12-16 09:32:03 +00003025 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003026 return;
3027 }
3028 }
3029 }
3030
3031 mComputeShaderLocalSizeDeclared = true;
3032 }
Shaob5cc1192017-07-06 10:47:20 +08003033 else if (typeQualifier.qualifier == EvqGeometryIn)
3034 {
3035 if (mShaderVersion < 310)
3036 {
3037 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3038 return;
3039 }
3040
3041 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3042 {
3043 return;
3044 }
3045 }
3046 else if (typeQualifier.qualifier == EvqGeometryOut)
3047 {
3048 if (mShaderVersion < 310)
3049 {
3050 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3051 "layout");
3052 return;
3053 }
3054
3055 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3056 {
3057 return;
3058 }
3059 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003060 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3061 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003062 {
3063 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3064 // specification.
3065 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3066 {
3067 error(typeQualifier.line, "Number of views does not match the previous declaration",
3068 "layout");
3069 return;
3070 }
3071
3072 if (layoutQualifier.numViews == -1)
3073 {
3074 error(typeQualifier.line, "No num_views specified", "layout");
3075 return;
3076 }
3077
3078 if (layoutQualifier.numViews > mMaxNumViews)
3079 {
3080 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3081 "layout");
3082 return;
3083 }
3084
3085 mNumViews = layoutQualifier.numViews;
3086 }
Martin Radev802abe02016-08-04 17:48:32 +03003087 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003088 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003089 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003090 {
Martin Radev802abe02016-08-04 17:48:32 +03003091 return;
3092 }
3093
Jiajia Qinbc585152017-06-23 15:42:17 +08003094 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003095 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003096 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003097 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003098 return;
3099 }
3100
3101 if (mShaderVersion < 300)
3102 {
3103 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3104 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003105 return;
3106 }
3107
Olli Etuaho09b04a22016-12-15 13:30:26 +00003108 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003109
3110 if (layoutQualifier.matrixPacking != EmpUnspecified)
3111 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003112 if (typeQualifier.qualifier == EvqUniform)
3113 {
3114 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3115 }
3116 else if (typeQualifier.qualifier == EvqBuffer)
3117 {
3118 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3119 }
Martin Radev802abe02016-08-04 17:48:32 +03003120 }
3121
3122 if (layoutQualifier.blockStorage != EbsUnspecified)
3123 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003124 if (typeQualifier.qualifier == EvqUniform)
3125 {
3126 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3127 }
3128 else if (typeQualifier.qualifier == EvqBuffer)
3129 {
3130 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3131 }
Martin Radev802abe02016-08-04 17:48:32 +03003132 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003133 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003134}
3135
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003136TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3137 const TFunction &function,
3138 const TSourceLoc &location,
3139 bool insertParametersToSymbolTable)
3140{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003141 checkIsNotReserved(location, function.getName());
3142
Olli Etuahofe486322017-03-21 09:30:54 +00003143 TIntermFunctionPrototype *prototype =
3144 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003145 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3146 // point to the data that already exists in the symbol table.
3147 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3148 prototype->setLine(location);
3149
3150 for (size_t i = 0; i < function.getParamCount(); i++)
3151 {
3152 const TConstParameter &param = function.getParam(i);
3153
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003154 TIntermSymbol *symbol = nullptr;
3155
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003156 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3157 // be used for unused args).
3158 if (param.name != nullptr)
3159 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003160 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003161 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003162 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003163 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3164 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003165 {
3166 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3167 variable->getType());
3168 }
3169 else
3170 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003171 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003172 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003173 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003174 // Unsized type of a named parameter should have already been checked and sanitized.
3175 ASSERT(!param.type->isUnsizedArray());
3176 }
3177 else
3178 {
3179 if (param.type->isUnsizedArray())
3180 {
3181 error(location, "function parameter array must be sized at compile time", "[]");
3182 // We don't need to size the arrays since the parameter is unnamed and hence
3183 // inaccessible.
3184 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003185 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003186 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003187 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003188 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3189 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003190 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003191 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003192 symbol->setLine(location);
3193 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003194 }
3195 return prototype;
3196}
3197
Olli Etuaho16c745a2017-01-16 17:02:27 +00003198TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3199 const TFunction &parsedFunction,
3200 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003201{
Olli Etuaho476197f2016-10-11 13:59:08 +01003202 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3203 // first declaration. Either way the instance in the symbol table is used to track whether the
3204 // function is declared multiple times.
3205 TFunction *function = static_cast<TFunction *>(
3206 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3207 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003208 {
3209 // ESSL 1.00.17 section 4.2.7.
3210 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3211 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003212 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003213 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003214
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003215 TIntermFunctionPrototype *prototype =
3216 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003217
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003218 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003219
3220 if (!symbolTable.atGlobalLevel())
3221 {
3222 // ESSL 3.00.4 section 4.2.4.
3223 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003224 }
3225
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003226 return prototype;
3227}
3228
Olli Etuaho336b1472016-10-05 16:37:55 +01003229TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003230 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003231 TIntermBlock *functionBody,
3232 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003233{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003234 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003235 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3236 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003237 error(location, "function does not return a value:",
3238 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003239 }
3240
Olli Etuahof51fdd22016-10-03 10:03:40 +01003241 if (functionBody == nullptr)
3242 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003243 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003244 functionBody->setLine(location);
3245 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003246 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003247 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003248 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003249
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003250 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003251 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003252}
3253
Olli Etuaho476197f2016-10-11 13:59:08 +01003254void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3255 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003256 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003257{
Olli Etuaho476197f2016-10-11 13:59:08 +01003258 ASSERT(function);
3259 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003260 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003261 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003262
3263 if (builtIn)
3264 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003265 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003266 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003267 else
Jamie Madill185fb402015-06-12 15:48:48 -04003268 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003269 TFunction *prevDec = static_cast<TFunction *>(
3270 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3271
3272 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3273 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3274 // occurance.
3275 if (*function != prevDec)
3276 {
3277 // Swap the parameters of the previous declaration to the parameters of the function
3278 // definition (parameter names may differ).
3279 prevDec->swapParameters(**function);
3280
3281 // The function definition will share the same symbol as any previous declaration.
3282 *function = prevDec;
3283 }
3284
3285 if ((*function)->isDefined())
3286 {
3287 error(location, "function already has a body", (*function)->getName().c_str());
3288 }
3289
3290 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003291 }
Jamie Madill185fb402015-06-12 15:48:48 -04003292
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003293 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003294 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003295 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003296
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003297 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003298 setLoopNestingLevel(0);
3299}
3300
Jamie Madillb98c3a82015-07-23 14:26:04 -04003301TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003302{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003303 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003304 // We don't know at this point whether this is a function definition or a prototype.
3305 // The definition production code will check for redefinitions.
3306 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003307 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003308 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3309 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003310 //
3311 TFunction *prevDec =
3312 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303313
Olli Etuahod80f2942017-11-06 12:44:45 +02003314 for (size_t i = 0u; i < function->getParamCount(); ++i)
3315 {
3316 auto &param = function->getParam(i);
3317 if (param.type->isStructSpecifier())
3318 {
3319 // ESSL 3.00.6 section 12.10.
3320 error(location, "Function parameter type cannot be a structure definition",
Olli Etuaho7af63722017-11-13 15:03:40 +02003321 function->getName().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003322 }
3323 }
3324
Martin Radevda6254b2016-12-14 17:00:36 +02003325 if (getShaderVersion() >= 300 &&
3326 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3327 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303328 {
Martin Radevda6254b2016-12-14 17:00:36 +02003329 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303330 // Therefore overloading or redefining builtin functions is an error.
3331 error(location, "Name of a built-in function cannot be redeclared as function",
3332 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303333 }
3334 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003335 {
3336 if (prevDec->getReturnType() != function->getReturnType())
3337 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003338 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003339 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003340 }
3341 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3342 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003343 if (prevDec->getParam(i).type->getQualifier() !=
3344 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003345 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003346 error(location,
3347 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003348 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003349 }
3350 }
3351 }
3352
3353 //
3354 // Check for previously declared variables using the same name.
3355 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003356 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003357 if (prevSym)
3358 {
3359 if (!prevSym->isFunction())
3360 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003361 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003362 }
3363 }
3364 else
3365 {
3366 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003367 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003368 }
3369
3370 // We're at the inner scope level of the function's arguments and body statement.
3371 // Add the function prototype to the surrounding scope instead.
3372 symbolTable.getOuterLevel()->insert(function);
3373
Olli Etuaho78d13742017-01-18 13:06:10 +00003374 // Raise error message if main function takes any parameters or return anything other than void
3375 if (function->getName() == "main")
3376 {
3377 if (function->getParamCount() > 0)
3378 {
3379 error(location, "function cannot take any parameter(s)", "main");
3380 }
3381 if (function->getReturnType().getBasicType() != EbtVoid)
3382 {
3383 error(location, "main function cannot return a value",
3384 function->getReturnType().getBasicString());
3385 }
3386 }
3387
Jamie Madill185fb402015-06-12 15:48:48 -04003388 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003389 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3390 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003391 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3392 //
3393 return function;
3394}
3395
Olli Etuaho9de84a52016-06-14 17:36:01 +03003396TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3397 const TString *name,
3398 const TSourceLoc &location)
3399{
3400 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3401 {
3402 error(location, "no qualifiers allowed for function return",
3403 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003404 }
3405 if (!type.layoutQualifier.isEmpty())
3406 {
3407 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003408 }
jchen10cc2a10e2017-05-03 14:05:12 +08003409 // make sure an opaque type is not involved as well...
3410 std::string reason(getBasicString(type.getBasicType()));
3411 reason += "s can't be function return values";
3412 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003413 if (mShaderVersion < 300)
3414 {
3415 // Array return values are forbidden, but there's also no valid syntax for declaring array
3416 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003417 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003418
3419 if (type.isStructureContainingArrays())
3420 {
3421 // ESSL 1.00.17 section 6.1 Function Definitions
3422 error(location, "structures containing arrays can't be function return values",
3423 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003424 }
3425 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003426
3427 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003428 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003429}
3430
Olli Etuahocce89652017-06-19 16:04:09 +03003431TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3432{
Olli Etuahocce89652017-06-19 16:04:09 +03003433 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003434 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003435}
3436
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003437TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003438{
Olli Etuahocce89652017-06-19 16:04:09 +03003439 if (mShaderVersion < 300 && publicType.array)
3440 {
3441 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3442 "[]");
3443 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003444 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003445 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003446 error(publicType.getLine(), "constructor can't be a structure definition",
3447 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003448 }
3449
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003450 TType *type = new TType(publicType);
3451 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003452 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003453 error(publicType.getLine(), "cannot construct this type",
3454 getBasicString(publicType.getBasicType()));
3455 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003456 }
3457
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003458 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003459}
3460
Olli Etuaho55bde912017-10-25 13:41:13 +03003461void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3462 const char *errorMessage,
3463 const char *token,
3464 TType *arrayType)
3465{
3466 if (arrayType->isUnsizedArray())
3467 {
3468 error(line, errorMessage, token);
3469 arrayType->sizeUnsizedArrays(TVector<unsigned int>());
3470 }
3471}
3472
3473TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003474 const TString *name,
3475 const TSourceLoc &nameLoc)
3476{
Olli Etuaho55bde912017-10-25 13:41:13 +03003477 ASSERT(type);
3478 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3479 type);
3480 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003481 {
3482 error(nameLoc, "illegal use of type 'void'", name->c_str());
3483 }
3484 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003485 TParameter param = {name, type};
3486 return param;
3487}
3488
Olli Etuaho55bde912017-10-25 13:41:13 +03003489TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3490 const TString *name,
3491 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003492{
Olli Etuaho55bde912017-10-25 13:41:13 +03003493 TType *type = new TType(publicType);
3494 return parseParameterDeclarator(type, name, nameLoc);
3495}
3496
3497TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3498 const TSourceLoc &nameLoc,
3499 unsigned int arraySize,
3500 const TSourceLoc &arrayLoc,
3501 TPublicType *elementType)
3502{
3503 checkArrayElementIsNotArray(arrayLoc, *elementType);
3504 TType *arrayType = new TType(*elementType);
3505 arrayType->makeArray(arraySize);
3506 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003507}
3508
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003509bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3510 TType type,
3511 const TSourceLoc &line)
3512{
3513 if (arguments->empty())
3514 {
3515 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3516 return false;
3517 }
3518 for (TIntermNode *arg : *arguments)
3519 {
3520 TIntermTyped *element = arg->getAsTyped();
3521 ASSERT(element);
3522 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3523 if (dimensionalityFromElement > type.getArraySizes().size())
3524 {
3525 error(line, "constructing from a non-dereferenced array", "constructor");
3526 return false;
3527 }
3528 else if (dimensionalityFromElement < type.getArraySizes().size())
3529 {
3530 if (dimensionalityFromElement == 1u)
3531 {
3532 error(line, "implicitly sized array of arrays constructor argument is not an array",
3533 "constructor");
3534 }
3535 else
3536 {
3537 error(line,
3538 "implicitly sized array of arrays constructor argument dimensionality is too "
3539 "low",
3540 "constructor");
3541 }
3542 return false;
3543 }
3544 }
3545 return true;
3546}
3547
Jamie Madillb98c3a82015-07-23 14:26:04 -04003548// This function is used to test for the correctness of the parameters passed to various constructor
3549// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003550//
Olli Etuaho856c4972016-08-08 11:38:39 +03003551// 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 +00003552//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003553TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003554 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303555 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003556{
Olli Etuaho856c4972016-08-08 11:38:39 +03003557 if (type.isUnsizedArray())
3558 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003559 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003560 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003561 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003562 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003563 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003564 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3565 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003566 if (type.getOutermostArraySize() == 0u)
3567 {
3568 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3569 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003570 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3571 {
3572 if (type.getArraySizes()[i] == 0u)
3573 {
3574 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3575 }
3576 }
3577 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003578 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003579
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003580 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003581 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003582 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003583 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003584
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003585 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003586 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003587
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003588 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3589 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003590 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003591 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003592 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003593 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003594}
3595
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003596//
3597// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003598// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003599//
Olli Etuaho13389b62016-10-16 11:48:18 +01003600TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003601 const TTypeQualifierBuilder &typeQualifierBuilder,
3602 const TSourceLoc &nameLine,
3603 const TString &blockName,
3604 TFieldList *fieldList,
3605 const TString *instanceName,
3606 const TSourceLoc &instanceLine,
3607 TIntermTyped *arrayIndex,
3608 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003609{
Olli Etuaho856c4972016-08-08 11:38:39 +03003610 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003611
Olli Etuaho77ba4082016-12-16 12:01:18 +00003612 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003613
Jiajia Qinbc585152017-06-23 15:42:17 +08003614 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003615 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003616 error(typeQualifier.line,
3617 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3618 "3.10",
3619 getQualifierString(typeQualifier.qualifier));
3620 }
3621 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3622 {
3623 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003624 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003625 }
3626
Martin Radev70866b82016-07-22 15:27:42 +03003627 if (typeQualifier.invariant)
3628 {
3629 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3630 }
3631
Jiajia Qinbc585152017-06-23 15:42:17 +08003632 if (typeQualifier.qualifier != EvqBuffer)
3633 {
3634 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3635 }
Olli Etuaho43364892017-02-13 16:00:12 +00003636
jchen10af713a22017-04-19 09:10:56 +08003637 // add array index
3638 unsigned int arraySize = 0;
3639 if (arrayIndex != nullptr)
3640 {
3641 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3642 }
3643
3644 if (mShaderVersion < 310)
3645 {
3646 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3647 }
3648 else
3649 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003650 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3651 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003652 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003653
Andrei Volykhina5527072017-03-22 16:46:30 +03003654 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3655
Jamie Madill099c0f32013-06-20 11:55:52 -04003656 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003657 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003658 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3659 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003660
Jamie Madill099c0f32013-06-20 11:55:52 -04003661 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3662 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003663 if (typeQualifier.qualifier == EvqUniform)
3664 {
3665 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3666 }
3667 else if (typeQualifier.qualifier == EvqBuffer)
3668 {
3669 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3670 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003671 }
3672
Jamie Madill1566ef72013-06-20 11:55:54 -04003673 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3674 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003675 if (typeQualifier.qualifier == EvqUniform)
3676 {
3677 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3678 }
3679 else if (typeQualifier.qualifier == EvqBuffer)
3680 {
3681 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3682 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003683 }
3684
Olli Etuaho856c4972016-08-08 11:38:39 +03003685 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003686
Martin Radev2cc85b32016-08-05 16:22:53 +03003687 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3688
Olli Etuaho0f684632017-07-13 12:42:15 +03003689 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303690 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003691 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003692 }
3693
Jamie Madill98493dd2013-07-08 14:39:03 -04003694 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303695 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3696 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003697 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303698 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003699 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303700 {
jchen10cc2a10e2017-05-03 14:05:12 +08003701 std::string reason("unsupported type - ");
3702 reason += fieldType->getBasicString();
3703 reason += " types are not allowed in interface blocks";
3704 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003705 }
3706
Jamie Madill98493dd2013-07-08 14:39:03 -04003707 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003708 switch (qualifier)
3709 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003710 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003711 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003712 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003713 if (typeQualifier.qualifier == EvqBuffer)
3714 {
3715 error(field->line(), "invalid qualifier on shader storage block member",
3716 getQualifierString(qualifier));
3717 }
3718 break;
3719 case EvqBuffer:
3720 if (typeQualifier.qualifier == EvqUniform)
3721 {
3722 error(field->line(), "invalid qualifier on uniform block member",
3723 getQualifierString(qualifier));
3724 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003725 break;
3726 default:
3727 error(field->line(), "invalid qualifier on interface block member",
3728 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003729 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003730 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003731
Martin Radev70866b82016-07-22 15:27:42 +03003732 if (fieldType->isInvariant())
3733 {
3734 error(field->line(), "invalid qualifier on interface block member", "invariant");
3735 }
3736
Jamie Madilla5efff92013-06-06 11:56:47 -04003737 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003738 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003739 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003740 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003741
Jamie Madill98493dd2013-07-08 14:39:03 -04003742 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003743 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003744 error(field->line(), "invalid layout qualifier: cannot be used here",
3745 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003746 }
3747
Jamie Madill98493dd2013-07-08 14:39:03 -04003748 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003749 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003750 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003751 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003752 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003753 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003754 warning(field->line(),
3755 "extraneous layout qualifier: only has an effect on matrix types",
3756 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003757 }
3758
Jamie Madill98493dd2013-07-08 14:39:03 -04003759 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003760
3761 if (typeQualifier.qualifier == EvqBuffer)
3762 {
3763 // set memory qualifiers
3764 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3765 // qualified with a memory qualifier, it is as if all of its members were declared with
3766 // the same memory qualifier.
3767 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3768 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3769 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3770 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3771 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3772 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3773 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3774 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3775 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3776 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3777 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003778 }
3779
Jamie Madillb98c3a82015-07-23 14:26:04 -04003780 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003781 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003782 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3783 if (arrayIndex != nullptr)
3784 {
3785 interfaceBlockType.makeArray(arraySize);
3786 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003787
3788 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003789 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003790
Jamie Madill98493dd2013-07-08 14:39:03 -04003791 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003792 {
3793 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003794 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3795 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003796 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303797 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003798
3799 // set parent pointer of the field variable
3800 fieldType->setInterfaceBlock(interfaceBlock);
3801
Olli Etuaho0f684632017-07-13 12:42:15 +03003802 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003803
Olli Etuaho0f684632017-07-13 12:42:15 +03003804 if (fieldVariable)
3805 {
3806 fieldVariable->setQualifier(typeQualifier.qualifier);
3807 }
3808 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303809 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003810 error(field->line(), "redefinition of an interface block member name",
3811 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003812 }
3813 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003814 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003815 }
3816 else
3817 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003818 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003819
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003820 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003821 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3822 if (instanceTypeDef)
3823 {
3824 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003825 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003826 }
3827 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303828 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003829 error(instanceLine, "redefinition of an interface block instance name",
3830 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003831 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003832 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003833 }
3834
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003835 TIntermDeclaration *declaration = nullptr;
3836
3837 if (symbolId)
3838 {
3839 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3840 blockSymbol->setLine(typeQualifier.line);
3841 declaration = new TIntermDeclaration();
3842 declaration->appendDeclarator(blockSymbol);
3843 declaration->setLine(nameLine);
3844 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003845
3846 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003847 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003848}
3849
Olli Etuaho383b7912016-08-05 11:22:59 +03003850void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003851{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003852 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003853
3854 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003855 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303856 if (mStructNestingLevel > 1)
3857 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003858 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003859 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003860}
3861
3862void TParseContext::exitStructDeclaration()
3863{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003864 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003865}
3866
Olli Etuaho8a176262016-08-16 14:23:01 +03003867void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003868{
Jamie Madillacb4b812016-11-07 13:50:29 -05003869 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303870 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003871 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003872 }
3873
Arun Patole7e7e68d2015-05-22 12:02:25 +05303874 if (field.type()->getBasicType() != EbtStruct)
3875 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003876 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003877 }
3878
3879 // We're already inside a structure definition at this point, so add
3880 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303881 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3882 {
Jamie Madill41a49272014-03-18 16:10:13 -04003883 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003884 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3885 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003886 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003887 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003888 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003889 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003890}
3891
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003892//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003893// Parse an array index expression
3894//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003895TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3896 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303897 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003898{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003899 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3900 {
3901 if (baseExpression->getAsSymbolNode())
3902 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303903 error(location, " left of '[' is not of type array, matrix, or vector ",
3904 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003905 }
3906 else
3907 {
3908 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3909 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003910
Olli Etuaho3ec75682017-07-05 17:02:55 +03003911 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003912 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003913
Jiawei Shaod8105a02017-08-08 09:54:36 +08003914 if (baseExpression->getQualifier() == EvqPerVertexIn)
3915 {
3916 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3917 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3918 {
3919 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3920 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3921 }
3922 }
3923
Jamie Madill21c1e452014-12-29 11:33:41 -05003924 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3925
Olli Etuaho36b05142015-11-12 13:10:42 +02003926 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3927 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3928 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3929 // index is a constant expression.
3930 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3931 {
3932 if (baseExpression->isInterfaceBlock())
3933 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003934 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3935 switch (baseExpression->getQualifier())
3936 {
3937 case EvqPerVertexIn:
3938 break;
3939 case EvqUniform:
3940 case EvqBuffer:
3941 error(location,
3942 "array indexes for uniform block arrays and shader storage block arrays "
3943 "must be constant integral expressions",
3944 "[");
3945 break;
3946 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003947 // We can reach here only in error cases.
3948 ASSERT(mDiagnostics->numErrors() > 0);
3949 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003950 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003951 }
3952 else if (baseExpression->getQualifier() == EvqFragmentOut)
3953 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003954 error(location,
3955 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003956 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003957 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3958 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003959 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003960 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003961 }
3962
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003963 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003964 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003965 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3966 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3967 // constant fold expressions that are not constant expressions). The most compatible way to
3968 // handle this case is to report a warning instead of an error and force the index to be in
3969 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003970 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003971 int index = 0;
3972 if (indexConstantUnion->getBasicType() == EbtInt)
3973 {
3974 index = indexConstantUnion->getIConst(0);
3975 }
3976 else if (indexConstantUnion->getBasicType() == EbtUInt)
3977 {
3978 index = static_cast<int>(indexConstantUnion->getUConst(0));
3979 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003980
3981 int safeIndex = -1;
3982
3983 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003984 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003985 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003986 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003987 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003988 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003989 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003990 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003991 "GL_EXT_draw_buffers is disabled",
3992 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003993 safeIndex = 0;
3994 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003995 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003996 // Only do generic out-of-range check if similar error hasn't already been reported.
3997 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003998 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003999 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004000 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00004001 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004002 }
4003 }
4004 else if (baseExpression->isMatrix())
4005 {
4006 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03004007 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00004008 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04004009 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004010 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04004011 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004012 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
4013 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00004014 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004015 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004016
4017 ASSERT(safeIndex >= 0);
4018 // Data of constant unions can't be changed, because it may be shared with other
4019 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4020 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03004021 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004022 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004023 TConstantUnion *safeConstantUnion = new TConstantUnion();
4024 safeConstantUnion->setIConst(safeIndex);
4025 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03004026 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004027 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004028
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004029 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4030 node->setLine(location);
4031 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004032 }
Jamie Madill7164cf42013-07-08 13:30:59 -04004033 else
4034 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004035 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4036 node->setLine(location);
4037 // Indirect indexing can never be constant folded.
4038 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04004039 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004040}
4041
Olli Etuaho90892fb2016-07-14 14:44:51 +03004042int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
4043 const TSourceLoc &location,
4044 int index,
4045 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00004046 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004047{
4048 if (index >= arraySize || index < 0)
4049 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004050 std::stringstream reasonStream;
4051 reasonStream << reason << " '" << index << "'";
4052 std::string token = reasonStream.str();
4053 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03004054 if (index < 0)
4055 {
4056 return 0;
4057 }
4058 else
4059 {
4060 return arraySize - 1;
4061 }
4062 }
4063 return index;
4064}
4065
Jamie Madillb98c3a82015-07-23 14:26:04 -04004066TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4067 const TSourceLoc &dotLocation,
4068 const TString &fieldString,
4069 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004070{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004071 if (baseExpression->isArray())
4072 {
4073 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004074 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004075 }
4076
4077 if (baseExpression->isVector())
4078 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004079 TVector<int> fieldOffsets;
4080 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4081 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004082 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004083 fieldOffsets.resize(1);
4084 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004085 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004086 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4087 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004088
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004089 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004090 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004091 else if (baseExpression->getBasicType() == EbtStruct)
4092 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304093 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004094 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004095 {
4096 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004097 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004098 }
4099 else
4100 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004101 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004102 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004103 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004104 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004105 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004106 {
4107 fieldFound = true;
4108 break;
4109 }
4110 }
4111 if (fieldFound)
4112 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004113 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004114 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004115 TIntermBinary *node =
4116 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4117 node->setLine(dotLocation);
4118 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004119 }
4120 else
4121 {
4122 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004123 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124 }
4125 }
4126 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004127 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004128 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304129 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004130 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004131 {
4132 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004133 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004134 }
4135 else
4136 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004137 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004138 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004139 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004140 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004141 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004142 {
4143 fieldFound = true;
4144 break;
4145 }
4146 }
4147 if (fieldFound)
4148 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004149 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004150 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004151 TIntermBinary *node =
4152 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4153 node->setLine(dotLocation);
4154 // Indexing interface blocks can never be constant folded.
4155 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004156 }
4157 else
4158 {
4159 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004160 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004161 }
4162 }
4163 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004164 else
4165 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004166 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004167 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004168 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304169 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004170 }
4171 else
4172 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304173 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004174 " field selection requires structure, vector, or interface block on left hand "
4175 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304176 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004177 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004178 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004179 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004180}
4181
Jamie Madillb98c3a82015-07-23 14:26:04 -04004182TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4183 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004184{
Martin Radev802abe02016-08-04 17:48:32 +03004185 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004186
4187 if (qualifierType == "shared")
4188 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004189 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004190 {
4191 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4192 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004193 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004194 }
4195 else if (qualifierType == "packed")
4196 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004197 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004198 {
4199 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4200 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004201 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004202 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004203 else if (qualifierType == "std430")
4204 {
4205 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4206 qualifier.blockStorage = EbsStd430;
4207 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004208 else if (qualifierType == "std140")
4209 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004210 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004211 }
4212 else if (qualifierType == "row_major")
4213 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004214 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004215 }
4216 else if (qualifierType == "column_major")
4217 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004218 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004219 }
4220 else if (qualifierType == "location")
4221 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004222 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4223 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004224 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004225 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004226 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004227 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4228 {
4229 qualifier.yuv = true;
4230 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004231 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004232 else if (qualifierType == "rgba32f")
4233 {
4234 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4235 qualifier.imageInternalFormat = EiifRGBA32F;
4236 }
4237 else if (qualifierType == "rgba16f")
4238 {
4239 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4240 qualifier.imageInternalFormat = EiifRGBA16F;
4241 }
4242 else if (qualifierType == "r32f")
4243 {
4244 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4245 qualifier.imageInternalFormat = EiifR32F;
4246 }
4247 else if (qualifierType == "rgba8")
4248 {
4249 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4250 qualifier.imageInternalFormat = EiifRGBA8;
4251 }
4252 else if (qualifierType == "rgba8_snorm")
4253 {
4254 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4255 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4256 }
4257 else if (qualifierType == "rgba32i")
4258 {
4259 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4260 qualifier.imageInternalFormat = EiifRGBA32I;
4261 }
4262 else if (qualifierType == "rgba16i")
4263 {
4264 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4265 qualifier.imageInternalFormat = EiifRGBA16I;
4266 }
4267 else if (qualifierType == "rgba8i")
4268 {
4269 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4270 qualifier.imageInternalFormat = EiifRGBA8I;
4271 }
4272 else if (qualifierType == "r32i")
4273 {
4274 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4275 qualifier.imageInternalFormat = EiifR32I;
4276 }
4277 else if (qualifierType == "rgba32ui")
4278 {
4279 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4280 qualifier.imageInternalFormat = EiifRGBA32UI;
4281 }
4282 else if (qualifierType == "rgba16ui")
4283 {
4284 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4285 qualifier.imageInternalFormat = EiifRGBA16UI;
4286 }
4287 else if (qualifierType == "rgba8ui")
4288 {
4289 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4290 qualifier.imageInternalFormat = EiifRGBA8UI;
4291 }
4292 else if (qualifierType == "r32ui")
4293 {
4294 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4295 qualifier.imageInternalFormat = EiifR32UI;
4296 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004297 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4298 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004299 {
4300 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4301 qualifier.primitiveType = EptPoints;
4302 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004303 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4304 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004305 {
4306 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4307 qualifier.primitiveType = EptLines;
4308 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004309 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4310 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004311 {
4312 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4313 qualifier.primitiveType = EptLinesAdjacency;
4314 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004315 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4316 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004317 {
4318 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4319 qualifier.primitiveType = EptTriangles;
4320 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004321 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4322 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004323 {
4324 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4325 qualifier.primitiveType = EptTrianglesAdjacency;
4326 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004327 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4328 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004329 {
4330 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4331 qualifier.primitiveType = EptLineStrip;
4332 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004333 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4334 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004335 {
4336 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4337 qualifier.primitiveType = EptTriangleStrip;
4338 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004339
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004340 else
4341 {
4342 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004343 }
4344
Jamie Madilla5efff92013-06-06 11:56:47 -04004345 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004346}
4347
Martin Radev802abe02016-08-04 17:48:32 +03004348void TParseContext::parseLocalSize(const TString &qualifierType,
4349 const TSourceLoc &qualifierTypeLine,
4350 int intValue,
4351 const TSourceLoc &intValueLine,
4352 const std::string &intValueString,
4353 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004354 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004355{
Olli Etuaho856c4972016-08-08 11:38:39 +03004356 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004357 if (intValue < 1)
4358 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004359 std::stringstream reasonStream;
4360 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4361 std::string reason = reasonStream.str();
4362 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004363 }
4364 (*localSize)[index] = intValue;
4365}
4366
Olli Etuaho09b04a22016-12-15 13:30:26 +00004367void TParseContext::parseNumViews(int intValue,
4368 const TSourceLoc &intValueLine,
4369 const std::string &intValueString,
4370 int *numViews)
4371{
4372 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4373 // specification.
4374 if (intValue < 1)
4375 {
4376 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4377 }
4378 *numViews = intValue;
4379}
4380
Shaob5cc1192017-07-06 10:47:20 +08004381void TParseContext::parseInvocations(int intValue,
4382 const TSourceLoc &intValueLine,
4383 const std::string &intValueString,
4384 int *numInvocations)
4385{
4386 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4387 // it doesn't make sense to accept invocations <= 0.
4388 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4389 {
4390 error(intValueLine,
4391 "out of range: invocations must be in the range of [1, "
4392 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4393 intValueString.c_str());
4394 }
4395 else
4396 {
4397 *numInvocations = intValue;
4398 }
4399}
4400
4401void TParseContext::parseMaxVertices(int intValue,
4402 const TSourceLoc &intValueLine,
4403 const std::string &intValueString,
4404 int *maxVertices)
4405{
4406 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4407 // it doesn't make sense to accept max_vertices < 0.
4408 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4409 {
4410 error(
4411 intValueLine,
4412 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4413 intValueString.c_str());
4414 }
4415 else
4416 {
4417 *maxVertices = intValue;
4418 }
4419}
4420
Jamie Madillb98c3a82015-07-23 14:26:04 -04004421TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4422 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004423 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304424 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004425{
Martin Radev802abe02016-08-04 17:48:32 +03004426 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004427
Martin Radev802abe02016-08-04 17:48:32 +03004428 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004429
Martin Radev802abe02016-08-04 17:48:32 +03004430 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004431 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004432 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004433 if (intValue < 0)
4434 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004435 error(intValueLine, "out of range: location must be non-negative",
4436 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004437 }
4438 else
4439 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004440 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004441 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004442 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004443 }
Olli Etuaho43364892017-02-13 16:00:12 +00004444 else if (qualifierType == "binding")
4445 {
4446 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4447 if (intValue < 0)
4448 {
4449 error(intValueLine, "out of range: binding must be non-negative",
4450 intValueString.c_str());
4451 }
4452 else
4453 {
4454 qualifier.binding = intValue;
4455 }
4456 }
jchen104cdac9e2017-05-08 11:01:20 +08004457 else if (qualifierType == "offset")
4458 {
4459 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4460 if (intValue < 0)
4461 {
4462 error(intValueLine, "out of range: offset must be non-negative",
4463 intValueString.c_str());
4464 }
4465 else
4466 {
4467 qualifier.offset = intValue;
4468 }
4469 }
Martin Radev802abe02016-08-04 17:48:32 +03004470 else if (qualifierType == "local_size_x")
4471 {
4472 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4473 &qualifier.localSize);
4474 }
4475 else if (qualifierType == "local_size_y")
4476 {
4477 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4478 &qualifier.localSize);
4479 }
4480 else if (qualifierType == "local_size_z")
4481 {
4482 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4483 &qualifier.localSize);
4484 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004485 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004486 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004487 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4488 {
4489 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4490 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004491 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004492 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4493 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004494 {
4495 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4496 }
Jiawei Shao0e883132017-10-26 09:53:50 +08004497 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_OES &&
4498 checkCanUseExtension(qualifierTypeLine, TExtension::OES_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004499 {
4500 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4501 }
4502
Martin Radev802abe02016-08-04 17:48:32 +03004503 else
4504 {
4505 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004506 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004507
Jamie Madilla5efff92013-06-06 11:56:47 -04004508 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004509}
4510
Olli Etuaho613b9592016-09-05 12:05:53 +03004511TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4512{
4513 return new TTypeQualifierBuilder(
4514 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4515 mShaderVersion);
4516}
4517
Olli Etuahocce89652017-06-19 16:04:09 +03004518TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4519 const TSourceLoc &loc)
4520{
4521 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4522 return new TStorageQualifierWrapper(qualifier, loc);
4523}
4524
4525TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4526{
4527 if (getShaderType() == GL_VERTEX_SHADER)
4528 {
4529 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4530 }
4531 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4532}
4533
4534TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4535{
4536 if (declaringFunction())
4537 {
4538 return new TStorageQualifierWrapper(EvqIn, loc);
4539 }
Shaob5cc1192017-07-06 10:47:20 +08004540
4541 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004542 {
Shaob5cc1192017-07-06 10:47:20 +08004543 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004544 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004545 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004546 {
4547 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4548 }
4549 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004550 }
Shaob5cc1192017-07-06 10:47:20 +08004551 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004552 {
Shaob5cc1192017-07-06 10:47:20 +08004553 if (mShaderVersion < 300)
4554 {
4555 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4556 }
4557 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004558 }
Shaob5cc1192017-07-06 10:47:20 +08004559 case GL_COMPUTE_SHADER:
4560 {
4561 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4562 }
4563 case GL_GEOMETRY_SHADER_OES:
4564 {
4565 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4566 }
4567 default:
4568 {
4569 UNREACHABLE();
4570 return new TStorageQualifierWrapper(EvqLast, loc);
4571 }
Olli Etuahocce89652017-06-19 16:04:09 +03004572 }
Olli Etuahocce89652017-06-19 16:04:09 +03004573}
4574
4575TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4576{
4577 if (declaringFunction())
4578 {
4579 return new TStorageQualifierWrapper(EvqOut, loc);
4580 }
Shaob5cc1192017-07-06 10:47:20 +08004581 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004582 {
Shaob5cc1192017-07-06 10:47:20 +08004583 case GL_VERTEX_SHADER:
4584 {
4585 if (mShaderVersion < 300)
4586 {
4587 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4588 }
4589 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4590 }
4591 case GL_FRAGMENT_SHADER:
4592 {
4593 if (mShaderVersion < 300)
4594 {
4595 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4596 }
4597 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4598 }
4599 case GL_COMPUTE_SHADER:
4600 {
4601 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4602 return new TStorageQualifierWrapper(EvqLast, loc);
4603 }
4604 case GL_GEOMETRY_SHADER_OES:
4605 {
4606 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4607 }
4608 default:
4609 {
4610 UNREACHABLE();
4611 return new TStorageQualifierWrapper(EvqLast, loc);
4612 }
Olli Etuahocce89652017-06-19 16:04:09 +03004613 }
Olli Etuahocce89652017-06-19 16:04:09 +03004614}
4615
4616TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4617{
4618 if (!declaringFunction())
4619 {
4620 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4621 }
4622 return new TStorageQualifierWrapper(EvqInOut, loc);
4623}
4624
Jamie Madillb98c3a82015-07-23 14:26:04 -04004625TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004626 TLayoutQualifier rightQualifier,
4627 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004628{
Martin Radevc28888b2016-07-22 15:27:42 +03004629 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004630 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004631}
4632
Olli Etuahocce89652017-06-19 16:04:09 +03004633TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4634{
4635 checkIsNotReserved(loc, *identifier);
4636 TType *type = new TType(EbtVoid, EbpUndefined);
4637 return new TField(type, identifier, loc);
4638}
4639
4640TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4641 const TSourceLoc &loc,
Olli Etuaho4ddae352017-10-26 16:20:18 +03004642 unsigned int arraySize,
Olli Etuahocce89652017-06-19 16:04:09 +03004643 const TSourceLoc &arraySizeLoc)
4644{
4645 checkIsNotReserved(loc, *identifier);
4646
4647 TType *type = new TType(EbtVoid, EbpUndefined);
Olli Etuaho4ddae352017-10-26 16:20:18 +03004648 type->makeArray(arraySize);
Olli Etuahocce89652017-06-19 16:04:09 +03004649
4650 return new TField(type, identifier, loc);
4651}
4652
Olli Etuaho722bfb52017-10-26 17:00:11 +03004653void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4654 const TFieldList::const_iterator end,
4655 const TString &name,
4656 const TSourceLoc &location)
4657{
4658 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4659 {
4660 if ((*fieldIter)->name() == name)
4661 {
4662 error(location, "duplicate field name in structure", name.c_str());
4663 }
4664 }
4665}
4666
4667TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4668{
4669 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4670 ++fieldIter)
4671 {
4672 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4673 location);
4674 }
4675 return fields;
4676}
4677
Olli Etuaho4de340a2016-12-16 09:32:03 +00004678TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4679 const TFieldList *newlyAddedFields,
4680 const TSourceLoc &location)
4681{
4682 for (TField *field : *newlyAddedFields)
4683 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004684 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4685 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004686 processedFields->push_back(field);
4687 }
4688 return processedFields;
4689}
4690
Martin Radev70866b82016-07-22 15:27:42 +03004691TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4692 const TTypeQualifierBuilder &typeQualifierBuilder,
4693 TPublicType *typeSpecifier,
4694 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004695{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004696 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004697
Martin Radev70866b82016-07-22 15:27:42 +03004698 typeSpecifier->qualifier = typeQualifier.qualifier;
4699 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004700 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004701 typeSpecifier->invariant = typeQualifier.invariant;
4702 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304703 {
Martin Radev70866b82016-07-22 15:27:42 +03004704 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004705 }
Martin Radev70866b82016-07-22 15:27:42 +03004706 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004707}
4708
Jamie Madillb98c3a82015-07-23 14:26:04 -04004709TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004710 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004711{
Martin Radev4a9cd802016-09-01 16:51:51 +03004712 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4713 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004714
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004715 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4716 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004717
Martin Radev4a9cd802016-09-01 16:51:51 +03004718 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004719
Olli Etuaho55bde912017-10-25 13:41:13 +03004720 for (TField *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304721 {
Olli Etuaho55bde912017-10-25 13:41:13 +03004722 auto declaratorArraySizes = declarator->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004723 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004724 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304725 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004726 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004727 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004728
Olli Etuaho55bde912017-10-25 13:41:13 +03004729 TType *type = declarator->type();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004730 *type = TType(typeSpecifier);
4731 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304732 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004733 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004734 }
Olli Etuaho55bde912017-10-25 13:41:13 +03004735 checkIsNotUnsizedArray(typeSpecifier.getLine(),
4736 "array members of structs must specify a size",
4737 declarator->name().c_str(), type);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004738
Olli Etuaho55bde912017-10-25 13:41:13 +03004739 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *declarator);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004740 }
4741
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004742 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004743}
4744
Martin Radev4a9cd802016-09-01 16:51:51 +03004745TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4746 const TSourceLoc &nameLine,
4747 const TString *structName,
4748 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004749{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004750 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004751
Jamie Madill9b820842015-02-12 10:40:10 -05004752 // Store a bool in the struct if we're at global scope, to allow us to
4753 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004754 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004755
Jamie Madill98493dd2013-07-08 14:39:03 -04004756 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004757 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004758 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004759 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304760 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004761 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004762 }
4763 }
4764
4765 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004766 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004767 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004768 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004769 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004770 switch (qualifier)
4771 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004772 case EvqGlobal:
4773 case EvqTemporary:
4774 break;
4775 default:
4776 error(field.line(), "invalid qualifier on struct member",
4777 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004778 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004779 }
Martin Radev70866b82016-07-22 15:27:42 +03004780 if (field.type()->isInvariant())
4781 {
4782 error(field.line(), "invalid qualifier on struct member", "invariant");
4783 }
jchen104cdac9e2017-05-08 11:01:20 +08004784 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4785 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004786 {
4787 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4788 }
4789
Olli Etuaho43364892017-02-13 16:00:12 +00004790 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4791
4792 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004793
4794 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004795 }
4796
Martin Radev4a9cd802016-09-01 16:51:51 +03004797 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004798 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004799 exitStructDeclaration();
4800
Martin Radev4a9cd802016-09-01 16:51:51 +03004801 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004802}
4803
Jamie Madillb98c3a82015-07-23 14:26:04 -04004804TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004805 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004806 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004807{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004808 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004809 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004810 init->isVector())
4811 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004812 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4813 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004814 return nullptr;
4815 }
4816
Olli Etuaho923ecef2017-10-11 12:01:38 +03004817 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004818 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004819 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004820 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004821 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004822 }
4823
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004824 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4825 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004826 return node;
4827}
4828
4829TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4830{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004831 if (mSwitchNestingLevel == 0)
4832 {
4833 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004834 return nullptr;
4835 }
4836 if (condition == nullptr)
4837 {
4838 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004839 return nullptr;
4840 }
4841 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004842 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004843 {
4844 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004845 }
4846 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004847 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4848 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4849 // fold in case labels.
4850 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004851 {
4852 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004853 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004854 TIntermCase *node = new TIntermCase(condition);
4855 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004856 return node;
4857}
4858
4859TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4860{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004861 if (mSwitchNestingLevel == 0)
4862 {
4863 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004864 return nullptr;
4865 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004866 TIntermCase *node = new TIntermCase(nullptr);
4867 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004868 return node;
4869}
4870
Jamie Madillb98c3a82015-07-23 14:26:04 -04004871TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4872 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004873 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004874{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004875 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004876
4877 switch (op)
4878 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004879 case EOpLogicalNot:
4880 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4881 child->isVector())
4882 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004883 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004884 return nullptr;
4885 }
4886 break;
4887 case EOpBitwiseNot:
4888 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4889 child->isMatrix() || child->isArray())
4890 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004891 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004892 return nullptr;
4893 }
4894 break;
4895 case EOpPostIncrement:
4896 case EOpPreIncrement:
4897 case EOpPostDecrement:
4898 case EOpPreDecrement:
4899 case EOpNegative:
4900 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004901 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4902 child->getBasicType() == EbtBool || child->isArray() ||
4903 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004904 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004905 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004906 return nullptr;
4907 }
4908 // Operators for built-ins are already type checked against their prototype.
4909 default:
4910 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004911 }
4912
Jiajia Qinbc585152017-06-23 15:42:17 +08004913 if (child->getMemoryQualifier().writeonly)
4914 {
4915 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4916 return nullptr;
4917 }
4918
Olli Etuahof119a262016-08-19 15:54:22 +03004919 TIntermUnary *node = new TIntermUnary(op, child);
4920 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004921
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004922 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004923}
4924
Olli Etuaho09b22472015-02-11 11:47:26 +02004925TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4926{
Olli Etuahocce89652017-06-19 16:04:09 +03004927 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004928 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004929 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004930 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004931 return child;
4932 }
4933 return node;
4934}
4935
Jamie Madillb98c3a82015-07-23 14:26:04 -04004936TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4937 TIntermTyped *child,
4938 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004939{
Olli Etuaho856c4972016-08-08 11:38:39 +03004940 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004941 return addUnaryMath(op, child, loc);
4942}
4943
Jamie Madillb98c3a82015-07-23 14:26:04 -04004944bool TParseContext::binaryOpCommonCheck(TOperator op,
4945 TIntermTyped *left,
4946 TIntermTyped *right,
4947 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004948{
jchen10b4cf5652017-05-05 18:51:17 +08004949 // Check opaque types are not allowed to be operands in expressions other than array indexing
4950 // and structure member selection.
4951 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4952 {
4953 switch (op)
4954 {
4955 case EOpIndexDirect:
4956 case EOpIndexIndirect:
4957 break;
4958 case EOpIndexDirectStruct:
4959 UNREACHABLE();
4960
4961 default:
4962 error(loc, "Invalid operation for variables with an opaque type",
4963 GetOperatorString(op));
4964 return false;
4965 }
4966 }
jchen10cc2a10e2017-05-03 14:05:12 +08004967
Jiajia Qinbc585152017-06-23 15:42:17 +08004968 if (right->getMemoryQualifier().writeonly)
4969 {
4970 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4971 return false;
4972 }
4973
4974 if (left->getMemoryQualifier().writeonly)
4975 {
4976 switch (op)
4977 {
4978 case EOpAssign:
4979 case EOpInitialize:
4980 case EOpIndexDirect:
4981 case EOpIndexIndirect:
4982 case EOpIndexDirectStruct:
4983 case EOpIndexDirectInterfaceBlock:
4984 break;
4985 default:
4986 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4987 return false;
4988 }
4989 }
4990
Olli Etuaho244be012016-08-18 15:26:02 +03004991 if (left->getType().getStruct() || right->getType().getStruct())
4992 {
4993 switch (op)
4994 {
4995 case EOpIndexDirectStruct:
4996 ASSERT(left->getType().getStruct());
4997 break;
4998 case EOpEqual:
4999 case EOpNotEqual:
5000 case EOpAssign:
5001 case EOpInitialize:
5002 if (left->getType() != right->getType())
5003 {
5004 return false;
5005 }
5006 break;
5007 default:
5008 error(loc, "Invalid operation for structs", GetOperatorString(op));
5009 return false;
5010 }
5011 }
5012
Olli Etuaho94050052017-05-08 14:17:44 +03005013 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5014 {
5015 switch (op)
5016 {
5017 case EOpIndexDirectInterfaceBlock:
5018 ASSERT(left->getType().getInterfaceBlock());
5019 break;
5020 default:
5021 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5022 return false;
5023 }
5024 }
5025
Olli Etuahod6b14282015-03-17 14:31:35 +02005026 if (left->isArray() || right->isArray())
5027 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005028 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005029 {
5030 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5031 return false;
5032 }
5033
5034 if (left->isArray() != right->isArray())
5035 {
5036 error(loc, "array / non-array mismatch", GetOperatorString(op));
5037 return false;
5038 }
5039
5040 switch (op)
5041 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005042 case EOpEqual:
5043 case EOpNotEqual:
5044 case EOpAssign:
5045 case EOpInitialize:
5046 break;
5047 default:
5048 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5049 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005050 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005051 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03005052 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005053 {
5054 error(loc, "array size mismatch", GetOperatorString(op));
5055 return false;
5056 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005057 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005058
5059 // Check ops which require integer / ivec parameters
5060 bool isBitShift = false;
5061 switch (op)
5062 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005063 case EOpBitShiftLeft:
5064 case EOpBitShiftRight:
5065 case EOpBitShiftLeftAssign:
5066 case EOpBitShiftRightAssign:
5067 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5068 // check that the basic type is an integer type.
5069 isBitShift = true;
5070 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5071 {
5072 return false;
5073 }
5074 break;
5075 case EOpBitwiseAnd:
5076 case EOpBitwiseXor:
5077 case EOpBitwiseOr:
5078 case EOpBitwiseAndAssign:
5079 case EOpBitwiseXorAssign:
5080 case EOpBitwiseOrAssign:
5081 // It is enough to check the type of only one operand, since later it
5082 // is checked that the operand types match.
5083 if (!IsInteger(left->getBasicType()))
5084 {
5085 return false;
5086 }
5087 break;
5088 default:
5089 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005090 }
5091
5092 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5093 // So the basic type should usually match.
5094 if (!isBitShift && left->getBasicType() != right->getBasicType())
5095 {
5096 return false;
5097 }
5098
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005099 // Check that:
5100 // 1. Type sizes match exactly on ops that require that.
5101 // 2. Restrictions for structs that contain arrays or samplers are respected.
5102 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005103 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005104 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005105 case EOpAssign:
5106 case EOpInitialize:
5107 case EOpEqual:
5108 case EOpNotEqual:
5109 // ESSL 1.00 sections 5.7, 5.8, 5.9
5110 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5111 {
5112 error(loc, "undefined operation for structs containing arrays",
5113 GetOperatorString(op));
5114 return false;
5115 }
5116 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5117 // we interpret the spec so that this extends to structs containing samplers,
5118 // similarly to ESSL 1.00 spec.
5119 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5120 left->getType().isStructureContainingSamplers())
5121 {
5122 error(loc, "undefined operation for structs containing samplers",
5123 GetOperatorString(op));
5124 return false;
5125 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005126
Olli Etuahoe1805592017-01-02 16:41:20 +00005127 if ((left->getNominalSize() != right->getNominalSize()) ||
5128 (left->getSecondarySize() != right->getSecondarySize()))
5129 {
5130 error(loc, "dimension mismatch", GetOperatorString(op));
5131 return false;
5132 }
5133 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005134 case EOpLessThan:
5135 case EOpGreaterThan:
5136 case EOpLessThanEqual:
5137 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005138 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005139 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005140 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005141 return false;
5142 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005143 break;
5144 case EOpAdd:
5145 case EOpSub:
5146 case EOpDiv:
5147 case EOpIMod:
5148 case EOpBitShiftLeft:
5149 case EOpBitShiftRight:
5150 case EOpBitwiseAnd:
5151 case EOpBitwiseXor:
5152 case EOpBitwiseOr:
5153 case EOpAddAssign:
5154 case EOpSubAssign:
5155 case EOpDivAssign:
5156 case EOpIModAssign:
5157 case EOpBitShiftLeftAssign:
5158 case EOpBitShiftRightAssign:
5159 case EOpBitwiseAndAssign:
5160 case EOpBitwiseXorAssign:
5161 case EOpBitwiseOrAssign:
5162 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5163 {
5164 return false;
5165 }
5166
5167 // Are the sizes compatible?
5168 if (left->getNominalSize() != right->getNominalSize() ||
5169 left->getSecondarySize() != right->getSecondarySize())
5170 {
5171 // If the nominal sizes of operands do not match:
5172 // One of them must be a scalar.
5173 if (!left->isScalar() && !right->isScalar())
5174 return false;
5175
5176 // In the case of compound assignment other than multiply-assign,
5177 // the right side needs to be a scalar. Otherwise a vector/matrix
5178 // would be assigned to a scalar. A scalar can't be shifted by a
5179 // vector either.
5180 if (!right->isScalar() &&
5181 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5182 return false;
5183 }
5184 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005185 default:
5186 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005187 }
5188
Olli Etuahod6b14282015-03-17 14:31:35 +02005189 return true;
5190}
5191
Olli Etuaho1dded802016-08-18 18:13:13 +03005192bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5193 const TType &left,
5194 const TType &right)
5195{
5196 switch (op)
5197 {
5198 case EOpMul:
5199 case EOpMulAssign:
5200 return left.getNominalSize() == right.getNominalSize() &&
5201 left.getSecondarySize() == right.getSecondarySize();
5202 case EOpVectorTimesScalar:
5203 return true;
5204 case EOpVectorTimesScalarAssign:
5205 ASSERT(!left.isMatrix() && !right.isMatrix());
5206 return left.isVector() && !right.isVector();
5207 case EOpVectorTimesMatrix:
5208 return left.getNominalSize() == right.getRows();
5209 case EOpVectorTimesMatrixAssign:
5210 ASSERT(!left.isMatrix() && right.isMatrix());
5211 return left.isVector() && left.getNominalSize() == right.getRows() &&
5212 left.getNominalSize() == right.getCols();
5213 case EOpMatrixTimesVector:
5214 return left.getCols() == right.getNominalSize();
5215 case EOpMatrixTimesScalar:
5216 return true;
5217 case EOpMatrixTimesScalarAssign:
5218 ASSERT(left.isMatrix() && !right.isMatrix());
5219 return !right.isVector();
5220 case EOpMatrixTimesMatrix:
5221 return left.getCols() == right.getRows();
5222 case EOpMatrixTimesMatrixAssign:
5223 ASSERT(left.isMatrix() && right.isMatrix());
5224 // We need to check two things:
5225 // 1. The matrix multiplication step is valid.
5226 // 2. The result will have the same number of columns as the lvalue.
5227 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5228
5229 default:
5230 UNREACHABLE();
5231 return false;
5232 }
5233}
5234
Jamie Madillb98c3a82015-07-23 14:26:04 -04005235TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5236 TIntermTyped *left,
5237 TIntermTyped *right,
5238 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005239{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005240 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005241 return nullptr;
5242
Olli Etuahofc1806e2015-03-17 13:03:11 +02005243 switch (op)
5244 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005245 case EOpEqual:
5246 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005247 case EOpLessThan:
5248 case EOpGreaterThan:
5249 case EOpLessThanEqual:
5250 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005251 break;
5252 case EOpLogicalOr:
5253 case EOpLogicalXor:
5254 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005255 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5256 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005257 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005258 {
5259 return nullptr;
5260 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005261 // Basic types matching should have been already checked.
5262 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005263 break;
5264 case EOpAdd:
5265 case EOpSub:
5266 case EOpDiv:
5267 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005268 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5269 !right->getType().getStruct());
5270 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005271 {
5272 return nullptr;
5273 }
5274 break;
5275 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005276 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5277 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005278 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005279 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005280 {
5281 return nullptr;
5282 }
5283 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005284 default:
5285 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005286 }
5287
Olli Etuaho1dded802016-08-18 18:13:13 +03005288 if (op == EOpMul)
5289 {
5290 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5291 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5292 {
5293 return nullptr;
5294 }
5295 }
5296
Olli Etuaho3fdec912016-08-18 15:08:06 +03005297 TIntermBinary *node = new TIntermBinary(op, left, right);
5298 node->setLine(loc);
5299
Olli Etuaho3fdec912016-08-18 15:08:06 +03005300 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005301 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005302}
5303
Jamie Madillb98c3a82015-07-23 14:26:04 -04005304TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5305 TIntermTyped *left,
5306 TIntermTyped *right,
5307 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005308{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005309 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005310 if (node == 0)
5311 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005312 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5313 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005314 return left;
5315 }
5316 return node;
5317}
5318
Jamie Madillb98c3a82015-07-23 14:26:04 -04005319TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5320 TIntermTyped *left,
5321 TIntermTyped *right,
5322 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005323{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005324 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005325 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005326 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005327 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5328 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005329 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005330 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005331 }
5332 return node;
5333}
5334
Olli Etuaho13389b62016-10-16 11:48:18 +01005335TIntermBinary *TParseContext::createAssign(TOperator op,
5336 TIntermTyped *left,
5337 TIntermTyped *right,
5338 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005339{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005340 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005341 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005342 if (op == EOpMulAssign)
5343 {
5344 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5345 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5346 {
5347 return nullptr;
5348 }
5349 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005350 TIntermBinary *node = new TIntermBinary(op, left, right);
5351 node->setLine(loc);
5352
Olli Etuaho3fdec912016-08-18 15:08:06 +03005353 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005354 }
5355 return nullptr;
5356}
5357
Jamie Madillb98c3a82015-07-23 14:26:04 -04005358TIntermTyped *TParseContext::addAssign(TOperator op,
5359 TIntermTyped *left,
5360 TIntermTyped *right,
5361 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005362{
Olli Etuahocce89652017-06-19 16:04:09 +03005363 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005364 TIntermTyped *node = createAssign(op, left, right, loc);
5365 if (node == nullptr)
5366 {
5367 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005368 return left;
5369 }
5370 return node;
5371}
5372
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005373TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5374 TIntermTyped *right,
5375 const TSourceLoc &loc)
5376{
Corentin Wallez0d959252016-07-12 17:26:32 -04005377 // WebGL2 section 5.26, the following results in an error:
5378 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005379 if (mShaderSpec == SH_WEBGL2_SPEC &&
5380 (left->isArray() || left->getBasicType() == EbtVoid ||
5381 left->getType().isStructureContainingArrays() || right->isArray() ||
5382 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005383 {
5384 error(loc,
5385 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5386 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005387 }
5388
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005389 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5390 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5391 commaNode->getTypePointer()->setQualifier(resultQualifier);
5392 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005393}
5394
Olli Etuaho49300862015-02-20 14:54:49 +02005395TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5396{
5397 switch (op)
5398 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005399 case EOpContinue:
5400 if (mLoopNestingLevel <= 0)
5401 {
5402 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005403 }
5404 break;
5405 case EOpBreak:
5406 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5407 {
5408 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005409 }
5410 break;
5411 case EOpReturn:
5412 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5413 {
5414 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005415 }
5416 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005417 case EOpKill:
5418 if (mShaderType != GL_FRAGMENT_SHADER)
5419 {
5420 error(loc, "discard supported in fragment shaders only", "discard");
5421 }
5422 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005423 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005424 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005425 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005426 }
Olli Etuahocce89652017-06-19 16:04:09 +03005427 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005428}
5429
Jamie Madillb98c3a82015-07-23 14:26:04 -04005430TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005431 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005432 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005433{
Olli Etuahocce89652017-06-19 16:04:09 +03005434 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005435 {
Olli Etuahocce89652017-06-19 16:04:09 +03005436 ASSERT(op == EOpReturn);
5437 mFunctionReturnsValue = true;
5438 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5439 {
5440 error(loc, "void function cannot return a value", "return");
5441 }
5442 else if (*mCurrentFunctionType != expression->getType())
5443 {
5444 error(loc, "function return is not matching type:", "return");
5445 }
Olli Etuaho49300862015-02-20 14:54:49 +02005446 }
Olli Etuahocce89652017-06-19 16:04:09 +03005447 TIntermBranch *node = new TIntermBranch(op, expression);
5448 node->setLine(loc);
5449 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005450}
5451
Martin Radev84aa2dc2017-09-11 15:51:02 +03005452void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5453{
5454 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5455 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5456 bool isTextureGather = (name == "textureGather");
5457 bool isTextureGatherOffset = (name == "textureGatherOffset");
5458 if (isTextureGather || isTextureGatherOffset)
5459 {
5460 TIntermNode *componentNode = nullptr;
5461 TIntermSequence *arguments = functionCall->getSequence();
5462 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5463 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5464 ASSERT(sampler != nullptr);
5465 switch (sampler->getBasicType())
5466 {
5467 case EbtSampler2D:
5468 case EbtISampler2D:
5469 case EbtUSampler2D:
5470 case EbtSampler2DArray:
5471 case EbtISampler2DArray:
5472 case EbtUSampler2DArray:
5473 if ((isTextureGather && arguments->size() == 3u) ||
5474 (isTextureGatherOffset && arguments->size() == 4u))
5475 {
5476 componentNode = arguments->back();
5477 }
5478 break;
5479 case EbtSamplerCube:
5480 case EbtISamplerCube:
5481 case EbtUSamplerCube:
5482 ASSERT(!isTextureGatherOffset);
5483 if (arguments->size() == 3u)
5484 {
5485 componentNode = arguments->back();
5486 }
5487 break;
5488 case EbtSampler2DShadow:
5489 case EbtSampler2DArrayShadow:
5490 case EbtSamplerCubeShadow:
5491 break;
5492 default:
5493 UNREACHABLE();
5494 break;
5495 }
5496 if (componentNode)
5497 {
5498 const TIntermConstantUnion *componentConstantUnion =
5499 componentNode->getAsConstantUnion();
5500 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5501 {
5502 error(functionCall->getLine(), "Texture component must be a constant expression",
5503 name.c_str());
5504 }
5505 else
5506 {
5507 int component = componentConstantUnion->getIConst(0);
5508 if (component < 0 || component > 3)
5509 {
5510 error(functionCall->getLine(), "Component must be in the range [0;3]",
5511 name.c_str());
5512 }
5513 }
5514 }
5515 }
5516}
5517
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005518void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5519{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005520 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005521 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005522 TIntermNode *offset = nullptr;
5523 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005524 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005525 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5526 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5527 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005528 {
5529 offset = arguments->back();
5530 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005531 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005532 {
5533 // A bias parameter might follow the offset parameter.
5534 ASSERT(arguments->size() >= 3);
5535 offset = (*arguments)[2];
5536 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005537 else if (name == "textureGatherOffset")
5538 {
5539 ASSERT(arguments->size() >= 3u);
5540 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5541 ASSERT(sampler != nullptr);
5542 switch (sampler->getBasicType())
5543 {
5544 case EbtSampler2D:
5545 case EbtISampler2D:
5546 case EbtUSampler2D:
5547 case EbtSampler2DArray:
5548 case EbtISampler2DArray:
5549 case EbtUSampler2DArray:
5550 offset = (*arguments)[2];
5551 break;
5552 case EbtSampler2DShadow:
5553 case EbtSampler2DArrayShadow:
5554 offset = (*arguments)[3];
5555 break;
5556 default:
5557 UNREACHABLE();
5558 break;
5559 }
5560 useTextureGatherOffsetConstraints = true;
5561 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005562 if (offset != nullptr)
5563 {
5564 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5565 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5566 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005567 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005568 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005569 }
5570 else
5571 {
5572 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5573 size_t size = offsetConstantUnion->getType().getObjectSize();
5574 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005575 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5576 : mMinProgramTexelOffset;
5577 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5578 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005579 for (size_t i = 0u; i < size; ++i)
5580 {
5581 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005582 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005583 {
5584 std::stringstream tokenStream;
5585 tokenStream << offsetValue;
5586 std::string token = tokenStream.str();
5587 error(offset->getLine(), "Texture offset value out of valid range",
5588 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005589 }
5590 }
5591 }
5592 }
5593}
5594
Martin Radev2cc85b32016-08-05 16:22:53 +03005595// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5596void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5597{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005598 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005599 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5600
5601 if (name.compare(0, 5, "image") == 0)
5602 {
5603 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005604 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005605
Olli Etuaho485eefd2017-02-14 17:40:06 +00005606 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005607
5608 if (name.compare(5, 5, "Store") == 0)
5609 {
5610 if (memoryQualifier.readonly)
5611 {
5612 error(imageNode->getLine(),
5613 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005614 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005615 }
5616 }
5617 else if (name.compare(5, 4, "Load") == 0)
5618 {
5619 if (memoryQualifier.writeonly)
5620 {
5621 error(imageNode->getLine(),
5622 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005623 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005624 }
5625 }
5626 }
5627}
5628
5629// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5630void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5631 const TFunction *functionDefinition,
5632 const TIntermAggregate *functionCall)
5633{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005634 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005635
5636 const TIntermSequence &arguments = *functionCall->getSequence();
5637
5638 ASSERT(functionDefinition->getParamCount() == arguments.size());
5639
5640 for (size_t i = 0; i < arguments.size(); ++i)
5641 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005642 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5643 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005644 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5645 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5646
5647 if (IsImage(functionArgumentType.getBasicType()))
5648 {
5649 const TMemoryQualifier &functionArgumentMemoryQualifier =
5650 functionArgumentType.getMemoryQualifier();
5651 const TMemoryQualifier &functionParameterMemoryQualifier =
5652 functionParameterType.getMemoryQualifier();
5653 if (functionArgumentMemoryQualifier.readonly &&
5654 !functionParameterMemoryQualifier.readonly)
5655 {
5656 error(functionCall->getLine(),
5657 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005658 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005659 }
5660
5661 if (functionArgumentMemoryQualifier.writeonly &&
5662 !functionParameterMemoryQualifier.writeonly)
5663 {
5664 error(functionCall->getLine(),
5665 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005666 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005667 }
Martin Radev049edfa2016-11-11 14:35:37 +02005668
5669 if (functionArgumentMemoryQualifier.coherent &&
5670 !functionParameterMemoryQualifier.coherent)
5671 {
5672 error(functionCall->getLine(),
5673 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005674 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005675 }
5676
5677 if (functionArgumentMemoryQualifier.volatileQualifier &&
5678 !functionParameterMemoryQualifier.volatileQualifier)
5679 {
5680 error(functionCall->getLine(),
5681 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005682 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005683 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005684 }
5685 }
5686}
5687
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005688TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005689{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005690 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005691}
5692
5693TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005694 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005695 TIntermNode *thisNode,
5696 const TSourceLoc &loc)
5697{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005698 if (thisNode != nullptr)
5699 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005700 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005701 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005702
5703 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005704 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005705 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005706 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005707 }
5708 else
5709 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005710 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005711 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5712 }
5713}
5714
5715TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5716 TIntermSequence *arguments,
5717 TIntermNode *thisNode,
5718 const TSourceLoc &loc)
5719{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005720 TIntermTyped *typedThis = thisNode->getAsTyped();
5721 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5722 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5723 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5724 // So accessing fnCall->getName() below is safe.
5725 if (fnCall->getName() != "length")
5726 {
5727 error(loc, "invalid method", fnCall->getName().c_str());
5728 }
5729 else if (!arguments->empty())
5730 {
5731 error(loc, "method takes no parameters", "length");
5732 }
5733 else if (typedThis == nullptr || !typedThis->isArray())
5734 {
5735 error(loc, "length can only be called on arrays", "length");
5736 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005737 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5738 mGeometryShaderInputPrimitiveType == EptUndefined)
5739 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08005740 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005741 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5742 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005743 else
5744 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005745 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5746 node->setLine(loc);
5747 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005748 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005749 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005750}
5751
5752TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5753 TIntermSequence *arguments,
5754 const TSourceLoc &loc)
5755{
5756 // First find by unmangled name to check whether the function name has been
5757 // hidden by a variable name or struct typename.
5758 // If a function is found, check for one with a matching argument list.
5759 bool builtIn;
5760 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5761 if (symbol != nullptr && !symbol->isFunction())
5762 {
5763 error(loc, "function name expected", fnCall->getName().c_str());
5764 }
5765 else
5766 {
5767 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5768 mShaderVersion, &builtIn);
5769 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005770 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005771 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5772 }
5773 else
5774 {
5775 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005776 //
5777 // A declared function.
5778 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005779 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005780 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005781 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005782 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005783 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005784 if (builtIn && op != EOpNull)
5785 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005786 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005787 if (fnCandidate->getParamCount() == 1)
5788 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005789 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005790 TIntermNode *unaryParamNode = arguments->front();
5791 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005792 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005793 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005794 }
5795 else
5796 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005797 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005798 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005799 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005800
5801 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005802 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305803
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005804 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305805 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005806 // See if we can constant fold a built-in. Note that this may be possible
5807 // even if it is not const-qualified.
5808 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305809 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005810 else
5811 {
5812 return callNode;
5813 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005814 }
5815 }
5816 else
5817 {
5818 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005819 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005820
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005821 // If builtIn == false, the function is user defined - could be an overloaded
5822 // built-in as well.
5823 // if builtIn == true, it's a builtIn function with no op associated with it.
5824 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005825 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005826 {
Olli Etuahofe486322017-03-21 09:30:54 +00005827 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005828 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005829 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005830 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005831 }
5832 else
5833 {
Olli Etuahofe486322017-03-21 09:30:54 +00005834 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005835 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005836 }
5837
Jiajia Qinbc585152017-06-23 15:42:17 +08005838 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005839
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005840 callNode->setLine(loc);
5841
5842 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005843 }
5844 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005845 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005846
5847 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005848 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005849}
5850
Jamie Madillb98c3a82015-07-23 14:26:04 -04005851TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005852 TIntermTyped *trueExpression,
5853 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005854 const TSourceLoc &loc)
5855{
Olli Etuaho56229f12017-07-10 14:16:33 +03005856 if (!checkIsScalarBool(loc, cond))
5857 {
5858 return falseExpression;
5859 }
Olli Etuaho52901742015-04-15 13:42:45 +03005860
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005861 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005862 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005863 std::stringstream reasonStream;
5864 reasonStream << "mismatching ternary operator operand types '"
5865 << trueExpression->getCompleteString() << " and '"
5866 << falseExpression->getCompleteString() << "'";
5867 std::string reason = reasonStream.str();
5868 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005869 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005870 }
Olli Etuahode318b22016-10-25 16:18:25 +01005871 if (IsOpaqueType(trueExpression->getBasicType()))
5872 {
5873 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005874 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005875 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5876 // Note that structs containing opaque types don't need to be checked as structs are
5877 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005878 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005879 return falseExpression;
5880 }
5881
Jiajia Qinbc585152017-06-23 15:42:17 +08005882 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5883 falseExpression->getMemoryQualifier().writeonly)
5884 {
5885 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5886 return falseExpression;
5887 }
5888
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005889 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005890 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005891 // ESSL 3.00.6 section 5.7:
5892 // Ternary operator support is optional for arrays. No certainty that it works across all
5893 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5894 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005895 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005896 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005897 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005898 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005899 }
Olli Etuaho94050052017-05-08 14:17:44 +03005900 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5901 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005902 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005903 return falseExpression;
5904 }
5905
Corentin Wallez0d959252016-07-12 17:26:32 -04005906 // WebGL2 section 5.26, the following results in an error:
5907 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005908 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005909 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005910 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005911 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005912 }
5913
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005914 // Note that the node resulting from here can be a constant union without being qualified as
5915 // constant.
5916 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5917 node->setLine(loc);
5918
5919 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005920}
Olli Etuaho49300862015-02-20 14:54:49 +02005921
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005922//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005923// Parse an array of strings using yyparse.
5924//
5925// Returns 0 for success.
5926//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005927int PaParseStrings(size_t count,
5928 const char *const string[],
5929 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305930 TParseContext *context)
5931{
Yunchao He4f285442017-04-21 12:15:49 +08005932 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005933 return 1;
5934
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005935 if (glslang_initialize(context))
5936 return 1;
5937
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005938 int error = glslang_scan(count, string, length, context);
5939 if (!error)
5940 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005941
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005942 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005943
alokp@chromium.org6b495712012-06-29 00:06:58 +00005944 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005945}
Jamie Madill45bcc782016-11-07 13:58:48 -05005946
5947} // namespace sh