blob: 1c6160faba8077dbc02e4a37eb07cd562b11c36f [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),
210 mGeometryShaderInputArraySize(0)
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 Shaoe8ef2bc2017-08-29 13:38:57 +0800479 case EvqFlatIn:
480 case EvqSmoothIn:
481 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400482 message = "can't modify an input";
483 break;
484 case EvqUniform:
485 message = "can't modify a uniform";
486 break;
487 case EvqVaryingIn:
488 message = "can't modify a varying";
489 break;
490 case EvqFragCoord:
491 message = "can't modify gl_FragCoord";
492 break;
493 case EvqFrontFacing:
494 message = "can't modify gl_FrontFacing";
495 break;
496 case EvqPointCoord:
497 message = "can't modify gl_PointCoord";
498 break;
Martin Radevb0883602016-08-04 17:48:58 +0300499 case EvqNumWorkGroups:
500 message = "can't modify gl_NumWorkGroups";
501 break;
502 case EvqWorkGroupSize:
503 message = "can't modify gl_WorkGroupSize";
504 break;
505 case EvqWorkGroupID:
506 message = "can't modify gl_WorkGroupID";
507 break;
508 case EvqLocalInvocationID:
509 message = "can't modify gl_LocalInvocationID";
510 break;
511 case EvqGlobalInvocationID:
512 message = "can't modify gl_GlobalInvocationID";
513 break;
514 case EvqLocalInvocationIndex:
515 message = "can't modify gl_LocalInvocationIndex";
516 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300517 case EvqViewIDOVR:
518 message = "can't modify gl_ViewID_OVR";
519 break;
Martin Radev802abe02016-08-04 17:48:32 +0300520 case EvqComputeIn:
521 message = "can't modify work group size variable";
522 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800523 case EvqPerVertexIn:
524 message = "can't modify any member in gl_in";
525 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800526 case EvqPrimitiveIDIn:
527 message = "can't modify gl_PrimitiveIDIn";
528 break;
529 case EvqInvocationID:
530 message = "can't modify gl_InvocationID";
531 break;
532 case EvqPrimitiveID:
533 if (mShaderType == GL_FRAGMENT_SHADER)
534 {
535 message = "can't modify gl_PrimitiveID in a fragment shader";
536 }
537 break;
538 case EvqLayer:
539 if (mShaderType == GL_FRAGMENT_SHADER)
540 {
541 message = "can't modify gl_Layer in a fragment shader";
542 }
543 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400544 default:
545 //
546 // Type that can't be written to?
547 //
548 if (node->getBasicType() == EbtVoid)
549 {
550 message = "can't modify void";
551 }
jchen10cc2a10e2017-05-03 14:05:12 +0800552 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400553 {
jchen10cc2a10e2017-05-03 14:05:12 +0800554 message = "can't modify a variable with type ";
555 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300556 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800557 else if (node->getMemoryQualifier().readonly)
558 {
559 message = "can't modify a readonly variable";
560 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000561 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562
jchen10cc2a10e2017-05-03 14:05:12 +0800563 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530564 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000565 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566
Olli Etuaho8a176262016-08-16 14:23:01 +0300567 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000568 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000569
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000570 //
571 // Everything else is okay, no error.
572 //
jchen10cc2a10e2017-05-03 14:05:12 +0800573 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300574 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000576 //
577 // If we get here, we have an error and a message.
578 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530579 if (symNode)
580 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000581 const char *symbol = symNode->getSymbol().c_str();
582 std::stringstream reasonStream;
583 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
584 std::string reason = reasonStream.str();
585 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000586 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530587 else
588 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000589 std::stringstream reasonStream;
590 reasonStream << "l-value required (" << message << ")";
591 std::string reason = reasonStream.str();
592 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000593 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594
Olli Etuaho8a176262016-08-16 14:23:01 +0300595 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596}
597
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000598// Both test, and if necessary spit out an error, to see if the node is really
599// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300600void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000601{
Olli Etuaho383b7912016-08-05 11:22:59 +0300602 if (node->getQualifier() != EvqConst)
603 {
604 error(node->getLine(), "constant expression required", "");
605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606}
607
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608// Both test, and if necessary spit out an error, to see if the node is really
609// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300610void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000611{
Olli Etuaho383b7912016-08-05 11:22:59 +0300612 if (!node->isScalarInt())
613 {
614 error(node->getLine(), "integer expression required", token);
615 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000616}
617
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000618// Both test, and if necessary spit out an error, to see if we are currently
619// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800620bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000621{
Olli Etuaho856c4972016-08-08 11:38:39 +0300622 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300623 {
624 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800625 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300626 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800627 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628}
629
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300630// ESSL 3.00.5 sections 3.8 and 3.9.
631// If it starts "gl_" or contains two consecutive underscores, it's reserved.
632// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300633bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530635 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300636 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530637 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300638 error(line, reservedErrMsg, "gl_");
639 return false;
640 }
641 if (sh::IsWebGLBasedSpec(mShaderSpec))
642 {
643 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530644 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300645 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300646 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000647 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300648 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530649 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300650 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300651 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000652 }
653 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300654 if (identifier.find("__") != TString::npos)
655 {
656 error(line,
657 "identifiers containing two consecutive underscores (__) are reserved as "
658 "possible future keywords",
659 identifier.c_str());
660 return false;
661 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300662 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663}
664
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300665// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300666bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800667 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300668 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800670 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530671 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200672 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300673 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000674 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200675
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300676 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530677 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300678 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200679 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300680 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200681 {
jchen10cc2a10e2017-05-03 14:05:12 +0800682 std::string reason("cannot convert a variable with type ");
683 reason += getBasicString(argTyped->getBasicType());
684 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300685 return false;
686 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800687 else if (argTyped->getMemoryQualifier().writeonly)
688 {
689 error(line, "cannot convert a variable with writeonly", "constructor");
690 return false;
691 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200692 if (argTyped->getBasicType() == EbtVoid)
693 {
694 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300695 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200696 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000697 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000698
Olli Etuaho856c4972016-08-08 11:38:39 +0300699 if (type.isArray())
700 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300701 // The size of an unsized constructor should already have been determined.
702 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300703 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300704 {
705 error(line, "array constructor needs one argument per array element", "constructor");
706 return false;
707 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300708 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
709 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800710 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300711 {
712 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500713 if (argType.isArray())
714 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300715 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500716 return false;
717 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300718 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300719 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000720 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300721 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300722 }
723 }
724 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300725 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300726 {
727 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300728 if (fields.size() != arguments->size())
729 {
730 error(line,
731 "Number of constructor parameters does not match the number of structure fields",
732 "constructor");
733 return false;
734 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300735
736 for (size_t i = 0; i < fields.size(); i++)
737 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800738 if (i >= arguments->size() ||
739 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300740 {
741 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000742 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300743 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300744 }
745 }
746 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300747 else
748 {
749 // We're constructing a scalar, vector, or matrix.
750
751 // Note: It's okay to have too many components available, but not okay to have unused
752 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
753 // there is an extra argument, so 'overFull' will become true.
754
755 size_t size = 0;
756 bool full = false;
757 bool overFull = false;
758 bool matrixArg = false;
759 for (TIntermNode *arg : *arguments)
760 {
761 const TIntermTyped *argTyped = arg->getAsTyped();
762 ASSERT(argTyped != nullptr);
763
Olli Etuaho487b63a2017-05-23 15:55:09 +0300764 if (argTyped->getBasicType() == EbtStruct)
765 {
766 error(line, "a struct cannot be used as a constructor argument for this type",
767 "constructor");
768 return false;
769 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300770 if (argTyped->getType().isArray())
771 {
772 error(line, "constructing from a non-dereferenced array", "constructor");
773 return false;
774 }
775 if (argTyped->getType().isMatrix())
776 {
777 matrixArg = true;
778 }
779
780 size += argTyped->getType().getObjectSize();
781 if (full)
782 {
783 overFull = true;
784 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300785 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300786 {
787 full = true;
788 }
789 }
790
791 if (type.isMatrix() && matrixArg)
792 {
793 if (arguments->size() != 1)
794 {
795 error(line, "constructing matrix from matrix can only take one argument",
796 "constructor");
797 return false;
798 }
799 }
800 else
801 {
802 if (size != 1 && size < type.getObjectSize())
803 {
804 error(line, "not enough data provided for construction", "constructor");
805 return false;
806 }
807 if (overFull)
808 {
809 error(line, "too many arguments", "constructor");
810 return false;
811 }
812 }
813 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300814
Olli Etuaho8a176262016-08-16 14:23:01 +0300815 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816}
817
Jamie Madillb98c3a82015-07-23 14:26:04 -0400818// This function checks to see if a void variable has been declared and raise an error message for
819// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820//
821// returns true in case of an error
822//
Olli Etuaho856c4972016-08-08 11:38:39 +0300823bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400824 const TString &identifier,
825 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300827 if (type == EbtVoid)
828 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000829 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300830 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300831 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832
Olli Etuaho8a176262016-08-16 14:23:01 +0300833 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834}
835
Jamie Madillb98c3a82015-07-23 14:26:04 -0400836// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300837// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300838bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300840 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530841 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000842 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300843 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530844 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300845 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846}
847
Jamie Madillb98c3a82015-07-23 14:26:04 -0400848// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300849// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300850void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851{
Martin Radev4a9cd802016-09-01 16:51:51 +0300852 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530853 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000854 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530855 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
jchen10cc2a10e2017-05-03 14:05:12 +0800858bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
859 const TTypeSpecifierNonArray &pType,
860 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530862 if (pType.type == EbtStruct)
863 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300864 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530865 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000866 std::stringstream reasonStream;
867 reasonStream << reason << " (structure contains a sampler)";
868 std::string reasonStr = reasonStream.str();
869 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300870 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000871 }
jchen10cc2a10e2017-05-03 14:05:12 +0800872 // only samplers need to be checked from structs, since other opaque types can't be struct
873 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300874 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530875 }
jchen10cc2a10e2017-05-03 14:05:12 +0800876 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530877 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000878 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300879 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000880 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881
Olli Etuaho8a176262016-08-16 14:23:01 +0300882 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883}
884
Olli Etuaho856c4972016-08-08 11:38:39 +0300885void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
886 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400887{
888 if (pType.layoutQualifier.location != -1)
889 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400890 error(line, "location must only be specified for a single input or output variable",
891 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400892 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400893}
894
Olli Etuaho856c4972016-08-08 11:38:39 +0300895void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
896 const TLayoutQualifier &layoutQualifier)
897{
898 if (layoutQualifier.location != -1)
899 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000900 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
901 if (mShaderVersion >= 310)
902 {
903 errorMsg =
904 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
905 }
906 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300907 }
908}
909
Martin Radev2cc85b32016-08-05 16:22:53 +0300910void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
911 TQualifier qualifier,
912 const TType &type)
913{
Martin Radev2cc85b32016-08-05 16:22:53 +0300914 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800915 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530916 {
jchen10cc2a10e2017-05-03 14:05:12 +0800917 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919}
920
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300922unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530924 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000925
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200926 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
927 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
928 // fold as array size.
929 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000930 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000931 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300932 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000933 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934
Olli Etuaho856c4972016-08-08 11:38:39 +0300935 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400936
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000937 if (constant->getBasicType() == EbtUInt)
938 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300939 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000940 }
941 else
942 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300943 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000944
Olli Etuaho856c4972016-08-08 11:38:39 +0300945 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000946 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400947 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300948 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000949 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400950
Olli Etuaho856c4972016-08-08 11:38:39 +0300951 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400952 }
953
Olli Etuaho856c4972016-08-08 11:38:39 +0300954 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400955 {
956 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300957 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400958 }
959
960 // The size of arrays is restricted here to prevent issues further down the
961 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
962 // 4096 registers so this should be reasonable even for aggressively optimizable code.
963 const unsigned int sizeLimit = 65536;
964
Olli Etuaho856c4972016-08-08 11:38:39 +0300965 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400966 {
967 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300968 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000969 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300970
971 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300975bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
976 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977{
Olli Etuaho8a176262016-08-16 14:23:01 +0300978 if ((elementQualifier.qualifier == EvqAttribute) ||
979 (elementQualifier.qualifier == EvqVertexIn) ||
980 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300981 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400982 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300983 TType(elementQualifier).getQualifierString());
984 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000985 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986
Olli Etuaho8a176262016-08-16 14:23:01 +0300987 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988}
989
Olli Etuaho8a176262016-08-16 14:23:01 +0300990// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +0300991bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
992 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993{
Olli Etuaho8a176262016-08-16 14:23:01 +0300994 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400995 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300996 error(line, "cannot declare arrays of arrays",
997 TType(elementType).getCompleteString().c_str());
998 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000999 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001000 return true;
1001}
1002
1003// Check if this qualified element type can be formed into an array. This is only called when array
1004// brackets are associated with an identifier in a declaration, like this:
1005// float a[2];
1006// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1007// are associated with the type, like this:
1008// float[2] a;
1009bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1010 const TPublicType &elementType)
1011{
1012 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1013 {
1014 return false;
1015 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001016 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1017 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1018 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001019 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001020 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001021 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001022 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001023 TType(elementType).getCompleteString().c_str());
1024 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001025 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001026 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027}
1028
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001030void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1031 const TString &identifier,
1032 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033{
Olli Etuaho3739d232015-04-08 12:23:44 +03001034 ASSERT(type != nullptr);
1035 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001036 {
1037 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +03001038 type->qualifier = EvqTemporary;
1039
1040 // Generate informative error messages for ESSL1.
1041 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001042 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001043 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301044 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001045 "structures containing arrays may not be declared constant since they cannot be "
1046 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301047 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001048 }
1049 else
1050 {
1051 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1052 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001053 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001054 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001055 if (type->isUnsizedArray())
1056 {
1057 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001058 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059}
1060
Olli Etuaho2935c582015-04-08 14:32:06 +03001061// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062// and update the symbol table.
1063//
Olli Etuaho2935c582015-04-08 14:32:06 +03001064// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001066bool TParseContext::declareVariable(const TSourceLoc &line,
1067 const TString &identifier,
1068 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001069 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070{
Olli Etuaho2935c582015-04-08 14:32:06 +03001071 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072
Olli Etuaho43364892017-02-13 16:00:12 +00001073 checkBindingIsValid(line, type);
1074
Olli Etuaho856c4972016-08-08 11:38:39 +03001075 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076
Olli Etuaho2935c582015-04-08 14:32:06 +03001077 // gl_LastFragData may be redeclared with a new precision qualifier
1078 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1079 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001080 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1081 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001082 if (type.isArrayOfArrays())
1083 {
1084 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1085 identifier.c_str());
1086 return false;
1087 }
1088 else if (static_cast<int>(type.getOutermostArraySize()) ==
1089 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001090 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001091 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001092 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001093 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001094 }
1095 }
1096 else
1097 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001098 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1099 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001100 return false;
1101 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
Olli Etuaho8a176262016-08-16 14:23:01 +03001104 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001105 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106
Olli Etuaho0f684632017-07-13 12:42:15 +03001107 (*variable) = symbolTable.declareVariable(&identifier, type);
1108 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001109 {
1110 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001111 return false;
1112 }
1113
Olli Etuaho8a176262016-08-16 14:23:01 +03001114 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001115 return false;
1116
1117 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118}
1119
Martin Radev70866b82016-07-22 15:27:42 +03001120void TParseContext::checkIsParameterQualifierValid(
1121 const TSourceLoc &line,
1122 const TTypeQualifierBuilder &typeQualifierBuilder,
1123 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301124{
Olli Etuahocce89652017-06-19 16:04:09 +03001125 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001126 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001127
1128 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301129 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001130 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1131 }
1132
1133 if (!IsImage(type->getBasicType()))
1134 {
Olli Etuaho43364892017-02-13 16:00:12 +00001135 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001136 }
1137 else
1138 {
1139 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141
Martin Radev70866b82016-07-22 15:27:42 +03001142 type->setQualifier(typeQualifier.qualifier);
1143
1144 if (typeQualifier.precision != EbpUndefined)
1145 {
1146 type->setPrecision(typeQualifier.precision);
1147 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001148}
1149
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001150bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001151{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001152 ASSERT(extension != TExtension::UNDEFINED);
Jamie Madillb98c3a82015-07-23 14:26:04 -04001153 const TExtensionBehavior &extBehavior = extensionBehavior();
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001154 TExtensionBehavior::const_iterator iter = extBehavior.find(extension);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301155 if (iter == extBehavior.end())
1156 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001157 error(line, "extension is not supported", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001158 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001159 }
zmo@google.comf5450912011-09-09 01:37:19 +00001160 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301161 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1162 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001163 error(line, "extension is disabled", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001164 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001165 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301166 if (iter->second == EBhWarn)
1167 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001168 warning(line, "extension is being used", GetExtensionNameString(extension));
Olli Etuaho8a176262016-08-16 14:23:01 +03001169 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001170 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001171
Olli Etuaho8a176262016-08-16 14:23:01 +03001172 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001173}
1174
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001175// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1176// compile-time or link-time errors are the same whether or not the declaration is empty".
1177// This function implements all the checks that are done on qualifiers regardless of if the
1178// declaration is empty.
1179void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1180 const sh::TLayoutQualifier &layoutQualifier,
1181 const TSourceLoc &location)
1182{
1183 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1184 {
1185 error(location, "Shared memory declarations cannot have layout specified", "layout");
1186 }
1187
1188 if (layoutQualifier.matrixPacking != EmpUnspecified)
1189 {
1190 error(location, "layout qualifier only valid for interface blocks",
1191 getMatrixPackingString(layoutQualifier.matrixPacking));
1192 return;
1193 }
1194
1195 if (layoutQualifier.blockStorage != EbsUnspecified)
1196 {
1197 error(location, "layout qualifier only valid for interface blocks",
1198 getBlockStorageString(layoutQualifier.blockStorage));
1199 return;
1200 }
1201
1202 if (qualifier == EvqFragmentOut)
1203 {
1204 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1205 {
1206 error(location, "invalid layout qualifier combination", "yuv");
1207 return;
1208 }
1209 }
1210 else
1211 {
1212 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1213 }
1214
Olli Etuaho95468d12017-05-04 11:14:34 +03001215 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1216 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001217 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1218 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001219 {
1220 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1221 }
1222
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001223 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1224 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1225 {
1226 canHaveLocation = true;
1227 // We're not checking whether the uniform location is in range here since that depends on
1228 // the type of the variable.
1229 // The type can only be fully determined for non-empty declarations.
1230 }
1231 if (!canHaveLocation)
1232 {
1233 checkLocationIsNotSpecified(location, layoutQualifier);
1234 }
1235}
1236
jchen104cdac9e2017-05-08 11:01:20 +08001237void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1238 const TSourceLoc &location)
1239{
1240 if (publicType.precision != EbpHigh)
1241 {
1242 error(location, "Can only be highp", "atomic counter");
1243 }
1244 // dEQP enforces compile error if location is specified. See uniform_location.test.
1245 if (publicType.layoutQualifier.location != -1)
1246 {
1247 error(location, "location must not be set for atomic_uint", "layout");
1248 }
1249 if (publicType.layoutQualifier.binding == -1)
1250 {
1251 error(location, "no binding specified", "atomic counter");
1252 }
1253}
1254
Martin Radevb8b01222016-11-20 23:25:53 +02001255void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1256 const TSourceLoc &location)
1257{
1258 if (publicType.isUnsizedArray())
1259 {
1260 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1261 // error. It is assumed that this applies to empty declarations as well.
1262 error(location, "empty array declaration needs to specify a size", "");
1263 }
Martin Radevb8b01222016-11-20 23:25:53 +02001264}
1265
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001266// These checks are done for all declarations that are non-empty. They're done for non-empty
1267// declarations starting a declarator list, and declarators that follow an empty declaration.
1268void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1269 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001270{
Olli Etuahofa33d582015-04-09 14:33:12 +03001271 switch (publicType.qualifier)
1272 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001273 case EvqVaryingIn:
1274 case EvqVaryingOut:
1275 case EvqAttribute:
1276 case EvqVertexIn:
1277 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001278 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001279 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001280 {
1281 error(identifierLocation, "cannot be used with a structure",
1282 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001283 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001284 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001285 break;
1286 case EvqBuffer:
1287 if (publicType.getBasicType() != EbtInterfaceBlock)
1288 {
1289 error(identifierLocation,
1290 "cannot declare buffer variables at global scope(outside a block)",
1291 getQualifierString(publicType.qualifier));
1292 return;
1293 }
1294 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001295 default:
1296 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001297 }
jchen10cc2a10e2017-05-03 14:05:12 +08001298 std::string reason(getBasicString(publicType.getBasicType()));
1299 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001300 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001301 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001302 {
1303 return;
1304 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001305
Andrei Volykhina5527072017-03-22 16:46:30 +03001306 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1307 publicType.qualifier != EvqConst) &&
1308 publicType.getBasicType() == EbtYuvCscStandardEXT)
1309 {
1310 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1311 getQualifierString(publicType.qualifier));
1312 return;
1313 }
1314
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001315 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1316 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001317 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1318 // But invalid shaders may still reach here with an unsized array declaration.
1319 if (!publicType.isUnsizedArray())
1320 {
1321 TType type(publicType);
1322 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1323 publicType.layoutQualifier);
1324 }
1325 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001326
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001327 // check for layout qualifier issues
1328 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001329
Martin Radev2cc85b32016-08-05 16:22:53 +03001330 if (IsImage(publicType.getBasicType()))
1331 {
1332
1333 switch (layoutQualifier.imageInternalFormat)
1334 {
1335 case EiifRGBA32F:
1336 case EiifRGBA16F:
1337 case EiifR32F:
1338 case EiifRGBA8:
1339 case EiifRGBA8_SNORM:
1340 if (!IsFloatImage(publicType.getBasicType()))
1341 {
1342 error(identifierLocation,
1343 "internal image format requires a floating image type",
1344 getBasicString(publicType.getBasicType()));
1345 return;
1346 }
1347 break;
1348 case EiifRGBA32I:
1349 case EiifRGBA16I:
1350 case EiifRGBA8I:
1351 case EiifR32I:
1352 if (!IsIntegerImage(publicType.getBasicType()))
1353 {
1354 error(identifierLocation,
1355 "internal image format requires an integer image type",
1356 getBasicString(publicType.getBasicType()));
1357 return;
1358 }
1359 break;
1360 case EiifRGBA32UI:
1361 case EiifRGBA16UI:
1362 case EiifRGBA8UI:
1363 case EiifR32UI:
1364 if (!IsUnsignedImage(publicType.getBasicType()))
1365 {
1366 error(identifierLocation,
1367 "internal image format requires an unsigned image type",
1368 getBasicString(publicType.getBasicType()));
1369 return;
1370 }
1371 break;
1372 case EiifUnspecified:
1373 error(identifierLocation, "layout qualifier", "No image internal format specified");
1374 return;
1375 default:
1376 error(identifierLocation, "layout qualifier", "unrecognized token");
1377 return;
1378 }
1379
1380 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1381 switch (layoutQualifier.imageInternalFormat)
1382 {
1383 case EiifR32F:
1384 case EiifR32I:
1385 case EiifR32UI:
1386 break;
1387 default:
1388 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1389 {
1390 error(identifierLocation, "layout qualifier",
1391 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1392 "image variables must be qualified readonly and/or writeonly");
1393 return;
1394 }
1395 break;
1396 }
1397 }
1398 else
1399 {
Olli Etuaho43364892017-02-13 16:00:12 +00001400 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001401 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1402 }
jchen104cdac9e2017-05-08 11:01:20 +08001403
1404 if (IsAtomicCounter(publicType.getBasicType()))
1405 {
1406 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1407 }
1408 else
1409 {
1410 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1411 }
Olli Etuaho43364892017-02-13 16:00:12 +00001412}
Martin Radev2cc85b32016-08-05 16:22:53 +03001413
Olli Etuaho43364892017-02-13 16:00:12 +00001414void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1415{
1416 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001417 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1418 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1419 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1420 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1421 // when it comes to which shaders are accepted by the compiler.
1422 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001423 if (IsImage(type.getBasicType()))
1424 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001425 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1426 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001427 }
1428 else if (IsSampler(type.getBasicType()))
1429 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001430 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1431 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001432 }
jchen104cdac9e2017-05-08 11:01:20 +08001433 else if (IsAtomicCounter(type.getBasicType()))
1434 {
1435 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1436 }
Olli Etuaho43364892017-02-13 16:00:12 +00001437 else
1438 {
1439 ASSERT(!IsOpaqueType(type.getBasicType()));
1440 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001441 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001442}
1443
Olli Etuaho856c4972016-08-08 11:38:39 +03001444void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1445 const TString &layoutQualifierName,
1446 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001447{
1448
1449 if (mShaderVersion < versionRequired)
1450 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001451 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001452 }
1453}
1454
Olli Etuaho856c4972016-08-08 11:38:39 +03001455bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1456 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001457{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001458 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001459 for (size_t i = 0u; i < localSize.size(); ++i)
1460 {
1461 if (localSize[i] != -1)
1462 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001463 error(location,
1464 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1465 "global layout declaration",
1466 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001467 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001468 }
1469 }
1470
Olli Etuaho8a176262016-08-16 14:23:01 +03001471 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001472}
1473
Olli Etuaho43364892017-02-13 16:00:12 +00001474void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001475 TLayoutImageInternalFormat internalFormat)
1476{
1477 if (internalFormat != EiifUnspecified)
1478 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001479 error(location, "invalid layout qualifier: only valid when used with images",
1480 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001481 }
Olli Etuaho43364892017-02-13 16:00:12 +00001482}
1483
1484void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1485{
1486 if (binding != -1)
1487 {
1488 error(location,
1489 "invalid layout qualifier: only valid when used with opaque types or blocks",
1490 "binding");
1491 }
1492}
1493
jchen104cdac9e2017-05-08 11:01:20 +08001494void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1495{
1496 if (offset != -1)
1497 {
1498 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1499 "offset");
1500 }
1501}
1502
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001503void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1504 int binding,
1505 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001506{
1507 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001508 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001509 {
1510 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1511 }
1512}
1513
1514void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1515 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001516 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001517{
1518 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001519 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001520 {
1521 error(location, "sampler binding greater than maximum texture units", "binding");
1522 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001523}
1524
Jiajia Qinbc585152017-06-23 15:42:17 +08001525void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1526 const TQualifier &qualifier,
1527 int binding,
1528 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001529{
1530 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001531 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001532 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001533 if (binding + size > mMaxUniformBufferBindings)
1534 {
1535 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1536 "binding");
1537 }
1538 }
1539 else if (qualifier == EvqBuffer)
1540 {
1541 if (binding + size > mMaxShaderStorageBufferBindings)
1542 {
1543 error(location,
1544 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1545 "binding");
1546 }
jchen10af713a22017-04-19 09:10:56 +08001547 }
1548}
jchen104cdac9e2017-05-08 11:01:20 +08001549void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1550{
1551 if (binding >= mMaxAtomicCounterBindings)
1552 {
1553 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1554 "binding");
1555 }
1556}
jchen10af713a22017-04-19 09:10:56 +08001557
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001558void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1559 int objectLocationCount,
1560 const TLayoutQualifier &layoutQualifier)
1561{
1562 int loc = layoutQualifier.location;
1563 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1564 {
1565 error(location, "Uniform location out of range", "location");
1566 }
1567}
1568
Andrei Volykhina5527072017-03-22 16:46:30 +03001569void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1570{
1571 if (yuv != false)
1572 {
1573 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1574 }
1575}
1576
Jiajia Qinbc585152017-06-23 15:42:17 +08001577void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1578 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001579{
1580 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1581 {
1582 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001583 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1584 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1585 qual == EvqInOut || qual == EvqConstReadOnly))
1586 {
1587 if (argument->getMemoryQualifier().writeonly)
1588 {
1589 error(argument->getLine(),
1590 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1591 fnCall->getFunctionSymbolInfo()->getName().c_str());
1592 return;
1593 }
1594 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001595 if (qual == EvqOut || qual == EvqInOut)
1596 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001597 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001598 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001599 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001600 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001601 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001602 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001603 }
1604 }
1605 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001606}
1607
Martin Radev70866b82016-07-22 15:27:42 +03001608void TParseContext::checkInvariantVariableQualifier(bool invariant,
1609 const TQualifier qualifier,
1610 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001611{
Martin Radev70866b82016-07-22 15:27:42 +03001612 if (!invariant)
1613 return;
1614
1615 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001616 {
Martin Radev70866b82016-07-22 15:27:42 +03001617 // input variables in the fragment shader can be also qualified as invariant
1618 if (!sh::CanBeInvariantESSL1(qualifier))
1619 {
1620 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1621 }
1622 }
1623 else
1624 {
1625 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1626 {
1627 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1628 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001629 }
1630}
1631
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001632bool TParseContext::supportsExtension(TExtension extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001633{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001634 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001635 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1636 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001637}
1638
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001639bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001640{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001641 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001642}
1643
Jamie Madillb98c3a82015-07-23 14:26:04 -04001644void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1645 const char *extName,
1646 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001647{
1648 pp::SourceLocation srcLoc;
1649 srcLoc.file = loc.first_file;
1650 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001651 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001652}
1653
Jamie Madillb98c3a82015-07-23 14:26:04 -04001654void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1655 const char *name,
1656 const char *value,
1657 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001658{
1659 pp::SourceLocation srcLoc;
1660 srcLoc.file = loc.first_file;
1661 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001662 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001663}
1664
Martin Radev4c4c8e72016-08-04 12:25:34 +03001665sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001666{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001667 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001668 for (size_t i = 0u; i < result.size(); ++i)
1669 {
1670 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1671 {
1672 result[i] = 1;
1673 }
1674 else
1675 {
1676 result[i] = mComputeShaderLocalSize[i];
1677 }
1678 }
1679 return result;
1680}
1681
Olli Etuaho56229f12017-07-10 14:16:33 +03001682TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1683 const TSourceLoc &line)
1684{
1685 TIntermConstantUnion *node = new TIntermConstantUnion(
1686 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1687 node->setLine(line);
1688 return node;
1689}
1690
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691/////////////////////////////////////////////////////////////////////////////////
1692//
1693// Non-Errors.
1694//
1695/////////////////////////////////////////////////////////////////////////////////
1696
Jamie Madill5c097022014-08-20 16:38:32 -04001697const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1698 const TString *name,
1699 const TSymbol *symbol)
1700{
Jamie Madill5c097022014-08-20 16:38:32 -04001701 if (!symbol)
1702 {
1703 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001704 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001705 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001706
1707 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001708 {
1709 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001710 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001711 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001712
1713 const TVariable *variable = static_cast<const TVariable *>(symbol);
1714
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001715 if (variable->getExtension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001716 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001717 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001718 }
1719
Olli Etuaho0f684632017-07-13 12:42:15 +03001720 // Reject shaders using both gl_FragData and gl_FragColor
1721 TQualifier qualifier = variable->getType().getQualifier();
1722 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001723 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001724 mUsesFragData = true;
1725 }
1726 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1727 {
1728 mUsesFragColor = true;
1729 }
1730 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1731 {
1732 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001733 }
1734
Olli Etuaho0f684632017-07-13 12:42:15 +03001735 // This validation is not quite correct - it's only an error to write to
1736 // both FragData and FragColor. For simplicity, and because users shouldn't
1737 // be rewarded for reading from undefined varaibles, return an error
1738 // if they are both referenced, rather than assigned.
1739 if (mUsesFragData && mUsesFragColor)
1740 {
1741 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1742 if (mUsesSecondaryOutputs)
1743 {
1744 errorMessage =
1745 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1746 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1747 }
1748 error(location, errorMessage, name->c_str());
1749 }
1750
1751 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1752 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1753 qualifier == EvqWorkGroupSize)
1754 {
1755 error(location,
1756 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1757 "gl_WorkGroupSize");
1758 }
Jamie Madill5c097022014-08-20 16:38:32 -04001759 return variable;
1760}
1761
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001762TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1763 const TString *name,
1764 const TSymbol *symbol)
1765{
1766 const TVariable *variable = getNamedVariable(location, name, symbol);
1767
Olli Etuaho0f684632017-07-13 12:42:15 +03001768 if (!variable)
1769 {
1770 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1771 node->setLine(location);
1772 return node;
1773 }
1774
Olli Etuaho56229f12017-07-10 14:16:33 +03001775 TIntermTyped *node = nullptr;
1776
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001777 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001778 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001779 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001780 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001781 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001782 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1783 mComputeShaderLocalSizeDeclared)
1784 {
1785 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1786 // needs to be added to the AST as a constant and not as a symbol.
1787 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1788 TConstantUnion *constArray = new TConstantUnion[3];
1789 for (size_t i = 0; i < 3; ++i)
1790 {
1791 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1792 }
1793
1794 ASSERT(variable->getType().getBasicType() == EbtUInt);
1795 ASSERT(variable->getType().getObjectSize() == 3);
1796
1797 TType type(variable->getType());
1798 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001799 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001800 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08001801 // TODO(jiawei.shao@intel.com): set array sizes for user-defined geometry shader inputs.
1802 else if (variable->getType().getQualifier() == EvqPerVertexIn)
1803 {
1804 TType type(variable->getType());
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001805 type.setArraySize(0, mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001806 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), type);
1807 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001808 else
1809 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001810 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001811 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001812 ASSERT(node != nullptr);
1813 node->setLine(location);
1814 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001815}
1816
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001817// Initializers show up in several places in the grammar. Have one set of
1818// code to handle them here.
1819//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001820// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001821bool TParseContext::executeInitializer(const TSourceLoc &line,
1822 const TString &identifier,
1823 const TPublicType &pType,
1824 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001825 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001826{
Olli Etuaho13389b62016-10-16 11:48:18 +01001827 ASSERT(initNode != nullptr);
1828 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001829 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830
Olli Etuaho2935c582015-04-08 14:32:06 +03001831 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001832 if (type.isUnsizedArray())
1833 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001834 // In case initializer is not an array or type has more dimensions than initializer, this
1835 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1836 // actually is an array or not. Having a non-array initializer for an unsized array will
1837 // result in an error later, so we don't generate an error message here.
1838 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001839 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001840 if (!declareVariable(line, identifier, type, &variable))
1841 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001842 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001843 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001844
Olli Etuahob0c645e2015-05-12 14:25:36 +03001845 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001846 if (symbolTable.atGlobalLevel() &&
1847 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001848 {
1849 // Error message does not completely match behavior with ESSL 1.00, but
1850 // we want to steer developers towards only using constant expressions.
1851 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001852 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001853 }
1854 if (globalInitWarning)
1855 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001856 warning(
1857 line,
1858 "global variable initializers should be constant expressions "
1859 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1860 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001861 }
1862
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001863 //
1864 // identifier must be of type constant, a global, or a temporary
1865 //
1866 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301867 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1868 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001869 error(line, " cannot initialize this type of qualifier ",
1870 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001871 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001872 }
1873 //
1874 // test for and propagate constant
1875 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001876
Arun Patole7e7e68d2015-05-22 12:02:25 +05301877 if (qualifier == EvqConst)
1878 {
1879 if (qualifier != initializer->getType().getQualifier())
1880 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001881 std::stringstream reasonStream;
1882 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1883 << "'";
1884 std::string reason = reasonStream.str();
1885 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001886 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001887 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001888 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301889 if (type != initializer->getType())
1890 {
1891 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001892 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001893 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001894 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001895 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001896
1897 // Save the constant folded value to the variable if possible. For example array
1898 // initializers are not folded, since that way copying the array literal to multiple places
1899 // in the shader is avoided.
1900 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1901 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301902 if (initializer->getAsConstantUnion())
1903 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001904 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001905 ASSERT(*initNode == nullptr);
1906 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301907 }
1908 else if (initializer->getAsSymbolNode())
1909 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001910 const TSymbol *symbol =
1911 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1912 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001914 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001915 if (constArray)
1916 {
1917 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001918 ASSERT(*initNode == nullptr);
1919 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001920 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001921 }
1922 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001923
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001924 TIntermSymbol *intermSymbol =
1925 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1926 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001927 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1928 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001929 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001930 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001931 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001932 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933
Olli Etuaho914b79a2017-06-19 16:03:19 +03001934 return true;
1935}
1936
1937TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1938 const TString &identifier,
1939 TIntermTyped *initializer,
1940 const TSourceLoc &loc)
1941{
1942 checkIsScalarBool(loc, pType);
1943 TIntermBinary *initNode = nullptr;
1944 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1945 {
1946 // The initializer is valid. The init condition needs to have a node - either the
1947 // initializer node, or a constant node in case the initialized variable is const and won't
1948 // be recorded in the AST.
1949 if (initNode == nullptr)
1950 {
1951 return initializer;
1952 }
1953 else
1954 {
1955 TIntermDeclaration *declaration = new TIntermDeclaration();
1956 declaration->appendDeclarator(initNode);
1957 return declaration;
1958 }
1959 }
1960 return nullptr;
1961}
1962
1963TIntermNode *TParseContext::addLoop(TLoopType type,
1964 TIntermNode *init,
1965 TIntermNode *cond,
1966 TIntermTyped *expr,
1967 TIntermNode *body,
1968 const TSourceLoc &line)
1969{
1970 TIntermNode *node = nullptr;
1971 TIntermTyped *typedCond = nullptr;
1972 if (cond)
1973 {
1974 typedCond = cond->getAsTyped();
1975 }
1976 if (cond == nullptr || typedCond)
1977 {
Olli Etuahocce89652017-06-19 16:04:09 +03001978 if (type == ELoopDoWhile)
1979 {
1980 checkIsScalarBool(line, typedCond);
1981 }
1982 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1983 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1984 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1985 !typedCond->isVector()));
1986
Olli Etuaho3ec75682017-07-05 17:02:55 +03001987 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001988 node->setLine(line);
1989 return node;
1990 }
1991
Olli Etuahocce89652017-06-19 16:04:09 +03001992 ASSERT(type != ELoopDoWhile);
1993
Olli Etuaho914b79a2017-06-19 16:03:19 +03001994 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1995 ASSERT(declaration);
1996 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1997 ASSERT(declarator->getLeft()->getAsSymbolNode());
1998
1999 // The condition is a declaration. In the AST representation we don't support declarations as
2000 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2001 // the loop.
2002 TIntermBlock *block = new TIntermBlock();
2003
2004 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2005 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2006 block->appendStatement(declareCondition);
2007
2008 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2009 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002010 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002011 block->appendStatement(loop);
2012 loop->setLine(line);
2013 block->setLine(line);
2014 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015}
2016
Olli Etuahocce89652017-06-19 16:04:09 +03002017TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2018 TIntermNodePair code,
2019 const TSourceLoc &loc)
2020{
Olli Etuaho56229f12017-07-10 14:16:33 +03002021 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002022
2023 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002024 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002025 {
2026 if (cond->getAsConstantUnion()->getBConst(0) == true)
2027 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002028 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002029 }
2030 else
2031 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002032 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002033 }
2034 }
2035
Olli Etuaho3ec75682017-07-05 17:02:55 +03002036 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002037 node->setLine(loc);
2038
2039 return node;
2040}
2041
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002042void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2043{
2044 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2045 typeSpecifier->getBasicType());
2046
2047 if (mShaderVersion < 300 && typeSpecifier->array)
2048 {
2049 error(typeSpecifier->getLine(), "not supported", "first-class array");
2050 typeSpecifier->clearArrayness();
2051 }
2052}
2053
Martin Radev70866b82016-07-22 15:27:42 +03002054TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302055 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002056{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002057 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002058
Martin Radev70866b82016-07-22 15:27:42 +03002059 TPublicType returnType = typeSpecifier;
2060 returnType.qualifier = typeQualifier.qualifier;
2061 returnType.invariant = typeQualifier.invariant;
2062 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002063 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002064 returnType.precision = typeSpecifier.precision;
2065
2066 if (typeQualifier.precision != EbpUndefined)
2067 {
2068 returnType.precision = typeQualifier.precision;
2069 }
2070
Martin Radev4a9cd802016-09-01 16:51:51 +03002071 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2072 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002073
Martin Radev4a9cd802016-09-01 16:51:51 +03002074 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2075 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002076
Martin Radev4a9cd802016-09-01 16:51:51 +03002077 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002078
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002079 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002080 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002081 if (typeSpecifier.array)
2082 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002083 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002084 returnType.clearArrayness();
2085 }
2086
Martin Radev70866b82016-07-22 15:27:42 +03002087 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002088 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002089 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002090 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002091 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002092 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002093
Martin Radev70866b82016-07-22 15:27:42 +03002094 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002095 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002096 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002097 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002098 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002099 }
2100 }
2101 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002102 {
Martin Radev70866b82016-07-22 15:27:42 +03002103 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002104 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002105 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002106 }
Martin Radev70866b82016-07-22 15:27:42 +03002107 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2108 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002109 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002110 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2111 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002112 }
Martin Radev70866b82016-07-22 15:27:42 +03002113 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002114 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002115 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002116 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002117 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002118 }
2119
2120 return returnType;
2121}
2122
Olli Etuaho856c4972016-08-08 11:38:39 +03002123void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2124 const TPublicType &type,
2125 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002126{
2127 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002128 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002129 {
2130 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002131 }
2132
2133 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2134 switch (qualifier)
2135 {
2136 case EvqVertexIn:
2137 // ESSL 3.00 section 4.3.4
2138 if (type.array)
2139 {
2140 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002141 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002142 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002143 return;
2144 case EvqFragmentOut:
2145 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002146 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002147 {
2148 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002149 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002150 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002151 return;
2152 default:
2153 break;
2154 }
2155
2156 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2157 // restrictions.
2158 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002159 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2160 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002161 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2162 {
2163 error(qualifierLocation, "must use 'flat' interpolation here",
2164 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002165 }
2166
Martin Radev4a9cd802016-09-01 16:51:51 +03002167 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002168 {
2169 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2170 // These restrictions are only implied by the ESSL 3.00 spec, but
2171 // the ESSL 3.10 spec lists these restrictions explicitly.
2172 if (type.array)
2173 {
2174 error(qualifierLocation, "cannot be an array of structures",
2175 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002176 }
2177 if (type.isStructureContainingArrays())
2178 {
2179 error(qualifierLocation, "cannot be a structure containing an array",
2180 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002181 }
2182 if (type.isStructureContainingType(EbtStruct))
2183 {
2184 error(qualifierLocation, "cannot be a structure containing a structure",
2185 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002186 }
2187 if (type.isStructureContainingType(EbtBool))
2188 {
2189 error(qualifierLocation, "cannot be a structure containing a bool",
2190 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002191 }
2192 }
2193}
2194
Martin Radev2cc85b32016-08-05 16:22:53 +03002195void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2196{
2197 if (qualifier.getType() == QtStorage)
2198 {
2199 const TStorageQualifierWrapper &storageQualifier =
2200 static_cast<const TStorageQualifierWrapper &>(qualifier);
2201 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2202 !symbolTable.atGlobalLevel())
2203 {
2204 error(storageQualifier.getLine(),
2205 "Local variables can only use the const storage qualifier.",
2206 storageQualifier.getQualifierString().c_str());
2207 }
2208 }
2209}
2210
Olli Etuaho43364892017-02-13 16:00:12 +00002211void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002212 const TSourceLoc &location)
2213{
Jiajia Qinbc585152017-06-23 15:42:17 +08002214 const std::string reason(
2215 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2216 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002217 if (memoryQualifier.readonly)
2218 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002219 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002220 }
2221 if (memoryQualifier.writeonly)
2222 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002223 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002224 }
Martin Radev049edfa2016-11-11 14:35:37 +02002225 if (memoryQualifier.coherent)
2226 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002227 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002228 }
2229 if (memoryQualifier.restrictQualifier)
2230 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002231 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002232 }
2233 if (memoryQualifier.volatileQualifier)
2234 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002235 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002236 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002237}
2238
jchen104cdac9e2017-05-08 11:01:20 +08002239// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2240// intermediate tree.
2241void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2242 size_t size,
2243 bool forceAppend,
2244 const TSourceLoc &loc,
2245 TType &type)
2246{
2247 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2248 int offset;
2249 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2250 {
2251 offset = bindingState.appendSpan(size);
2252 }
2253 else
2254 {
2255 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2256 }
2257 if (offset == -1)
2258 {
2259 error(loc, "Offset overlapping", "atomic counter");
2260 return;
2261 }
2262 TLayoutQualifier qualifier = type.getLayoutQualifier();
2263 qualifier.offset = offset;
2264 type.setLayoutQualifier(qualifier);
2265}
2266
Olli Etuaho13389b62016-10-16 11:48:18 +01002267TIntermDeclaration *TParseContext::parseSingleDeclaration(
2268 TPublicType &publicType,
2269 const TSourceLoc &identifierOrTypeLocation,
2270 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002271{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002272 TType type(publicType);
2273 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2274 mDirectiveHandler.pragma().stdgl.invariantAll)
2275 {
2276 TQualifier qualifier = type.getQualifier();
2277
2278 // The directive handler has already taken care of rejecting invalid uses of this pragma
2279 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2280 // affected variable declarations:
2281 //
2282 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2283 // elsewhere, in TranslatorGLSL.)
2284 //
2285 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2286 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2287 // the way this is currently implemented we have to enable this compiler option before
2288 // parsing the shader and determining the shading language version it uses. If this were
2289 // implemented as a post-pass, the workaround could be more targeted.
2290 //
2291 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2292 // the specification, but there are desktop OpenGL drivers that expect that this is the
2293 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2294 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2295 {
2296 type.setInvariant(true);
2297 }
2298 }
2299
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002300 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2301 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002302
Olli Etuahobab4c082015-04-24 16:38:49 +03002303 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002304 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002305
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002306 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002307 if (emptyDeclaration)
2308 {
Martin Radevb8b01222016-11-20 23:25:53 +02002309 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002310 // In most cases we don't need to create a symbol node for an empty declaration.
2311 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2312 if (type.getBasicType() == EbtStruct)
2313 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002314 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002315 }
jchen104cdac9e2017-05-08 11:01:20 +08002316 else if (IsAtomicCounter(publicType.getBasicType()))
2317 {
2318 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2319 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002320 }
2321 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002322 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002323 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002324
Olli Etuaho856c4972016-08-08 11:38:39 +03002325 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002326
jchen104cdac9e2017-05-08 11:01:20 +08002327 if (IsAtomicCounter(publicType.getBasicType()))
2328 {
2329
2330 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2331 identifierOrTypeLocation, type);
2332 }
2333
Olli Etuaho2935c582015-04-08 14:32:06 +03002334 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002335 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002336
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002337 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002338 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002339 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002340 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002341 }
2342
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002343 TIntermDeclaration *declaration = new TIntermDeclaration();
2344 declaration->setLine(identifierOrTypeLocation);
2345 if (symbol)
2346 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002347 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002348 declaration->appendDeclarator(symbol);
2349 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002350 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002351}
2352
Olli Etuaho13389b62016-10-16 11:48:18 +01002353TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2354 const TSourceLoc &identifierLocation,
2355 const TString &identifier,
2356 const TSourceLoc &indexLocation,
2357 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002358{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002359 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002360
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002361 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2362 identifierLocation);
2363
2364 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002365
Olli Etuaho856c4972016-08-08 11:38:39 +03002366 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002367
Olli Etuaho8a176262016-08-16 14:23:01 +03002368 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002369
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002370 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002371
Olli Etuaho856c4972016-08-08 11:38:39 +03002372 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002373 // Make the type an array even if size check failed.
2374 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002375 arrayType.makeArray(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002376
jchen104cdac9e2017-05-08 11:01:20 +08002377 if (IsAtomicCounter(publicType.getBasicType()))
2378 {
2379 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2380 identifierLocation, arrayType);
2381 }
2382
Olli Etuaho2935c582015-04-08 14:32:06 +03002383 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002384 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002385
Olli Etuaho13389b62016-10-16 11:48:18 +01002386 TIntermDeclaration *declaration = new TIntermDeclaration();
2387 declaration->setLine(identifierLocation);
2388
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002389 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002390 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002391 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2392 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002393 declaration->appendDeclarator(symbol);
2394 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002395
Olli Etuaho13389b62016-10-16 11:48:18 +01002396 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002397}
2398
Olli Etuaho13389b62016-10-16 11:48:18 +01002399TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2400 const TSourceLoc &identifierLocation,
2401 const TString &identifier,
2402 const TSourceLoc &initLocation,
2403 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002404{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002405 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002406
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002407 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2408 identifierLocation);
2409
2410 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002411
Olli Etuaho13389b62016-10-16 11:48:18 +01002412 TIntermDeclaration *declaration = new TIntermDeclaration();
2413 declaration->setLine(identifierLocation);
2414
2415 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002416 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002417 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002418 if (initNode)
2419 {
2420 declaration->appendDeclarator(initNode);
2421 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002422 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002423 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002424}
2425
Olli Etuaho13389b62016-10-16 11:48:18 +01002426TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002427 TPublicType &publicType,
2428 const TSourceLoc &identifierLocation,
2429 const TString &identifier,
2430 const TSourceLoc &indexLocation,
2431 TIntermTyped *indexExpression,
2432 const TSourceLoc &initLocation,
2433 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002434{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002435 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002436
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002437 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2438 identifierLocation);
2439
2440 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002441
Olli Etuaho8a176262016-08-16 14:23:01 +03002442 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002443
2444 TPublicType arrayType(publicType);
2445
Olli Etuaho856c4972016-08-08 11:38:39 +03002446 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002447 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2448 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002449 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002450 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002451 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002452 }
2453 // Make the type an array even if size check failed.
2454 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2455 arrayType.setArraySize(size);
2456
Olli Etuaho13389b62016-10-16 11:48:18 +01002457 TIntermDeclaration *declaration = new TIntermDeclaration();
2458 declaration->setLine(identifierLocation);
2459
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002460 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002461 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002462 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002463 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002464 if (initNode)
2465 {
2466 declaration->appendDeclarator(initNode);
2467 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002468 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002469
2470 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002471}
2472
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002473TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002474 const TTypeQualifierBuilder &typeQualifierBuilder,
2475 const TSourceLoc &identifierLoc,
2476 const TString *identifier,
2477 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002478{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002479 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002480
Martin Radev70866b82016-07-22 15:27:42 +03002481 if (!typeQualifier.invariant)
2482 {
2483 error(identifierLoc, "Expected invariant", identifier->c_str());
2484 return nullptr;
2485 }
2486 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2487 {
2488 return nullptr;
2489 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002490 if (!symbol)
2491 {
2492 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002493 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002494 }
Martin Radev70866b82016-07-22 15:27:42 +03002495 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002496 {
Martin Radev70866b82016-07-22 15:27:42 +03002497 error(identifierLoc, "invariant declaration specifies qualifier",
2498 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002499 }
Martin Radev70866b82016-07-22 15:27:42 +03002500 if (typeQualifier.precision != EbpUndefined)
2501 {
2502 error(identifierLoc, "invariant declaration specifies precision",
2503 getPrecisionString(typeQualifier.precision));
2504 }
2505 if (!typeQualifier.layoutQualifier.isEmpty())
2506 {
2507 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2508 }
2509
2510 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002511 if (!variable)
2512 {
2513 return nullptr;
2514 }
Martin Radev70866b82016-07-22 15:27:42 +03002515 const TType &type = variable->getType();
2516
2517 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2518 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002519 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002520
2521 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2522
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002523 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2524 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002525
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002526 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002527}
2528
Olli Etuaho13389b62016-10-16 11:48:18 +01002529void TParseContext::parseDeclarator(TPublicType &publicType,
2530 const TSourceLoc &identifierLocation,
2531 const TString &identifier,
2532 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002533{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002534 // If the declaration starting this declarator list was empty (example: int,), some checks were
2535 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002536 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002537 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002538 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2539 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002540 }
2541
Olli Etuaho856c4972016-08-08 11:38:39 +03002542 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002543
Olli Etuaho856c4972016-08-08 11:38:39 +03002544 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002545
Olli Etuaho2935c582015-04-08 14:32:06 +03002546 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002547 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002548 if (IsAtomicCounter(publicType.getBasicType()))
2549 {
2550 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2551 identifierLocation, type);
2552 }
Olli Etuaho43364892017-02-13 16:00:12 +00002553 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002554
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002555 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002556 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002557 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2558 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002559 declarationOut->appendDeclarator(symbol);
2560 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002561}
2562
Olli Etuaho13389b62016-10-16 11:48:18 +01002563void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2564 const TSourceLoc &identifierLocation,
2565 const TString &identifier,
2566 const TSourceLoc &arrayLocation,
2567 TIntermTyped *indexExpression,
2568 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002569{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002570 // If the declaration starting this declarator list was empty (example: int,), some checks were
2571 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002572 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002573 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002574 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2575 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002576 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002577
Olli Etuaho856c4972016-08-08 11:38:39 +03002578 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002579
Olli Etuaho856c4972016-08-08 11:38:39 +03002580 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002581
Olli Etuaho8a176262016-08-16 14:23:01 +03002582 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002583 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002584 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002585 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002586 arrayType.makeArray(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002587
jchen104cdac9e2017-05-08 11:01:20 +08002588 if (IsAtomicCounter(publicType.getBasicType()))
2589 {
2590 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2591 true, identifierLocation, arrayType);
2592 }
2593
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002594 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002595 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002596
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002597 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002598 {
2599 TIntermSymbol *symbol =
2600 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2601 symbol->setLine(identifierLocation);
2602 declarationOut->appendDeclarator(symbol);
2603 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002604 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002605}
2606
Olli Etuaho13389b62016-10-16 11:48:18 +01002607void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2608 const TSourceLoc &identifierLocation,
2609 const TString &identifier,
2610 const TSourceLoc &initLocation,
2611 TIntermTyped *initializer,
2612 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002613{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002614 // If the declaration starting this declarator list was empty (example: int,), some checks were
2615 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002616 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002617 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002618 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2619 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002620 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002621
Olli Etuaho856c4972016-08-08 11:38:39 +03002622 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002623
Olli Etuaho13389b62016-10-16 11:48:18 +01002624 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002625 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002626 {
2627 //
2628 // build the intermediate representation
2629 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002630 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002631 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002632 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002633 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002634 }
2635}
2636
Olli Etuaho13389b62016-10-16 11:48:18 +01002637void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2638 const TSourceLoc &identifierLocation,
2639 const TString &identifier,
2640 const TSourceLoc &indexLocation,
2641 TIntermTyped *indexExpression,
2642 const TSourceLoc &initLocation,
2643 TIntermTyped *initializer,
2644 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002645{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002646 // If the declaration starting this declarator list was empty (example: int,), some checks were
2647 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002648 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002649 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002650 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2651 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002652 }
2653
Olli Etuaho856c4972016-08-08 11:38:39 +03002654 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002655
Olli Etuaho8a176262016-08-16 14:23:01 +03002656 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002657
2658 TPublicType arrayType(publicType);
2659
Olli Etuaho856c4972016-08-08 11:38:39 +03002660 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002661 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2662 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002663 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002664 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002665 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002666 }
2667 // Make the type an array even if size check failed.
2668 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2669 arrayType.setArraySize(size);
2670
2671 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002672 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002673 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002674 {
2675 if (initNode)
2676 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002677 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002678 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002679 }
2680}
2681
jchen104cdac9e2017-05-08 11:01:20 +08002682void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2683 const TSourceLoc &location)
2684{
2685 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2686 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2687 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2688 {
2689 error(location, "Requires both binding and offset", "layout");
2690 return;
2691 }
2692 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2693}
2694
Olli Etuahocce89652017-06-19 16:04:09 +03002695void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2696 const TPublicType &type,
2697 const TSourceLoc &loc)
2698{
2699 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2700 !getFragmentPrecisionHigh())
2701 {
2702 error(loc, "precision is not supported in fragment shader", "highp");
2703 }
2704
2705 if (!CanSetDefaultPrecisionOnType(type))
2706 {
2707 error(loc, "illegal type argument for default precision qualifier",
2708 getBasicString(type.getBasicType()));
2709 return;
2710 }
2711 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2712}
2713
Shaob5cc1192017-07-06 10:47:20 +08002714bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2715{
2716 switch (typeQualifier.layoutQualifier.primitiveType)
2717 {
2718 case EptLines:
2719 case EptLinesAdjacency:
2720 case EptTriangles:
2721 case EptTrianglesAdjacency:
2722 return typeQualifier.qualifier == EvqGeometryIn;
2723
2724 case EptLineStrip:
2725 case EptTriangleStrip:
2726 return typeQualifier.qualifier == EvqGeometryOut;
2727
2728 case EptPoints:
2729 return true;
2730
2731 default:
2732 UNREACHABLE();
2733 return false;
2734 }
2735}
2736
Jiawei Shaod8105a02017-08-08 09:54:36 +08002737void TParseContext::setGeometryShaderInputArraySizes()
2738{
2739 // TODO(jiawei.shao@intel.com): check former input array sizes match the input primitive
2740 // declaration.
2741 ASSERT(mGeometryShaderInputArraySize == 0);
2742 mGeometryShaderInputArraySize =
2743 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType);
2744}
2745
Shaob5cc1192017-07-06 10:47:20 +08002746bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2747{
2748 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2749
2750 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2751
2752 if (layoutQualifier.maxVertices != -1)
2753 {
2754 error(typeQualifier.line,
2755 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2756 return false;
2757 }
2758
2759 // Set mGeometryInputPrimitiveType if exists
2760 if (layoutQualifier.primitiveType != EptUndefined)
2761 {
2762 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2763 {
2764 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2765 return false;
2766 }
2767
2768 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2769 {
2770 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shaod8105a02017-08-08 09:54:36 +08002771 setGeometryShaderInputArraySizes();
Shaob5cc1192017-07-06 10:47:20 +08002772 }
2773 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2774 {
2775 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2776 "layout");
2777 return false;
2778 }
2779 }
2780
2781 // Set mGeometryInvocations if exists
2782 if (layoutQualifier.invocations > 0)
2783 {
2784 if (mGeometryShaderInvocations == 0)
2785 {
2786 mGeometryShaderInvocations = layoutQualifier.invocations;
2787 }
2788 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2789 {
2790 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2791 "layout");
2792 return false;
2793 }
2794 }
2795
2796 return true;
2797}
2798
2799bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2800{
2801 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2802
2803 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2804
2805 if (layoutQualifier.invocations > 0)
2806 {
2807 error(typeQualifier.line,
2808 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2809 return false;
2810 }
2811
2812 // Set mGeometryOutputPrimitiveType if exists
2813 if (layoutQualifier.primitiveType != EptUndefined)
2814 {
2815 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2816 {
2817 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2818 return false;
2819 }
2820
2821 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2822 {
2823 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2824 }
2825 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2826 {
2827 error(typeQualifier.line,
2828 "primitive doesn't match earlier output primitive declaration", "layout");
2829 return false;
2830 }
2831 }
2832
2833 // Set mGeometryMaxVertices if exists
2834 if (layoutQualifier.maxVertices > -1)
2835 {
2836 if (mGeometryShaderMaxVertices == -1)
2837 {
2838 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2839 }
2840 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2841 {
2842 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2843 "layout");
2844 return false;
2845 }
2846 }
2847
2848 return true;
2849}
2850
Martin Radev70866b82016-07-22 15:27:42 +03002851void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002852{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002853 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002854 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002855
Martin Radev70866b82016-07-22 15:27:42 +03002856 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2857 typeQualifier.line);
2858
Jamie Madillc2128ff2016-07-04 10:26:17 -04002859 // It should never be the case, but some strange parser errors can send us here.
2860 if (layoutQualifier.isEmpty())
2861 {
2862 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002863 return;
2864 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002865
Martin Radev802abe02016-08-04 17:48:32 +03002866 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002867 {
Olli Etuaho43364892017-02-13 16:00:12 +00002868 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002869 return;
2870 }
2871
Olli Etuaho43364892017-02-13 16:00:12 +00002872 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2873
2874 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002875
2876 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2877
Andrei Volykhina5527072017-03-22 16:46:30 +03002878 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2879
jchen104cdac9e2017-05-08 11:01:20 +08002880 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2881
Martin Radev802abe02016-08-04 17:48:32 +03002882 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002883 {
Martin Radev802abe02016-08-04 17:48:32 +03002884 if (mComputeShaderLocalSizeDeclared &&
2885 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2886 {
2887 error(typeQualifier.line, "Work group size does not match the previous declaration",
2888 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002889 return;
2890 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002891
Martin Radev802abe02016-08-04 17:48:32 +03002892 if (mShaderVersion < 310)
2893 {
2894 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002895 return;
2896 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002897
Martin Radev4c4c8e72016-08-04 12:25:34 +03002898 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002899 {
2900 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002901 return;
2902 }
2903
2904 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2905 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2906
2907 const TConstantUnion *maxComputeWorkGroupSizeData =
2908 maxComputeWorkGroupSize->getConstPointer();
2909
2910 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2911 {
2912 if (layoutQualifier.localSize[i] != -1)
2913 {
2914 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2915 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2916 if (mComputeShaderLocalSize[i] < 1 ||
2917 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2918 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002919 std::stringstream reasonStream;
2920 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2921 << maxComputeWorkGroupSizeValue;
2922 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002923
Olli Etuaho4de340a2016-12-16 09:32:03 +00002924 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002925 return;
2926 }
2927 }
2928 }
2929
2930 mComputeShaderLocalSizeDeclared = true;
2931 }
Shaob5cc1192017-07-06 10:47:20 +08002932 else if (typeQualifier.qualifier == EvqGeometryIn)
2933 {
2934 if (mShaderVersion < 310)
2935 {
2936 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2937 return;
2938 }
2939
2940 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2941 {
2942 return;
2943 }
2944 }
2945 else if (typeQualifier.qualifier == EvqGeometryOut)
2946 {
2947 if (mShaderVersion < 310)
2948 {
2949 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2950 "layout");
2951 return;
2952 }
2953
2954 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2955 {
2956 return;
2957 }
2958 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03002959 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
2960 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002961 {
2962 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2963 // specification.
2964 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2965 {
2966 error(typeQualifier.line, "Number of views does not match the previous declaration",
2967 "layout");
2968 return;
2969 }
2970
2971 if (layoutQualifier.numViews == -1)
2972 {
2973 error(typeQualifier.line, "No num_views specified", "layout");
2974 return;
2975 }
2976
2977 if (layoutQualifier.numViews > mMaxNumViews)
2978 {
2979 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2980 "layout");
2981 return;
2982 }
2983
2984 mNumViews = layoutQualifier.numViews;
2985 }
Martin Radev802abe02016-08-04 17:48:32 +03002986 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002987 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002988 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002989 {
Martin Radev802abe02016-08-04 17:48:32 +03002990 return;
2991 }
2992
Jiajia Qinbc585152017-06-23 15:42:17 +08002993 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03002994 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002995 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00002996 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002997 return;
2998 }
2999
3000 if (mShaderVersion < 300)
3001 {
3002 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3003 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003004 return;
3005 }
3006
Olli Etuaho09b04a22016-12-15 13:30:26 +00003007 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003008
3009 if (layoutQualifier.matrixPacking != EmpUnspecified)
3010 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003011 if (typeQualifier.qualifier == EvqUniform)
3012 {
3013 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3014 }
3015 else if (typeQualifier.qualifier == EvqBuffer)
3016 {
3017 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3018 }
Martin Radev802abe02016-08-04 17:48:32 +03003019 }
3020
3021 if (layoutQualifier.blockStorage != EbsUnspecified)
3022 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003023 if (typeQualifier.qualifier == EvqUniform)
3024 {
3025 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3026 }
3027 else if (typeQualifier.qualifier == EvqBuffer)
3028 {
3029 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3030 }
Martin Radev802abe02016-08-04 17:48:32 +03003031 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003032 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003033}
3034
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003035TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3036 const TFunction &function,
3037 const TSourceLoc &location,
3038 bool insertParametersToSymbolTable)
3039{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003040 checkIsNotReserved(location, function.getName());
3041
Olli Etuahofe486322017-03-21 09:30:54 +00003042 TIntermFunctionPrototype *prototype =
3043 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003044 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3045 // point to the data that already exists in the symbol table.
3046 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3047 prototype->setLine(location);
3048
3049 for (size_t i = 0; i < function.getParamCount(); i++)
3050 {
3051 const TConstParameter &param = function.getParam(i);
3052
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003053 TIntermSymbol *symbol = nullptr;
3054
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003055 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3056 // be used for unused args).
3057 if (param.name != nullptr)
3058 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003059 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003060 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003061 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003062 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3063 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003064 {
3065 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3066 variable->getType());
3067 }
3068 else
3069 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003070 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003071 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003072 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003073 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003074 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003075 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003076 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3077 // symbol.
3078 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003079 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003080 symbol->setLine(location);
3081 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003082 }
3083 return prototype;
3084}
3085
Olli Etuaho16c745a2017-01-16 17:02:27 +00003086TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3087 const TFunction &parsedFunction,
3088 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003089{
Olli Etuaho476197f2016-10-11 13:59:08 +01003090 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3091 // first declaration. Either way the instance in the symbol table is used to track whether the
3092 // function is declared multiple times.
3093 TFunction *function = static_cast<TFunction *>(
3094 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3095 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003096 {
3097 // ESSL 1.00.17 section 4.2.7.
3098 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3099 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003100 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003101 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003102
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003103 TIntermFunctionPrototype *prototype =
3104 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003105
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003106 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003107
3108 if (!symbolTable.atGlobalLevel())
3109 {
3110 // ESSL 3.00.4 section 4.2.4.
3111 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003112 }
3113
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003114 return prototype;
3115}
3116
Olli Etuaho336b1472016-10-05 16:37:55 +01003117TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003118 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003119 TIntermBlock *functionBody,
3120 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003121{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003122 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003123 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3124 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003125 error(location, "function does not return a value:",
3126 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003127 }
3128
Olli Etuahof51fdd22016-10-03 10:03:40 +01003129 if (functionBody == nullptr)
3130 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003131 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003132 functionBody->setLine(location);
3133 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003134 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003135 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003136 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003137
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003138 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003139 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003140}
3141
Olli Etuaho476197f2016-10-11 13:59:08 +01003142void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3143 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003144 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003145{
Olli Etuaho476197f2016-10-11 13:59:08 +01003146 ASSERT(function);
3147 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003148 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003149 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003150
3151 if (builtIn)
3152 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003153 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003154 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003155 else
Jamie Madill185fb402015-06-12 15:48:48 -04003156 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003157 TFunction *prevDec = static_cast<TFunction *>(
3158 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3159
3160 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3161 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3162 // occurance.
3163 if (*function != prevDec)
3164 {
3165 // Swap the parameters of the previous declaration to the parameters of the function
3166 // definition (parameter names may differ).
3167 prevDec->swapParameters(**function);
3168
3169 // The function definition will share the same symbol as any previous declaration.
3170 *function = prevDec;
3171 }
3172
3173 if ((*function)->isDefined())
3174 {
3175 error(location, "function already has a body", (*function)->getName().c_str());
3176 }
3177
3178 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003179 }
Jamie Madill185fb402015-06-12 15:48:48 -04003180
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003181 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003182 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003183 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003184
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003185 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003186 setLoopNestingLevel(0);
3187}
3188
Jamie Madillb98c3a82015-07-23 14:26:04 -04003189TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003190{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003191 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003192 // We don't know at this point whether this is a function definition or a prototype.
3193 // The definition production code will check for redefinitions.
3194 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003195 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003196 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3197 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003198 //
3199 TFunction *prevDec =
3200 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303201
Martin Radevda6254b2016-12-14 17:00:36 +02003202 if (getShaderVersion() >= 300 &&
3203 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3204 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303205 {
Martin Radevda6254b2016-12-14 17:00:36 +02003206 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303207 // Therefore overloading or redefining builtin functions is an error.
3208 error(location, "Name of a built-in function cannot be redeclared as function",
3209 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303210 }
3211 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003212 {
3213 if (prevDec->getReturnType() != function->getReturnType())
3214 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003215 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003216 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003217 }
3218 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3219 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003220 if (prevDec->getParam(i).type->getQualifier() !=
3221 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003222 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003223 error(location,
3224 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003225 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003226 }
3227 }
3228 }
3229
3230 //
3231 // Check for previously declared variables using the same name.
3232 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003233 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003234 if (prevSym)
3235 {
3236 if (!prevSym->isFunction())
3237 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003238 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003239 }
3240 }
3241 else
3242 {
3243 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003244 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003245 }
3246
3247 // We're at the inner scope level of the function's arguments and body statement.
3248 // Add the function prototype to the surrounding scope instead.
3249 symbolTable.getOuterLevel()->insert(function);
3250
Olli Etuaho78d13742017-01-18 13:06:10 +00003251 // Raise error message if main function takes any parameters or return anything other than void
3252 if (function->getName() == "main")
3253 {
3254 if (function->getParamCount() > 0)
3255 {
3256 error(location, "function cannot take any parameter(s)", "main");
3257 }
3258 if (function->getReturnType().getBasicType() != EbtVoid)
3259 {
3260 error(location, "main function cannot return a value",
3261 function->getReturnType().getBasicString());
3262 }
3263 }
3264
Jamie Madill185fb402015-06-12 15:48:48 -04003265 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003266 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3267 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003268 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3269 //
3270 return function;
3271}
3272
Olli Etuaho9de84a52016-06-14 17:36:01 +03003273TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3274 const TString *name,
3275 const TSourceLoc &location)
3276{
3277 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3278 {
3279 error(location, "no qualifiers allowed for function return",
3280 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003281 }
3282 if (!type.layoutQualifier.isEmpty())
3283 {
3284 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003285 }
jchen10cc2a10e2017-05-03 14:05:12 +08003286 // make sure an opaque type is not involved as well...
3287 std::string reason(getBasicString(type.getBasicType()));
3288 reason += "s can't be function return values";
3289 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003290 if (mShaderVersion < 300)
3291 {
3292 // Array return values are forbidden, but there's also no valid syntax for declaring array
3293 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003294 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003295
3296 if (type.isStructureContainingArrays())
3297 {
3298 // ESSL 1.00.17 section 6.1 Function Definitions
3299 error(location, "structures containing arrays can't be function return values",
3300 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003301 }
3302 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003303
3304 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003305 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003306}
3307
Olli Etuahocce89652017-06-19 16:04:09 +03003308TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3309{
Olli Etuahocce89652017-06-19 16:04:09 +03003310 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003311 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003312}
3313
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003314TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003315{
Olli Etuahocce89652017-06-19 16:04:09 +03003316 if (mShaderVersion < 300 && publicType.array)
3317 {
3318 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3319 "[]");
3320 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003321 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003322 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003323 error(publicType.getLine(), "constructor can't be a structure definition",
3324 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003325 }
3326
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003327 TType *type = new TType(publicType);
3328 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003329 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003330 error(publicType.getLine(), "cannot construct this type",
3331 getBasicString(publicType.getBasicType()));
3332 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003333 }
3334
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003335 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003336}
3337
Olli Etuahocce89652017-06-19 16:04:09 +03003338TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3339 const TString *name,
3340 const TSourceLoc &nameLoc)
3341{
3342 if (publicType.getBasicType() == EbtVoid)
3343 {
3344 error(nameLoc, "illegal use of type 'void'", name->c_str());
3345 }
3346 checkIsNotReserved(nameLoc, *name);
3347 TType *type = new TType(publicType);
3348 TParameter param = {name, type};
3349 return param;
3350}
3351
3352TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3353 const TSourceLoc &identifierLoc,
3354 TIntermTyped *arraySize,
3355 const TSourceLoc &arrayLoc,
3356 TPublicType *type)
3357{
Olli Etuahoe0803872017-08-23 15:30:23 +03003358 checkArrayElementIsNotArray(arrayLoc, *type);
Olli Etuahocce89652017-06-19 16:04:09 +03003359 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3360 type->setArraySize(size);
3361 return parseParameterDeclarator(*type, identifier, identifierLoc);
3362}
3363
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003364bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3365 TType type,
3366 const TSourceLoc &line)
3367{
3368 if (arguments->empty())
3369 {
3370 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3371 return false;
3372 }
3373 for (TIntermNode *arg : *arguments)
3374 {
3375 TIntermTyped *element = arg->getAsTyped();
3376 ASSERT(element);
3377 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3378 if (dimensionalityFromElement > type.getArraySizes().size())
3379 {
3380 error(line, "constructing from a non-dereferenced array", "constructor");
3381 return false;
3382 }
3383 else if (dimensionalityFromElement < type.getArraySizes().size())
3384 {
3385 if (dimensionalityFromElement == 1u)
3386 {
3387 error(line, "implicitly sized array of arrays constructor argument is not an array",
3388 "constructor");
3389 }
3390 else
3391 {
3392 error(line,
3393 "implicitly sized array of arrays constructor argument dimensionality is too "
3394 "low",
3395 "constructor");
3396 }
3397 return false;
3398 }
3399 }
3400 return true;
3401}
3402
Jamie Madillb98c3a82015-07-23 14:26:04 -04003403// This function is used to test for the correctness of the parameters passed to various constructor
3404// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003405//
Olli Etuaho856c4972016-08-08 11:38:39 +03003406// 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 +00003407//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003408TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003409 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303410 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003411{
Olli Etuaho856c4972016-08-08 11:38:39 +03003412 if (type.isUnsizedArray())
3413 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003414 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003415 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003416 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003417 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003418 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003419 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3420 ASSERT(firstElement);
3421 type.setArraySize(type.getArraySizes().size() - 1u,
3422 static_cast<unsigned int>(arguments->size()));
3423 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3424 {
3425 if (type.getArraySizes()[i] == 0u)
3426 {
3427 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3428 }
3429 }
3430 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003431 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003432
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003433 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003434 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003435 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003436 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003437
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003438 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003439 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003440
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003441 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3442 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003443 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003444 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003445 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003446 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003447}
3448
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003449//
3450// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003451// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003452//
Olli Etuaho13389b62016-10-16 11:48:18 +01003453TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003454 const TTypeQualifierBuilder &typeQualifierBuilder,
3455 const TSourceLoc &nameLine,
3456 const TString &blockName,
3457 TFieldList *fieldList,
3458 const TString *instanceName,
3459 const TSourceLoc &instanceLine,
3460 TIntermTyped *arrayIndex,
3461 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003462{
Olli Etuaho856c4972016-08-08 11:38:39 +03003463 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003464
Olli Etuaho77ba4082016-12-16 12:01:18 +00003465 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003466
Jiajia Qinbc585152017-06-23 15:42:17 +08003467 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003468 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003469 error(typeQualifier.line,
3470 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3471 "3.10",
3472 getQualifierString(typeQualifier.qualifier));
3473 }
3474 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3475 {
3476 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003477 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003478 }
3479
Martin Radev70866b82016-07-22 15:27:42 +03003480 if (typeQualifier.invariant)
3481 {
3482 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3483 }
3484
Jiajia Qinbc585152017-06-23 15:42:17 +08003485 if (typeQualifier.qualifier != EvqBuffer)
3486 {
3487 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3488 }
Olli Etuaho43364892017-02-13 16:00:12 +00003489
jchen10af713a22017-04-19 09:10:56 +08003490 // add array index
3491 unsigned int arraySize = 0;
3492 if (arrayIndex != nullptr)
3493 {
3494 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3495 }
3496
3497 if (mShaderVersion < 310)
3498 {
3499 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3500 }
3501 else
3502 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003503 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3504 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003505 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003506
Andrei Volykhina5527072017-03-22 16:46:30 +03003507 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3508
Jamie Madill099c0f32013-06-20 11:55:52 -04003509 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003510 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003511
Jamie Madill099c0f32013-06-20 11:55:52 -04003512 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3513 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003514 if (typeQualifier.qualifier == EvqUniform)
3515 {
3516 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3517 }
3518 else if (typeQualifier.qualifier == EvqBuffer)
3519 {
3520 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3521 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003522 }
3523
Jamie Madill1566ef72013-06-20 11:55:54 -04003524 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3525 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003526 if (typeQualifier.qualifier == EvqUniform)
3527 {
3528 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3529 }
3530 else if (typeQualifier.qualifier == EvqBuffer)
3531 {
3532 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3533 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003534 }
3535
Olli Etuaho856c4972016-08-08 11:38:39 +03003536 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003537
Martin Radev2cc85b32016-08-05 16:22:53 +03003538 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3539
Olli Etuaho0f684632017-07-13 12:42:15 +03003540 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303541 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003542 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003543 }
3544
Jamie Madill98493dd2013-07-08 14:39:03 -04003545 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303546 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3547 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003548 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303549 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003550 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303551 {
jchen10cc2a10e2017-05-03 14:05:12 +08003552 std::string reason("unsupported type - ");
3553 reason += fieldType->getBasicString();
3554 reason += " types are not allowed in interface blocks";
3555 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003556 }
3557
Jamie Madill98493dd2013-07-08 14:39:03 -04003558 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003559 switch (qualifier)
3560 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003561 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003562 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003563 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003564 if (typeQualifier.qualifier == EvqBuffer)
3565 {
3566 error(field->line(), "invalid qualifier on shader storage block member",
3567 getQualifierString(qualifier));
3568 }
3569 break;
3570 case EvqBuffer:
3571 if (typeQualifier.qualifier == EvqUniform)
3572 {
3573 error(field->line(), "invalid qualifier on uniform block member",
3574 getQualifierString(qualifier));
3575 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003576 break;
3577 default:
3578 error(field->line(), "invalid qualifier on interface block member",
3579 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003580 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003581 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003582
Martin Radev70866b82016-07-22 15:27:42 +03003583 if (fieldType->isInvariant())
3584 {
3585 error(field->line(), "invalid qualifier on interface block member", "invariant");
3586 }
3587
Jamie Madilla5efff92013-06-06 11:56:47 -04003588 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003589 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003590 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003591 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003592
Jamie Madill98493dd2013-07-08 14:39:03 -04003593 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003594 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003595 error(field->line(), "invalid layout qualifier: cannot be used here",
3596 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003597 }
3598
Jamie Madill98493dd2013-07-08 14:39:03 -04003599 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003600 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003601 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003602 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003603 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003604 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003605 warning(field->line(),
3606 "extraneous layout qualifier: only has an effect on matrix types",
3607 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003608 }
3609
Jamie Madill98493dd2013-07-08 14:39:03 -04003610 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003611
3612 if (typeQualifier.qualifier == EvqBuffer)
3613 {
3614 // set memory qualifiers
3615 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3616 // qualified with a memory qualifier, it is as if all of its members were declared with
3617 // the same memory qualifier.
3618 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3619 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3620 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3621 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3622 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3623 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3624 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3625 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3626 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3627 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3628 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003629 }
3630
Jamie Madillb98c3a82015-07-23 14:26:04 -04003631 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003632 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003633 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3634 if (arrayIndex != nullptr)
3635 {
3636 interfaceBlockType.makeArray(arraySize);
3637 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003638
3639 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003640 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003641
Jamie Madill98493dd2013-07-08 14:39:03 -04003642 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003643 {
3644 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003645 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3646 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003647 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303648 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003649
3650 // set parent pointer of the field variable
3651 fieldType->setInterfaceBlock(interfaceBlock);
3652
Olli Etuaho0f684632017-07-13 12:42:15 +03003653 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003654
Olli Etuaho0f684632017-07-13 12:42:15 +03003655 if (fieldVariable)
3656 {
3657 fieldVariable->setQualifier(typeQualifier.qualifier);
3658 }
3659 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303660 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003661 error(field->line(), "redefinition of an interface block member name",
3662 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003663 }
3664 }
3665 }
3666 else
3667 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003668 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003669
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003670 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003671 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3672 if (instanceTypeDef)
3673 {
3674 instanceTypeDef->setQualifier(typeQualifier.qualifier);
3675 symbolId = instanceTypeDef->getUniqueId();
3676 }
3677 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303678 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003679 error(instanceLine, "redefinition of an interface block instance name",
3680 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003681 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003682 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003683 }
3684
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003685 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3686 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003687 TIntermDeclaration *declaration = new TIntermDeclaration();
3688 declaration->appendDeclarator(blockSymbol);
3689 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003690
3691 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003692 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003693}
3694
Olli Etuaho383b7912016-08-05 11:22:59 +03003695void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003696{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003697 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003698
3699 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003700 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303701 if (mStructNestingLevel > 1)
3702 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003703 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003704 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003705}
3706
3707void TParseContext::exitStructDeclaration()
3708{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003709 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003710}
3711
Olli Etuaho8a176262016-08-16 14:23:01 +03003712void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003713{
Jamie Madillacb4b812016-11-07 13:50:29 -05003714 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303715 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003716 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003717 }
3718
Arun Patole7e7e68d2015-05-22 12:02:25 +05303719 if (field.type()->getBasicType() != EbtStruct)
3720 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003721 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003722 }
3723
3724 // We're already inside a structure definition at this point, so add
3725 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303726 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3727 {
Jamie Madill41a49272014-03-18 16:10:13 -04003728 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003729 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3730 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003731 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003732 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003733 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003734 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003735}
3736
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003737//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003738// Parse an array index expression
3739//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003740TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3741 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303742 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003743{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003744 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3745 {
3746 if (baseExpression->getAsSymbolNode())
3747 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303748 error(location, " left of '[' is not of type array, matrix, or vector ",
3749 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003750 }
3751 else
3752 {
3753 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3754 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003755
Olli Etuaho3ec75682017-07-05 17:02:55 +03003756 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003757 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003758
Jiawei Shaod8105a02017-08-08 09:54:36 +08003759 if (baseExpression->getQualifier() == EvqPerVertexIn)
3760 {
3761 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3762 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3763 {
3764 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3765 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3766 }
3767 }
3768
Jamie Madill21c1e452014-12-29 11:33:41 -05003769 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3770
Olli Etuaho36b05142015-11-12 13:10:42 +02003771 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3772 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3773 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3774 // index is a constant expression.
3775 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3776 {
3777 if (baseExpression->isInterfaceBlock())
3778 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003779 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3780 switch (baseExpression->getQualifier())
3781 {
3782 case EvqPerVertexIn:
3783 break;
3784 case EvqUniform:
3785 case EvqBuffer:
3786 error(location,
3787 "array indexes for uniform block arrays and shader storage block arrays "
3788 "must be constant integral expressions",
3789 "[");
3790 break;
3791 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003792 // We can reach here only in error cases.
3793 ASSERT(mDiagnostics->numErrors() > 0);
3794 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003795 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003796 }
3797 else if (baseExpression->getQualifier() == EvqFragmentOut)
3798 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003799 error(location,
3800 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003801 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003802 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3803 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003804 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003805 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003806 }
3807
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003808 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003809 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003810 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3811 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3812 // constant fold expressions that are not constant expressions). The most compatible way to
3813 // handle this case is to report a warning instead of an error and force the index to be in
3814 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003815 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003816 int index = 0;
3817 if (indexConstantUnion->getBasicType() == EbtInt)
3818 {
3819 index = indexConstantUnion->getIConst(0);
3820 }
3821 else if (indexConstantUnion->getBasicType() == EbtUInt)
3822 {
3823 index = static_cast<int>(indexConstantUnion->getUConst(0));
3824 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003825
3826 int safeIndex = -1;
3827
3828 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003829 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003830 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003831 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003832 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003833 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003834 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003835 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003836 "GL_EXT_draw_buffers is disabled",
3837 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003838 safeIndex = 0;
3839 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003840 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003841 // Only do generic out-of-range check if similar error hasn't already been reported.
3842 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003843 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003844 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003845 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003846 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003847 }
3848 }
3849 else if (baseExpression->isMatrix())
3850 {
3851 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003852 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003853 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003854 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003855 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003856 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003857 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3858 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003859 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003860 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003861
3862 ASSERT(safeIndex >= 0);
3863 // Data of constant unions can't be changed, because it may be shared with other
3864 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3865 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003866 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003867 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003868 TConstantUnion *safeConstantUnion = new TConstantUnion();
3869 safeConstantUnion->setIConst(safeIndex);
3870 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003871 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003872 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003873
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003874 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3875 node->setLine(location);
3876 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003877 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003878 else
3879 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003880 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3881 node->setLine(location);
3882 // Indirect indexing can never be constant folded.
3883 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003884 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003885}
3886
Olli Etuaho90892fb2016-07-14 14:44:51 +03003887int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3888 const TSourceLoc &location,
3889 int index,
3890 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003891 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003892{
3893 if (index >= arraySize || index < 0)
3894 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003895 std::stringstream reasonStream;
3896 reasonStream << reason << " '" << index << "'";
3897 std::string token = reasonStream.str();
3898 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003899 if (index < 0)
3900 {
3901 return 0;
3902 }
3903 else
3904 {
3905 return arraySize - 1;
3906 }
3907 }
3908 return index;
3909}
3910
Jamie Madillb98c3a82015-07-23 14:26:04 -04003911TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3912 const TSourceLoc &dotLocation,
3913 const TString &fieldString,
3914 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003915{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003916 if (baseExpression->isArray())
3917 {
3918 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003919 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003920 }
3921
3922 if (baseExpression->isVector())
3923 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003924 TVector<int> fieldOffsets;
3925 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3926 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003927 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003928 fieldOffsets.resize(1);
3929 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003930 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003931 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3932 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003933
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003934 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003935 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003936 else if (baseExpression->getBasicType() == EbtStruct)
3937 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303938 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003939 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003940 {
3941 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003942 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003943 }
3944 else
3945 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003946 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003947 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003948 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003949 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003950 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003951 {
3952 fieldFound = true;
3953 break;
3954 }
3955 }
3956 if (fieldFound)
3957 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003958 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003959 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003960 TIntermBinary *node =
3961 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3962 node->setLine(dotLocation);
3963 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003964 }
3965 else
3966 {
3967 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003968 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003969 }
3970 }
3971 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003972 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003973 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303974 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003975 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003976 {
3977 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003978 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003979 }
3980 else
3981 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003982 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003983 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003984 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003985 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003986 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003987 {
3988 fieldFound = true;
3989 break;
3990 }
3991 }
3992 if (fieldFound)
3993 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003994 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003995 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003996 TIntermBinary *node =
3997 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3998 node->setLine(dotLocation);
3999 // Indexing interface blocks can never be constant folded.
4000 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004001 }
4002 else
4003 {
4004 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004005 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004006 }
4007 }
4008 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004009 else
4010 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004011 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004012 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004013 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304014 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004015 }
4016 else
4017 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304018 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004019 " field selection requires structure, vector, or interface block on left hand "
4020 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304021 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004022 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004023 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004024 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004025}
4026
Jamie Madillb98c3a82015-07-23 14:26:04 -04004027TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4028 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004029{
Martin Radev802abe02016-08-04 17:48:32 +03004030 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004031
4032 if (qualifierType == "shared")
4033 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004034 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004035 {
4036 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4037 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004038 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004039 }
4040 else if (qualifierType == "packed")
4041 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004042 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004043 {
4044 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4045 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004046 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004047 }
4048 else if (qualifierType == "std140")
4049 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004050 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004051 }
4052 else if (qualifierType == "row_major")
4053 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004054 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004055 }
4056 else if (qualifierType == "column_major")
4057 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004058 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004059 }
4060 else if (qualifierType == "location")
4061 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004062 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4063 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004064 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004065 else if (qualifierType == "yuv" && isExtensionEnabled(TExtension::EXT_YUV_target) &&
Andrei Volykhina5527072017-03-22 16:46:30 +03004066 mShaderType == GL_FRAGMENT_SHADER)
4067 {
4068 qualifier.yuv = true;
4069 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004070 else if (qualifierType == "rgba32f")
4071 {
4072 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4073 qualifier.imageInternalFormat = EiifRGBA32F;
4074 }
4075 else if (qualifierType == "rgba16f")
4076 {
4077 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4078 qualifier.imageInternalFormat = EiifRGBA16F;
4079 }
4080 else if (qualifierType == "r32f")
4081 {
4082 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4083 qualifier.imageInternalFormat = EiifR32F;
4084 }
4085 else if (qualifierType == "rgba8")
4086 {
4087 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4088 qualifier.imageInternalFormat = EiifRGBA8;
4089 }
4090 else if (qualifierType == "rgba8_snorm")
4091 {
4092 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4093 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4094 }
4095 else if (qualifierType == "rgba32i")
4096 {
4097 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4098 qualifier.imageInternalFormat = EiifRGBA32I;
4099 }
4100 else if (qualifierType == "rgba16i")
4101 {
4102 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4103 qualifier.imageInternalFormat = EiifRGBA16I;
4104 }
4105 else if (qualifierType == "rgba8i")
4106 {
4107 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4108 qualifier.imageInternalFormat = EiifRGBA8I;
4109 }
4110 else if (qualifierType == "r32i")
4111 {
4112 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4113 qualifier.imageInternalFormat = EiifR32I;
4114 }
4115 else if (qualifierType == "rgba32ui")
4116 {
4117 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4118 qualifier.imageInternalFormat = EiifRGBA32UI;
4119 }
4120 else if (qualifierType == "rgba16ui")
4121 {
4122 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4123 qualifier.imageInternalFormat = EiifRGBA16UI;
4124 }
4125 else if (qualifierType == "rgba8ui")
4126 {
4127 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4128 qualifier.imageInternalFormat = EiifRGBA8UI;
4129 }
4130 else if (qualifierType == "r32ui")
4131 {
4132 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4133 qualifier.imageInternalFormat = EiifR32UI;
4134 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004135 else if (qualifierType == "points" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004136 mShaderType == GL_GEOMETRY_SHADER_OES)
4137 {
4138 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4139 qualifier.primitiveType = EptPoints;
4140 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004141 else if (qualifierType == "lines" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004142 mShaderType == GL_GEOMETRY_SHADER_OES)
4143 {
4144 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4145 qualifier.primitiveType = EptLines;
4146 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004147 else if (qualifierType == "lines_adjacency" &&
4148 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004149 mShaderType == GL_GEOMETRY_SHADER_OES)
4150 {
4151 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4152 qualifier.primitiveType = EptLinesAdjacency;
4153 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004154 else if (qualifierType == "triangles" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004155 mShaderType == GL_GEOMETRY_SHADER_OES)
4156 {
4157 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4158 qualifier.primitiveType = EptTriangles;
4159 }
4160 else if (qualifierType == "triangles_adjacency" &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004161 isExtensionEnabled(TExtension::OES_geometry_shader) &&
4162 mShaderType == GL_GEOMETRY_SHADER_OES)
Shaob5cc1192017-07-06 10:47:20 +08004163 {
4164 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4165 qualifier.primitiveType = EptTrianglesAdjacency;
4166 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004167 else if (qualifierType == "line_strip" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004168 mShaderType == GL_GEOMETRY_SHADER_OES)
4169 {
4170 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4171 qualifier.primitiveType = EptLineStrip;
4172 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004173 else if (qualifierType == "triangle_strip" &&
4174 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004175 mShaderType == GL_GEOMETRY_SHADER_OES)
4176 {
4177 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4178 qualifier.primitiveType = EptTriangleStrip;
4179 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004180
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004181 else
4182 {
4183 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004184 }
4185
Jamie Madilla5efff92013-06-06 11:56:47 -04004186 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004187}
4188
Martin Radev802abe02016-08-04 17:48:32 +03004189void TParseContext::parseLocalSize(const TString &qualifierType,
4190 const TSourceLoc &qualifierTypeLine,
4191 int intValue,
4192 const TSourceLoc &intValueLine,
4193 const std::string &intValueString,
4194 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004195 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004196{
Olli Etuaho856c4972016-08-08 11:38:39 +03004197 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004198 if (intValue < 1)
4199 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004200 std::stringstream reasonStream;
4201 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4202 std::string reason = reasonStream.str();
4203 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004204 }
4205 (*localSize)[index] = intValue;
4206}
4207
Olli Etuaho09b04a22016-12-15 13:30:26 +00004208void TParseContext::parseNumViews(int intValue,
4209 const TSourceLoc &intValueLine,
4210 const std::string &intValueString,
4211 int *numViews)
4212{
4213 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4214 // specification.
4215 if (intValue < 1)
4216 {
4217 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4218 }
4219 *numViews = intValue;
4220}
4221
Shaob5cc1192017-07-06 10:47:20 +08004222void TParseContext::parseInvocations(int intValue,
4223 const TSourceLoc &intValueLine,
4224 const std::string &intValueString,
4225 int *numInvocations)
4226{
4227 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4228 // it doesn't make sense to accept invocations <= 0.
4229 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4230 {
4231 error(intValueLine,
4232 "out of range: invocations must be in the range of [1, "
4233 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4234 intValueString.c_str());
4235 }
4236 else
4237 {
4238 *numInvocations = intValue;
4239 }
4240}
4241
4242void TParseContext::parseMaxVertices(int intValue,
4243 const TSourceLoc &intValueLine,
4244 const std::string &intValueString,
4245 int *maxVertices)
4246{
4247 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4248 // it doesn't make sense to accept max_vertices < 0.
4249 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4250 {
4251 error(
4252 intValueLine,
4253 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4254 intValueString.c_str());
4255 }
4256 else
4257 {
4258 *maxVertices = intValue;
4259 }
4260}
4261
Jamie Madillb98c3a82015-07-23 14:26:04 -04004262TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4263 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004264 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304265 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004266{
Martin Radev802abe02016-08-04 17:48:32 +03004267 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004268
Martin Radev802abe02016-08-04 17:48:32 +03004269 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004270
Martin Radev802abe02016-08-04 17:48:32 +03004271 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004272 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004273 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004274 if (intValue < 0)
4275 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004276 error(intValueLine, "out of range: location must be non-negative",
4277 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004278 }
4279 else
4280 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004281 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004282 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004283 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004284 }
Olli Etuaho43364892017-02-13 16:00:12 +00004285 else if (qualifierType == "binding")
4286 {
4287 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4288 if (intValue < 0)
4289 {
4290 error(intValueLine, "out of range: binding must be non-negative",
4291 intValueString.c_str());
4292 }
4293 else
4294 {
4295 qualifier.binding = intValue;
4296 }
4297 }
jchen104cdac9e2017-05-08 11:01:20 +08004298 else if (qualifierType == "offset")
4299 {
4300 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4301 if (intValue < 0)
4302 {
4303 error(intValueLine, "out of range: offset must be non-negative",
4304 intValueString.c_str());
4305 }
4306 else
4307 {
4308 qualifier.offset = intValue;
4309 }
4310 }
Martin Radev802abe02016-08-04 17:48:32 +03004311 else if (qualifierType == "local_size_x")
4312 {
4313 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4314 &qualifier.localSize);
4315 }
4316 else if (qualifierType == "local_size_y")
4317 {
4318 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4319 &qualifier.localSize);
4320 }
4321 else if (qualifierType == "local_size_z")
4322 {
4323 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4324 &qualifier.localSize);
4325 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004326 else if (qualifierType == "num_views" && isExtensionEnabled(TExtension::OVR_multiview) &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004327 mShaderType == GL_VERTEX_SHADER)
4328 {
4329 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4330 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004331 else if (qualifierType == "invocations" &&
4332 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004333 mShaderType == GL_GEOMETRY_SHADER_OES)
4334 {
4335 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4336 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004337 else if (qualifierType == "max_vertices" &&
4338 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004339 mShaderType == GL_GEOMETRY_SHADER_OES)
4340 {
4341 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4342 }
4343
Martin Radev802abe02016-08-04 17:48:32 +03004344 else
4345 {
4346 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004347 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004348
Jamie Madilla5efff92013-06-06 11:56:47 -04004349 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004350}
4351
Olli Etuaho613b9592016-09-05 12:05:53 +03004352TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4353{
4354 return new TTypeQualifierBuilder(
4355 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4356 mShaderVersion);
4357}
4358
Olli Etuahocce89652017-06-19 16:04:09 +03004359TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4360 const TSourceLoc &loc)
4361{
4362 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4363 return new TStorageQualifierWrapper(qualifier, loc);
4364}
4365
4366TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4367{
4368 if (getShaderType() == GL_VERTEX_SHADER)
4369 {
4370 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4371 }
4372 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4373}
4374
4375TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4376{
4377 if (declaringFunction())
4378 {
4379 return new TStorageQualifierWrapper(EvqIn, loc);
4380 }
Shaob5cc1192017-07-06 10:47:20 +08004381
4382 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004383 {
Shaob5cc1192017-07-06 10:47:20 +08004384 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004385 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004386 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004387 {
4388 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4389 }
4390 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004391 }
Shaob5cc1192017-07-06 10:47:20 +08004392 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004393 {
Shaob5cc1192017-07-06 10:47:20 +08004394 if (mShaderVersion < 300)
4395 {
4396 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4397 }
4398 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004399 }
Shaob5cc1192017-07-06 10:47:20 +08004400 case GL_COMPUTE_SHADER:
4401 {
4402 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4403 }
4404 case GL_GEOMETRY_SHADER_OES:
4405 {
4406 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4407 }
4408 default:
4409 {
4410 UNREACHABLE();
4411 return new TStorageQualifierWrapper(EvqLast, loc);
4412 }
Olli Etuahocce89652017-06-19 16:04:09 +03004413 }
Olli Etuahocce89652017-06-19 16:04:09 +03004414}
4415
4416TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4417{
4418 if (declaringFunction())
4419 {
4420 return new TStorageQualifierWrapper(EvqOut, loc);
4421 }
Shaob5cc1192017-07-06 10:47:20 +08004422 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004423 {
Shaob5cc1192017-07-06 10:47:20 +08004424 case GL_VERTEX_SHADER:
4425 {
4426 if (mShaderVersion < 300)
4427 {
4428 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4429 }
4430 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4431 }
4432 case GL_FRAGMENT_SHADER:
4433 {
4434 if (mShaderVersion < 300)
4435 {
4436 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4437 }
4438 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4439 }
4440 case GL_COMPUTE_SHADER:
4441 {
4442 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4443 return new TStorageQualifierWrapper(EvqLast, loc);
4444 }
4445 case GL_GEOMETRY_SHADER_OES:
4446 {
4447 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4448 }
4449 default:
4450 {
4451 UNREACHABLE();
4452 return new TStorageQualifierWrapper(EvqLast, loc);
4453 }
Olli Etuahocce89652017-06-19 16:04:09 +03004454 }
Olli Etuahocce89652017-06-19 16:04:09 +03004455}
4456
4457TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4458{
4459 if (!declaringFunction())
4460 {
4461 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4462 }
4463 return new TStorageQualifierWrapper(EvqInOut, loc);
4464}
4465
Jamie Madillb98c3a82015-07-23 14:26:04 -04004466TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004467 TLayoutQualifier rightQualifier,
4468 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004469{
Martin Radevc28888b2016-07-22 15:27:42 +03004470 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004471 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004472}
4473
Olli Etuahocce89652017-06-19 16:04:09 +03004474TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4475{
4476 checkIsNotReserved(loc, *identifier);
4477 TType *type = new TType(EbtVoid, EbpUndefined);
4478 return new TField(type, identifier, loc);
4479}
4480
4481TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4482 const TSourceLoc &loc,
4483 TIntermTyped *arraySize,
4484 const TSourceLoc &arraySizeLoc)
4485{
4486 checkIsNotReserved(loc, *identifier);
4487
4488 TType *type = new TType(EbtVoid, EbpUndefined);
4489 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004490 type->makeArray(size);
Olli Etuahocce89652017-06-19 16:04:09 +03004491
4492 return new TField(type, identifier, loc);
4493}
4494
Olli Etuaho4de340a2016-12-16 09:32:03 +00004495TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4496 const TFieldList *newlyAddedFields,
4497 const TSourceLoc &location)
4498{
4499 for (TField *field : *newlyAddedFields)
4500 {
4501 for (TField *oldField : *processedFields)
4502 {
4503 if (oldField->name() == field->name())
4504 {
4505 error(location, "duplicate field name in structure", field->name().c_str());
4506 }
4507 }
4508 processedFields->push_back(field);
4509 }
4510 return processedFields;
4511}
4512
Martin Radev70866b82016-07-22 15:27:42 +03004513TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4514 const TTypeQualifierBuilder &typeQualifierBuilder,
4515 TPublicType *typeSpecifier,
4516 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004517{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004518 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004519
Martin Radev70866b82016-07-22 15:27:42 +03004520 typeSpecifier->qualifier = typeQualifier.qualifier;
4521 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004522 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004523 typeSpecifier->invariant = typeQualifier.invariant;
4524 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304525 {
Martin Radev70866b82016-07-22 15:27:42 +03004526 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004527 }
Martin Radev70866b82016-07-22 15:27:42 +03004528 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004529}
4530
Jamie Madillb98c3a82015-07-23 14:26:04 -04004531TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004532 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004533{
Martin Radev4a9cd802016-09-01 16:51:51 +03004534 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4535 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004536
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004537 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4538 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004539
Martin Radev4a9cd802016-09-01 16:51:51 +03004540 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004541
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004542 for (unsigned int i = 0; i < declaratorList->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304543 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004544 auto declaratorArraySizes = (*declaratorList)[i]->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004545 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004546 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304547 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004548 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004549 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004550
4551 TType *type = (*declaratorList)[i]->type();
4552 *type = TType(typeSpecifier);
4553 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304554 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004555 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004556 }
4557
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004558 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*declaratorList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004559 }
4560
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004561 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004562}
4563
Martin Radev4a9cd802016-09-01 16:51:51 +03004564TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4565 const TSourceLoc &nameLine,
4566 const TString *structName,
4567 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004568{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004569 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004570
Jamie Madill9b820842015-02-12 10:40:10 -05004571 // Store a bool in the struct if we're at global scope, to allow us to
4572 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004573 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004574
Jamie Madill98493dd2013-07-08 14:39:03 -04004575 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004576 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004577 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004578 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304579 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004580 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004581 }
4582 }
4583
4584 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004585 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004586 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004587 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004588 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004589 switch (qualifier)
4590 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004591 case EvqGlobal:
4592 case EvqTemporary:
4593 break;
4594 default:
4595 error(field.line(), "invalid qualifier on struct member",
4596 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004597 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004598 }
Martin Radev70866b82016-07-22 15:27:42 +03004599 if (field.type()->isInvariant())
4600 {
4601 error(field.line(), "invalid qualifier on struct member", "invariant");
4602 }
jchen104cdac9e2017-05-08 11:01:20 +08004603 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4604 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004605 {
4606 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4607 }
4608
Olli Etuaho43364892017-02-13 16:00:12 +00004609 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4610
4611 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004612
4613 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004614 }
4615
Martin Radev4a9cd802016-09-01 16:51:51 +03004616 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004617 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004618 exitStructDeclaration();
4619
Martin Radev4a9cd802016-09-01 16:51:51 +03004620 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004621}
4622
Jamie Madillb98c3a82015-07-23 14:26:04 -04004623TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004624 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004625 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004626{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004627 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004628 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004629 init->isVector())
4630 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004631 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4632 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004633 return nullptr;
4634 }
4635
Olli Etuahoac5274d2015-02-20 10:19:08 +02004636 if (statementList)
4637 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004638 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004639 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004640 return nullptr;
4641 }
4642 }
4643
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004644 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4645 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004646 return node;
4647}
4648
4649TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4650{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004651 if (mSwitchNestingLevel == 0)
4652 {
4653 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004654 return nullptr;
4655 }
4656 if (condition == nullptr)
4657 {
4658 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004659 return nullptr;
4660 }
4661 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004662 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004663 {
4664 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004665 }
4666 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004667 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4668 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4669 // fold in case labels.
4670 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004671 {
4672 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004673 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004674 TIntermCase *node = new TIntermCase(condition);
4675 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004676 return node;
4677}
4678
4679TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4680{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004681 if (mSwitchNestingLevel == 0)
4682 {
4683 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004684 return nullptr;
4685 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004686 TIntermCase *node = new TIntermCase(nullptr);
4687 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004688 return node;
4689}
4690
Jamie Madillb98c3a82015-07-23 14:26:04 -04004691TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4692 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004693 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004694{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004695 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004696
4697 switch (op)
4698 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004699 case EOpLogicalNot:
4700 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4701 child->isVector())
4702 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004703 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004704 return nullptr;
4705 }
4706 break;
4707 case EOpBitwiseNot:
4708 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4709 child->isMatrix() || child->isArray())
4710 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004711 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004712 return nullptr;
4713 }
4714 break;
4715 case EOpPostIncrement:
4716 case EOpPreIncrement:
4717 case EOpPostDecrement:
4718 case EOpPreDecrement:
4719 case EOpNegative:
4720 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004721 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4722 child->getBasicType() == EbtBool || child->isArray() ||
4723 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004724 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004725 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004726 return nullptr;
4727 }
4728 // Operators for built-ins are already type checked against their prototype.
4729 default:
4730 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004731 }
4732
Jiajia Qinbc585152017-06-23 15:42:17 +08004733 if (child->getMemoryQualifier().writeonly)
4734 {
4735 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4736 return nullptr;
4737 }
4738
Olli Etuahof119a262016-08-19 15:54:22 +03004739 TIntermUnary *node = new TIntermUnary(op, child);
4740 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004741
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004742 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004743}
4744
Olli Etuaho09b22472015-02-11 11:47:26 +02004745TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4746{
Olli Etuahocce89652017-06-19 16:04:09 +03004747 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004748 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004749 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004750 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004751 return child;
4752 }
4753 return node;
4754}
4755
Jamie Madillb98c3a82015-07-23 14:26:04 -04004756TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4757 TIntermTyped *child,
4758 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004759{
Olli Etuaho856c4972016-08-08 11:38:39 +03004760 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004761 return addUnaryMath(op, child, loc);
4762}
4763
Jamie Madillb98c3a82015-07-23 14:26:04 -04004764bool TParseContext::binaryOpCommonCheck(TOperator op,
4765 TIntermTyped *left,
4766 TIntermTyped *right,
4767 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004768{
jchen10b4cf5652017-05-05 18:51:17 +08004769 // Check opaque types are not allowed to be operands in expressions other than array indexing
4770 // and structure member selection.
4771 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4772 {
4773 switch (op)
4774 {
4775 case EOpIndexDirect:
4776 case EOpIndexIndirect:
4777 break;
4778 case EOpIndexDirectStruct:
4779 UNREACHABLE();
4780
4781 default:
4782 error(loc, "Invalid operation for variables with an opaque type",
4783 GetOperatorString(op));
4784 return false;
4785 }
4786 }
jchen10cc2a10e2017-05-03 14:05:12 +08004787
Jiajia Qinbc585152017-06-23 15:42:17 +08004788 if (right->getMemoryQualifier().writeonly)
4789 {
4790 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4791 return false;
4792 }
4793
4794 if (left->getMemoryQualifier().writeonly)
4795 {
4796 switch (op)
4797 {
4798 case EOpAssign:
4799 case EOpInitialize:
4800 case EOpIndexDirect:
4801 case EOpIndexIndirect:
4802 case EOpIndexDirectStruct:
4803 case EOpIndexDirectInterfaceBlock:
4804 break;
4805 default:
4806 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4807 return false;
4808 }
4809 }
4810
Olli Etuaho244be012016-08-18 15:26:02 +03004811 if (left->getType().getStruct() || right->getType().getStruct())
4812 {
4813 switch (op)
4814 {
4815 case EOpIndexDirectStruct:
4816 ASSERT(left->getType().getStruct());
4817 break;
4818 case EOpEqual:
4819 case EOpNotEqual:
4820 case EOpAssign:
4821 case EOpInitialize:
4822 if (left->getType() != right->getType())
4823 {
4824 return false;
4825 }
4826 break;
4827 default:
4828 error(loc, "Invalid operation for structs", GetOperatorString(op));
4829 return false;
4830 }
4831 }
4832
Olli Etuaho94050052017-05-08 14:17:44 +03004833 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4834 {
4835 switch (op)
4836 {
4837 case EOpIndexDirectInterfaceBlock:
4838 ASSERT(left->getType().getInterfaceBlock());
4839 break;
4840 default:
4841 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4842 return false;
4843 }
4844 }
4845
Olli Etuahod6b14282015-03-17 14:31:35 +02004846 if (left->isArray() || right->isArray())
4847 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004848 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004849 {
4850 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4851 return false;
4852 }
4853
4854 if (left->isArray() != right->isArray())
4855 {
4856 error(loc, "array / non-array mismatch", GetOperatorString(op));
4857 return false;
4858 }
4859
4860 switch (op)
4861 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004862 case EOpEqual:
4863 case EOpNotEqual:
4864 case EOpAssign:
4865 case EOpInitialize:
4866 break;
4867 default:
4868 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4869 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004870 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004871 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004872 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004873 {
4874 error(loc, "array size mismatch", GetOperatorString(op));
4875 return false;
4876 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004877 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004878
4879 // Check ops which require integer / ivec parameters
4880 bool isBitShift = false;
4881 switch (op)
4882 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004883 case EOpBitShiftLeft:
4884 case EOpBitShiftRight:
4885 case EOpBitShiftLeftAssign:
4886 case EOpBitShiftRightAssign:
4887 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4888 // check that the basic type is an integer type.
4889 isBitShift = true;
4890 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4891 {
4892 return false;
4893 }
4894 break;
4895 case EOpBitwiseAnd:
4896 case EOpBitwiseXor:
4897 case EOpBitwiseOr:
4898 case EOpBitwiseAndAssign:
4899 case EOpBitwiseXorAssign:
4900 case EOpBitwiseOrAssign:
4901 // It is enough to check the type of only one operand, since later it
4902 // is checked that the operand types match.
4903 if (!IsInteger(left->getBasicType()))
4904 {
4905 return false;
4906 }
4907 break;
4908 default:
4909 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004910 }
4911
4912 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4913 // So the basic type should usually match.
4914 if (!isBitShift && left->getBasicType() != right->getBasicType())
4915 {
4916 return false;
4917 }
4918
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004919 // Check that:
4920 // 1. Type sizes match exactly on ops that require that.
4921 // 2. Restrictions for structs that contain arrays or samplers are respected.
4922 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004923 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004924 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004925 case EOpAssign:
4926 case EOpInitialize:
4927 case EOpEqual:
4928 case EOpNotEqual:
4929 // ESSL 1.00 sections 5.7, 5.8, 5.9
4930 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4931 {
4932 error(loc, "undefined operation for structs containing arrays",
4933 GetOperatorString(op));
4934 return false;
4935 }
4936 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4937 // we interpret the spec so that this extends to structs containing samplers,
4938 // similarly to ESSL 1.00 spec.
4939 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4940 left->getType().isStructureContainingSamplers())
4941 {
4942 error(loc, "undefined operation for structs containing samplers",
4943 GetOperatorString(op));
4944 return false;
4945 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004946
Olli Etuahoe1805592017-01-02 16:41:20 +00004947 if ((left->getNominalSize() != right->getNominalSize()) ||
4948 (left->getSecondarySize() != right->getSecondarySize()))
4949 {
4950 error(loc, "dimension mismatch", GetOperatorString(op));
4951 return false;
4952 }
4953 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004954 case EOpLessThan:
4955 case EOpGreaterThan:
4956 case EOpLessThanEqual:
4957 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004958 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004959 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004960 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004961 return false;
4962 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004963 break;
4964 case EOpAdd:
4965 case EOpSub:
4966 case EOpDiv:
4967 case EOpIMod:
4968 case EOpBitShiftLeft:
4969 case EOpBitShiftRight:
4970 case EOpBitwiseAnd:
4971 case EOpBitwiseXor:
4972 case EOpBitwiseOr:
4973 case EOpAddAssign:
4974 case EOpSubAssign:
4975 case EOpDivAssign:
4976 case EOpIModAssign:
4977 case EOpBitShiftLeftAssign:
4978 case EOpBitShiftRightAssign:
4979 case EOpBitwiseAndAssign:
4980 case EOpBitwiseXorAssign:
4981 case EOpBitwiseOrAssign:
4982 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4983 {
4984 return false;
4985 }
4986
4987 // Are the sizes compatible?
4988 if (left->getNominalSize() != right->getNominalSize() ||
4989 left->getSecondarySize() != right->getSecondarySize())
4990 {
4991 // If the nominal sizes of operands do not match:
4992 // One of them must be a scalar.
4993 if (!left->isScalar() && !right->isScalar())
4994 return false;
4995
4996 // In the case of compound assignment other than multiply-assign,
4997 // the right side needs to be a scalar. Otherwise a vector/matrix
4998 // would be assigned to a scalar. A scalar can't be shifted by a
4999 // vector either.
5000 if (!right->isScalar() &&
5001 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5002 return false;
5003 }
5004 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005005 default:
5006 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005007 }
5008
Olli Etuahod6b14282015-03-17 14:31:35 +02005009 return true;
5010}
5011
Olli Etuaho1dded802016-08-18 18:13:13 +03005012bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5013 const TType &left,
5014 const TType &right)
5015{
5016 switch (op)
5017 {
5018 case EOpMul:
5019 case EOpMulAssign:
5020 return left.getNominalSize() == right.getNominalSize() &&
5021 left.getSecondarySize() == right.getSecondarySize();
5022 case EOpVectorTimesScalar:
5023 return true;
5024 case EOpVectorTimesScalarAssign:
5025 ASSERT(!left.isMatrix() && !right.isMatrix());
5026 return left.isVector() && !right.isVector();
5027 case EOpVectorTimesMatrix:
5028 return left.getNominalSize() == right.getRows();
5029 case EOpVectorTimesMatrixAssign:
5030 ASSERT(!left.isMatrix() && right.isMatrix());
5031 return left.isVector() && left.getNominalSize() == right.getRows() &&
5032 left.getNominalSize() == right.getCols();
5033 case EOpMatrixTimesVector:
5034 return left.getCols() == right.getNominalSize();
5035 case EOpMatrixTimesScalar:
5036 return true;
5037 case EOpMatrixTimesScalarAssign:
5038 ASSERT(left.isMatrix() && !right.isMatrix());
5039 return !right.isVector();
5040 case EOpMatrixTimesMatrix:
5041 return left.getCols() == right.getRows();
5042 case EOpMatrixTimesMatrixAssign:
5043 ASSERT(left.isMatrix() && right.isMatrix());
5044 // We need to check two things:
5045 // 1. The matrix multiplication step is valid.
5046 // 2. The result will have the same number of columns as the lvalue.
5047 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5048
5049 default:
5050 UNREACHABLE();
5051 return false;
5052 }
5053}
5054
Jamie Madillb98c3a82015-07-23 14:26:04 -04005055TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5056 TIntermTyped *left,
5057 TIntermTyped *right,
5058 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005059{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005060 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005061 return nullptr;
5062
Olli Etuahofc1806e2015-03-17 13:03:11 +02005063 switch (op)
5064 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005065 case EOpEqual:
5066 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005067 case EOpLessThan:
5068 case EOpGreaterThan:
5069 case EOpLessThanEqual:
5070 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005071 break;
5072 case EOpLogicalOr:
5073 case EOpLogicalXor:
5074 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005075 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5076 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005077 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005078 {
5079 return nullptr;
5080 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005081 // Basic types matching should have been already checked.
5082 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005083 break;
5084 case EOpAdd:
5085 case EOpSub:
5086 case EOpDiv:
5087 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005088 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5089 !right->getType().getStruct());
5090 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005091 {
5092 return nullptr;
5093 }
5094 break;
5095 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005096 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5097 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005098 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005099 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005100 {
5101 return nullptr;
5102 }
5103 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005104 default:
5105 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005106 }
5107
Olli Etuaho1dded802016-08-18 18:13:13 +03005108 if (op == EOpMul)
5109 {
5110 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5111 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5112 {
5113 return nullptr;
5114 }
5115 }
5116
Olli Etuaho3fdec912016-08-18 15:08:06 +03005117 TIntermBinary *node = new TIntermBinary(op, left, right);
5118 node->setLine(loc);
5119
Olli Etuaho3fdec912016-08-18 15:08:06 +03005120 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005121 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005122}
5123
Jamie Madillb98c3a82015-07-23 14:26:04 -04005124TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5125 TIntermTyped *left,
5126 TIntermTyped *right,
5127 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005128{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005129 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005130 if (node == 0)
5131 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005132 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5133 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005134 return left;
5135 }
5136 return node;
5137}
5138
Jamie Madillb98c3a82015-07-23 14:26:04 -04005139TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5140 TIntermTyped *left,
5141 TIntermTyped *right,
5142 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005143{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005144 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005145 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005146 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005147 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5148 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005149 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005150 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005151 }
5152 return node;
5153}
5154
Olli Etuaho13389b62016-10-16 11:48:18 +01005155TIntermBinary *TParseContext::createAssign(TOperator op,
5156 TIntermTyped *left,
5157 TIntermTyped *right,
5158 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005159{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005160 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005161 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005162 if (op == EOpMulAssign)
5163 {
5164 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5165 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5166 {
5167 return nullptr;
5168 }
5169 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005170 TIntermBinary *node = new TIntermBinary(op, left, right);
5171 node->setLine(loc);
5172
Olli Etuaho3fdec912016-08-18 15:08:06 +03005173 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005174 }
5175 return nullptr;
5176}
5177
Jamie Madillb98c3a82015-07-23 14:26:04 -04005178TIntermTyped *TParseContext::addAssign(TOperator op,
5179 TIntermTyped *left,
5180 TIntermTyped *right,
5181 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005182{
Olli Etuahocce89652017-06-19 16:04:09 +03005183 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005184 TIntermTyped *node = createAssign(op, left, right, loc);
5185 if (node == nullptr)
5186 {
5187 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005188 return left;
5189 }
5190 return node;
5191}
5192
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005193TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5194 TIntermTyped *right,
5195 const TSourceLoc &loc)
5196{
Corentin Wallez0d959252016-07-12 17:26:32 -04005197 // WebGL2 section 5.26, the following results in an error:
5198 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005199 if (mShaderSpec == SH_WEBGL2_SPEC &&
5200 (left->isArray() || left->getBasicType() == EbtVoid ||
5201 left->getType().isStructureContainingArrays() || right->isArray() ||
5202 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005203 {
5204 error(loc,
5205 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5206 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005207 }
5208
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005209 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5210 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5211 commaNode->getTypePointer()->setQualifier(resultQualifier);
5212 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005213}
5214
Olli Etuaho49300862015-02-20 14:54:49 +02005215TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5216{
5217 switch (op)
5218 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005219 case EOpContinue:
5220 if (mLoopNestingLevel <= 0)
5221 {
5222 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005223 }
5224 break;
5225 case EOpBreak:
5226 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5227 {
5228 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005229 }
5230 break;
5231 case EOpReturn:
5232 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5233 {
5234 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005235 }
5236 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005237 case EOpKill:
5238 if (mShaderType != GL_FRAGMENT_SHADER)
5239 {
5240 error(loc, "discard supported in fragment shaders only", "discard");
5241 }
5242 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005243 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005244 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005245 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005246 }
Olli Etuahocce89652017-06-19 16:04:09 +03005247 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005248}
5249
Jamie Madillb98c3a82015-07-23 14:26:04 -04005250TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005251 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005252 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005253{
Olli Etuahocce89652017-06-19 16:04:09 +03005254 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005255 {
Olli Etuahocce89652017-06-19 16:04:09 +03005256 ASSERT(op == EOpReturn);
5257 mFunctionReturnsValue = true;
5258 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5259 {
5260 error(loc, "void function cannot return a value", "return");
5261 }
5262 else if (*mCurrentFunctionType != expression->getType())
5263 {
5264 error(loc, "function return is not matching type:", "return");
5265 }
Olli Etuaho49300862015-02-20 14:54:49 +02005266 }
Olli Etuahocce89652017-06-19 16:04:09 +03005267 TIntermBranch *node = new TIntermBranch(op, expression);
5268 node->setLine(loc);
5269 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005270}
5271
Martin Radev84aa2dc2017-09-11 15:51:02 +03005272void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5273{
5274 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5275 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5276 bool isTextureGather = (name == "textureGather");
5277 bool isTextureGatherOffset = (name == "textureGatherOffset");
5278 if (isTextureGather || isTextureGatherOffset)
5279 {
5280 TIntermNode *componentNode = nullptr;
5281 TIntermSequence *arguments = functionCall->getSequence();
5282 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5283 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5284 ASSERT(sampler != nullptr);
5285 switch (sampler->getBasicType())
5286 {
5287 case EbtSampler2D:
5288 case EbtISampler2D:
5289 case EbtUSampler2D:
5290 case EbtSampler2DArray:
5291 case EbtISampler2DArray:
5292 case EbtUSampler2DArray:
5293 if ((isTextureGather && arguments->size() == 3u) ||
5294 (isTextureGatherOffset && arguments->size() == 4u))
5295 {
5296 componentNode = arguments->back();
5297 }
5298 break;
5299 case EbtSamplerCube:
5300 case EbtISamplerCube:
5301 case EbtUSamplerCube:
5302 ASSERT(!isTextureGatherOffset);
5303 if (arguments->size() == 3u)
5304 {
5305 componentNode = arguments->back();
5306 }
5307 break;
5308 case EbtSampler2DShadow:
5309 case EbtSampler2DArrayShadow:
5310 case EbtSamplerCubeShadow:
5311 break;
5312 default:
5313 UNREACHABLE();
5314 break;
5315 }
5316 if (componentNode)
5317 {
5318 const TIntermConstantUnion *componentConstantUnion =
5319 componentNode->getAsConstantUnion();
5320 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5321 {
5322 error(functionCall->getLine(), "Texture component must be a constant expression",
5323 name.c_str());
5324 }
5325 else
5326 {
5327 int component = componentConstantUnion->getIConst(0);
5328 if (component < 0 || component > 3)
5329 {
5330 error(functionCall->getLine(), "Component must be in the range [0;3]",
5331 name.c_str());
5332 }
5333 }
5334 }
5335 }
5336}
5337
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005338void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5339{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005340 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005341 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005342 TIntermNode *offset = nullptr;
5343 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005344 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005345 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5346 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5347 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005348 {
5349 offset = arguments->back();
5350 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005351 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005352 {
5353 // A bias parameter might follow the offset parameter.
5354 ASSERT(arguments->size() >= 3);
5355 offset = (*arguments)[2];
5356 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005357 else if (name == "textureGatherOffset")
5358 {
5359 ASSERT(arguments->size() >= 3u);
5360 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5361 ASSERT(sampler != nullptr);
5362 switch (sampler->getBasicType())
5363 {
5364 case EbtSampler2D:
5365 case EbtISampler2D:
5366 case EbtUSampler2D:
5367 case EbtSampler2DArray:
5368 case EbtISampler2DArray:
5369 case EbtUSampler2DArray:
5370 offset = (*arguments)[2];
5371 break;
5372 case EbtSampler2DShadow:
5373 case EbtSampler2DArrayShadow:
5374 offset = (*arguments)[3];
5375 break;
5376 default:
5377 UNREACHABLE();
5378 break;
5379 }
5380 useTextureGatherOffsetConstraints = true;
5381 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005382 if (offset != nullptr)
5383 {
5384 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5385 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5386 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005387 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005388 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005389 }
5390 else
5391 {
5392 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5393 size_t size = offsetConstantUnion->getType().getObjectSize();
5394 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005395 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5396 : mMinProgramTexelOffset;
5397 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5398 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005399 for (size_t i = 0u; i < size; ++i)
5400 {
5401 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005402 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005403 {
5404 std::stringstream tokenStream;
5405 tokenStream << offsetValue;
5406 std::string token = tokenStream.str();
5407 error(offset->getLine(), "Texture offset value out of valid range",
5408 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005409 }
5410 }
5411 }
5412 }
5413}
5414
Martin Radev2cc85b32016-08-05 16:22:53 +03005415// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5416void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5417{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005418 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005419 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5420
5421 if (name.compare(0, 5, "image") == 0)
5422 {
5423 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005424 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005425
Olli Etuaho485eefd2017-02-14 17:40:06 +00005426 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005427
5428 if (name.compare(5, 5, "Store") == 0)
5429 {
5430 if (memoryQualifier.readonly)
5431 {
5432 error(imageNode->getLine(),
5433 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005434 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005435 }
5436 }
5437 else if (name.compare(5, 4, "Load") == 0)
5438 {
5439 if (memoryQualifier.writeonly)
5440 {
5441 error(imageNode->getLine(),
5442 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005443 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005444 }
5445 }
5446 }
5447}
5448
5449// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5450void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5451 const TFunction *functionDefinition,
5452 const TIntermAggregate *functionCall)
5453{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005454 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005455
5456 const TIntermSequence &arguments = *functionCall->getSequence();
5457
5458 ASSERT(functionDefinition->getParamCount() == arguments.size());
5459
5460 for (size_t i = 0; i < arguments.size(); ++i)
5461 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005462 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5463 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005464 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5465 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5466
5467 if (IsImage(functionArgumentType.getBasicType()))
5468 {
5469 const TMemoryQualifier &functionArgumentMemoryQualifier =
5470 functionArgumentType.getMemoryQualifier();
5471 const TMemoryQualifier &functionParameterMemoryQualifier =
5472 functionParameterType.getMemoryQualifier();
5473 if (functionArgumentMemoryQualifier.readonly &&
5474 !functionParameterMemoryQualifier.readonly)
5475 {
5476 error(functionCall->getLine(),
5477 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005478 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005479 }
5480
5481 if (functionArgumentMemoryQualifier.writeonly &&
5482 !functionParameterMemoryQualifier.writeonly)
5483 {
5484 error(functionCall->getLine(),
5485 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005486 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005487 }
Martin Radev049edfa2016-11-11 14:35:37 +02005488
5489 if (functionArgumentMemoryQualifier.coherent &&
5490 !functionParameterMemoryQualifier.coherent)
5491 {
5492 error(functionCall->getLine(),
5493 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005494 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005495 }
5496
5497 if (functionArgumentMemoryQualifier.volatileQualifier &&
5498 !functionParameterMemoryQualifier.volatileQualifier)
5499 {
5500 error(functionCall->getLine(),
5501 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005502 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005503 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005504 }
5505 }
5506}
5507
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005508TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005509{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005510 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005511}
5512
5513TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005514 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005515 TIntermNode *thisNode,
5516 const TSourceLoc &loc)
5517{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005518 if (thisNode != nullptr)
5519 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005520 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005521 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005522
5523 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005524 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005525 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005526 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005527 }
5528 else
5529 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005530 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005531 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5532 }
5533}
5534
5535TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5536 TIntermSequence *arguments,
5537 TIntermNode *thisNode,
5538 const TSourceLoc &loc)
5539{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005540 TIntermTyped *typedThis = thisNode->getAsTyped();
5541 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5542 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5543 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5544 // So accessing fnCall->getName() below is safe.
5545 if (fnCall->getName() != "length")
5546 {
5547 error(loc, "invalid method", fnCall->getName().c_str());
5548 }
5549 else if (!arguments->empty())
5550 {
5551 error(loc, "method takes no parameters", "length");
5552 }
5553 else if (typedThis == nullptr || !typedThis->isArray())
5554 {
5555 error(loc, "length can only be called on arrays", "length");
5556 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005557 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5558 mGeometryShaderInputPrimitiveType == EptUndefined)
5559 {
5560 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5561 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005562 else
5563 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005564 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5565 node->setLine(loc);
5566 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005567 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005568 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005569}
5570
5571TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5572 TIntermSequence *arguments,
5573 const TSourceLoc &loc)
5574{
5575 // First find by unmangled name to check whether the function name has been
5576 // hidden by a variable name or struct typename.
5577 // If a function is found, check for one with a matching argument list.
5578 bool builtIn;
5579 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5580 if (symbol != nullptr && !symbol->isFunction())
5581 {
5582 error(loc, "function name expected", fnCall->getName().c_str());
5583 }
5584 else
5585 {
5586 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5587 mShaderVersion, &builtIn);
5588 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005589 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005590 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5591 }
5592 else
5593 {
5594 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005595 //
5596 // A declared function.
5597 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005598 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005599 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005600 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005601 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005602 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005603 if (builtIn && op != EOpNull)
5604 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005605 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005606 if (fnCandidate->getParamCount() == 1)
5607 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005608 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005609 TIntermNode *unaryParamNode = arguments->front();
5610 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005611 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005612 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005613 }
5614 else
5615 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005616 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005617 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005618 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005619
5620 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005621 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305622
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005623 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305624 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005625 // See if we can constant fold a built-in. Note that this may be possible
5626 // even if it is not const-qualified.
5627 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305628 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005629 else
5630 {
5631 return callNode;
5632 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005633 }
5634 }
5635 else
5636 {
5637 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005638 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005639
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005640 // If builtIn == false, the function is user defined - could be an overloaded
5641 // built-in as well.
5642 // if builtIn == true, it's a builtIn function with no op associated with it.
5643 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005644 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005645 {
Olli Etuahofe486322017-03-21 09:30:54 +00005646 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005647 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005648 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005649 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005650 }
5651 else
5652 {
Olli Etuahofe486322017-03-21 09:30:54 +00005653 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005654 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005655 }
5656
Jiajia Qinbc585152017-06-23 15:42:17 +08005657 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005658
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005659 callNode->setLine(loc);
5660
5661 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005662 }
5663 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005664 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005665
5666 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005667 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005668}
5669
Jamie Madillb98c3a82015-07-23 14:26:04 -04005670TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005671 TIntermTyped *trueExpression,
5672 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005673 const TSourceLoc &loc)
5674{
Olli Etuaho56229f12017-07-10 14:16:33 +03005675 if (!checkIsScalarBool(loc, cond))
5676 {
5677 return falseExpression;
5678 }
Olli Etuaho52901742015-04-15 13:42:45 +03005679
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005680 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005681 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005682 std::stringstream reasonStream;
5683 reasonStream << "mismatching ternary operator operand types '"
5684 << trueExpression->getCompleteString() << " and '"
5685 << falseExpression->getCompleteString() << "'";
5686 std::string reason = reasonStream.str();
5687 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005688 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005689 }
Olli Etuahode318b22016-10-25 16:18:25 +01005690 if (IsOpaqueType(trueExpression->getBasicType()))
5691 {
5692 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005693 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005694 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5695 // Note that structs containing opaque types don't need to be checked as structs are
5696 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005697 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005698 return falseExpression;
5699 }
5700
Jiajia Qinbc585152017-06-23 15:42:17 +08005701 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5702 falseExpression->getMemoryQualifier().writeonly)
5703 {
5704 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5705 return falseExpression;
5706 }
5707
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005708 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005709 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005710 // ESSL 3.00.6 section 5.7:
5711 // Ternary operator support is optional for arrays. No certainty that it works across all
5712 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5713 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005714 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005715 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005716 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005717 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005718 }
Olli Etuaho94050052017-05-08 14:17:44 +03005719 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5720 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005721 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005722 return falseExpression;
5723 }
5724
Corentin Wallez0d959252016-07-12 17:26:32 -04005725 // WebGL2 section 5.26, the following results in an error:
5726 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005727 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005728 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005729 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005730 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005731 }
5732
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005733 // Note that the node resulting from here can be a constant union without being qualified as
5734 // constant.
5735 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5736 node->setLine(loc);
5737
5738 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005739}
Olli Etuaho49300862015-02-20 14:54:49 +02005740
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005741//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005742// Parse an array of strings using yyparse.
5743//
5744// Returns 0 for success.
5745//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005746int PaParseStrings(size_t count,
5747 const char *const string[],
5748 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305749 TParseContext *context)
5750{
Yunchao He4f285442017-04-21 12:15:49 +08005751 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005752 return 1;
5753
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005754 if (glslang_initialize(context))
5755 return 1;
5756
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005757 int error = glslang_scan(count, string, length, context);
5758 if (!error)
5759 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005760
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005761 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005762
alokp@chromium.org6b495712012-06-29 00:06:58 +00005763 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005764}
Jamie Madill45bcc782016-11-07 13:58:48 -05005765
5766} // namespace sh