blob: e5f9f59cc21e13883ae5cedb8f9818d1a5027045 [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 =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800904 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000905 }
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;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001224 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001225 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001226 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001227 // 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 Etuaho2d88e9b2017-07-21 16:52:03 +03002314 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", 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.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003078 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *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 = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003640 const TSymbolUniqueId *symbolId = nullptr;
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 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003665 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003666 }
3667 else
3668 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003669 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003670
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003671 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003672 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3673 if (instanceTypeDef)
3674 {
3675 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003676 symbolId = &instanceTypeDef->getUniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003677 }
3678 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303679 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003680 error(instanceLine, "redefinition of an interface block instance name",
3681 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003682 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003683 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003684 }
3685
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003686 TIntermDeclaration *declaration = nullptr;
3687
3688 if (symbolId)
3689 {
3690 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3691 blockSymbol->setLine(typeQualifier.line);
3692 declaration = new TIntermDeclaration();
3693 declaration->appendDeclarator(blockSymbol);
3694 declaration->setLine(nameLine);
3695 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003696
3697 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003698 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003699}
3700
Olli Etuaho383b7912016-08-05 11:22:59 +03003701void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003702{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003703 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003704
3705 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003706 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303707 if (mStructNestingLevel > 1)
3708 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003709 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003710 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003711}
3712
3713void TParseContext::exitStructDeclaration()
3714{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003715 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003716}
3717
Olli Etuaho8a176262016-08-16 14:23:01 +03003718void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003719{
Jamie Madillacb4b812016-11-07 13:50:29 -05003720 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303721 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003722 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003723 }
3724
Arun Patole7e7e68d2015-05-22 12:02:25 +05303725 if (field.type()->getBasicType() != EbtStruct)
3726 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003727 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003728 }
3729
3730 // We're already inside a structure definition at this point, so add
3731 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303732 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3733 {
Jamie Madill41a49272014-03-18 16:10:13 -04003734 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003735 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3736 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003737 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003738 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003739 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003740 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003741}
3742
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003743//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003744// Parse an array index expression
3745//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003746TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3747 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303748 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003749{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003750 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3751 {
3752 if (baseExpression->getAsSymbolNode())
3753 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303754 error(location, " left of '[' is not of type array, matrix, or vector ",
3755 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003756 }
3757 else
3758 {
3759 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3760 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003761
Olli Etuaho3ec75682017-07-05 17:02:55 +03003762 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003763 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003764
Jiawei Shaod8105a02017-08-08 09:54:36 +08003765 if (baseExpression->getQualifier() == EvqPerVertexIn)
3766 {
3767 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3768 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3769 {
3770 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3771 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3772 }
3773 }
3774
Jamie Madill21c1e452014-12-29 11:33:41 -05003775 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3776
Olli Etuaho36b05142015-11-12 13:10:42 +02003777 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3778 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3779 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3780 // index is a constant expression.
3781 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3782 {
3783 if (baseExpression->isInterfaceBlock())
3784 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003785 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3786 switch (baseExpression->getQualifier())
3787 {
3788 case EvqPerVertexIn:
3789 break;
3790 case EvqUniform:
3791 case EvqBuffer:
3792 error(location,
3793 "array indexes for uniform block arrays and shader storage block arrays "
3794 "must be constant integral expressions",
3795 "[");
3796 break;
3797 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003798 // We can reach here only in error cases.
3799 ASSERT(mDiagnostics->numErrors() > 0);
3800 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003801 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003802 }
3803 else if (baseExpression->getQualifier() == EvqFragmentOut)
3804 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003805 error(location,
3806 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003807 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003808 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3809 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003810 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003811 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003812 }
3813
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003814 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003815 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003816 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3817 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3818 // constant fold expressions that are not constant expressions). The most compatible way to
3819 // handle this case is to report a warning instead of an error and force the index to be in
3820 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003821 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003822 int index = 0;
3823 if (indexConstantUnion->getBasicType() == EbtInt)
3824 {
3825 index = indexConstantUnion->getIConst(0);
3826 }
3827 else if (indexConstantUnion->getBasicType() == EbtUInt)
3828 {
3829 index = static_cast<int>(indexConstantUnion->getUConst(0));
3830 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003831
3832 int safeIndex = -1;
3833
3834 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003835 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003836 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003837 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003838 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003839 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003840 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003841 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003842 "GL_EXT_draw_buffers is disabled",
3843 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003844 safeIndex = 0;
3845 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003846 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003847 // Only do generic out-of-range check if similar error hasn't already been reported.
3848 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003849 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003850 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003851 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003852 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003853 }
3854 }
3855 else if (baseExpression->isMatrix())
3856 {
3857 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003858 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003859 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003860 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003861 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003862 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003863 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3864 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003865 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003866 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003867
3868 ASSERT(safeIndex >= 0);
3869 // Data of constant unions can't be changed, because it may be shared with other
3870 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3871 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003872 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003873 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003874 TConstantUnion *safeConstantUnion = new TConstantUnion();
3875 safeConstantUnion->setIConst(safeIndex);
3876 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003877 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003878 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003879
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003880 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3881 node->setLine(location);
3882 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003883 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003884 else
3885 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003886 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3887 node->setLine(location);
3888 // Indirect indexing can never be constant folded.
3889 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003890 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003891}
3892
Olli Etuaho90892fb2016-07-14 14:44:51 +03003893int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3894 const TSourceLoc &location,
3895 int index,
3896 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003897 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003898{
3899 if (index >= arraySize || index < 0)
3900 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003901 std::stringstream reasonStream;
3902 reasonStream << reason << " '" << index << "'";
3903 std::string token = reasonStream.str();
3904 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003905 if (index < 0)
3906 {
3907 return 0;
3908 }
3909 else
3910 {
3911 return arraySize - 1;
3912 }
3913 }
3914 return index;
3915}
3916
Jamie Madillb98c3a82015-07-23 14:26:04 -04003917TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3918 const TSourceLoc &dotLocation,
3919 const TString &fieldString,
3920 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003921{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003922 if (baseExpression->isArray())
3923 {
3924 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003925 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003926 }
3927
3928 if (baseExpression->isVector())
3929 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003930 TVector<int> fieldOffsets;
3931 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3932 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003933 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003934 fieldOffsets.resize(1);
3935 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003936 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003937 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3938 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003939
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003940 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003941 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003942 else if (baseExpression->getBasicType() == EbtStruct)
3943 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303944 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003945 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003946 {
3947 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003948 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003949 }
3950 else
3951 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003952 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003953 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003954 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003955 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003956 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003957 {
3958 fieldFound = true;
3959 break;
3960 }
3961 }
3962 if (fieldFound)
3963 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003964 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003965 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003966 TIntermBinary *node =
3967 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3968 node->setLine(dotLocation);
3969 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003970 }
3971 else
3972 {
3973 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003974 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003975 }
3976 }
3977 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003978 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003979 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303980 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003981 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003982 {
3983 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003984 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003985 }
3986 else
3987 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003988 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003989 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003990 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003991 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003992 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003993 {
3994 fieldFound = true;
3995 break;
3996 }
3997 }
3998 if (fieldFound)
3999 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004000 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004001 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004002 TIntermBinary *node =
4003 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4004 node->setLine(dotLocation);
4005 // Indexing interface blocks can never be constant folded.
4006 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004007 }
4008 else
4009 {
4010 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004011 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004012 }
4013 }
4014 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004015 else
4016 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004017 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004018 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004019 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304020 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004021 }
4022 else
4023 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304024 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004025 " field selection requires structure, vector, or interface block on left hand "
4026 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304027 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004028 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004029 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004030 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004031}
4032
Jamie Madillb98c3a82015-07-23 14:26:04 -04004033TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4034 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004035{
Martin Radev802abe02016-08-04 17:48:32 +03004036 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004037
4038 if (qualifierType == "shared")
4039 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004040 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004041 {
4042 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4043 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004044 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004045 }
4046 else if (qualifierType == "packed")
4047 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004048 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004049 {
4050 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4051 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004052 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004053 }
4054 else if (qualifierType == "std140")
4055 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004056 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004057 }
4058 else if (qualifierType == "row_major")
4059 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004060 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004061 }
4062 else if (qualifierType == "column_major")
4063 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004064 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004065 }
4066 else if (qualifierType == "location")
4067 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004068 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4069 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004070 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004071 else if (qualifierType == "yuv" && isExtensionEnabled(TExtension::EXT_YUV_target) &&
Andrei Volykhina5527072017-03-22 16:46:30 +03004072 mShaderType == GL_FRAGMENT_SHADER)
4073 {
4074 qualifier.yuv = true;
4075 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004076 else if (qualifierType == "rgba32f")
4077 {
4078 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4079 qualifier.imageInternalFormat = EiifRGBA32F;
4080 }
4081 else if (qualifierType == "rgba16f")
4082 {
4083 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4084 qualifier.imageInternalFormat = EiifRGBA16F;
4085 }
4086 else if (qualifierType == "r32f")
4087 {
4088 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4089 qualifier.imageInternalFormat = EiifR32F;
4090 }
4091 else if (qualifierType == "rgba8")
4092 {
4093 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4094 qualifier.imageInternalFormat = EiifRGBA8;
4095 }
4096 else if (qualifierType == "rgba8_snorm")
4097 {
4098 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4099 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4100 }
4101 else if (qualifierType == "rgba32i")
4102 {
4103 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4104 qualifier.imageInternalFormat = EiifRGBA32I;
4105 }
4106 else if (qualifierType == "rgba16i")
4107 {
4108 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4109 qualifier.imageInternalFormat = EiifRGBA16I;
4110 }
4111 else if (qualifierType == "rgba8i")
4112 {
4113 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4114 qualifier.imageInternalFormat = EiifRGBA8I;
4115 }
4116 else if (qualifierType == "r32i")
4117 {
4118 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4119 qualifier.imageInternalFormat = EiifR32I;
4120 }
4121 else if (qualifierType == "rgba32ui")
4122 {
4123 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4124 qualifier.imageInternalFormat = EiifRGBA32UI;
4125 }
4126 else if (qualifierType == "rgba16ui")
4127 {
4128 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4129 qualifier.imageInternalFormat = EiifRGBA16UI;
4130 }
4131 else if (qualifierType == "rgba8ui")
4132 {
4133 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4134 qualifier.imageInternalFormat = EiifRGBA8UI;
4135 }
4136 else if (qualifierType == "r32ui")
4137 {
4138 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4139 qualifier.imageInternalFormat = EiifR32UI;
4140 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004141 else if (qualifierType == "points" && 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 = EptPoints;
4146 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004147 else if (qualifierType == "lines" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004148 mShaderType == GL_GEOMETRY_SHADER_OES)
4149 {
4150 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4151 qualifier.primitiveType = EptLines;
4152 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004153 else if (qualifierType == "lines_adjacency" &&
4154 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 = EptLinesAdjacency;
4159 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004160 else if (qualifierType == "triangles" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004161 mShaderType == GL_GEOMETRY_SHADER_OES)
4162 {
4163 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4164 qualifier.primitiveType = EptTriangles;
4165 }
4166 else if (qualifierType == "triangles_adjacency" &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004167 isExtensionEnabled(TExtension::OES_geometry_shader) &&
4168 mShaderType == GL_GEOMETRY_SHADER_OES)
Shaob5cc1192017-07-06 10:47:20 +08004169 {
4170 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4171 qualifier.primitiveType = EptTrianglesAdjacency;
4172 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004173 else if (qualifierType == "line_strip" && isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004174 mShaderType == GL_GEOMETRY_SHADER_OES)
4175 {
4176 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4177 qualifier.primitiveType = EptLineStrip;
4178 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004179 else if (qualifierType == "triangle_strip" &&
4180 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004181 mShaderType == GL_GEOMETRY_SHADER_OES)
4182 {
4183 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4184 qualifier.primitiveType = EptTriangleStrip;
4185 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004186
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004187 else
4188 {
4189 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004190 }
4191
Jamie Madilla5efff92013-06-06 11:56:47 -04004192 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004193}
4194
Martin Radev802abe02016-08-04 17:48:32 +03004195void TParseContext::parseLocalSize(const TString &qualifierType,
4196 const TSourceLoc &qualifierTypeLine,
4197 int intValue,
4198 const TSourceLoc &intValueLine,
4199 const std::string &intValueString,
4200 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004201 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004202{
Olli Etuaho856c4972016-08-08 11:38:39 +03004203 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004204 if (intValue < 1)
4205 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004206 std::stringstream reasonStream;
4207 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4208 std::string reason = reasonStream.str();
4209 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004210 }
4211 (*localSize)[index] = intValue;
4212}
4213
Olli Etuaho09b04a22016-12-15 13:30:26 +00004214void TParseContext::parseNumViews(int intValue,
4215 const TSourceLoc &intValueLine,
4216 const std::string &intValueString,
4217 int *numViews)
4218{
4219 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4220 // specification.
4221 if (intValue < 1)
4222 {
4223 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4224 }
4225 *numViews = intValue;
4226}
4227
Shaob5cc1192017-07-06 10:47:20 +08004228void TParseContext::parseInvocations(int intValue,
4229 const TSourceLoc &intValueLine,
4230 const std::string &intValueString,
4231 int *numInvocations)
4232{
4233 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4234 // it doesn't make sense to accept invocations <= 0.
4235 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4236 {
4237 error(intValueLine,
4238 "out of range: invocations must be in the range of [1, "
4239 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4240 intValueString.c_str());
4241 }
4242 else
4243 {
4244 *numInvocations = intValue;
4245 }
4246}
4247
4248void TParseContext::parseMaxVertices(int intValue,
4249 const TSourceLoc &intValueLine,
4250 const std::string &intValueString,
4251 int *maxVertices)
4252{
4253 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4254 // it doesn't make sense to accept max_vertices < 0.
4255 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4256 {
4257 error(
4258 intValueLine,
4259 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4260 intValueString.c_str());
4261 }
4262 else
4263 {
4264 *maxVertices = intValue;
4265 }
4266}
4267
Jamie Madillb98c3a82015-07-23 14:26:04 -04004268TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4269 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004270 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304271 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004272{
Martin Radev802abe02016-08-04 17:48:32 +03004273 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004274
Martin Radev802abe02016-08-04 17:48:32 +03004275 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004276
Martin Radev802abe02016-08-04 17:48:32 +03004277 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004278 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004279 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004280 if (intValue < 0)
4281 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004282 error(intValueLine, "out of range: location must be non-negative",
4283 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004284 }
4285 else
4286 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004287 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004288 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004289 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004290 }
Olli Etuaho43364892017-02-13 16:00:12 +00004291 else if (qualifierType == "binding")
4292 {
4293 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4294 if (intValue < 0)
4295 {
4296 error(intValueLine, "out of range: binding must be non-negative",
4297 intValueString.c_str());
4298 }
4299 else
4300 {
4301 qualifier.binding = intValue;
4302 }
4303 }
jchen104cdac9e2017-05-08 11:01:20 +08004304 else if (qualifierType == "offset")
4305 {
4306 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4307 if (intValue < 0)
4308 {
4309 error(intValueLine, "out of range: offset must be non-negative",
4310 intValueString.c_str());
4311 }
4312 else
4313 {
4314 qualifier.offset = intValue;
4315 }
4316 }
Martin Radev802abe02016-08-04 17:48:32 +03004317 else if (qualifierType == "local_size_x")
4318 {
4319 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4320 &qualifier.localSize);
4321 }
4322 else if (qualifierType == "local_size_y")
4323 {
4324 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4325 &qualifier.localSize);
4326 }
4327 else if (qualifierType == "local_size_z")
4328 {
4329 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4330 &qualifier.localSize);
4331 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004332 else if (qualifierType == "num_views" && isExtensionEnabled(TExtension::OVR_multiview) &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004333 mShaderType == GL_VERTEX_SHADER)
4334 {
4335 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4336 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004337 else if (qualifierType == "invocations" &&
4338 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004339 mShaderType == GL_GEOMETRY_SHADER_OES)
4340 {
4341 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4342 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004343 else if (qualifierType == "max_vertices" &&
4344 isExtensionEnabled(TExtension::OES_geometry_shader) &&
Shaob5cc1192017-07-06 10:47:20 +08004345 mShaderType == GL_GEOMETRY_SHADER_OES)
4346 {
4347 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4348 }
4349
Martin Radev802abe02016-08-04 17:48:32 +03004350 else
4351 {
4352 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004353 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004354
Jamie Madilla5efff92013-06-06 11:56:47 -04004355 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004356}
4357
Olli Etuaho613b9592016-09-05 12:05:53 +03004358TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4359{
4360 return new TTypeQualifierBuilder(
4361 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4362 mShaderVersion);
4363}
4364
Olli Etuahocce89652017-06-19 16:04:09 +03004365TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4366 const TSourceLoc &loc)
4367{
4368 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4369 return new TStorageQualifierWrapper(qualifier, loc);
4370}
4371
4372TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4373{
4374 if (getShaderType() == GL_VERTEX_SHADER)
4375 {
4376 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4377 }
4378 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4379}
4380
4381TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4382{
4383 if (declaringFunction())
4384 {
4385 return new TStorageQualifierWrapper(EvqIn, loc);
4386 }
Shaob5cc1192017-07-06 10:47:20 +08004387
4388 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004389 {
Shaob5cc1192017-07-06 10:47:20 +08004390 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004391 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004392 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004393 {
4394 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4395 }
4396 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004397 }
Shaob5cc1192017-07-06 10:47:20 +08004398 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004399 {
Shaob5cc1192017-07-06 10:47:20 +08004400 if (mShaderVersion < 300)
4401 {
4402 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4403 }
4404 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004405 }
Shaob5cc1192017-07-06 10:47:20 +08004406 case GL_COMPUTE_SHADER:
4407 {
4408 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4409 }
4410 case GL_GEOMETRY_SHADER_OES:
4411 {
4412 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4413 }
4414 default:
4415 {
4416 UNREACHABLE();
4417 return new TStorageQualifierWrapper(EvqLast, loc);
4418 }
Olli Etuahocce89652017-06-19 16:04:09 +03004419 }
Olli Etuahocce89652017-06-19 16:04:09 +03004420}
4421
4422TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4423{
4424 if (declaringFunction())
4425 {
4426 return new TStorageQualifierWrapper(EvqOut, loc);
4427 }
Shaob5cc1192017-07-06 10:47:20 +08004428 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004429 {
Shaob5cc1192017-07-06 10:47:20 +08004430 case GL_VERTEX_SHADER:
4431 {
4432 if (mShaderVersion < 300)
4433 {
4434 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4435 }
4436 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4437 }
4438 case GL_FRAGMENT_SHADER:
4439 {
4440 if (mShaderVersion < 300)
4441 {
4442 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4443 }
4444 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4445 }
4446 case GL_COMPUTE_SHADER:
4447 {
4448 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4449 return new TStorageQualifierWrapper(EvqLast, loc);
4450 }
4451 case GL_GEOMETRY_SHADER_OES:
4452 {
4453 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4454 }
4455 default:
4456 {
4457 UNREACHABLE();
4458 return new TStorageQualifierWrapper(EvqLast, loc);
4459 }
Olli Etuahocce89652017-06-19 16:04:09 +03004460 }
Olli Etuahocce89652017-06-19 16:04:09 +03004461}
4462
4463TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4464{
4465 if (!declaringFunction())
4466 {
4467 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4468 }
4469 return new TStorageQualifierWrapper(EvqInOut, loc);
4470}
4471
Jamie Madillb98c3a82015-07-23 14:26:04 -04004472TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004473 TLayoutQualifier rightQualifier,
4474 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004475{
Martin Radevc28888b2016-07-22 15:27:42 +03004476 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004477 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004478}
4479
Olli Etuahocce89652017-06-19 16:04:09 +03004480TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4481{
4482 checkIsNotReserved(loc, *identifier);
4483 TType *type = new TType(EbtVoid, EbpUndefined);
4484 return new TField(type, identifier, loc);
4485}
4486
4487TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4488 const TSourceLoc &loc,
4489 TIntermTyped *arraySize,
4490 const TSourceLoc &arraySizeLoc)
4491{
4492 checkIsNotReserved(loc, *identifier);
4493
4494 TType *type = new TType(EbtVoid, EbpUndefined);
4495 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004496 type->makeArray(size);
Olli Etuahocce89652017-06-19 16:04:09 +03004497
4498 return new TField(type, identifier, loc);
4499}
4500
Olli Etuaho4de340a2016-12-16 09:32:03 +00004501TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4502 const TFieldList *newlyAddedFields,
4503 const TSourceLoc &location)
4504{
4505 for (TField *field : *newlyAddedFields)
4506 {
4507 for (TField *oldField : *processedFields)
4508 {
4509 if (oldField->name() == field->name())
4510 {
4511 error(location, "duplicate field name in structure", field->name().c_str());
4512 }
4513 }
4514 processedFields->push_back(field);
4515 }
4516 return processedFields;
4517}
4518
Martin Radev70866b82016-07-22 15:27:42 +03004519TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4520 const TTypeQualifierBuilder &typeQualifierBuilder,
4521 TPublicType *typeSpecifier,
4522 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004523{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004524 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004525
Martin Radev70866b82016-07-22 15:27:42 +03004526 typeSpecifier->qualifier = typeQualifier.qualifier;
4527 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004528 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004529 typeSpecifier->invariant = typeQualifier.invariant;
4530 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304531 {
Martin Radev70866b82016-07-22 15:27:42 +03004532 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004533 }
Martin Radev70866b82016-07-22 15:27:42 +03004534 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004535}
4536
Jamie Madillb98c3a82015-07-23 14:26:04 -04004537TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004538 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004539{
Martin Radev4a9cd802016-09-01 16:51:51 +03004540 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4541 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004542
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004543 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4544 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004545
Martin Radev4a9cd802016-09-01 16:51:51 +03004546 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004547
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004548 for (unsigned int i = 0; i < declaratorList->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304549 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004550 auto declaratorArraySizes = (*declaratorList)[i]->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004551 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004552 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304553 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004554 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004555 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004556
4557 TType *type = (*declaratorList)[i]->type();
4558 *type = TType(typeSpecifier);
4559 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304560 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004561 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004562 }
4563
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004564 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*declaratorList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004565 }
4566
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004567 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004568}
4569
Martin Radev4a9cd802016-09-01 16:51:51 +03004570TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4571 const TSourceLoc &nameLine,
4572 const TString *structName,
4573 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004574{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004575 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004576
Jamie Madill9b820842015-02-12 10:40:10 -05004577 // Store a bool in the struct if we're at global scope, to allow us to
4578 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004579 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004580
Jamie Madill98493dd2013-07-08 14:39:03 -04004581 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004582 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004583 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004584 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304585 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004586 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004587 }
4588 }
4589
4590 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004591 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004592 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004593 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004594 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004595 switch (qualifier)
4596 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004597 case EvqGlobal:
4598 case EvqTemporary:
4599 break;
4600 default:
4601 error(field.line(), "invalid qualifier on struct member",
4602 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004603 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004604 }
Martin Radev70866b82016-07-22 15:27:42 +03004605 if (field.type()->isInvariant())
4606 {
4607 error(field.line(), "invalid qualifier on struct member", "invariant");
4608 }
jchen104cdac9e2017-05-08 11:01:20 +08004609 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4610 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004611 {
4612 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4613 }
4614
Olli Etuaho43364892017-02-13 16:00:12 +00004615 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4616
4617 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004618
4619 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004620 }
4621
Martin Radev4a9cd802016-09-01 16:51:51 +03004622 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004623 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004624 exitStructDeclaration();
4625
Martin Radev4a9cd802016-09-01 16:51:51 +03004626 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004627}
4628
Jamie Madillb98c3a82015-07-23 14:26:04 -04004629TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004630 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004631 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004632{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004633 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004634 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004635 init->isVector())
4636 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004637 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4638 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004639 return nullptr;
4640 }
4641
Olli Etuaho923ecef2017-10-11 12:01:38 +03004642 ASSERT(statementList);
4643 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004644 {
Olli Etuaho923ecef2017-10-11 12:01:38 +03004645 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004646 }
4647
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004648 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4649 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004650 return node;
4651}
4652
4653TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4654{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004655 if (mSwitchNestingLevel == 0)
4656 {
4657 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004658 return nullptr;
4659 }
4660 if (condition == nullptr)
4661 {
4662 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004663 return nullptr;
4664 }
4665 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004666 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004667 {
4668 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004669 }
4670 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004671 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4672 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4673 // fold in case labels.
4674 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004675 {
4676 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004677 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004678 TIntermCase *node = new TIntermCase(condition);
4679 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004680 return node;
4681}
4682
4683TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4684{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004685 if (mSwitchNestingLevel == 0)
4686 {
4687 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004688 return nullptr;
4689 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004690 TIntermCase *node = new TIntermCase(nullptr);
4691 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004692 return node;
4693}
4694
Jamie Madillb98c3a82015-07-23 14:26:04 -04004695TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4696 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004697 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004698{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004699 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004700
4701 switch (op)
4702 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004703 case EOpLogicalNot:
4704 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4705 child->isVector())
4706 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004707 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004708 return nullptr;
4709 }
4710 break;
4711 case EOpBitwiseNot:
4712 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4713 child->isMatrix() || child->isArray())
4714 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004715 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004716 return nullptr;
4717 }
4718 break;
4719 case EOpPostIncrement:
4720 case EOpPreIncrement:
4721 case EOpPostDecrement:
4722 case EOpPreDecrement:
4723 case EOpNegative:
4724 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004725 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4726 child->getBasicType() == EbtBool || child->isArray() ||
4727 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004728 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004729 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004730 return nullptr;
4731 }
4732 // Operators for built-ins are already type checked against their prototype.
4733 default:
4734 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004735 }
4736
Jiajia Qinbc585152017-06-23 15:42:17 +08004737 if (child->getMemoryQualifier().writeonly)
4738 {
4739 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4740 return nullptr;
4741 }
4742
Olli Etuahof119a262016-08-19 15:54:22 +03004743 TIntermUnary *node = new TIntermUnary(op, child);
4744 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004745
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004746 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004747}
4748
Olli Etuaho09b22472015-02-11 11:47:26 +02004749TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4750{
Olli Etuahocce89652017-06-19 16:04:09 +03004751 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004752 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004753 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004754 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004755 return child;
4756 }
4757 return node;
4758}
4759
Jamie Madillb98c3a82015-07-23 14:26:04 -04004760TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4761 TIntermTyped *child,
4762 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004763{
Olli Etuaho856c4972016-08-08 11:38:39 +03004764 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004765 return addUnaryMath(op, child, loc);
4766}
4767
Jamie Madillb98c3a82015-07-23 14:26:04 -04004768bool TParseContext::binaryOpCommonCheck(TOperator op,
4769 TIntermTyped *left,
4770 TIntermTyped *right,
4771 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004772{
jchen10b4cf5652017-05-05 18:51:17 +08004773 // Check opaque types are not allowed to be operands in expressions other than array indexing
4774 // and structure member selection.
4775 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4776 {
4777 switch (op)
4778 {
4779 case EOpIndexDirect:
4780 case EOpIndexIndirect:
4781 break;
4782 case EOpIndexDirectStruct:
4783 UNREACHABLE();
4784
4785 default:
4786 error(loc, "Invalid operation for variables with an opaque type",
4787 GetOperatorString(op));
4788 return false;
4789 }
4790 }
jchen10cc2a10e2017-05-03 14:05:12 +08004791
Jiajia Qinbc585152017-06-23 15:42:17 +08004792 if (right->getMemoryQualifier().writeonly)
4793 {
4794 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4795 return false;
4796 }
4797
4798 if (left->getMemoryQualifier().writeonly)
4799 {
4800 switch (op)
4801 {
4802 case EOpAssign:
4803 case EOpInitialize:
4804 case EOpIndexDirect:
4805 case EOpIndexIndirect:
4806 case EOpIndexDirectStruct:
4807 case EOpIndexDirectInterfaceBlock:
4808 break;
4809 default:
4810 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4811 return false;
4812 }
4813 }
4814
Olli Etuaho244be012016-08-18 15:26:02 +03004815 if (left->getType().getStruct() || right->getType().getStruct())
4816 {
4817 switch (op)
4818 {
4819 case EOpIndexDirectStruct:
4820 ASSERT(left->getType().getStruct());
4821 break;
4822 case EOpEqual:
4823 case EOpNotEqual:
4824 case EOpAssign:
4825 case EOpInitialize:
4826 if (left->getType() != right->getType())
4827 {
4828 return false;
4829 }
4830 break;
4831 default:
4832 error(loc, "Invalid operation for structs", GetOperatorString(op));
4833 return false;
4834 }
4835 }
4836
Olli Etuaho94050052017-05-08 14:17:44 +03004837 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4838 {
4839 switch (op)
4840 {
4841 case EOpIndexDirectInterfaceBlock:
4842 ASSERT(left->getType().getInterfaceBlock());
4843 break;
4844 default:
4845 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4846 return false;
4847 }
4848 }
4849
Olli Etuahod6b14282015-03-17 14:31:35 +02004850 if (left->isArray() || right->isArray())
4851 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004852 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004853 {
4854 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4855 return false;
4856 }
4857
4858 if (left->isArray() != right->isArray())
4859 {
4860 error(loc, "array / non-array mismatch", GetOperatorString(op));
4861 return false;
4862 }
4863
4864 switch (op)
4865 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004866 case EOpEqual:
4867 case EOpNotEqual:
4868 case EOpAssign:
4869 case EOpInitialize:
4870 break;
4871 default:
4872 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4873 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004874 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004875 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004876 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004877 {
4878 error(loc, "array size mismatch", GetOperatorString(op));
4879 return false;
4880 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004881 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004882
4883 // Check ops which require integer / ivec parameters
4884 bool isBitShift = false;
4885 switch (op)
4886 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004887 case EOpBitShiftLeft:
4888 case EOpBitShiftRight:
4889 case EOpBitShiftLeftAssign:
4890 case EOpBitShiftRightAssign:
4891 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4892 // check that the basic type is an integer type.
4893 isBitShift = true;
4894 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4895 {
4896 return false;
4897 }
4898 break;
4899 case EOpBitwiseAnd:
4900 case EOpBitwiseXor:
4901 case EOpBitwiseOr:
4902 case EOpBitwiseAndAssign:
4903 case EOpBitwiseXorAssign:
4904 case EOpBitwiseOrAssign:
4905 // It is enough to check the type of only one operand, since later it
4906 // is checked that the operand types match.
4907 if (!IsInteger(left->getBasicType()))
4908 {
4909 return false;
4910 }
4911 break;
4912 default:
4913 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004914 }
4915
4916 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4917 // So the basic type should usually match.
4918 if (!isBitShift && left->getBasicType() != right->getBasicType())
4919 {
4920 return false;
4921 }
4922
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004923 // Check that:
4924 // 1. Type sizes match exactly on ops that require that.
4925 // 2. Restrictions for structs that contain arrays or samplers are respected.
4926 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004927 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004928 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004929 case EOpAssign:
4930 case EOpInitialize:
4931 case EOpEqual:
4932 case EOpNotEqual:
4933 // ESSL 1.00 sections 5.7, 5.8, 5.9
4934 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4935 {
4936 error(loc, "undefined operation for structs containing arrays",
4937 GetOperatorString(op));
4938 return false;
4939 }
4940 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4941 // we interpret the spec so that this extends to structs containing samplers,
4942 // similarly to ESSL 1.00 spec.
4943 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4944 left->getType().isStructureContainingSamplers())
4945 {
4946 error(loc, "undefined operation for structs containing samplers",
4947 GetOperatorString(op));
4948 return false;
4949 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004950
Olli Etuahoe1805592017-01-02 16:41:20 +00004951 if ((left->getNominalSize() != right->getNominalSize()) ||
4952 (left->getSecondarySize() != right->getSecondarySize()))
4953 {
4954 error(loc, "dimension mismatch", GetOperatorString(op));
4955 return false;
4956 }
4957 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004958 case EOpLessThan:
4959 case EOpGreaterThan:
4960 case EOpLessThanEqual:
4961 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004962 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004963 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004964 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004965 return false;
4966 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004967 break;
4968 case EOpAdd:
4969 case EOpSub:
4970 case EOpDiv:
4971 case EOpIMod:
4972 case EOpBitShiftLeft:
4973 case EOpBitShiftRight:
4974 case EOpBitwiseAnd:
4975 case EOpBitwiseXor:
4976 case EOpBitwiseOr:
4977 case EOpAddAssign:
4978 case EOpSubAssign:
4979 case EOpDivAssign:
4980 case EOpIModAssign:
4981 case EOpBitShiftLeftAssign:
4982 case EOpBitShiftRightAssign:
4983 case EOpBitwiseAndAssign:
4984 case EOpBitwiseXorAssign:
4985 case EOpBitwiseOrAssign:
4986 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4987 {
4988 return false;
4989 }
4990
4991 // Are the sizes compatible?
4992 if (left->getNominalSize() != right->getNominalSize() ||
4993 left->getSecondarySize() != right->getSecondarySize())
4994 {
4995 // If the nominal sizes of operands do not match:
4996 // One of them must be a scalar.
4997 if (!left->isScalar() && !right->isScalar())
4998 return false;
4999
5000 // In the case of compound assignment other than multiply-assign,
5001 // the right side needs to be a scalar. Otherwise a vector/matrix
5002 // would be assigned to a scalar. A scalar can't be shifted by a
5003 // vector either.
5004 if (!right->isScalar() &&
5005 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5006 return false;
5007 }
5008 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005009 default:
5010 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005011 }
5012
Olli Etuahod6b14282015-03-17 14:31:35 +02005013 return true;
5014}
5015
Olli Etuaho1dded802016-08-18 18:13:13 +03005016bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5017 const TType &left,
5018 const TType &right)
5019{
5020 switch (op)
5021 {
5022 case EOpMul:
5023 case EOpMulAssign:
5024 return left.getNominalSize() == right.getNominalSize() &&
5025 left.getSecondarySize() == right.getSecondarySize();
5026 case EOpVectorTimesScalar:
5027 return true;
5028 case EOpVectorTimesScalarAssign:
5029 ASSERT(!left.isMatrix() && !right.isMatrix());
5030 return left.isVector() && !right.isVector();
5031 case EOpVectorTimesMatrix:
5032 return left.getNominalSize() == right.getRows();
5033 case EOpVectorTimesMatrixAssign:
5034 ASSERT(!left.isMatrix() && right.isMatrix());
5035 return left.isVector() && left.getNominalSize() == right.getRows() &&
5036 left.getNominalSize() == right.getCols();
5037 case EOpMatrixTimesVector:
5038 return left.getCols() == right.getNominalSize();
5039 case EOpMatrixTimesScalar:
5040 return true;
5041 case EOpMatrixTimesScalarAssign:
5042 ASSERT(left.isMatrix() && !right.isMatrix());
5043 return !right.isVector();
5044 case EOpMatrixTimesMatrix:
5045 return left.getCols() == right.getRows();
5046 case EOpMatrixTimesMatrixAssign:
5047 ASSERT(left.isMatrix() && right.isMatrix());
5048 // We need to check two things:
5049 // 1. The matrix multiplication step is valid.
5050 // 2. The result will have the same number of columns as the lvalue.
5051 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5052
5053 default:
5054 UNREACHABLE();
5055 return false;
5056 }
5057}
5058
Jamie Madillb98c3a82015-07-23 14:26:04 -04005059TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5060 TIntermTyped *left,
5061 TIntermTyped *right,
5062 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005063{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005064 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005065 return nullptr;
5066
Olli Etuahofc1806e2015-03-17 13:03:11 +02005067 switch (op)
5068 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005069 case EOpEqual:
5070 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005071 case EOpLessThan:
5072 case EOpGreaterThan:
5073 case EOpLessThanEqual:
5074 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005075 break;
5076 case EOpLogicalOr:
5077 case EOpLogicalXor:
5078 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005079 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5080 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005081 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005082 {
5083 return nullptr;
5084 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005085 // Basic types matching should have been already checked.
5086 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005087 break;
5088 case EOpAdd:
5089 case EOpSub:
5090 case EOpDiv:
5091 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005092 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5093 !right->getType().getStruct());
5094 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005095 {
5096 return nullptr;
5097 }
5098 break;
5099 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005100 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5101 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005102 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005103 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005104 {
5105 return nullptr;
5106 }
5107 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005108 default:
5109 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005110 }
5111
Olli Etuaho1dded802016-08-18 18:13:13 +03005112 if (op == EOpMul)
5113 {
5114 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5115 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5116 {
5117 return nullptr;
5118 }
5119 }
5120
Olli Etuaho3fdec912016-08-18 15:08:06 +03005121 TIntermBinary *node = new TIntermBinary(op, left, right);
5122 node->setLine(loc);
5123
Olli Etuaho3fdec912016-08-18 15:08:06 +03005124 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005125 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005126}
5127
Jamie Madillb98c3a82015-07-23 14:26:04 -04005128TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5129 TIntermTyped *left,
5130 TIntermTyped *right,
5131 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005132{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005133 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005134 if (node == 0)
5135 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005136 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5137 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005138 return left;
5139 }
5140 return node;
5141}
5142
Jamie Madillb98c3a82015-07-23 14:26:04 -04005143TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5144 TIntermTyped *left,
5145 TIntermTyped *right,
5146 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005147{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005148 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005149 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005150 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005151 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5152 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005153 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005154 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005155 }
5156 return node;
5157}
5158
Olli Etuaho13389b62016-10-16 11:48:18 +01005159TIntermBinary *TParseContext::createAssign(TOperator op,
5160 TIntermTyped *left,
5161 TIntermTyped *right,
5162 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005163{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005164 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005165 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005166 if (op == EOpMulAssign)
5167 {
5168 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5169 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5170 {
5171 return nullptr;
5172 }
5173 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005174 TIntermBinary *node = new TIntermBinary(op, left, right);
5175 node->setLine(loc);
5176
Olli Etuaho3fdec912016-08-18 15:08:06 +03005177 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005178 }
5179 return nullptr;
5180}
5181
Jamie Madillb98c3a82015-07-23 14:26:04 -04005182TIntermTyped *TParseContext::addAssign(TOperator op,
5183 TIntermTyped *left,
5184 TIntermTyped *right,
5185 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005186{
Olli Etuahocce89652017-06-19 16:04:09 +03005187 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005188 TIntermTyped *node = createAssign(op, left, right, loc);
5189 if (node == nullptr)
5190 {
5191 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005192 return left;
5193 }
5194 return node;
5195}
5196
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005197TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5198 TIntermTyped *right,
5199 const TSourceLoc &loc)
5200{
Corentin Wallez0d959252016-07-12 17:26:32 -04005201 // WebGL2 section 5.26, the following results in an error:
5202 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005203 if (mShaderSpec == SH_WEBGL2_SPEC &&
5204 (left->isArray() || left->getBasicType() == EbtVoid ||
5205 left->getType().isStructureContainingArrays() || right->isArray() ||
5206 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005207 {
5208 error(loc,
5209 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5210 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005211 }
5212
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005213 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5214 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5215 commaNode->getTypePointer()->setQualifier(resultQualifier);
5216 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005217}
5218
Olli Etuaho49300862015-02-20 14:54:49 +02005219TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5220{
5221 switch (op)
5222 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005223 case EOpContinue:
5224 if (mLoopNestingLevel <= 0)
5225 {
5226 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005227 }
5228 break;
5229 case EOpBreak:
5230 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5231 {
5232 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005233 }
5234 break;
5235 case EOpReturn:
5236 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5237 {
5238 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005239 }
5240 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005241 case EOpKill:
5242 if (mShaderType != GL_FRAGMENT_SHADER)
5243 {
5244 error(loc, "discard supported in fragment shaders only", "discard");
5245 }
5246 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005247 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005248 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005249 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005250 }
Olli Etuahocce89652017-06-19 16:04:09 +03005251 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005252}
5253
Jamie Madillb98c3a82015-07-23 14:26:04 -04005254TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005255 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005256 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005257{
Olli Etuahocce89652017-06-19 16:04:09 +03005258 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005259 {
Olli Etuahocce89652017-06-19 16:04:09 +03005260 ASSERT(op == EOpReturn);
5261 mFunctionReturnsValue = true;
5262 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5263 {
5264 error(loc, "void function cannot return a value", "return");
5265 }
5266 else if (*mCurrentFunctionType != expression->getType())
5267 {
5268 error(loc, "function return is not matching type:", "return");
5269 }
Olli Etuaho49300862015-02-20 14:54:49 +02005270 }
Olli Etuahocce89652017-06-19 16:04:09 +03005271 TIntermBranch *node = new TIntermBranch(op, expression);
5272 node->setLine(loc);
5273 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005274}
5275
Martin Radev84aa2dc2017-09-11 15:51:02 +03005276void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5277{
5278 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5279 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5280 bool isTextureGather = (name == "textureGather");
5281 bool isTextureGatherOffset = (name == "textureGatherOffset");
5282 if (isTextureGather || isTextureGatherOffset)
5283 {
5284 TIntermNode *componentNode = nullptr;
5285 TIntermSequence *arguments = functionCall->getSequence();
5286 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5287 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5288 ASSERT(sampler != nullptr);
5289 switch (sampler->getBasicType())
5290 {
5291 case EbtSampler2D:
5292 case EbtISampler2D:
5293 case EbtUSampler2D:
5294 case EbtSampler2DArray:
5295 case EbtISampler2DArray:
5296 case EbtUSampler2DArray:
5297 if ((isTextureGather && arguments->size() == 3u) ||
5298 (isTextureGatherOffset && arguments->size() == 4u))
5299 {
5300 componentNode = arguments->back();
5301 }
5302 break;
5303 case EbtSamplerCube:
5304 case EbtISamplerCube:
5305 case EbtUSamplerCube:
5306 ASSERT(!isTextureGatherOffset);
5307 if (arguments->size() == 3u)
5308 {
5309 componentNode = arguments->back();
5310 }
5311 break;
5312 case EbtSampler2DShadow:
5313 case EbtSampler2DArrayShadow:
5314 case EbtSamplerCubeShadow:
5315 break;
5316 default:
5317 UNREACHABLE();
5318 break;
5319 }
5320 if (componentNode)
5321 {
5322 const TIntermConstantUnion *componentConstantUnion =
5323 componentNode->getAsConstantUnion();
5324 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5325 {
5326 error(functionCall->getLine(), "Texture component must be a constant expression",
5327 name.c_str());
5328 }
5329 else
5330 {
5331 int component = componentConstantUnion->getIConst(0);
5332 if (component < 0 || component > 3)
5333 {
5334 error(functionCall->getLine(), "Component must be in the range [0;3]",
5335 name.c_str());
5336 }
5337 }
5338 }
5339 }
5340}
5341
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005342void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5343{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005344 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005345 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005346 TIntermNode *offset = nullptr;
5347 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005348 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005349 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5350 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5351 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005352 {
5353 offset = arguments->back();
5354 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005355 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005356 {
5357 // A bias parameter might follow the offset parameter.
5358 ASSERT(arguments->size() >= 3);
5359 offset = (*arguments)[2];
5360 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005361 else if (name == "textureGatherOffset")
5362 {
5363 ASSERT(arguments->size() >= 3u);
5364 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5365 ASSERT(sampler != nullptr);
5366 switch (sampler->getBasicType())
5367 {
5368 case EbtSampler2D:
5369 case EbtISampler2D:
5370 case EbtUSampler2D:
5371 case EbtSampler2DArray:
5372 case EbtISampler2DArray:
5373 case EbtUSampler2DArray:
5374 offset = (*arguments)[2];
5375 break;
5376 case EbtSampler2DShadow:
5377 case EbtSampler2DArrayShadow:
5378 offset = (*arguments)[3];
5379 break;
5380 default:
5381 UNREACHABLE();
5382 break;
5383 }
5384 useTextureGatherOffsetConstraints = true;
5385 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005386 if (offset != nullptr)
5387 {
5388 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5389 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5390 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005391 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005392 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005393 }
5394 else
5395 {
5396 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5397 size_t size = offsetConstantUnion->getType().getObjectSize();
5398 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005399 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5400 : mMinProgramTexelOffset;
5401 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5402 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005403 for (size_t i = 0u; i < size; ++i)
5404 {
5405 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005406 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005407 {
5408 std::stringstream tokenStream;
5409 tokenStream << offsetValue;
5410 std::string token = tokenStream.str();
5411 error(offset->getLine(), "Texture offset value out of valid range",
5412 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005413 }
5414 }
5415 }
5416 }
5417}
5418
Martin Radev2cc85b32016-08-05 16:22:53 +03005419// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5420void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5421{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005422 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005423 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5424
5425 if (name.compare(0, 5, "image") == 0)
5426 {
5427 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005428 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005429
Olli Etuaho485eefd2017-02-14 17:40:06 +00005430 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005431
5432 if (name.compare(5, 5, "Store") == 0)
5433 {
5434 if (memoryQualifier.readonly)
5435 {
5436 error(imageNode->getLine(),
5437 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005438 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005439 }
5440 }
5441 else if (name.compare(5, 4, "Load") == 0)
5442 {
5443 if (memoryQualifier.writeonly)
5444 {
5445 error(imageNode->getLine(),
5446 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005447 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005448 }
5449 }
5450 }
5451}
5452
5453// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5454void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5455 const TFunction *functionDefinition,
5456 const TIntermAggregate *functionCall)
5457{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005458 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005459
5460 const TIntermSequence &arguments = *functionCall->getSequence();
5461
5462 ASSERT(functionDefinition->getParamCount() == arguments.size());
5463
5464 for (size_t i = 0; i < arguments.size(); ++i)
5465 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005466 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5467 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005468 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5469 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5470
5471 if (IsImage(functionArgumentType.getBasicType()))
5472 {
5473 const TMemoryQualifier &functionArgumentMemoryQualifier =
5474 functionArgumentType.getMemoryQualifier();
5475 const TMemoryQualifier &functionParameterMemoryQualifier =
5476 functionParameterType.getMemoryQualifier();
5477 if (functionArgumentMemoryQualifier.readonly &&
5478 !functionParameterMemoryQualifier.readonly)
5479 {
5480 error(functionCall->getLine(),
5481 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005482 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005483 }
5484
5485 if (functionArgumentMemoryQualifier.writeonly &&
5486 !functionParameterMemoryQualifier.writeonly)
5487 {
5488 error(functionCall->getLine(),
5489 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005490 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005491 }
Martin Radev049edfa2016-11-11 14:35:37 +02005492
5493 if (functionArgumentMemoryQualifier.coherent &&
5494 !functionParameterMemoryQualifier.coherent)
5495 {
5496 error(functionCall->getLine(),
5497 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005498 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005499 }
5500
5501 if (functionArgumentMemoryQualifier.volatileQualifier &&
5502 !functionParameterMemoryQualifier.volatileQualifier)
5503 {
5504 error(functionCall->getLine(),
5505 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005506 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005507 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005508 }
5509 }
5510}
5511
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005512TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005513{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005514 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005515}
5516
5517TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005518 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005519 TIntermNode *thisNode,
5520 const TSourceLoc &loc)
5521{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005522 if (thisNode != nullptr)
5523 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005524 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005525 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005526
5527 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005528 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005529 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005530 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005531 }
5532 else
5533 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005534 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005535 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5536 }
5537}
5538
5539TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5540 TIntermSequence *arguments,
5541 TIntermNode *thisNode,
5542 const TSourceLoc &loc)
5543{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005544 TIntermTyped *typedThis = thisNode->getAsTyped();
5545 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5546 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5547 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5548 // So accessing fnCall->getName() below is safe.
5549 if (fnCall->getName() != "length")
5550 {
5551 error(loc, "invalid method", fnCall->getName().c_str());
5552 }
5553 else if (!arguments->empty())
5554 {
5555 error(loc, "method takes no parameters", "length");
5556 }
5557 else if (typedThis == nullptr || !typedThis->isArray())
5558 {
5559 error(loc, "length can only be called on arrays", "length");
5560 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005561 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5562 mGeometryShaderInputPrimitiveType == EptUndefined)
5563 {
5564 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5565 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005566 else
5567 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005568 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5569 node->setLine(loc);
5570 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005571 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005572 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005573}
5574
5575TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5576 TIntermSequence *arguments,
5577 const TSourceLoc &loc)
5578{
5579 // First find by unmangled name to check whether the function name has been
5580 // hidden by a variable name or struct typename.
5581 // If a function is found, check for one with a matching argument list.
5582 bool builtIn;
5583 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5584 if (symbol != nullptr && !symbol->isFunction())
5585 {
5586 error(loc, "function name expected", fnCall->getName().c_str());
5587 }
5588 else
5589 {
5590 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5591 mShaderVersion, &builtIn);
5592 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005593 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005594 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5595 }
5596 else
5597 {
5598 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005599 //
5600 // A declared function.
5601 //
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03005602 if (builtIn && fnCandidate->getExtension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005603 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005604 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005605 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005606 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005607 if (builtIn && op != EOpNull)
5608 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005609 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005610 if (fnCandidate->getParamCount() == 1)
5611 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005612 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005613 TIntermNode *unaryParamNode = arguments->front();
5614 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005615 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005616 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005617 }
5618 else
5619 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005620 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005621 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005622 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005623
5624 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005625 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305626
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005627 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305628 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005629 // See if we can constant fold a built-in. Note that this may be possible
5630 // even if it is not const-qualified.
5631 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305632 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005633 else
5634 {
5635 return callNode;
5636 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005637 }
5638 }
5639 else
5640 {
5641 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005642 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005643
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005644 // If builtIn == false, the function is user defined - could be an overloaded
5645 // built-in as well.
5646 // if builtIn == true, it's a builtIn function with no op associated with it.
5647 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005648 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005649 {
Olli Etuahofe486322017-03-21 09:30:54 +00005650 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005651 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005652 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005653 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005654 }
5655 else
5656 {
Olli Etuahofe486322017-03-21 09:30:54 +00005657 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005658 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005659 }
5660
Jiajia Qinbc585152017-06-23 15:42:17 +08005661 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005662
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005663 callNode->setLine(loc);
5664
5665 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005666 }
5667 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005668 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005669
5670 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005671 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005672}
5673
Jamie Madillb98c3a82015-07-23 14:26:04 -04005674TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005675 TIntermTyped *trueExpression,
5676 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005677 const TSourceLoc &loc)
5678{
Olli Etuaho56229f12017-07-10 14:16:33 +03005679 if (!checkIsScalarBool(loc, cond))
5680 {
5681 return falseExpression;
5682 }
Olli Etuaho52901742015-04-15 13:42:45 +03005683
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005684 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005685 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005686 std::stringstream reasonStream;
5687 reasonStream << "mismatching ternary operator operand types '"
5688 << trueExpression->getCompleteString() << " and '"
5689 << falseExpression->getCompleteString() << "'";
5690 std::string reason = reasonStream.str();
5691 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005692 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005693 }
Olli Etuahode318b22016-10-25 16:18:25 +01005694 if (IsOpaqueType(trueExpression->getBasicType()))
5695 {
5696 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005697 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005698 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5699 // Note that structs containing opaque types don't need to be checked as structs are
5700 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005701 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005702 return falseExpression;
5703 }
5704
Jiajia Qinbc585152017-06-23 15:42:17 +08005705 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5706 falseExpression->getMemoryQualifier().writeonly)
5707 {
5708 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5709 return falseExpression;
5710 }
5711
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005712 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005713 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005714 // ESSL 3.00.6 section 5.7:
5715 // Ternary operator support is optional for arrays. No certainty that it works across all
5716 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5717 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005718 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005719 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005720 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005721 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005722 }
Olli Etuaho94050052017-05-08 14:17:44 +03005723 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5724 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005725 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005726 return falseExpression;
5727 }
5728
Corentin Wallez0d959252016-07-12 17:26:32 -04005729 // WebGL2 section 5.26, the following results in an error:
5730 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005731 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005732 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005733 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005734 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005735 }
5736
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005737 // Note that the node resulting from here can be a constant union without being qualified as
5738 // constant.
5739 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5740 node->setLine(loc);
5741
5742 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005743}
Olli Etuaho49300862015-02-20 14:54:49 +02005744
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005745//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005746// Parse an array of strings using yyparse.
5747//
5748// Returns 0 for success.
5749//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005750int PaParseStrings(size_t count,
5751 const char *const string[],
5752 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305753 TParseContext *context)
5754{
Yunchao He4f285442017-04-21 12:15:49 +08005755 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005756 return 1;
5757
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005758 if (glslang_initialize(context))
5759 return 1;
5760
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005761 int error = glslang_scan(count, string, length, context);
5762 if (!error)
5763 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005764
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005765 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005766
alokp@chromium.org6b495712012-06-29 00:06:58 +00005767 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005768}
Jamie Madill45bcc782016-11-07 13:58:48 -05005769
5770} // namespace sh